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, 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);
86 String varname=cc.getVarName();
88 //Make sure the variable is declared as a parameter to the task
89 VarDescriptor vd=(VarDescriptor)td.getParameterTable().get(varname);
91 throw new Error("Parameter "+varname+" in Constraint Check not declared");
94 //Make sure it correspods to a class
95 TypeDescriptor type_d=vd.getType();
96 if (!type_d.isClass())
97 throw new Error("Cannot have non-object argument for flag_effect");
102 public void checkFlagEffects(TaskDescriptor td, Vector vfe) {
104 return; /* No flag effects to check */
105 for(int i=0;i<vfe.size();i++) {
106 FlagEffects fe=(FlagEffects) vfe.get(i);
107 String varname=fe.getName();
108 //Make sure the variable is declared as a parameter to the task
109 VarDescriptor vd=(VarDescriptor)td.getParameterTable().get(varname);
111 throw new Error("Parameter "+varname+" in Flag Effects not declared");
114 //Make sure it correspods to a class
115 TypeDescriptor type_d=vd.getType();
116 if (!type_d.isClass())
117 throw new Error("Cannot have non-object argument for flag_effect");
119 ClassDescriptor cd=type_d.getClassDesc();
120 for(int j=0;j<fe.numEffects();j++) {
121 FlagEffect flag=fe.getEffect(j);
122 String name=flag.getName();
123 FlagDescriptor flag_d=(FlagDescriptor)cd.getFlagTable().get(name);
124 //Make sure the flag is declared
126 throw new Error("Flag descriptor "+name+" undefined in class: "+cd.getSymbol());
127 flag.setFlag(flag_d);
132 public void checkTask(TaskDescriptor td) {
133 for(int i=0;i<td.numParameters();i++) {
134 /* Check that parameter is well typed */
135 TypeDescriptor param_type=td.getParamType(i);
136 checkTypeDescriptor(param_type);
138 /* Check the parameter's flag expression is well formed */
139 FlagExpressionNode fen=td.getFlag(td.getParameter(i));
140 if (!param_type.isClass())
141 throw new Error("Cannot have non-object argument to a task");
142 ClassDescriptor cd=param_type.getClassDesc();
143 checkFlagExpressionNode(cd, fen);
144 checkFlagEffects(td, td.getFlagEffects());
146 /* Check that the task code is valid */
147 BlockNode bn=state.getMethodBody(td);
148 checkBlockNode(td, td.getParameterTable(),bn);
152 public void checkFlagExpressionNode(ClassDescriptor cd, FlagExpressionNode fen) {
154 case Kind.FlagOpNode:
156 FlagOpNode fon=(FlagOpNode)fen;
157 checkFlagExpressionNode(cd, fon.getLeft());
158 if (fon.getRight()!=null)
159 checkFlagExpressionNode(cd, fon.getRight());
164 FlagNode fn=(FlagNode)fen;
165 String name=fn.getFlagName();
166 FlagDescriptor fd=(FlagDescriptor)cd.getFlagTable().get(name);
168 throw new Error("Undeclared flag: "+name);
173 throw new Error("Unrecognized FlagExpressionNode");
177 public void checkMethod(ClassDescriptor cd, MethodDescriptor md) {
178 /* Check return type */
179 if (!md.isConstructor())
180 if (!md.getReturnType().isVoid())
181 checkTypeDescriptor(md.getReturnType());
183 for(int i=0;i<md.numParameters();i++) {
184 TypeDescriptor param_type=md.getParamType(i);
185 checkTypeDescriptor(param_type);
187 /* Link the naming environments */
188 if (!md.isStatic()) /* Fields aren't accessible directly in a static method, so don't link in this table */
189 md.getParameterTable().setParent(cd.getFieldTable());
191 if (!md.isStatic()) {
192 VarDescriptor thisvd=new VarDescriptor(new TypeDescriptor(cd),"this");
197 public void checkMethodBody(ClassDescriptor cd, MethodDescriptor md) {
198 System.out.println("Processing method:"+md);
199 BlockNode bn=state.getMethodBody(md);
200 checkBlockNode(md, md.getParameterTable(),bn);
203 public void checkBlockNode(Descriptor md, SymbolTable nametable, BlockNode bn) {
204 /* Link in the naming environment */
205 bn.getVarTable().setParent(nametable);
206 for(int i=0;i<bn.size();i++) {
207 BlockStatementNode bsn=bn.get(i);
208 checkBlockStatementNode(md, bn.getVarTable(),bsn);
212 public void checkBlockStatementNode(Descriptor md, SymbolTable nametable, BlockStatementNode bsn) {
214 case Kind.BlockExpressionNode:
215 checkBlockExpressionNode(md, nametable,(BlockExpressionNode)bsn);
218 case Kind.DeclarationNode:
219 checkDeclarationNode(md, nametable, (DeclarationNode)bsn);
222 case Kind.IfStatementNode:
223 checkIfStatementNode(md, nametable, (IfStatementNode)bsn);
227 checkLoopNode(md, nametable, (LoopNode)bsn);
230 case Kind.ReturnNode:
231 checkReturnNode(md, nametable, (ReturnNode)bsn);
234 case Kind.TaskExitNode:
235 checkTaskExitNode(md, nametable, (TaskExitNode)bsn);
238 case Kind.SubBlockNode:
239 checkSubBlockNode(md, nametable, (SubBlockNode)bsn);
245 void checkBlockExpressionNode(Descriptor md, SymbolTable nametable, BlockExpressionNode ben) {
246 checkExpressionNode(md, nametable, ben.getExpression(), null);
249 void checkDeclarationNode(Descriptor md, SymbolTable nametable, DeclarationNode dn) {
250 VarDescriptor vd=dn.getVarDescriptor();
251 checkTypeDescriptor(vd.getType());
252 Descriptor d=nametable.get(vd.getSymbol());
254 (d instanceof FieldDescriptor)) {
257 throw new Error(vd.getSymbol()+" defined a second time");
258 if (dn.getExpression()!=null)
259 checkExpressionNode(md, nametable, dn.getExpression(), vd.getType());
262 void checkSubBlockNode(Descriptor md, SymbolTable nametable, SubBlockNode sbn) {
263 checkBlockNode(md, nametable, sbn.getBlockNode());
266 void checkReturnNode(Descriptor d, SymbolTable nametable, ReturnNode rn) {
267 if (d instanceof TaskDescriptor)
268 throw new Error("Illegal return appears in Task: "+d.getSymbol());
269 MethodDescriptor md=(MethodDescriptor)d;
270 if (rn.getReturnExpression()!=null)
271 checkExpressionNode(md, nametable, rn.getReturnExpression(), md.getReturnType());
273 if (md.getReturnType()!=null&&!md.getReturnType().isVoid())
274 throw new Error("Need to return something for "+md);
277 void checkTaskExitNode(Descriptor md, SymbolTable nametable, TaskExitNode ten) {
278 if (md instanceof MethodDescriptor)
279 throw new Error("Illegal taskexit appears in Method: "+md.getSymbol());
280 checkFlagEffects((TaskDescriptor)md, ten.getFlagEffects());
281 checkConstraintCheck((TaskDescriptor) md, ten.getChecks());
284 void checkIfStatementNode(Descriptor md, SymbolTable nametable, IfStatementNode isn) {
285 checkExpressionNode(md, nametable, isn.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
286 checkBlockNode(md, nametable, isn.getTrueBlock());
287 if (isn.getFalseBlock()!=null)
288 checkBlockNode(md, nametable, isn.getFalseBlock());
291 void checkExpressionNode(Descriptor md, SymbolTable nametable, ExpressionNode en, TypeDescriptor td) {
293 case Kind.AssignmentNode:
294 checkAssignmentNode(md,nametable,(AssignmentNode)en,td);
297 checkCastNode(md,nametable,(CastNode)en,td);
299 case Kind.CreateObjectNode:
300 checkCreateObjectNode(md,nametable,(CreateObjectNode)en,td);
302 case Kind.FieldAccessNode:
303 checkFieldAccessNode(md,nametable,(FieldAccessNode)en,td);
305 case Kind.ArrayAccessNode:
306 checkArrayAccessNode(md,nametable,(ArrayAccessNode)en,td);
308 case Kind.LiteralNode:
309 checkLiteralNode(md,nametable,(LiteralNode)en,td);
311 case Kind.MethodInvokeNode:
312 checkMethodInvokeNode(md,nametable,(MethodInvokeNode)en,td);
315 checkNameNode(md,nametable,(NameNode)en,td);
318 checkOpNode(md,nametable,(OpNode)en,td);
324 void checkCastNode(Descriptor md, SymbolTable nametable, CastNode cn, TypeDescriptor td) {
325 /* Get type descriptor */
326 if (cn.getType()==null) {
327 NameDescriptor typenamed=cn.getTypeName().getName();
328 String typename=typenamed.toString();
329 TypeDescriptor ntd=new TypeDescriptor(typeutil.getClass(typename));
333 /* Check the type descriptor */
334 TypeDescriptor cast_type=cn.getType();
335 checkTypeDescriptor(cast_type);
339 if (!typeutil.isSuperorType(td,cast_type))
340 throw new Error("Cast node returns "+cast_type+", but need "+td);
343 ExpressionNode en=cn.getExpression();
344 checkExpressionNode(md, nametable, en, null);
345 TypeDescriptor etd=en.getType();
346 if (typeutil.isSuperorType(cast_type,etd)) /* Cast trivially succeeds */
349 if (typeutil.isSuperorType(etd,cast_type)) /* Cast may succeed */
352 /* Different branches */
353 /* TODO: change if add interfaces */
354 throw new Error("Cast will always fail\n"+cn.printNode(0));
357 void checkFieldAccessNode(Descriptor md, SymbolTable nametable, FieldAccessNode fan, TypeDescriptor td) {
358 ExpressionNode left=fan.getExpression();
359 checkExpressionNode(md,nametable,left,null);
360 TypeDescriptor ltd=left.getType();
361 String fieldname=fan.getFieldName();
363 FieldDescriptor fd=null;
364 if (ltd.isArray()&&fieldname.equals("length"))
365 fd=FieldDescriptor.arrayLength;
367 fd=(FieldDescriptor) ltd.getClassDesc().getFieldTable().get(fieldname);
369 throw new Error("Unknown field "+fieldname);
372 if (!typeutil.isSuperorType(td,fan.getType()))
373 throw new Error("Field node returns "+fan.getType()+", but need "+td);
376 void checkArrayAccessNode(Descriptor md, SymbolTable nametable, ArrayAccessNode aan, TypeDescriptor td) {
377 ExpressionNode left=aan.getExpression();
378 checkExpressionNode(md,nametable,left,null);
380 checkExpressionNode(md,nametable,aan.getIndex(),new TypeDescriptor(TypeDescriptor.INT));
381 TypeDescriptor ltd=left.getType();
384 if (!typeutil.isSuperorType(td,aan.getType()))
385 throw new Error("Field node returns "+aan.getType()+", but need "+td);
388 void checkLiteralNode(Descriptor md, SymbolTable nametable, LiteralNode ln, TypeDescriptor td) {
389 /* Resolve the type */
390 Object o=ln.getValue();
391 if (ln.getTypeString().equals("null")) {
392 ln.setType(new TypeDescriptor(TypeDescriptor.NULL));
393 } else if (o instanceof Integer) {
394 ln.setType(new TypeDescriptor(TypeDescriptor.INT));
395 } else if (o instanceof Long) {
396 ln.setType(new TypeDescriptor(TypeDescriptor.LONG));
397 } else if (o instanceof Float) {
398 ln.setType(new TypeDescriptor(TypeDescriptor.FLOAT));
399 } else if (o instanceof Boolean) {
400 ln.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
401 } else if (o instanceof Double) {
402 ln.setType(new TypeDescriptor(TypeDescriptor.DOUBLE));
403 } else if (o instanceof Character) {
404 ln.setType(new TypeDescriptor(TypeDescriptor.CHAR));
405 } else if (o instanceof String) {
406 ln.setType(new TypeDescriptor(typeutil.getClass(TypeUtil.StringClass)));
410 if (!typeutil.isSuperorType(td,ln.getType()))
411 throw new Error("Field node returns "+ln.getType()+", but need "+td);
414 void checkNameNode(Descriptor md, SymbolTable nametable, NameNode nn, TypeDescriptor td) {
415 NameDescriptor nd=nn.getName();
416 if (nd.getBase()!=null) {
418 /* Rewrite NameNode */
419 ExpressionNode en=translateNameDescriptorintoExpression(nd);
420 nn.setExpression(en);
421 checkExpressionNode(md,nametable,en,td);
423 String varname=nd.toString();
424 Descriptor d=(Descriptor)nametable.get(varname);
426 throw new Error("Name "+varname+" undefined");
428 if (d instanceof VarDescriptor) {
429 nn.setVar((VarDescriptor)d);
430 } else if (d instanceof FieldDescriptor) {
431 nn.setField((FieldDescriptor)d);
432 nn.setVar((VarDescriptor)nametable.get("this")); /* Need a pointer to this */
435 if (!typeutil.isSuperorType(td,nn.getType()))
436 throw new Error("Field node returns "+nn.getType()+", but need "+td);
440 void checkAssignmentNode(Descriptor md, SymbolTable nametable, AssignmentNode an, TypeDescriptor td) {
441 boolean postinc=true;
442 if (an.getOperation().getBaseOp()==null||
443 (an.getOperation().getBaseOp().getOp()!=Operation.POSTINC&&
444 an.getOperation().getBaseOp().getOp()!=Operation.POSTDEC))
448 checkExpressionNode(md, nametable, an.getSrc() ,td);
449 //TODO: Need check on validity of operation here
450 if (!((an.getDest() instanceof FieldAccessNode)||
451 (an.getDest() instanceof ArrayAccessNode)||
452 (an.getDest() instanceof NameNode)))
453 throw new Error("Bad lside in "+an.printNode(0));
454 checkExpressionNode(md, nametable, an.getDest(), null);
456 /* We want parameter variables to tasks to be immutable */
457 if (md instanceof TaskDescriptor) {
458 if (an.getDest() instanceof NameNode) {
459 NameNode nn=(NameNode)an.getDest();
460 if (nn.getVar()!=null) {
461 if (((TaskDescriptor)md).getParameterTable().contains(nn.getVar().getSymbol()))
462 throw new Error("Can't modify parameter "+nn.getVar()+ " to task "+td.getSymbol());
467 if (!postinc&&!typeutil.isSuperorType(an.getDest().getType(),an.getSrc().getType())) {
468 throw new Error("Type of rside ("+an.getSrc().getType()+") not compatible with type of lside ("+an.getDest().getType()+")"+an.printNode(0));
472 void checkLoopNode(Descriptor md, SymbolTable nametable, LoopNode ln) {
473 if (ln.getType()==LoopNode.WHILELOOP||ln.getType()==LoopNode.DOWHILELOOP) {
474 checkExpressionNode(md, nametable, ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
475 checkBlockNode(md, nametable, ln.getBody());
478 /* Link in the initializer naming environment */
479 BlockNode bn=ln.getInitializer();
480 bn.getVarTable().setParent(nametable);
481 for(int i=0;i<bn.size();i++) {
482 BlockStatementNode bsn=bn.get(i);
483 checkBlockStatementNode(md, bn.getVarTable(),bsn);
485 //check the condition
486 checkExpressionNode(md, bn.getVarTable(), ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
487 checkBlockNode(md, bn.getVarTable(), ln.getBody());
488 checkBlockNode(md, bn.getVarTable(), ln.getUpdate());
493 void checkCreateObjectNode(Descriptor md, SymbolTable nametable, CreateObjectNode con, TypeDescriptor td) {
494 TypeDescriptor[] tdarray=new TypeDescriptor[con.numArgs()];
495 for(int i=0;i<con.numArgs();i++) {
496 ExpressionNode en=con.getArg(i);
497 checkExpressionNode(md,nametable,en,null);
498 tdarray[i]=en.getType();
501 TypeDescriptor typetolookin=con.getType();
502 checkTypeDescriptor(typetolookin);
504 /* Check flag effects */
505 if (con.getFlagEffects()!=null) {
506 FlagEffects fe=con.getFlagEffects();
507 ClassDescriptor cd=typetolookin.getClassDesc();
509 for(int j=0;j<fe.numEffects();j++) {
510 FlagEffect flag=fe.getEffect(j);
511 String name=flag.getName();
512 FlagDescriptor flag_d=(FlagDescriptor)cd.getFlagTable().get(name);
513 //Make sure the flag is declared
515 throw new Error("Flag descriptor "+name+" undefined in class: "+cd.getSymbol());
516 flag.setFlag(flag_d);
521 if ((!typetolookin.isClass())&&(!typetolookin.isArray()))
522 throw new Error("Can't allocate primitive type:"+con.printNode(0));
524 if (!typetolookin.isArray()) {
525 //Array's don't need constructor calls
526 ClassDescriptor classtolookin=typetolookin.getClassDesc();
527 System.out.println("Looking for "+typetolookin.getSymbol());
528 System.out.println(classtolookin.getMethodTable());
530 Set methoddescriptorset=classtolookin.getMethodTable().getSet(typetolookin.getSymbol());
531 MethodDescriptor bestmd=null;
533 for(Iterator methodit=methoddescriptorset.iterator();methodit.hasNext();) {
534 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
535 /* Need correct number of parameters */
536 System.out.println("Examining: "+currmd);
537 if (con.numArgs()!=currmd.numParameters())
539 for(int i=0;i<con.numArgs();i++) {
540 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
543 /* Method okay so far */
547 if (isMoreSpecific(currmd,bestmd)) {
549 } else if (!isMoreSpecific(bestmd, currmd))
550 throw new Error("No method is most specific");
552 /* Is this more specific than bestmd */
556 throw new Error("No method found for "+con.printNode(0));
557 con.setConstructor(bestmd);
562 /** Check to see if md1 is more specific than md2... Informally
563 if md2 could always be called given the arguments passed into
566 boolean isMoreSpecific(MethodDescriptor md1, MethodDescriptor md2) {
567 /* Checks if md1 is more specific than md2 */
568 if (md1.numParameters()!=md2.numParameters())
570 for(int i=0;i<md1.numParameters();i++) {
571 if (!typeutil.isSuperorType(md2.getParamType(i), md1.getParamType(i)))
574 if (!typeutil.isSuperorType(md2.getReturnType(), md1.getReturnType()))
579 ExpressionNode translateNameDescriptorintoExpression(NameDescriptor nd) {
580 String id=nd.getIdentifier();
581 NameDescriptor base=nd.getBase();
583 return new NameNode(nd);
585 return new FieldAccessNode(translateNameDescriptorintoExpression(base),id);
589 void checkMethodInvokeNode(Descriptor md, SymbolTable nametable, MethodInvokeNode min, TypeDescriptor td) {
590 /*Typecheck subexpressions
591 and get types for expressions*/
593 TypeDescriptor[] tdarray=new TypeDescriptor[min.numArgs()];
594 for(int i=0;i<min.numArgs();i++) {
595 ExpressionNode en=min.getArg(i);
596 checkExpressionNode(md,nametable,en,null);
597 tdarray[i]=en.getType();
599 TypeDescriptor typetolookin=null;
600 if (min.getExpression()!=null) {
601 checkExpressionNode(md,nametable,min.getExpression(),null);
602 typetolookin=min.getExpression().getType();
603 } else if (min.getBaseName()!=null) {
604 String rootname=min.getBaseName().getRoot();
605 if (nametable.get(rootname)!=null) {
606 //we have an expression
607 min.setExpression(translateNameDescriptorintoExpression(min.getBaseName()));
608 checkExpressionNode(md, nametable, min.getExpression(), null);
609 typetolookin=min.getExpression().getType();
612 ClassDescriptor cd=typeutil.getClass(min.getBaseName().getSymbol());
614 throw new Error(min.getBaseName()+" undefined");
615 typetolookin=new TypeDescriptor(cd);
617 } else if (md instanceof MethodDescriptor) {
618 typetolookin=new TypeDescriptor(((MethodDescriptor)md).getClassDesc());
620 /* If this a task descriptor we throw an error at this point */
621 throw new Error("Unknown method call to "+min.getMethodName()+"in task"+md.getSymbol());
623 if (!typetolookin.isClass())
625 ClassDescriptor classtolookin=typetolookin.getClassDesc();
626 System.out.println("Method name="+min.getMethodName());
627 Set methoddescriptorset=classtolookin.getMethodTable().getSet(min.getMethodName());
628 MethodDescriptor bestmd=null;
630 for(Iterator methodit=methoddescriptorset.iterator();methodit.hasNext();) {
631 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
632 /* Need correct number of parameters */
633 if (min.numArgs()!=currmd.numParameters())
635 for(int i=0;i<min.numArgs();i++) {
636 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
639 /* Method okay so far */
643 if (isMoreSpecific(currmd,bestmd)) {
645 } else if (!isMoreSpecific(bestmd, currmd))
646 throw new Error("No method is most specific");
648 /* Is this more specific than bestmd */
652 throw new Error("No method found for :"+min.printNode(0));
653 min.setMethod(bestmd);
654 /* Check whether we need to set this parameter to implied this */
655 if (!bestmd.isStatic()) {
656 if (min.getExpression()==null) {
657 ExpressionNode en=new NameNode(new NameDescriptor("this"));
658 min.setExpression(en);
659 checkExpressionNode(md, nametable, min.getExpression(), null);
666 void checkOpNode(Descriptor md, SymbolTable nametable, OpNode on, TypeDescriptor td) {
667 checkExpressionNode(md, nametable, on.getLeft(), null);
668 if (on.getRight()!=null)
669 checkExpressionNode(md, nametable, on.getRight(), null);
670 TypeDescriptor ltd=on.getLeft().getType();
671 TypeDescriptor rtd=on.getRight()!=null?on.getRight().getType():null;
672 TypeDescriptor lefttype=null;
673 TypeDescriptor righttype=null;
674 Operation op=on.getOp();
677 case Operation.LOGIC_OR:
678 case Operation.LOGIC_AND:
679 if (!(ltd.isBoolean()&&rtd.isBoolean()))
683 on.setRightType(rtd);
684 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
687 case Operation.BIT_OR:
688 case Operation.BIT_XOR:
689 case Operation.BIT_AND:
690 // 5.6.2 Binary Numeric Promotion
691 //TODO unboxing of reference objects
692 if (ltd.isDouble()||rtd.isDouble())
694 else if (ltd.isFloat()||rtd.isFloat())
696 else if (ltd.isLong()||rtd.isLong())
697 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
699 lefttype=new TypeDescriptor(TypeDescriptor.INT);
702 on.setLeftType(lefttype);
703 on.setRightType(righttype);
704 on.setType(lefttype);
707 case Operation.EQUAL:
708 case Operation.NOTEQUAL:
709 // 5.6.2 Binary Numeric Promotion
710 //TODO unboxing of reference objects
711 if (ltd.isBoolean()||rtd.isBoolean()) {
712 if (!(ltd.isBoolean()&&rtd.isBoolean()))
714 righttype=lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
715 } else if (ltd.isPtr()||ltd.isArray()||rtd.isPtr()||rtd.isArray()) {
716 if (!((ltd.isPtr()||ltd.isArray())&&(rtd.isPtr()||rtd.isArray())))
720 } else if (ltd.isDouble()||rtd.isDouble())
721 righttype=lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
722 else if (ltd.isFloat()||rtd.isFloat())
723 righttype=lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
724 else if (ltd.isLong()||rtd.isLong())
725 righttype=lefttype=new TypeDescriptor(TypeDescriptor.LONG);
727 righttype=lefttype=new TypeDescriptor(TypeDescriptor.INT);
729 on.setLeftType(lefttype);
730 on.setRightType(righttype);
731 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
740 // 5.6.2 Binary Numeric Promotion
741 //TODO unboxing of reference objects
742 if (!ltd.isNumber()||!rtd.isNumber())
745 if (ltd.isDouble()||rtd.isDouble())
746 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
747 else if (ltd.isFloat()||rtd.isFloat())
748 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
749 else if (ltd.isLong()||rtd.isLong())
750 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
752 lefttype=new TypeDescriptor(TypeDescriptor.INT);
754 on.setLeftType(lefttype);
755 on.setRightType(righttype);
756 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
760 //TODO: Need special case for strings eventually
767 // 5.6.2 Binary Numeric Promotion
768 //TODO unboxing of reference objects
769 if (!ltd.isNumber()||!rtd.isNumber())
770 throw new Error("Error in "+on.printNode(0));
772 if (ltd.isDouble()||rtd.isDouble())
773 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
774 else if (ltd.isFloat()||rtd.isFloat())
775 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
776 else if (ltd.isLong()||rtd.isLong())
777 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
779 lefttype=new TypeDescriptor(TypeDescriptor.INT);
781 on.setLeftType(lefttype);
782 on.setRightType(righttype);
783 on.setType(lefttype);
786 case Operation.LEFTSHIFT:
787 case Operation.RIGHTSHIFT:
788 if (!rtd.isIntegerType())
790 //5.6.1 Unary Numeric Promotion
791 if (rtd.isByte()||rtd.isShort()||rtd.isInt())
792 righttype=new TypeDescriptor(TypeDescriptor.INT);
796 on.setRightType(righttype);
797 if (!ltd.isIntegerType())
799 case Operation.UNARYPLUS:
800 case Operation.UNARYMINUS:
801 /* case Operation.POSTINC:
802 case Operation.POSTDEC:
803 case Operation.PREINC:
804 case Operation.PREDEC:*/
807 //5.6.1 Unary Numeric Promotion
808 if (ltd.isByte()||ltd.isShort()||ltd.isInt())
809 lefttype=new TypeDescriptor(TypeDescriptor.INT);
812 on.setLeftType(lefttype);
813 on.setType(lefttype);
820 if (!typeutil.isSuperorType(td, on.getType())) {
821 System.out.println(td);
822 System.out.println(on.getType());
823 throw new Error("Type of rside not compatible with type of lside"+on.printNode(0));