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 flag.setFlag(flag_d);
124 public void checkTask(TaskDescriptor td) {
125 for(int i=0;i<td.numParameters();i++) {
126 /* Check that parameter is well typed */
127 TypeDescriptor param_type=td.getParamType(i);
128 checkTypeDescriptor(param_type);
130 /* Check the parameter's flag expression is well formed */
131 FlagExpressionNode fen=td.getFlag(td.getParameter(i));
132 if (!param_type.isClass())
133 throw new Error("Cannot have non-object argument to a task");
134 ClassDescriptor cd=param_type.getClassDesc();
135 checkFlagExpressionNode(cd, fen);
136 checkFlagEffects(td, td.getFlagEffects());
138 /* Check that the task code is valid */
139 BlockNode bn=state.getMethodBody(td);
140 checkBlockNode(td, td.getParameterTable(),bn);
144 public void checkFlagExpressionNode(ClassDescriptor cd, FlagExpressionNode fen) {
146 case Kind.FlagOpNode:
148 FlagOpNode fon=(FlagOpNode)fen;
149 checkFlagExpressionNode(cd, fon.getLeft());
150 if (fon.getRight()!=null)
151 checkFlagExpressionNode(cd, fon.getRight());
156 FlagNode fn=(FlagNode)fen;
157 String name=fn.getFlagName();
158 FlagDescriptor fd=(FlagDescriptor)cd.getFlagTable().get(name);
160 throw new Error("Undeclared flag: "+name);
165 throw new Error("Unrecognized FlagExpressionNode");
169 public void checkMethod(ClassDescriptor cd, MethodDescriptor md) {
170 /* Check return type */
171 if (!md.isConstructor())
172 if (!md.getReturnType().isVoid())
173 checkTypeDescriptor(md.getReturnType());
175 for(int i=0;i<md.numParameters();i++) {
176 TypeDescriptor param_type=md.getParamType(i);
177 checkTypeDescriptor(param_type);
179 /* Link the naming environments */
180 if (!md.isStatic()) /* Fields aren't accessible directly in a static method, so don't link in this table */
181 md.getParameterTable().setParent(cd.getFieldTable());
183 if (!md.isStatic()) {
184 VarDescriptor thisvd=new VarDescriptor(new TypeDescriptor(cd),"this");
189 public void checkMethodBody(ClassDescriptor cd, MethodDescriptor md) {
190 System.out.println("Processing method:"+md);
191 BlockNode bn=state.getMethodBody(md);
192 checkBlockNode(md, md.getParameterTable(),bn);
195 public void checkBlockNode(Descriptor md, SymbolTable nametable, BlockNode bn) {
196 /* Link in the naming environment */
197 bn.getVarTable().setParent(nametable);
198 for(int i=0;i<bn.size();i++) {
199 BlockStatementNode bsn=bn.get(i);
200 checkBlockStatementNode(md, bn.getVarTable(),bsn);
204 public void checkBlockStatementNode(Descriptor md, SymbolTable nametable, BlockStatementNode bsn) {
206 case Kind.BlockExpressionNode:
207 checkBlockExpressionNode(md, nametable,(BlockExpressionNode)bsn);
210 case Kind.DeclarationNode:
211 checkDeclarationNode(md, nametable, (DeclarationNode)bsn);
214 case Kind.IfStatementNode:
215 checkIfStatementNode(md, nametable, (IfStatementNode)bsn);
219 checkLoopNode(md, nametable, (LoopNode)bsn);
222 case Kind.ReturnNode:
223 checkReturnNode(md, nametable, (ReturnNode)bsn);
226 case Kind.TaskExitNode:
227 checkTaskExitNode(md, nametable, (TaskExitNode)bsn);
230 case Kind.SubBlockNode:
231 checkSubBlockNode(md, nametable, (SubBlockNode)bsn);
237 void checkBlockExpressionNode(Descriptor md, SymbolTable nametable, BlockExpressionNode ben) {
238 checkExpressionNode(md, nametable, ben.getExpression(), null);
241 void checkDeclarationNode(Descriptor md, SymbolTable nametable, DeclarationNode dn) {
242 VarDescriptor vd=dn.getVarDescriptor();
243 checkTypeDescriptor(vd.getType());
244 Descriptor d=nametable.get(vd.getSymbol());
246 (d instanceof FieldDescriptor)) {
249 throw new Error(vd.getSymbol()+" defined a second time");
250 if (dn.getExpression()!=null)
251 checkExpressionNode(md, nametable, dn.getExpression(), vd.getType());
254 void checkSubBlockNode(Descriptor md, SymbolTable nametable, SubBlockNode sbn) {
255 checkBlockNode(md, nametable, sbn.getBlockNode());
258 void checkReturnNode(Descriptor d, SymbolTable nametable, ReturnNode rn) {
259 if (d instanceof TaskDescriptor)
260 throw new Error("Illegal return appears in Task: "+d.getSymbol());
261 MethodDescriptor md=(MethodDescriptor)d;
262 if (rn.getReturnExpression()!=null)
263 checkExpressionNode(md, nametable, rn.getReturnExpression(), md.getReturnType());
265 if (md.getReturnType()!=null&&!md.getReturnType().isVoid())
266 throw new Error("Need to return something for "+md);
269 void checkTaskExitNode(Descriptor md, SymbolTable nametable, TaskExitNode ten) {
270 if (md instanceof MethodDescriptor)
271 throw new Error("Illegal taskexit appears in Method: "+md.getSymbol());
272 checkFlagEffects((TaskDescriptor)md, ten.getFlagEffects());
273 checkConstraintCheck((TaskDescriptor) md, nametable, ten.getChecks());
276 void checkIfStatementNode(Descriptor md, SymbolTable nametable, IfStatementNode isn) {
277 checkExpressionNode(md, nametable, isn.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
278 checkBlockNode(md, nametable, isn.getTrueBlock());
279 if (isn.getFalseBlock()!=null)
280 checkBlockNode(md, nametable, isn.getFalseBlock());
283 void checkExpressionNode(Descriptor md, SymbolTable nametable, ExpressionNode en, TypeDescriptor td) {
285 case Kind.AssignmentNode:
286 checkAssignmentNode(md,nametable,(AssignmentNode)en,td);
289 checkCastNode(md,nametable,(CastNode)en,td);
291 case Kind.CreateObjectNode:
292 checkCreateObjectNode(md,nametable,(CreateObjectNode)en,td);
294 case Kind.FieldAccessNode:
295 checkFieldAccessNode(md,nametable,(FieldAccessNode)en,td);
297 case Kind.ArrayAccessNode:
298 checkArrayAccessNode(md,nametable,(ArrayAccessNode)en,td);
300 case Kind.LiteralNode:
301 checkLiteralNode(md,nametable,(LiteralNode)en,td);
303 case Kind.MethodInvokeNode:
304 checkMethodInvokeNode(md,nametable,(MethodInvokeNode)en,td);
307 checkNameNode(md,nametable,(NameNode)en,td);
310 checkOpNode(md,nametable,(OpNode)en,td);
316 void checkCastNode(Descriptor md, SymbolTable nametable, CastNode cn, TypeDescriptor td) {
317 /* Get type descriptor */
318 if (cn.getType()==null) {
319 NameDescriptor typenamed=cn.getTypeName().getName();
320 String typename=typenamed.toString();
321 TypeDescriptor ntd=new TypeDescriptor(typeutil.getClass(typename));
325 /* Check the type descriptor */
326 TypeDescriptor cast_type=cn.getType();
327 checkTypeDescriptor(cast_type);
331 if (!typeutil.isSuperorType(td,cast_type))
332 throw new Error("Cast node returns "+cast_type+", but need "+td);
335 ExpressionNode en=cn.getExpression();
336 checkExpressionNode(md, nametable, en, null);
337 TypeDescriptor etd=en.getType();
338 if (typeutil.isSuperorType(cast_type,etd)) /* Cast trivially succeeds */
341 if (typeutil.isSuperorType(etd,cast_type)) /* Cast may succeed */
344 /* Different branches */
345 /* TODO: change if add interfaces */
346 throw new Error("Cast will always fail\n"+cn.printNode(0));
349 void checkFieldAccessNode(Descriptor md, SymbolTable nametable, FieldAccessNode fan, TypeDescriptor td) {
350 ExpressionNode left=fan.getExpression();
351 checkExpressionNode(md,nametable,left,null);
352 TypeDescriptor ltd=left.getType();
353 String fieldname=fan.getFieldName();
355 FieldDescriptor fd=null;
356 if (ltd.isArray()&&fieldname.equals("length"))
357 fd=FieldDescriptor.arrayLength;
359 fd=(FieldDescriptor) ltd.getClassDesc().getFieldTable().get(fieldname);
361 throw new Error("Unknown field "+fieldname);
364 if (!typeutil.isSuperorType(td,fan.getType()))
365 throw new Error("Field node returns "+fan.getType()+", but need "+td);
368 void checkArrayAccessNode(Descriptor md, SymbolTable nametable, ArrayAccessNode aan, TypeDescriptor td) {
369 ExpressionNode left=aan.getExpression();
370 checkExpressionNode(md,nametable,left,null);
372 checkExpressionNode(md,nametable,aan.getIndex(),new TypeDescriptor(TypeDescriptor.INT));
373 TypeDescriptor ltd=left.getType();
376 if (!typeutil.isSuperorType(td,aan.getType()))
377 throw new Error("Field node returns "+aan.getType()+", but need "+td);
380 void checkLiteralNode(Descriptor md, SymbolTable nametable, LiteralNode ln, TypeDescriptor td) {
381 /* Resolve the type */
382 Object o=ln.getValue();
383 if (ln.getTypeString().equals("null")) {
384 ln.setType(new TypeDescriptor(TypeDescriptor.NULL));
385 } else if (o instanceof Integer) {
386 ln.setType(new TypeDescriptor(TypeDescriptor.INT));
387 } else if (o instanceof Long) {
388 ln.setType(new TypeDescriptor(TypeDescriptor.LONG));
389 } else if (o instanceof Float) {
390 ln.setType(new TypeDescriptor(TypeDescriptor.FLOAT));
391 } else if (o instanceof Boolean) {
392 ln.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
393 } else if (o instanceof Double) {
394 ln.setType(new TypeDescriptor(TypeDescriptor.DOUBLE));
395 } else if (o instanceof Character) {
396 ln.setType(new TypeDescriptor(TypeDescriptor.CHAR));
397 } else if (o instanceof String) {
398 ln.setType(new TypeDescriptor(typeutil.getClass(TypeUtil.StringClass)));
402 if (!typeutil.isSuperorType(td,ln.getType()))
403 throw new Error("Field node returns "+ln.getType()+", but need "+td);
406 void checkNameNode(Descriptor md, SymbolTable nametable, NameNode nn, TypeDescriptor td) {
407 NameDescriptor nd=nn.getName();
408 if (nd.getBase()!=null) {
410 /* Rewrite NameNode */
411 ExpressionNode en=translateNameDescriptorintoExpression(nd);
412 nn.setExpression(en);
413 checkExpressionNode(md,nametable,en,td);
415 String varname=nd.toString();
416 Descriptor d=(Descriptor)nametable.get(varname);
418 throw new Error("Name "+varname+" undefined");
420 if (d instanceof VarDescriptor) {
421 nn.setVar((VarDescriptor)d);
422 } else if (d instanceof FieldDescriptor) {
423 nn.setField((FieldDescriptor)d);
424 nn.setVar((VarDescriptor)nametable.get("this")); /* Need a pointer to this */
427 if (!typeutil.isSuperorType(td,nn.getType()))
428 throw new Error("Field node returns "+nn.getType()+", but need "+td);
432 void checkAssignmentNode(Descriptor md, SymbolTable nametable, AssignmentNode an, TypeDescriptor td) {
433 boolean postinc=true;
434 if (an.getOperation().getBaseOp()==null||
435 (an.getOperation().getBaseOp().getOp()!=Operation.POSTINC&&
436 an.getOperation().getBaseOp().getOp()!=Operation.POSTDEC))
440 checkExpressionNode(md, nametable, an.getSrc() ,td);
441 //TODO: Need check on validity of operation here
442 if (!((an.getDest() instanceof FieldAccessNode)||
443 (an.getDest() instanceof ArrayAccessNode)||
444 (an.getDest() instanceof NameNode)))
445 throw new Error("Bad lside in "+an.printNode(0));
446 checkExpressionNode(md, nametable, an.getDest(), null);
448 /* We want parameter variables to tasks to be immutable */
449 if (md instanceof TaskDescriptor) {
450 if (an.getDest() instanceof NameNode) {
451 NameNode nn=(NameNode)an.getDest();
452 if (nn.getVar()!=null) {
453 if (((TaskDescriptor)md).getParameterTable().contains(nn.getVar().getSymbol()))
454 throw new Error("Can't modify parameter "+nn.getVar()+ " to task "+td.getSymbol());
459 if (!postinc&&!typeutil.isSuperorType(an.getDest().getType(),an.getSrc().getType())) {
460 throw new Error("Type of rside ("+an.getSrc().getType()+") not compatible with type of lside ("+an.getDest().getType()+")"+an.printNode(0));
464 void checkLoopNode(Descriptor md, SymbolTable nametable, LoopNode ln) {
465 if (ln.getType()==LoopNode.WHILELOOP||ln.getType()==LoopNode.DOWHILELOOP) {
466 checkExpressionNode(md, nametable, ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
467 checkBlockNode(md, nametable, ln.getBody());
470 /* Link in the initializer naming environment */
471 BlockNode bn=ln.getInitializer();
472 bn.getVarTable().setParent(nametable);
473 for(int i=0;i<bn.size();i++) {
474 BlockStatementNode bsn=bn.get(i);
475 checkBlockStatementNode(md, bn.getVarTable(),bsn);
477 //check the condition
478 checkExpressionNode(md, bn.getVarTable(), ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
479 checkBlockNode(md, bn.getVarTable(), ln.getBody());
480 checkBlockNode(md, bn.getVarTable(), ln.getUpdate());
485 void checkCreateObjectNode(Descriptor md, SymbolTable nametable, CreateObjectNode con, TypeDescriptor td) {
486 TypeDescriptor[] tdarray=new TypeDescriptor[con.numArgs()];
487 for(int i=0;i<con.numArgs();i++) {
488 ExpressionNode en=con.getArg(i);
489 checkExpressionNode(md,nametable,en,null);
490 tdarray[i]=en.getType();
493 TypeDescriptor typetolookin=con.getType();
494 checkTypeDescriptor(typetolookin);
496 /* Check flag effects */
497 if (con.getFlagEffects()!=null) {
498 FlagEffects fe=con.getFlagEffects();
499 ClassDescriptor cd=typetolookin.getClassDesc();
501 for(int j=0;j<fe.numEffects();j++) {
502 FlagEffect flag=fe.getEffect(j);
503 String name=flag.getName();
504 FlagDescriptor flag_d=(FlagDescriptor)cd.getFlagTable().get(name);
505 //Make sure the flag is declared
507 throw new Error("Flag descriptor "+name+" undefined in class: "+cd.getSymbol());
508 flag.setFlag(flag_d);
513 if ((!typetolookin.isClass())&&(!typetolookin.isArray()))
514 throw new Error("Can't allocate primitive type:"+con.printNode(0));
516 if (!typetolookin.isArray()) {
517 //Array's don't need constructor calls
518 ClassDescriptor classtolookin=typetolookin.getClassDesc();
519 System.out.println("Looking for "+typetolookin.getSymbol());
520 System.out.println(classtolookin.getMethodTable());
522 Set methoddescriptorset=classtolookin.getMethodTable().getSet(typetolookin.getSymbol());
523 MethodDescriptor bestmd=null;
525 for(Iterator methodit=methoddescriptorset.iterator();methodit.hasNext();) {
526 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
527 /* Need correct number of parameters */
528 System.out.println("Examining: "+currmd);
529 if (con.numArgs()!=currmd.numParameters())
531 for(int i=0;i<con.numArgs();i++) {
532 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
535 /* Method okay so far */
539 if (isMoreSpecific(currmd,bestmd)) {
541 } else if (!isMoreSpecific(bestmd, currmd))
542 throw new Error("No method is most specific");
544 /* Is this more specific than bestmd */
548 throw new Error("No method found for "+con.printNode(0));
549 con.setConstructor(bestmd);
554 /** Check to see if md1 is more specific than md2... Informally
555 if md2 could always be called given the arguments passed into
558 boolean isMoreSpecific(MethodDescriptor md1, MethodDescriptor md2) {
559 /* Checks if md1 is more specific than md2 */
560 if (md1.numParameters()!=md2.numParameters())
562 for(int i=0;i<md1.numParameters();i++) {
563 if (!typeutil.isSuperorType(md2.getParamType(i), md1.getParamType(i)))
566 if (!typeutil.isSuperorType(md2.getReturnType(), md1.getReturnType()))
571 ExpressionNode translateNameDescriptorintoExpression(NameDescriptor nd) {
572 String id=nd.getIdentifier();
573 NameDescriptor base=nd.getBase();
575 return new NameNode(nd);
577 return new FieldAccessNode(translateNameDescriptorintoExpression(base),id);
581 void checkMethodInvokeNode(Descriptor md, SymbolTable nametable, MethodInvokeNode min, TypeDescriptor td) {
582 /*Typecheck subexpressions
583 and get types for expressions*/
585 TypeDescriptor[] tdarray=new TypeDescriptor[min.numArgs()];
586 for(int i=0;i<min.numArgs();i++) {
587 ExpressionNode en=min.getArg(i);
588 checkExpressionNode(md,nametable,en,null);
589 tdarray[i]=en.getType();
591 TypeDescriptor typetolookin=null;
592 if (min.getExpression()!=null) {
593 checkExpressionNode(md,nametable,min.getExpression(),null);
594 typetolookin=min.getExpression().getType();
595 } else if (min.getBaseName()!=null) {
596 String rootname=min.getBaseName().getRoot();
597 if (nametable.get(rootname)!=null) {
598 //we have an expression
599 min.setExpression(translateNameDescriptorintoExpression(min.getBaseName()));
600 checkExpressionNode(md, nametable, min.getExpression(), null);
601 typetolookin=min.getExpression().getType();
604 ClassDescriptor cd=typeutil.getClass(min.getBaseName().getSymbol());
606 throw new Error(min.getBaseName()+" undefined");
607 typetolookin=new TypeDescriptor(cd);
609 } else if (md instanceof MethodDescriptor) {
610 typetolookin=new TypeDescriptor(((MethodDescriptor)md).getClassDesc());
612 /* If this a task descriptor we throw an error at this point */
613 throw new Error("Unknown method call to "+min.getMethodName()+"in task"+md.getSymbol());
615 if (!typetolookin.isClass())
617 ClassDescriptor classtolookin=typetolookin.getClassDesc();
618 System.out.println("Method name="+min.getMethodName());
619 Set methoddescriptorset=classtolookin.getMethodTable().getSet(min.getMethodName());
620 MethodDescriptor bestmd=null;
622 for(Iterator methodit=methoddescriptorset.iterator();methodit.hasNext();) {
623 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
624 /* Need correct number of parameters */
625 if (min.numArgs()!=currmd.numParameters())
627 for(int i=0;i<min.numArgs();i++) {
628 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
631 /* Method okay so far */
635 if (isMoreSpecific(currmd,bestmd)) {
637 } else if (!isMoreSpecific(bestmd, currmd))
638 throw new Error("No method is most specific");
640 /* Is this more specific than bestmd */
644 throw new Error("No method found for :"+min.printNode(0));
645 min.setMethod(bestmd);
646 /* Check whether we need to set this parameter to implied this */
647 if (!bestmd.isStatic()) {
648 if (min.getExpression()==null) {
649 ExpressionNode en=new NameNode(new NameDescriptor("this"));
650 min.setExpression(en);
651 checkExpressionNode(md, nametable, min.getExpression(), null);
658 void checkOpNode(Descriptor md, SymbolTable nametable, OpNode on, TypeDescriptor td) {
659 checkExpressionNode(md, nametable, on.getLeft(), null);
660 if (on.getRight()!=null)
661 checkExpressionNode(md, nametable, on.getRight(), null);
662 TypeDescriptor ltd=on.getLeft().getType();
663 TypeDescriptor rtd=on.getRight()!=null?on.getRight().getType():null;
664 TypeDescriptor lefttype=null;
665 TypeDescriptor righttype=null;
666 Operation op=on.getOp();
669 case Operation.LOGIC_OR:
670 case Operation.LOGIC_AND:
671 if (!(ltd.isBoolean()&&rtd.isBoolean()))
675 on.setRightType(rtd);
676 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
679 case Operation.BIT_OR:
680 case Operation.BIT_XOR:
681 case Operation.BIT_AND:
682 // 5.6.2 Binary Numeric Promotion
683 //TODO unboxing of reference objects
684 if (ltd.isDouble()||rtd.isDouble())
686 else if (ltd.isFloat()||rtd.isFloat())
688 else if (ltd.isLong()||rtd.isLong())
689 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
691 lefttype=new TypeDescriptor(TypeDescriptor.INT);
694 on.setLeftType(lefttype);
695 on.setRightType(righttype);
696 on.setType(lefttype);
699 case Operation.EQUAL:
700 case Operation.NOTEQUAL:
701 // 5.6.2 Binary Numeric Promotion
702 //TODO unboxing of reference objects
703 if (ltd.isBoolean()||rtd.isBoolean()) {
704 if (!(ltd.isBoolean()&&rtd.isBoolean()))
706 righttype=lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
707 } else if (ltd.isPtr()||ltd.isArray()||rtd.isPtr()||rtd.isArray()) {
708 if (!((ltd.isPtr()||ltd.isArray())&&(rtd.isPtr()||rtd.isArray())))
712 } else if (ltd.isDouble()||rtd.isDouble())
713 righttype=lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
714 else if (ltd.isFloat()||rtd.isFloat())
715 righttype=lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
716 else if (ltd.isLong()||rtd.isLong())
717 righttype=lefttype=new TypeDescriptor(TypeDescriptor.LONG);
719 righttype=lefttype=new TypeDescriptor(TypeDescriptor.INT);
721 on.setLeftType(lefttype);
722 on.setRightType(righttype);
723 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
732 // 5.6.2 Binary Numeric Promotion
733 //TODO unboxing of reference objects
734 if (!ltd.isNumber()||!rtd.isNumber())
737 if (ltd.isDouble()||rtd.isDouble())
738 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
739 else if (ltd.isFloat()||rtd.isFloat())
740 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
741 else if (ltd.isLong()||rtd.isLong())
742 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
744 lefttype=new TypeDescriptor(TypeDescriptor.INT);
746 on.setLeftType(lefttype);
747 on.setRightType(righttype);
748 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
752 //TODO: Need special case for strings eventually
759 // 5.6.2 Binary Numeric Promotion
760 //TODO unboxing of reference objects
761 if (!ltd.isNumber()||!rtd.isNumber())
762 throw new Error("Error in "+on.printNode(0));
764 if (ltd.isDouble()||rtd.isDouble())
765 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
766 else if (ltd.isFloat()||rtd.isFloat())
767 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
768 else if (ltd.isLong()||rtd.isLong())
769 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
771 lefttype=new TypeDescriptor(TypeDescriptor.INT);
773 on.setLeftType(lefttype);
774 on.setRightType(righttype);
775 on.setType(lefttype);
778 case Operation.LEFTSHIFT:
779 case Operation.RIGHTSHIFT:
780 if (!rtd.isIntegerType())
782 //5.6.1 Unary Numeric Promotion
783 if (rtd.isByte()||rtd.isShort()||rtd.isInt())
784 righttype=new TypeDescriptor(TypeDescriptor.INT);
788 on.setRightType(righttype);
789 if (!ltd.isIntegerType())
791 case Operation.UNARYPLUS:
792 case Operation.UNARYMINUS:
793 /* case Operation.POSTINC:
794 case Operation.POSTDEC:
795 case Operation.PREINC:
796 case Operation.PREDEC:*/
799 //5.6.1 Unary Numeric Promotion
800 if (ltd.isByte()||ltd.isShort()||ltd.isInt())
801 lefttype=new TypeDescriptor(TypeDescriptor.INT);
804 on.setLeftType(lefttype);
805 on.setType(lefttype);
812 if (!typeutil.isSuperorType(td, on.getType())) {
813 System.out.println(td);
814 System.out.println(on.getType());
815 throw new Error("Type of rside not compatible with type of lside"+on.printNode(0));