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) {
83 return; /* No flag effects to check */
84 for(int i=0;i<vfe.size();i++) {
85 FlagEffects fe=(FlagEffects) vfe.get(i);
86 String varname=fe.getName();
87 //Make sure the variable is declared as a parameter to the task
88 VarDescriptor vd=(VarDescriptor)td.getParameterTable().get(varname);
90 throw new Error("Parameter "+varname+" in Flag Effects not declared");
93 //Make sure it correspods to a class
94 TypeDescriptor type_d=vd.getType();
95 if (!type_d.isClass())
96 throw new Error("Cannot have non-object argument for flag_effect");
98 ClassDescriptor cd=type_d.getClassDesc();
99 for(int j=0;j<fe.numEffects();j++) {
100 FlagEffect flag=fe.getEffect(j);
101 String name=flag.getName();
102 FlagDescriptor flag_d=(FlagDescriptor)cd.getFlagTable().get(name);
103 //Make sure the flag is declared
105 throw new Error("Flag descriptor "+name+" undefined in class: "+cd.getSymbol());
106 flag.setFlag(flag_d);
111 public void checkTask(TaskDescriptor td) {
112 for(int i=0;i<td.numParameters();i++) {
113 /* Check that parameter is well typed */
114 TypeDescriptor param_type=td.getParamType(i);
115 checkTypeDescriptor(param_type);
117 /* Check the parameter's flag expression is well formed */
118 FlagExpressionNode fen=td.getFlag(td.getParameter(i));
119 if (!param_type.isClass())
120 throw new Error("Cannot have non-object argument to a task");
121 ClassDescriptor cd=param_type.getClassDesc();
122 checkFlagExpressionNode(cd, fen);
123 checkFlagEffects(td, td.getFlagEffects());
125 /* Check that the task code is valid */
126 BlockNode bn=state.getMethodBody(td);
127 checkBlockNode(td, td.getParameterTable(),bn);
131 public void checkFlagExpressionNode(ClassDescriptor cd, FlagExpressionNode fen) {
133 case Kind.FlagOpNode:
135 FlagOpNode fon=(FlagOpNode)fen;
136 checkFlagExpressionNode(cd, fon.getLeft());
137 if (fon.getRight()!=null)
138 checkFlagExpressionNode(cd, fon.getRight());
143 FlagNode fn=(FlagNode)fen;
144 String name=fn.getFlagName();
145 FlagDescriptor fd=(FlagDescriptor)cd.getFlagTable().get(name);
147 throw new Error("Undeclared flag: "+name);
152 throw new Error("Unrecognized FlagExpressionNode");
156 public void checkMethod(ClassDescriptor cd, MethodDescriptor md) {
157 /* Check return type */
158 if (!md.isConstructor())
159 if (!md.getReturnType().isVoid())
160 checkTypeDescriptor(md.getReturnType());
162 for(int i=0;i<md.numParameters();i++) {
163 TypeDescriptor param_type=md.getParamType(i);
164 checkTypeDescriptor(param_type);
166 /* Link the naming environments */
167 if (!md.isStatic()) /* Fields aren't accessible directly in a static method, so don't link in this table */
168 md.getParameterTable().setParent(cd.getFieldTable());
170 if (!md.isStatic()) {
171 VarDescriptor thisvd=new VarDescriptor(new TypeDescriptor(cd),"this");
176 public void checkMethodBody(ClassDescriptor cd, MethodDescriptor md) {
177 System.out.println("Processing method:"+md);
178 BlockNode bn=state.getMethodBody(md);
179 checkBlockNode(md, md.getParameterTable(),bn);
182 public void checkBlockNode(Descriptor md, SymbolTable nametable, BlockNode bn) {
183 /* Link in the naming environment */
184 bn.getVarTable().setParent(nametable);
185 for(int i=0;i<bn.size();i++) {
186 BlockStatementNode bsn=bn.get(i);
187 checkBlockStatementNode(md, bn.getVarTable(),bsn);
191 public void checkBlockStatementNode(Descriptor md, SymbolTable nametable, BlockStatementNode bsn) {
193 case Kind.BlockExpressionNode:
194 checkBlockExpressionNode(md, nametable,(BlockExpressionNode)bsn);
197 case Kind.DeclarationNode:
198 checkDeclarationNode(md, nametable, (DeclarationNode)bsn);
201 case Kind.IfStatementNode:
202 checkIfStatementNode(md, nametable, (IfStatementNode)bsn);
206 checkLoopNode(md, nametable, (LoopNode)bsn);
209 case Kind.ReturnNode:
210 checkReturnNode(md, nametable, (ReturnNode)bsn);
213 case Kind.TaskExitNode:
214 checkTaskExitNode(md, nametable, (TaskExitNode)bsn);
217 case Kind.SubBlockNode:
218 checkSubBlockNode(md, nametable, (SubBlockNode)bsn);
224 void checkBlockExpressionNode(Descriptor md, SymbolTable nametable, BlockExpressionNode ben) {
225 checkExpressionNode(md, nametable, ben.getExpression(), null);
228 void checkDeclarationNode(Descriptor md, SymbolTable nametable, DeclarationNode dn) {
229 VarDescriptor vd=dn.getVarDescriptor();
230 checkTypeDescriptor(vd.getType());
231 Descriptor d=nametable.get(vd.getSymbol());
233 (d instanceof FieldDescriptor)) {
236 throw new Error(vd.getSymbol()+" defined a second time");
237 if (dn.getExpression()!=null)
238 checkExpressionNode(md, nametable, dn.getExpression(), vd.getType());
241 void checkSubBlockNode(Descriptor md, SymbolTable nametable, SubBlockNode sbn) {
242 checkBlockNode(md, nametable, sbn.getBlockNode());
245 void checkReturnNode(Descriptor d, SymbolTable nametable, ReturnNode rn) {
246 if (d instanceof TaskDescriptor)
247 throw new Error("Illegal return appears in Task: "+d.getSymbol());
248 MethodDescriptor md=(MethodDescriptor)d;
249 if (rn.getReturnExpression()!=null)
250 checkExpressionNode(md, nametable, rn.getReturnExpression(), md.getReturnType());
252 if (md.getReturnType()!=null&&!md.getReturnType().isVoid())
253 throw new Error("Need to return something for "+md);
256 void checkTaskExitNode(Descriptor md, SymbolTable nametable, TaskExitNode ten) {
257 if (md instanceof MethodDescriptor)
258 throw new Error("Illegal taskexit appears in Method: "+md.getSymbol());
259 checkFlagEffects((TaskDescriptor)md, ten.getFlagEffects());
262 void checkIfStatementNode(Descriptor md, SymbolTable nametable, IfStatementNode isn) {
263 checkExpressionNode(md, nametable, isn.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
264 checkBlockNode(md, nametable, isn.getTrueBlock());
265 if (isn.getFalseBlock()!=null)
266 checkBlockNode(md, nametable, isn.getFalseBlock());
269 void checkExpressionNode(Descriptor md, SymbolTable nametable, ExpressionNode en, TypeDescriptor td) {
271 case Kind.AssignmentNode:
272 checkAssignmentNode(md,nametable,(AssignmentNode)en,td);
275 checkCastNode(md,nametable,(CastNode)en,td);
277 case Kind.CreateObjectNode:
278 checkCreateObjectNode(md,nametable,(CreateObjectNode)en,td);
280 case Kind.FieldAccessNode:
281 checkFieldAccessNode(md,nametable,(FieldAccessNode)en,td);
283 case Kind.ArrayAccessNode:
284 checkArrayAccessNode(md,nametable,(ArrayAccessNode)en,td);
286 case Kind.LiteralNode:
287 checkLiteralNode(md,nametable,(LiteralNode)en,td);
289 case Kind.MethodInvokeNode:
290 checkMethodInvokeNode(md,nametable,(MethodInvokeNode)en,td);
293 checkNameNode(md,nametable,(NameNode)en,td);
296 checkOpNode(md,nametable,(OpNode)en,td);
302 void checkCastNode(Descriptor md, SymbolTable nametable, CastNode cn, TypeDescriptor td) {
303 /* Get type descriptor */
304 if (cn.getType()==null) {
305 NameDescriptor typenamed=cn.getTypeName().getName();
306 String typename=typenamed.toString();
307 TypeDescriptor ntd=new TypeDescriptor(typeutil.getClass(typename));
311 /* Check the type descriptor */
312 TypeDescriptor cast_type=cn.getType();
313 checkTypeDescriptor(cast_type);
317 if (!typeutil.isSuperorType(td,cast_type))
318 throw new Error("Cast node returns "+cast_type+", but need "+td);
321 ExpressionNode en=cn.getExpression();
322 checkExpressionNode(md, nametable, en, null);
323 TypeDescriptor etd=en.getType();
324 if (typeutil.isSuperorType(cast_type,etd)) /* Cast trivially succeeds */
327 if (typeutil.isSuperorType(etd,cast_type)) /* Cast may succeed */
330 /* Different branches */
331 /* TODO: change if add interfaces */
332 throw new Error("Cast will always fail\n"+cn.printNode(0));
335 void checkFieldAccessNode(Descriptor md, SymbolTable nametable, FieldAccessNode fan, TypeDescriptor td) {
336 ExpressionNode left=fan.getExpression();
337 checkExpressionNode(md,nametable,left,null);
338 TypeDescriptor ltd=left.getType();
339 String fieldname=fan.getFieldName();
341 FieldDescriptor fd=null;
342 if (ltd.isArray()&&fieldname.equals("length"))
343 fd=FieldDescriptor.arrayLength;
345 fd=(FieldDescriptor) ltd.getClassDesc().getFieldTable().get(fieldname);
347 throw new Error("Unknown field "+fieldname);
350 if (!typeutil.isSuperorType(td,fan.getType()))
351 throw new Error("Field node returns "+fan.getType()+", but need "+td);
354 void checkArrayAccessNode(Descriptor md, SymbolTable nametable, ArrayAccessNode aan, TypeDescriptor td) {
355 ExpressionNode left=aan.getExpression();
356 checkExpressionNode(md,nametable,left,null);
358 checkExpressionNode(md,nametable,aan.getIndex(),new TypeDescriptor(TypeDescriptor.INT));
359 TypeDescriptor ltd=left.getType();
362 if (!typeutil.isSuperorType(td,aan.getType()))
363 throw new Error("Field node returns "+aan.getType()+", but need "+td);
366 void checkLiteralNode(Descriptor md, SymbolTable nametable, LiteralNode ln, TypeDescriptor td) {
367 /* Resolve the type */
368 Object o=ln.getValue();
369 if (ln.getTypeString().equals("null")) {
370 ln.setType(new TypeDescriptor(TypeDescriptor.NULL));
371 } else if (o instanceof Integer) {
372 ln.setType(new TypeDescriptor(TypeDescriptor.INT));
373 } else if (o instanceof Long) {
374 ln.setType(new TypeDescriptor(TypeDescriptor.LONG));
375 } else if (o instanceof Float) {
376 ln.setType(new TypeDescriptor(TypeDescriptor.FLOAT));
377 } else if (o instanceof Boolean) {
378 ln.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
379 } else if (o instanceof Double) {
380 ln.setType(new TypeDescriptor(TypeDescriptor.DOUBLE));
381 } else if (o instanceof Character) {
382 ln.setType(new TypeDescriptor(TypeDescriptor.CHAR));
383 } else if (o instanceof String) {
384 ln.setType(new TypeDescriptor(typeutil.getClass(TypeUtil.StringClass)));
388 if (!typeutil.isSuperorType(td,ln.getType()))
389 throw new Error("Field node returns "+ln.getType()+", but need "+td);
392 void checkNameNode(Descriptor md, SymbolTable nametable, NameNode nn, TypeDescriptor td) {
393 NameDescriptor nd=nn.getName();
394 if (nd.getBase()!=null) {
396 /* Rewrite NameNode */
397 ExpressionNode en=translateNameDescriptorintoExpression(nd);
398 nn.setExpression(en);
399 checkExpressionNode(md,nametable,en,td);
401 String varname=nd.toString();
402 Descriptor d=(Descriptor)nametable.get(varname);
404 throw new Error("Name "+varname+" undefined");
406 if (d instanceof VarDescriptor) {
407 nn.setVar((VarDescriptor)d);
408 } else if (d instanceof FieldDescriptor) {
409 nn.setField((FieldDescriptor)d);
410 nn.setVar((VarDescriptor)nametable.get("this")); /* Need a pointer to this */
413 if (!typeutil.isSuperorType(td,nn.getType()))
414 throw new Error("Field node returns "+nn.getType()+", but need "+td);
418 void checkAssignmentNode(Descriptor md, SymbolTable nametable, AssignmentNode an, TypeDescriptor td) {
419 boolean postinc=true;
420 if (an.getOperation().getBaseOp()==null||
421 (an.getOperation().getBaseOp().getOp()!=Operation.POSTINC&&
422 an.getOperation().getBaseOp().getOp()!=Operation.POSTDEC))
426 checkExpressionNode(md, nametable, an.getSrc() ,td);
427 //TODO: Need check on validity of operation here
428 if (!((an.getDest() instanceof FieldAccessNode)||
429 (an.getDest() instanceof ArrayAccessNode)||
430 (an.getDest() instanceof NameNode)))
431 throw new Error("Bad lside in "+an.printNode(0));
432 checkExpressionNode(md, nametable, an.getDest(), null);
434 /* We want parameter variables to tasks to be immutable */
435 if (md instanceof TaskDescriptor) {
436 if (an.getDest() instanceof NameNode) {
437 NameNode nn=(NameNode)an.getDest();
438 if (nn.getVar()!=null) {
439 if (((TaskDescriptor)md).getParameterTable().contains(nn.getVar().getSymbol()))
440 throw new Error("Can't modify parameter "+nn.getVar()+ " to task "+td.getSymbol());
445 if (!postinc&&!typeutil.isSuperorType(an.getDest().getType(),an.getSrc().getType())) {
446 throw new Error("Type of rside ("+an.getSrc().getType()+") not compatible with type of lside ("+an.getDest().getType()+")"+an.printNode(0));
450 void checkLoopNode(Descriptor md, SymbolTable nametable, LoopNode ln) {
451 if (ln.getType()==LoopNode.WHILELOOP||ln.getType()==LoopNode.DOWHILELOOP) {
452 checkExpressionNode(md, nametable, ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
453 checkBlockNode(md, nametable, ln.getBody());
456 /* Link in the initializer naming environment */
457 BlockNode bn=ln.getInitializer();
458 bn.getVarTable().setParent(nametable);
459 for(int i=0;i<bn.size();i++) {
460 BlockStatementNode bsn=bn.get(i);
461 checkBlockStatementNode(md, bn.getVarTable(),bsn);
463 //check the condition
464 checkExpressionNode(md, bn.getVarTable(), ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
465 checkBlockNode(md, bn.getVarTable(), ln.getBody());
466 checkBlockNode(md, bn.getVarTable(), ln.getUpdate());
471 void checkCreateObjectNode(Descriptor md, SymbolTable nametable, CreateObjectNode con, TypeDescriptor td) {
472 TypeDescriptor[] tdarray=new TypeDescriptor[con.numArgs()];
473 for(int i=0;i<con.numArgs();i++) {
474 ExpressionNode en=con.getArg(i);
475 checkExpressionNode(md,nametable,en,null);
476 tdarray[i]=en.getType();
479 TypeDescriptor typetolookin=con.getType();
480 checkTypeDescriptor(typetolookin);
482 /* Check flag effects */
483 if (con.getFlagEffects()!=null) {
484 FlagEffects fe=con.getFlagEffects();
485 ClassDescriptor cd=typetolookin.getClassDesc();
487 for(int j=0;j<fe.numEffects();j++) {
488 FlagEffect flag=fe.getEffect(j);
489 String name=flag.getName();
490 FlagDescriptor flag_d=(FlagDescriptor)cd.getFlagTable().get(name);
491 //Make sure the flag is declared
493 throw new Error("Flag descriptor "+name+" undefined in class: "+cd.getSymbol());
494 flag.setFlag(flag_d);
499 if ((!typetolookin.isClass())&&(!typetolookin.isArray()))
500 throw new Error("Can't allocate primitive type:"+con.printNode(0));
502 if (!typetolookin.isArray()) {
503 //Array's don't need constructor calls
504 ClassDescriptor classtolookin=typetolookin.getClassDesc();
505 System.out.println("Looking for "+typetolookin.getSymbol());
506 System.out.println(classtolookin.getMethodTable());
508 Set methoddescriptorset=classtolookin.getMethodTable().getSet(typetolookin.getSymbol());
509 MethodDescriptor bestmd=null;
511 for(Iterator methodit=methoddescriptorset.iterator();methodit.hasNext();) {
512 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
513 /* Need correct number of parameters */
514 System.out.println("Examining: "+currmd);
515 if (con.numArgs()!=currmd.numParameters())
517 for(int i=0;i<con.numArgs();i++) {
518 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
521 /* Method okay so far */
525 if (isMoreSpecific(currmd,bestmd)) {
527 } else if (!isMoreSpecific(bestmd, currmd))
528 throw new Error("No method is most specific");
530 /* Is this more specific than bestmd */
534 throw new Error("No method found for "+con.printNode(0));
535 con.setConstructor(bestmd);
540 /** Check to see if md1 is more specific than md2... Informally
541 if md2 could always be called given the arguments passed into
544 boolean isMoreSpecific(MethodDescriptor md1, MethodDescriptor md2) {
545 /* Checks if md1 is more specific than md2 */
546 if (md1.numParameters()!=md2.numParameters())
548 for(int i=0;i<md1.numParameters();i++) {
549 if (!typeutil.isSuperorType(md2.getParamType(i), md1.getParamType(i)))
552 if (!typeutil.isSuperorType(md2.getReturnType(), md1.getReturnType()))
557 ExpressionNode translateNameDescriptorintoExpression(NameDescriptor nd) {
558 String id=nd.getIdentifier();
559 NameDescriptor base=nd.getBase();
561 return new NameNode(nd);
563 return new FieldAccessNode(translateNameDescriptorintoExpression(base),id);
567 void checkMethodInvokeNode(Descriptor md, SymbolTable nametable, MethodInvokeNode min, TypeDescriptor td) {
568 /*Typecheck subexpressions
569 and get types for expressions*/
571 TypeDescriptor[] tdarray=new TypeDescriptor[min.numArgs()];
572 for(int i=0;i<min.numArgs();i++) {
573 ExpressionNode en=min.getArg(i);
574 checkExpressionNode(md,nametable,en,null);
575 tdarray[i]=en.getType();
577 TypeDescriptor typetolookin=null;
578 if (min.getExpression()!=null) {
579 checkExpressionNode(md,nametable,min.getExpression(),null);
580 typetolookin=min.getExpression().getType();
581 } else if (min.getBaseName()!=null) {
582 String rootname=min.getBaseName().getRoot();
583 if (nametable.get(rootname)!=null) {
584 //we have an expression
585 min.setExpression(translateNameDescriptorintoExpression(min.getBaseName()));
586 checkExpressionNode(md, nametable, min.getExpression(), null);
587 typetolookin=min.getExpression().getType();
590 ClassDescriptor cd=typeutil.getClass(min.getBaseName().getSymbol());
592 throw new Error(min.getBaseName()+" undefined");
593 typetolookin=new TypeDescriptor(cd);
595 } else if (md instanceof MethodDescriptor) {
596 typetolookin=new TypeDescriptor(((MethodDescriptor)md).getClassDesc());
598 /* If this a task descriptor we throw an error at this point */
599 throw new Error("Unknown method call to "+min.getMethodName()+"in task"+md.getSymbol());
601 if (!typetolookin.isClass())
603 ClassDescriptor classtolookin=typetolookin.getClassDesc();
604 System.out.println("Method name="+min.getMethodName());
605 Set methoddescriptorset=classtolookin.getMethodTable().getSet(min.getMethodName());
606 MethodDescriptor bestmd=null;
608 for(Iterator methodit=methoddescriptorset.iterator();methodit.hasNext();) {
609 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
610 /* Need correct number of parameters */
611 if (min.numArgs()!=currmd.numParameters())
613 for(int i=0;i<min.numArgs();i++) {
614 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
617 /* Method okay so far */
621 if (isMoreSpecific(currmd,bestmd)) {
623 } else if (!isMoreSpecific(bestmd, currmd))
624 throw new Error("No method is most specific");
626 /* Is this more specific than bestmd */
630 throw new Error("No method found for :"+min.printNode(0));
631 min.setMethod(bestmd);
632 /* Check whether we need to set this parameter to implied this */
633 if (!bestmd.isStatic()) {
634 if (min.getExpression()==null) {
635 ExpressionNode en=new NameNode(new NameDescriptor("this"));
636 min.setExpression(en);
637 checkExpressionNode(md, nametable, min.getExpression(), null);
644 void checkOpNode(Descriptor md, SymbolTable nametable, OpNode on, TypeDescriptor td) {
645 checkExpressionNode(md, nametable, on.getLeft(), null);
646 if (on.getRight()!=null)
647 checkExpressionNode(md, nametable, on.getRight(), null);
648 TypeDescriptor ltd=on.getLeft().getType();
649 TypeDescriptor rtd=on.getRight()!=null?on.getRight().getType():null;
650 TypeDescriptor lefttype=null;
651 TypeDescriptor righttype=null;
652 Operation op=on.getOp();
655 case Operation.LOGIC_OR:
656 case Operation.LOGIC_AND:
657 if (!(ltd.isBoolean()&&rtd.isBoolean()))
661 on.setRightType(rtd);
662 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
665 case Operation.BIT_OR:
666 case Operation.BIT_XOR:
667 case Operation.BIT_AND:
668 // 5.6.2 Binary Numeric Promotion
669 //TODO unboxing of reference objects
670 if (ltd.isDouble()||rtd.isDouble())
672 else if (ltd.isFloat()||rtd.isFloat())
674 else if (ltd.isLong()||rtd.isLong())
675 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
677 lefttype=new TypeDescriptor(TypeDescriptor.INT);
680 on.setLeftType(lefttype);
681 on.setRightType(righttype);
682 on.setType(lefttype);
685 case Operation.EQUAL:
686 case Operation.NOTEQUAL:
687 // 5.6.2 Binary Numeric Promotion
688 //TODO unboxing of reference objects
689 if (ltd.isBoolean()||rtd.isBoolean()) {
690 if (!(ltd.isBoolean()&&rtd.isBoolean()))
692 righttype=lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
693 } else if (ltd.isPtr()||ltd.isArray()||rtd.isPtr()||rtd.isArray()) {
694 if (!((ltd.isPtr()||ltd.isArray())&&(rtd.isPtr()||rtd.isArray())))
698 } else if (ltd.isDouble()||rtd.isDouble())
699 righttype=lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
700 else if (ltd.isFloat()||rtd.isFloat())
701 righttype=lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
702 else if (ltd.isLong()||rtd.isLong())
703 righttype=lefttype=new TypeDescriptor(TypeDescriptor.LONG);
705 righttype=lefttype=new TypeDescriptor(TypeDescriptor.INT);
707 on.setLeftType(lefttype);
708 on.setRightType(righttype);
709 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
718 // 5.6.2 Binary Numeric Promotion
719 //TODO unboxing of reference objects
720 if (!ltd.isNumber()||!rtd.isNumber())
723 if (ltd.isDouble()||rtd.isDouble())
724 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
725 else if (ltd.isFloat()||rtd.isFloat())
726 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
727 else if (ltd.isLong()||rtd.isLong())
728 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
730 lefttype=new TypeDescriptor(TypeDescriptor.INT);
732 on.setLeftType(lefttype);
733 on.setRightType(righttype);
734 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
738 //TODO: Need special case for strings eventually
745 // 5.6.2 Binary Numeric Promotion
746 //TODO unboxing of reference objects
747 if (!ltd.isNumber()||!rtd.isNumber())
748 throw new Error("Error in "+on.printNode(0));
750 if (ltd.isDouble()||rtd.isDouble())
751 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
752 else if (ltd.isFloat()||rtd.isFloat())
753 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
754 else if (ltd.isLong()||rtd.isLong())
755 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
757 lefttype=new TypeDescriptor(TypeDescriptor.INT);
759 on.setLeftType(lefttype);
760 on.setRightType(righttype);
761 on.setType(lefttype);
764 case Operation.LEFTSHIFT:
765 case Operation.RIGHTSHIFT:
766 if (!rtd.isIntegerType())
768 //5.6.1 Unary Numeric Promotion
769 if (rtd.isByte()||rtd.isShort()||rtd.isInt())
770 righttype=new TypeDescriptor(TypeDescriptor.INT);
774 on.setRightType(righttype);
775 if (!ltd.isIntegerType())
777 case Operation.UNARYPLUS:
778 case Operation.UNARYMINUS:
779 /* case Operation.POSTINC:
780 case Operation.POSTDEC:
781 case Operation.PREINC:
782 case Operation.PREDEC:*/
785 //5.6.1 Unary Numeric Promotion
786 if (ltd.isByte()||ltd.isShort()||ltd.isInt())
787 lefttype=new TypeDescriptor(TypeDescriptor.INT);
790 on.setLeftType(lefttype);
791 on.setType(lefttype);
798 if (!typeutil.isSuperorType(td, on.getType())) {
799 System.out.println(td);
800 System.out.println(on.getType());
801 throw new Error("Type of rside not compatible with type of lside"+on.printNode(0));