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 checkFlagEffects(TaskDescriptor td, Vector vfe) {
82 for(int i=0;i<vfe.size();i++) {
83 FlagEffects fe=(FlagEffects) vfe.get(i);
84 String varname=fe.getName();
85 //Make sure the variable is declared as a parameter to the task
86 VarDescriptor vd=(VarDescriptor)td.getParameterTable().get(varname);
88 throw new Error("Parameter "+varname+" in Flag Effects not declared");
91 //Make sure it correspods to a class
92 TypeDescriptor type_d=vd.getType();
93 if (!type_d.isClass())
94 throw new Error("Cannot have non-object argument for flag_effect");
96 ClassDescriptor cd=type_d.getClassDesc();
97 for(int j=0;j<fe.numEffects();j++) {
98 FlagEffect flag=fe.getEffect(j);
99 String name=flag.getName();
100 FlagDescriptor flag_d=(FlagDescriptor)cd.getFlagTable().get(name);
101 //Make sure the flag is declared
103 throw new Error("Flag descriptor "+name+" undefined in class: "+cd.getSymbol());
104 flag.setFlag(flag_d);
109 public void checkTask(TaskDescriptor td) {
110 for(int i=0;i<td.numParameters();i++) {
111 /* Check that parameter is well typed */
112 TypeDescriptor param_type=td.getParamType(i);
113 checkTypeDescriptor(param_type);
115 /* Check the parameter's flag expression is well formed */
116 FlagExpressionNode fen=td.getFlag(td.getParameter(i));
117 if (!param_type.isClass())
118 throw new Error("Cannot have non-object argument to a task");
119 ClassDescriptor cd=param_type.getClassDesc();
120 checkFlagExpressionNode(cd, fen);
121 checkFlagEffects(td, td.getFlagEffects());
123 /* Check that the task code is valid */
124 BlockNode bn=state.getMethodBody(td);
125 checkBlockNode(td, td.getParameterTable(),bn);
129 public void checkFlagExpressionNode(ClassDescriptor cd, FlagExpressionNode fen) {
131 case Kind.FlagOpNode:
133 FlagOpNode fon=(FlagOpNode)fen;
134 checkFlagExpressionNode(cd, fon.getLeft());
135 if (fon.getRight()!=null)
136 checkFlagExpressionNode(cd, fon.getRight());
141 FlagNode fn=(FlagNode)fen;
142 String name=fn.getFlagName();
143 FlagDescriptor fd=(FlagDescriptor)cd.getFlagTable().get(name);
145 throw new Error("Undeclared flag: "+name);
150 throw new Error("Unrecognized FlagExpressionNode");
154 public void checkMethod(ClassDescriptor cd, MethodDescriptor md) {
155 /* Check return type */
156 if (!md.isConstructor())
157 if (!md.getReturnType().isVoid())
158 checkTypeDescriptor(md.getReturnType());
160 for(int i=0;i<md.numParameters();i++) {
161 TypeDescriptor param_type=md.getParamType(i);
162 checkTypeDescriptor(param_type);
164 /* Link the naming environments */
165 if (!md.isStatic()) /* Fields aren't accessible directly in a static method, so don't link in this table */
166 md.getParameterTable().setParent(cd.getFieldTable());
168 if (!md.isStatic()) {
169 VarDescriptor thisvd=new VarDescriptor(new TypeDescriptor(cd),"this");
174 public void checkMethodBody(ClassDescriptor cd, MethodDescriptor md) {
175 System.out.println("Processing method:"+md);
176 BlockNode bn=state.getMethodBody(md);
177 checkBlockNode(md, md.getParameterTable(),bn);
180 public void checkBlockNode(Descriptor md, SymbolTable nametable, BlockNode bn) {
181 /* Link in the naming environment */
182 bn.getVarTable().setParent(nametable);
183 for(int i=0;i<bn.size();i++) {
184 BlockStatementNode bsn=bn.get(i);
185 checkBlockStatementNode(md, bn.getVarTable(),bsn);
189 public void checkBlockStatementNode(Descriptor md, SymbolTable nametable, BlockStatementNode bsn) {
191 case Kind.BlockExpressionNode:
192 checkBlockExpressionNode(md, nametable,(BlockExpressionNode)bsn);
195 case Kind.DeclarationNode:
196 checkDeclarationNode(md, nametable, (DeclarationNode)bsn);
199 case Kind.IfStatementNode:
200 checkIfStatementNode(md, nametable, (IfStatementNode)bsn);
204 checkLoopNode(md, nametable, (LoopNode)bsn);
207 case Kind.ReturnNode:
208 checkReturnNode(md, nametable, (ReturnNode)bsn);
211 case Kind.TaskExitNode:
212 checkTaskExitNode(md, nametable, (TaskExitNode)bsn);
215 case Kind.SubBlockNode:
216 checkSubBlockNode(md, nametable, (SubBlockNode)bsn);
222 void checkBlockExpressionNode(Descriptor md, SymbolTable nametable, BlockExpressionNode ben) {
223 checkExpressionNode(md, nametable, ben.getExpression(), null);
226 void checkDeclarationNode(Descriptor md, SymbolTable nametable, DeclarationNode dn) {
227 VarDescriptor vd=dn.getVarDescriptor();
228 checkTypeDescriptor(vd.getType());
229 Descriptor d=nametable.get(vd.getSymbol());
231 (d instanceof FieldDescriptor)) {
234 throw new Error(vd.getSymbol()+" defined a second time");
235 if (dn.getExpression()!=null)
236 checkExpressionNode(md, nametable, dn.getExpression(), vd.getType());
239 void checkSubBlockNode(Descriptor md, SymbolTable nametable, SubBlockNode sbn) {
240 checkBlockNode(md, nametable, sbn.getBlockNode());
243 void checkReturnNode(Descriptor d, SymbolTable nametable, ReturnNode rn) {
244 if (d instanceof TaskDescriptor)
245 throw new Error("Illegal return appears in Task: "+d.getSymbol());
246 MethodDescriptor md=(MethodDescriptor)d;
247 if (rn.getReturnExpression()!=null)
248 checkExpressionNode(md, nametable, rn.getReturnExpression(), md.getReturnType());
250 if (md.getReturnType()!=null&&!md.getReturnType().isVoid())
251 throw new Error("Need to return something for "+md);
254 void checkTaskExitNode(Descriptor md, SymbolTable nametable, TaskExitNode ten) {
255 if (md instanceof MethodDescriptor)
256 throw new Error("Illegal taskexit appears in Method: "+md.getSymbol());
257 checkFlagEffects((TaskDescriptor)md, ten.getFlagEffects());
260 void checkIfStatementNode(Descriptor md, SymbolTable nametable, IfStatementNode isn) {
261 checkExpressionNode(md, nametable, isn.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
262 checkBlockNode(md, nametable, isn.getTrueBlock());
263 if (isn.getFalseBlock()!=null)
264 checkBlockNode(md, nametable, isn.getFalseBlock());
267 void checkExpressionNode(Descriptor md, SymbolTable nametable, ExpressionNode en, TypeDescriptor td) {
269 case Kind.AssignmentNode:
270 checkAssignmentNode(md,nametable,(AssignmentNode)en,td);
273 checkCastNode(md,nametable,(CastNode)en,td);
275 case Kind.CreateObjectNode:
276 checkCreateObjectNode(md,nametable,(CreateObjectNode)en,td);
278 case Kind.FieldAccessNode:
279 checkFieldAccessNode(md,nametable,(FieldAccessNode)en,td);
281 case Kind.ArrayAccessNode:
282 checkArrayAccessNode(md,nametable,(ArrayAccessNode)en,td);
284 case Kind.LiteralNode:
285 checkLiteralNode(md,nametable,(LiteralNode)en,td);
287 case Kind.MethodInvokeNode:
288 checkMethodInvokeNode(md,nametable,(MethodInvokeNode)en,td);
291 checkNameNode(md,nametable,(NameNode)en,td);
294 checkOpNode(md,nametable,(OpNode)en,td);
300 void checkCastNode(Descriptor md, SymbolTable nametable, CastNode cn, TypeDescriptor td) {
301 /* Get type descriptor */
302 if (cn.getType()==null) {
303 NameDescriptor typenamed=cn.getTypeName().getName();
304 String typename=typenamed.toString();
305 TypeDescriptor ntd=new TypeDescriptor(typeutil.getClass(typename));
309 /* Check the type descriptor */
310 TypeDescriptor cast_type=cn.getType();
311 checkTypeDescriptor(cast_type);
315 if (!typeutil.isSuperorType(td,cast_type))
316 throw new Error("Cast node returns "+cast_type+", but need "+td);
319 ExpressionNode en=cn.getExpression();
320 checkExpressionNode(md, nametable, en, null);
321 TypeDescriptor etd=en.getType();
322 if (typeutil.isSuperorType(cast_type,etd)) /* Cast trivially succeeds */
325 if (typeutil.isSuperorType(etd,cast_type)) /* Cast may succeed */
328 /* Different branches */
329 /* TODO: change if add interfaces */
330 throw new Error("Cast will always fail\n"+cn.printNode(0));
333 void checkFieldAccessNode(Descriptor md, SymbolTable nametable, FieldAccessNode fan, TypeDescriptor td) {
334 ExpressionNode left=fan.getExpression();
335 checkExpressionNode(md,nametable,left,null);
336 TypeDescriptor ltd=left.getType();
337 String fieldname=fan.getFieldName();
339 FieldDescriptor fd=null;
340 if (ltd.isArray()&&fieldname.equals("length"))
341 fd=FieldDescriptor.arrayLength;
343 fd=(FieldDescriptor) ltd.getClassDesc().getFieldTable().get(fieldname);
345 throw new Error("Unknown field "+fieldname);
348 if (!typeutil.isSuperorType(td,fan.getType()))
349 throw new Error("Field node returns "+fan.getType()+", but need "+td);
352 void checkArrayAccessNode(Descriptor md, SymbolTable nametable, ArrayAccessNode aan, TypeDescriptor td) {
353 ExpressionNode left=aan.getExpression();
354 checkExpressionNode(md,nametable,left,null);
356 checkExpressionNode(md,nametable,aan.getIndex(),new TypeDescriptor(TypeDescriptor.INT));
357 TypeDescriptor ltd=left.getType();
360 if (!typeutil.isSuperorType(td,aan.getType()))
361 throw new Error("Field node returns "+aan.getType()+", but need "+td);
364 void checkLiteralNode(Descriptor md, SymbolTable nametable, LiteralNode ln, TypeDescriptor td) {
365 /* Resolve the type */
366 Object o=ln.getValue();
367 if (ln.getTypeString().equals("null")) {
368 ln.setType(new TypeDescriptor(TypeDescriptor.NULL));
369 } else if (o instanceof Integer) {
370 ln.setType(new TypeDescriptor(TypeDescriptor.INT));
371 } else if (o instanceof Long) {
372 ln.setType(new TypeDescriptor(TypeDescriptor.LONG));
373 } else if (o instanceof Float) {
374 ln.setType(new TypeDescriptor(TypeDescriptor.FLOAT));
375 } else if (o instanceof Boolean) {
376 ln.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
377 } else if (o instanceof Double) {
378 ln.setType(new TypeDescriptor(TypeDescriptor.DOUBLE));
379 } else if (o instanceof Character) {
380 ln.setType(new TypeDescriptor(TypeDescriptor.CHAR));
381 } else if (o instanceof String) {
382 ln.setType(new TypeDescriptor(typeutil.getClass(TypeUtil.StringClass)));
386 if (!typeutil.isSuperorType(td,ln.getType()))
387 throw new Error("Field node returns "+ln.getType()+", but need "+td);
390 void checkNameNode(Descriptor md, SymbolTable nametable, NameNode nn, TypeDescriptor td) {
391 NameDescriptor nd=nn.getName();
392 if (nd.getBase()!=null) {
394 /* Rewrite NameNode */
395 ExpressionNode en=translateNameDescriptorintoExpression(nd);
396 nn.setExpression(en);
397 checkExpressionNode(md,nametable,en,td);
399 String varname=nd.toString();
400 Descriptor d=(Descriptor)nametable.get(varname);
402 throw new Error("Name "+varname+" undefined");
404 if (d instanceof VarDescriptor) {
405 nn.setVar((VarDescriptor)d);
406 } else if (d instanceof FieldDescriptor) {
407 nn.setField((FieldDescriptor)d);
408 nn.setVar((VarDescriptor)nametable.get("this")); /* Need a pointer to this */
411 if (!typeutil.isSuperorType(td,nn.getType()))
412 throw new Error("Field node returns "+nn.getType()+", but need "+td);
416 void checkAssignmentNode(Descriptor md, SymbolTable nametable, AssignmentNode an, TypeDescriptor td) {
417 boolean postinc=true;
418 if (an.getOperation().getBaseOp()==null||
419 (an.getOperation().getBaseOp().getOp()!=Operation.POSTINC&&
420 an.getOperation().getBaseOp().getOp()!=Operation.POSTDEC))
424 checkExpressionNode(md, nametable, an.getSrc() ,td);
425 //TODO: Need check on validity of operation here
426 if (!((an.getDest() instanceof FieldAccessNode)||
427 (an.getDest() instanceof ArrayAccessNode)||
428 (an.getDest() instanceof NameNode)))
429 throw new Error("Bad lside in "+an.printNode(0));
430 checkExpressionNode(md, nametable, an.getDest(), null);
432 /* We want parameter variables to tasks to be immutable */
433 if (md instanceof TaskDescriptor) {
434 if (an.getDest() instanceof NameNode) {
435 NameNode nn=(NameNode)an.getDest();
436 if (nn.getVar()!=null) {
437 if (((TaskDescriptor)md).getParameterTable().contains(nn.getVar().getSymbol()))
438 throw new Error("Can't modify parameter "+nn.getVar()+ " to task "+td.getSymbol());
443 if (!postinc&&!typeutil.isSuperorType(an.getDest().getType(),an.getSrc().getType())) {
444 throw new Error("Type of rside ("+an.getSrc().getType()+") not compatible with type of lside ("+an.getDest().getType()+")"+an.printNode(0));
448 void checkLoopNode(Descriptor md, SymbolTable nametable, LoopNode ln) {
449 if (ln.getType()==LoopNode.WHILELOOP||ln.getType()==LoopNode.DOWHILELOOP) {
450 checkExpressionNode(md, nametable, ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
451 checkBlockNode(md, nametable, ln.getBody());
454 /* Link in the initializer naming environment */
455 BlockNode bn=ln.getInitializer();
456 bn.getVarTable().setParent(nametable);
457 for(int i=0;i<bn.size();i++) {
458 BlockStatementNode bsn=bn.get(i);
459 checkBlockStatementNode(md, bn.getVarTable(),bsn);
461 //check the condition
462 checkExpressionNode(md, bn.getVarTable(), ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
463 checkBlockNode(md, bn.getVarTable(), ln.getBody());
464 checkBlockNode(md, bn.getVarTable(), ln.getUpdate());
469 void checkCreateObjectNode(Descriptor md, SymbolTable nametable, CreateObjectNode con, TypeDescriptor td) {
470 TypeDescriptor[] tdarray=new TypeDescriptor[con.numArgs()];
471 for(int i=0;i<con.numArgs();i++) {
472 ExpressionNode en=con.getArg(i);
473 checkExpressionNode(md,nametable,en,null);
474 tdarray[i]=en.getType();
477 TypeDescriptor typetolookin=con.getType();
478 checkTypeDescriptor(typetolookin);
480 /* Check flag effects */
481 if (con.getFlagEffects()!=null) {
482 FlagEffects fe=con.getFlagEffects();
483 ClassDescriptor cd=typetolookin.getClassDesc();
485 for(int j=0;j<fe.numEffects();j++) {
486 FlagEffect flag=fe.getEffect(j);
487 String name=flag.getName();
488 FlagDescriptor flag_d=(FlagDescriptor)cd.getFlagTable().get(name);
489 //Make sure the flag is declared
491 throw new Error("Flag descriptor "+name+" undefined in class: "+cd.getSymbol());
492 flag.setFlag(flag_d);
497 if ((!typetolookin.isClass())&&(!typetolookin.isArray()))
498 throw new Error("Can't allocate primitive type:"+con.printNode(0));
500 if (!typetolookin.isArray()) {
501 //Array's don't need constructor calls
502 ClassDescriptor classtolookin=typetolookin.getClassDesc();
503 System.out.println("Looking for "+typetolookin.getSymbol());
504 System.out.println(classtolookin.getMethodTable());
506 Set methoddescriptorset=classtolookin.getMethodTable().getSet(typetolookin.getSymbol());
507 MethodDescriptor bestmd=null;
509 for(Iterator methodit=methoddescriptorset.iterator();methodit.hasNext();) {
510 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
511 /* Need correct number of parameters */
512 System.out.println("Examining: "+currmd);
513 if (con.numArgs()!=currmd.numParameters())
515 for(int i=0;i<con.numArgs();i++) {
516 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
519 /* Method okay so far */
523 if (isMoreSpecific(currmd,bestmd)) {
525 } else if (!isMoreSpecific(bestmd, currmd))
526 throw new Error("No method is most specific");
528 /* Is this more specific than bestmd */
532 throw new Error("No method found for "+con.printNode(0));
533 con.setConstructor(bestmd);
538 /** Check to see if md1 is more specific than md2... Informally
539 if md2 could always be called given the arguments passed into
542 boolean isMoreSpecific(MethodDescriptor md1, MethodDescriptor md2) {
543 /* Checks if md1 is more specific than md2 */
544 if (md1.numParameters()!=md2.numParameters())
546 for(int i=0;i<md1.numParameters();i++) {
547 if (!typeutil.isSuperorType(md2.getParamType(i), md1.getParamType(i)))
550 if (!typeutil.isSuperorType(md2.getReturnType(), md1.getReturnType()))
555 ExpressionNode translateNameDescriptorintoExpression(NameDescriptor nd) {
556 String id=nd.getIdentifier();
557 NameDescriptor base=nd.getBase();
559 return new NameNode(nd);
561 return new FieldAccessNode(translateNameDescriptorintoExpression(base),id);
565 void checkMethodInvokeNode(Descriptor md, SymbolTable nametable, MethodInvokeNode min, TypeDescriptor td) {
566 /*Typecheck subexpressions
567 and get types for expressions*/
569 TypeDescriptor[] tdarray=new TypeDescriptor[min.numArgs()];
570 for(int i=0;i<min.numArgs();i++) {
571 ExpressionNode en=min.getArg(i);
572 checkExpressionNode(md,nametable,en,null);
573 tdarray[i]=en.getType();
575 TypeDescriptor typetolookin=null;
576 if (min.getExpression()!=null) {
577 checkExpressionNode(md,nametable,min.getExpression(),null);
578 typetolookin=min.getExpression().getType();
579 } else if (min.getBaseName()!=null) {
580 String rootname=min.getBaseName().getRoot();
581 if (nametable.get(rootname)!=null) {
582 //we have an expression
583 min.setExpression(translateNameDescriptorintoExpression(min.getBaseName()));
584 checkExpressionNode(md, nametable, min.getExpression(), null);
585 typetolookin=min.getExpression().getType();
588 ClassDescriptor cd=typeutil.getClass(min.getBaseName().getSymbol());
590 throw new Error(min.getBaseName()+" undefined");
591 typetolookin=new TypeDescriptor(cd);
593 } else if (md instanceof MethodDescriptor) {
594 typetolookin=new TypeDescriptor(((MethodDescriptor)md).getClassDesc());
596 /* If this a task descriptor we throw an error at this point */
597 throw new Error("Unknown method call to "+min.getMethodName()+"in task"+md.getSymbol());
599 if (!typetolookin.isClass())
601 ClassDescriptor classtolookin=typetolookin.getClassDesc();
602 System.out.println("Method name="+min.getMethodName());
603 Set methoddescriptorset=classtolookin.getMethodTable().getSet(min.getMethodName());
604 MethodDescriptor bestmd=null;
606 for(Iterator methodit=methoddescriptorset.iterator();methodit.hasNext();) {
607 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
608 /* Need correct number of parameters */
609 if (min.numArgs()!=currmd.numParameters())
611 for(int i=0;i<min.numArgs();i++) {
612 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
615 /* Method okay so far */
619 if (isMoreSpecific(currmd,bestmd)) {
621 } else if (!isMoreSpecific(bestmd, currmd))
622 throw new Error("No method is most specific");
624 /* Is this more specific than bestmd */
628 throw new Error("No method found for :"+min.printNode(0));
629 min.setMethod(bestmd);
630 /* Check whether we need to set this parameter to implied this */
631 if (!bestmd.isStatic()) {
632 if (min.getExpression()==null) {
633 ExpressionNode en=new NameNode(new NameDescriptor("this"));
634 min.setExpression(en);
635 checkExpressionNode(md, nametable, min.getExpression(), null);
642 void checkOpNode(Descriptor md, SymbolTable nametable, OpNode on, TypeDescriptor td) {
643 checkExpressionNode(md, nametable, on.getLeft(), null);
644 if (on.getRight()!=null)
645 checkExpressionNode(md, nametable, on.getRight(), null);
646 TypeDescriptor ltd=on.getLeft().getType();
647 TypeDescriptor rtd=on.getRight()!=null?on.getRight().getType():null;
648 TypeDescriptor lefttype=null;
649 TypeDescriptor righttype=null;
650 Operation op=on.getOp();
653 case Operation.LOGIC_OR:
654 case Operation.LOGIC_AND:
655 if (!(ltd.isBoolean()&&rtd.isBoolean()))
659 on.setRightType(rtd);
660 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
663 case Operation.BIT_OR:
664 case Operation.BIT_XOR:
665 case Operation.BIT_AND:
666 // 5.6.2 Binary Numeric Promotion
667 //TODO unboxing of reference objects
668 if (ltd.isDouble()||rtd.isDouble())
670 else if (ltd.isFloat()||rtd.isFloat())
672 else if (ltd.isLong()||rtd.isLong())
673 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
675 lefttype=new TypeDescriptor(TypeDescriptor.INT);
678 on.setLeftType(lefttype);
679 on.setRightType(righttype);
680 on.setType(lefttype);
683 case Operation.EQUAL:
684 case Operation.NOTEQUAL:
685 // 5.6.2 Binary Numeric Promotion
686 //TODO unboxing of reference objects
687 if (ltd.isBoolean()||rtd.isBoolean()) {
688 if (!(ltd.isBoolean()&&rtd.isBoolean()))
690 righttype=lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
691 } else if (ltd.isPtr()||rtd.isPtr()) {
692 if (!(ltd.isPtr()&&rtd.isPtr()))
696 } else if (ltd.isDouble()||rtd.isDouble())
697 righttype=lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
698 else if (ltd.isFloat()||rtd.isFloat())
699 righttype=lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
700 else if (ltd.isLong()||rtd.isLong())
701 righttype=lefttype=new TypeDescriptor(TypeDescriptor.LONG);
703 righttype=lefttype=new TypeDescriptor(TypeDescriptor.INT);
705 on.setLeftType(lefttype);
706 on.setRightType(righttype);
707 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
716 // 5.6.2 Binary Numeric Promotion
717 //TODO unboxing of reference objects
718 if (!ltd.isNumber()||!rtd.isNumber())
721 if (ltd.isDouble()||rtd.isDouble())
722 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
723 else if (ltd.isFloat()||rtd.isFloat())
724 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
725 else if (ltd.isLong()||rtd.isLong())
726 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
728 lefttype=new TypeDescriptor(TypeDescriptor.INT);
730 on.setLeftType(lefttype);
731 on.setRightType(righttype);
732 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
736 //TODO: Need special case for strings eventually
743 // 5.6.2 Binary Numeric Promotion
744 //TODO unboxing of reference objects
745 if (!ltd.isNumber()||!rtd.isNumber())
746 throw new Error("Error in "+on.printNode(0));
748 if (ltd.isDouble()||rtd.isDouble())
749 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
750 else if (ltd.isFloat()||rtd.isFloat())
751 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
752 else if (ltd.isLong()||rtd.isLong())
753 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
755 lefttype=new TypeDescriptor(TypeDescriptor.INT);
757 on.setLeftType(lefttype);
758 on.setRightType(righttype);
759 on.setType(lefttype);
762 case Operation.LEFTSHIFT:
763 case Operation.RIGHTSHIFT:
764 if (!rtd.isIntegerType())
766 //5.6.1 Unary Numeric Promotion
767 if (rtd.isByte()||rtd.isShort()||rtd.isInt())
768 righttype=new TypeDescriptor(TypeDescriptor.INT);
772 on.setRightType(righttype);
773 if (!ltd.isIntegerType())
775 case Operation.UNARYPLUS:
776 case Operation.UNARYMINUS:
777 /* case Operation.POSTINC:
778 case Operation.POSTDEC:
779 case Operation.PREINC:
780 case Operation.PREDEC:*/
783 //5.6.1 Unary Numeric Promotion
784 if (ltd.isByte()||ltd.isShort()||ltd.isInt())
785 lefttype=new TypeDescriptor(TypeDescriptor.INT);
788 on.setLeftType(lefttype);
789 on.setType(lefttype);
796 if (!typeutil.isSuperorType(td, on.getType())) {
797 System.out.println(td);
798 System.out.println(on.getType());
799 throw new Error("Type of rside not compatible with type of lside"+on.printNode(0));