6 public class SemanticCheck {
10 public SemanticCheck(State state, TypeUtil tu) {
15 public void semanticCheck() {
16 SymbolTable classtable=state.getClassSymbolTable();
17 Iterator it=classtable.getDescriptorsIterator();
18 // Do descriptors first
20 ClassDescriptor cd=(ClassDescriptor)it.next();
21 System.out.println("Checking class: "+cd);
22 //Set superclass link up
23 if (cd.getSuper()!=null) {
24 cd.setSuper(typeutil.getClass(cd.getSuper()));
25 // Link together Field, Method, and Flag tables so classes
26 // inherit these from their superclasses
27 cd.getFieldTable().setParent(cd.getSuperDesc().getFieldTable());
28 cd.getMethodTable().setParent(cd.getSuperDesc().getMethodTable());
29 cd.getFlagTable().setParent(cd.getSuperDesc().getFlagTable());
32 /* Check to see that fields are well typed */
33 for(Iterator field_it=cd.getFields();field_it.hasNext();) {
34 FieldDescriptor fd=(FieldDescriptor)field_it.next();
35 System.out.println("Checking field: "+fd);
39 for(Iterator method_it=cd.getMethods();method_it.hasNext();) {
40 MethodDescriptor md=(MethodDescriptor)method_it.next();
45 it=classtable.getDescriptorsIterator();
46 // Do descriptors first
48 ClassDescriptor cd=(ClassDescriptor)it.next();
49 for(Iterator method_it=cd.getMethods();method_it.hasNext();) {
50 MethodDescriptor md=(MethodDescriptor)method_it.next();
51 checkMethodBody(cd,md);
55 for(Iterator task_it=state.getTaskSymbolTable().getDescriptorsIterator();task_it.hasNext();) {
56 TaskDescriptor td=(TaskDescriptor)task_it.next();
63 public void checkTypeDescriptor(TypeDescriptor td) {
67 String name=td.toString();
68 ClassDescriptor field_cd=(ClassDescriptor)state.getClassSymbolTable().get(name);
70 throw new Error("Undefined class "+name);
71 td.setClassDescriptor(field_cd);
77 public void checkField(ClassDescriptor cd, FieldDescriptor fd) {
78 checkTypeDescriptor(fd.getType());
81 public void checkConstraintCheck(TaskDescriptor td, SymbolTable nametable, Vector ccs) {
83 return; /* No constraint checks to check */
84 for(int i=0;i<ccs.size();i++) {
85 ConstraintCheck cc=(ConstraintCheck) ccs.get(i);
87 for(int j=0;j<cc.numArgs();j++) {
88 ExpressionNode en=cc.getArg(j);
89 checkExpressionNode(td,nametable,en,null);
94 public void checkFlagEffects(TaskDescriptor td, Vector vfe) {
96 return; /* No flag effects to check */
97 for(int i=0;i<vfe.size();i++) {
98 FlagEffects fe=(FlagEffects) vfe.get(i);
99 String varname=fe.getName();
100 //Make sure the variable is declared as a parameter to the task
101 VarDescriptor vd=(VarDescriptor)td.getParameterTable().get(varname);
103 throw new Error("Parameter "+varname+" in Flag Effects not declared");
106 //Make sure it correspods to a class
107 TypeDescriptor type_d=vd.getType();
108 if (!type_d.isClass())
109 throw new Error("Cannot have non-object argument for flag_effect");
111 ClassDescriptor cd=type_d.getClassDesc();
112 for(int j=0;j<fe.numEffects();j++) {
113 FlagEffect flag=fe.getEffect(j);
114 String name=flag.getName();
115 FlagDescriptor flag_d=(FlagDescriptor)cd.getFlagTable().get(name);
116 //Make sure the flag is declared
118 throw new Error("Flag descriptor "+name+" undefined in class: "+cd.getSymbol());
119 if (flag_d.getExternal())
120 throw new Error("Attempting to modify external flag: "+name);
121 flag.setFlag(flag_d);
126 public void checkTask(TaskDescriptor td) {
127 for(int i=0;i<td.numParameters();i++) {
128 /* Check that parameter is well typed */
129 TypeDescriptor param_type=td.getParamType(i);
130 checkTypeDescriptor(param_type);
132 /* Check the parameter's flag expression is well formed */
133 FlagExpressionNode fen=td.getFlag(td.getParameter(i));
134 if (!param_type.isClass())
135 throw new Error("Cannot have non-object argument to a task");
136 ClassDescriptor cd=param_type.getClassDesc();
137 checkFlagExpressionNode(cd, fen);
138 checkFlagEffects(td, td.getFlagEffects());
140 /* Check that the task code is valid */
141 BlockNode bn=state.getMethodBody(td);
142 checkBlockNode(td, td.getParameterTable(),bn);
146 public void checkFlagExpressionNode(ClassDescriptor cd, FlagExpressionNode fen) {
148 case Kind.FlagOpNode:
150 FlagOpNode fon=(FlagOpNode)fen;
151 checkFlagExpressionNode(cd, fon.getLeft());
152 if (fon.getRight()!=null)
153 checkFlagExpressionNode(cd, fon.getRight());
158 FlagNode fn=(FlagNode)fen;
159 String name=fn.getFlagName();
160 FlagDescriptor fd=(FlagDescriptor)cd.getFlagTable().get(name);
162 throw new Error("Undeclared flag: "+name);
167 throw new Error("Unrecognized FlagExpressionNode");
171 public void checkMethod(ClassDescriptor cd, MethodDescriptor md) {
172 /* Check return type */
173 if (!md.isConstructor())
174 if (!md.getReturnType().isVoid())
175 checkTypeDescriptor(md.getReturnType());
177 for(int i=0;i<md.numParameters();i++) {
178 TypeDescriptor param_type=md.getParamType(i);
179 checkTypeDescriptor(param_type);
181 /* Link the naming environments */
182 if (!md.isStatic()) /* Fields aren't accessible directly in a static method, so don't link in this table */
183 md.getParameterTable().setParent(cd.getFieldTable());
185 if (!md.isStatic()) {
186 VarDescriptor thisvd=new VarDescriptor(new TypeDescriptor(cd),"this");
191 public void checkMethodBody(ClassDescriptor cd, MethodDescriptor md) {
192 System.out.println("Processing method:"+md);
193 BlockNode bn=state.getMethodBody(md);
194 checkBlockNode(md, md.getParameterTable(),bn);
197 public void checkBlockNode(Descriptor md, SymbolTable nametable, BlockNode bn) {
198 /* Link in the naming environment */
199 bn.getVarTable().setParent(nametable);
200 for(int i=0;i<bn.size();i++) {
201 BlockStatementNode bsn=bn.get(i);
202 checkBlockStatementNode(md, bn.getVarTable(),bsn);
206 public void checkBlockStatementNode(Descriptor md, SymbolTable nametable, BlockStatementNode bsn) {
208 case Kind.BlockExpressionNode:
209 checkBlockExpressionNode(md, nametable,(BlockExpressionNode)bsn);
212 case Kind.DeclarationNode:
213 checkDeclarationNode(md, nametable, (DeclarationNode)bsn);
216 case Kind.IfStatementNode:
217 checkIfStatementNode(md, nametable, (IfStatementNode)bsn);
221 checkLoopNode(md, nametable, (LoopNode)bsn);
224 case Kind.ReturnNode:
225 checkReturnNode(md, nametable, (ReturnNode)bsn);
228 case Kind.TaskExitNode:
229 checkTaskExitNode(md, nametable, (TaskExitNode)bsn);
232 case Kind.SubBlockNode:
233 checkSubBlockNode(md, nametable, (SubBlockNode)bsn);
239 void checkBlockExpressionNode(Descriptor md, SymbolTable nametable, BlockExpressionNode ben) {
240 checkExpressionNode(md, nametable, ben.getExpression(), null);
243 void checkDeclarationNode(Descriptor md, SymbolTable nametable, DeclarationNode dn) {
244 VarDescriptor vd=dn.getVarDescriptor();
245 checkTypeDescriptor(vd.getType());
246 Descriptor d=nametable.get(vd.getSymbol());
248 (d instanceof FieldDescriptor)) {
251 throw new Error(vd.getSymbol()+" defined a second time");
252 if (dn.getExpression()!=null)
253 checkExpressionNode(md, nametable, dn.getExpression(), vd.getType());
256 void checkSubBlockNode(Descriptor md, SymbolTable nametable, SubBlockNode sbn) {
257 checkBlockNode(md, nametable, sbn.getBlockNode());
260 void checkReturnNode(Descriptor d, SymbolTable nametable, ReturnNode rn) {
261 if (d instanceof TaskDescriptor)
262 throw new Error("Illegal return appears in Task: "+d.getSymbol());
263 MethodDescriptor md=(MethodDescriptor)d;
264 if (rn.getReturnExpression()!=null)
265 checkExpressionNode(md, nametable, rn.getReturnExpression(), md.getReturnType());
267 if (md.getReturnType()!=null&&!md.getReturnType().isVoid())
268 throw new Error("Need to return something for "+md);
271 void checkTaskExitNode(Descriptor md, SymbolTable nametable, TaskExitNode ten) {
272 if (md instanceof MethodDescriptor)
273 throw new Error("Illegal taskexit appears in Method: "+md.getSymbol());
274 checkFlagEffects((TaskDescriptor)md, ten.getFlagEffects());
275 checkConstraintCheck((TaskDescriptor) md, nametable, ten.getChecks());
278 void checkIfStatementNode(Descriptor md, SymbolTable nametable, IfStatementNode isn) {
279 checkExpressionNode(md, nametable, isn.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
280 checkBlockNode(md, nametable, isn.getTrueBlock());
281 if (isn.getFalseBlock()!=null)
282 checkBlockNode(md, nametable, isn.getFalseBlock());
285 void checkExpressionNode(Descriptor md, SymbolTable nametable, ExpressionNode en, TypeDescriptor td) {
287 case Kind.AssignmentNode:
288 checkAssignmentNode(md,nametable,(AssignmentNode)en,td);
291 checkCastNode(md,nametable,(CastNode)en,td);
293 case Kind.CreateObjectNode:
294 checkCreateObjectNode(md,nametable,(CreateObjectNode)en,td);
296 case Kind.FieldAccessNode:
297 checkFieldAccessNode(md,nametable,(FieldAccessNode)en,td);
299 case Kind.ArrayAccessNode:
300 checkArrayAccessNode(md,nametable,(ArrayAccessNode)en,td);
302 case Kind.LiteralNode:
303 checkLiteralNode(md,nametable,(LiteralNode)en,td);
305 case Kind.MethodInvokeNode:
306 checkMethodInvokeNode(md,nametable,(MethodInvokeNode)en,td);
309 checkNameNode(md,nametable,(NameNode)en,td);
312 checkOpNode(md,nametable,(OpNode)en,td);
318 void checkCastNode(Descriptor md, SymbolTable nametable, CastNode cn, TypeDescriptor td) {
319 /* Get type descriptor */
320 if (cn.getType()==null) {
321 NameDescriptor typenamed=cn.getTypeName().getName();
322 String typename=typenamed.toString();
323 TypeDescriptor ntd=new TypeDescriptor(typeutil.getClass(typename));
327 /* Check the type descriptor */
328 TypeDescriptor cast_type=cn.getType();
329 checkTypeDescriptor(cast_type);
333 if (!typeutil.isSuperorType(td,cast_type))
334 throw new Error("Cast node returns "+cast_type+", but need "+td);
337 ExpressionNode en=cn.getExpression();
338 checkExpressionNode(md, nametable, en, null);
339 TypeDescriptor etd=en.getType();
340 if (typeutil.isSuperorType(cast_type,etd)) /* Cast trivially succeeds */
343 if (typeutil.isSuperorType(etd,cast_type)) /* Cast may succeed */
346 /* Different branches */
347 /* TODO: change if add interfaces */
348 throw new Error("Cast will always fail\n"+cn.printNode(0));
351 void checkFieldAccessNode(Descriptor md, SymbolTable nametable, FieldAccessNode fan, TypeDescriptor td) {
352 ExpressionNode left=fan.getExpression();
353 checkExpressionNode(md,nametable,left,null);
354 TypeDescriptor ltd=left.getType();
355 String fieldname=fan.getFieldName();
357 FieldDescriptor fd=null;
358 if (ltd.isArray()&&fieldname.equals("length"))
359 fd=FieldDescriptor.arrayLength;
361 fd=(FieldDescriptor) ltd.getClassDesc().getFieldTable().get(fieldname);
363 throw new Error("Unknown field "+fieldname);
366 if (!typeutil.isSuperorType(td,fan.getType()))
367 throw new Error("Field node returns "+fan.getType()+", but need "+td);
370 void checkArrayAccessNode(Descriptor md, SymbolTable nametable, ArrayAccessNode aan, TypeDescriptor td) {
371 ExpressionNode left=aan.getExpression();
372 checkExpressionNode(md,nametable,left,null);
374 checkExpressionNode(md,nametable,aan.getIndex(),new TypeDescriptor(TypeDescriptor.INT));
375 TypeDescriptor ltd=left.getType();
378 if (!typeutil.isSuperorType(td,aan.getType()))
379 throw new Error("Field node returns "+aan.getType()+", but need "+td);
382 void checkLiteralNode(Descriptor md, SymbolTable nametable, LiteralNode ln, TypeDescriptor td) {
383 /* Resolve the type */
384 Object o=ln.getValue();
385 if (ln.getTypeString().equals("null")) {
386 ln.setType(new TypeDescriptor(TypeDescriptor.NULL));
387 } else if (o instanceof Integer) {
388 ln.setType(new TypeDescriptor(TypeDescriptor.INT));
389 } else if (o instanceof Long) {
390 ln.setType(new TypeDescriptor(TypeDescriptor.LONG));
391 } else if (o instanceof Float) {
392 ln.setType(new TypeDescriptor(TypeDescriptor.FLOAT));
393 } else if (o instanceof Boolean) {
394 ln.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
395 } else if (o instanceof Double) {
396 ln.setType(new TypeDescriptor(TypeDescriptor.DOUBLE));
397 } else if (o instanceof Character) {
398 ln.setType(new TypeDescriptor(TypeDescriptor.CHAR));
399 } else if (o instanceof String) {
400 ln.setType(new TypeDescriptor(typeutil.getClass(TypeUtil.StringClass)));
404 if (!typeutil.isSuperorType(td,ln.getType()))
405 throw new Error("Field node returns "+ln.getType()+", but need "+td);
408 void checkNameNode(Descriptor md, SymbolTable nametable, NameNode nn, TypeDescriptor td) {
409 NameDescriptor nd=nn.getName();
410 if (nd.getBase()!=null) {
412 /* Rewrite NameNode */
413 ExpressionNode en=translateNameDescriptorintoExpression(nd);
414 nn.setExpression(en);
415 checkExpressionNode(md,nametable,en,td);
417 String varname=nd.toString();
418 Descriptor d=(Descriptor)nametable.get(varname);
420 throw new Error("Name "+varname+" undefined");
422 if (d instanceof VarDescriptor) {
423 nn.setVar((VarDescriptor)d);
424 } else if (d instanceof FieldDescriptor) {
425 nn.setField((FieldDescriptor)d);
426 nn.setVar((VarDescriptor)nametable.get("this")); /* Need a pointer to this */
429 if (!typeutil.isSuperorType(td,nn.getType()))
430 throw new Error("Field node returns "+nn.getType()+", but need "+td);
434 void checkAssignmentNode(Descriptor md, SymbolTable nametable, AssignmentNode an, TypeDescriptor td) {
435 boolean postinc=true;
436 if (an.getOperation().getBaseOp()==null||
437 (an.getOperation().getBaseOp().getOp()!=Operation.POSTINC&&
438 an.getOperation().getBaseOp().getOp()!=Operation.POSTDEC))
442 checkExpressionNode(md, nametable, an.getSrc() ,td);
443 //TODO: Need check on validity of operation here
444 if (!((an.getDest() instanceof FieldAccessNode)||
445 (an.getDest() instanceof ArrayAccessNode)||
446 (an.getDest() instanceof NameNode)))
447 throw new Error("Bad lside in "+an.printNode(0));
448 checkExpressionNode(md, nametable, an.getDest(), null);
450 /* We want parameter variables to tasks to be immutable */
451 if (md instanceof TaskDescriptor) {
452 if (an.getDest() instanceof NameNode) {
453 NameNode nn=(NameNode)an.getDest();
454 if (nn.getVar()!=null) {
455 if (((TaskDescriptor)md).getParameterTable().contains(nn.getVar().getSymbol()))
456 throw new Error("Can't modify parameter "+nn.getVar()+ " to task "+td.getSymbol());
461 if (!postinc&&!typeutil.isSuperorType(an.getDest().getType(),an.getSrc().getType())) {
462 throw new Error("Type of rside ("+an.getSrc().getType()+") not compatible with type of lside ("+an.getDest().getType()+")"+an.printNode(0));
466 void checkLoopNode(Descriptor md, SymbolTable nametable, LoopNode ln) {
467 if (ln.getType()==LoopNode.WHILELOOP||ln.getType()==LoopNode.DOWHILELOOP) {
468 checkExpressionNode(md, nametable, ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
469 checkBlockNode(md, nametable, ln.getBody());
472 /* Link in the initializer naming environment */
473 BlockNode bn=ln.getInitializer();
474 bn.getVarTable().setParent(nametable);
475 for(int i=0;i<bn.size();i++) {
476 BlockStatementNode bsn=bn.get(i);
477 checkBlockStatementNode(md, bn.getVarTable(),bsn);
479 //check the condition
480 checkExpressionNode(md, bn.getVarTable(), ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
481 checkBlockNode(md, bn.getVarTable(), ln.getBody());
482 checkBlockNode(md, bn.getVarTable(), ln.getUpdate());
487 void checkCreateObjectNode(Descriptor md, SymbolTable nametable, CreateObjectNode con, TypeDescriptor td) {
488 TypeDescriptor[] tdarray=new TypeDescriptor[con.numArgs()];
489 for(int i=0;i<con.numArgs();i++) {
490 ExpressionNode en=con.getArg(i);
491 checkExpressionNode(md,nametable,en,null);
492 tdarray[i]=en.getType();
495 TypeDescriptor typetolookin=con.getType();
496 checkTypeDescriptor(typetolookin);
498 /* Check flag effects */
499 if (con.getFlagEffects()!=null) {
500 FlagEffects fe=con.getFlagEffects();
501 ClassDescriptor cd=typetolookin.getClassDesc();
503 for(int j=0;j<fe.numEffects();j++) {
504 FlagEffect flag=fe.getEffect(j);
505 String name=flag.getName();
506 FlagDescriptor flag_d=(FlagDescriptor)cd.getFlagTable().get(name);
507 //Make sure the flag is declared
509 throw new Error("Flag descriptor "+name+" undefined in class: "+cd.getSymbol());
510 if (flag_d.getExternal())
511 throw new Error("Attempting to modify external flag: "+name);
512 flag.setFlag(flag_d);
517 if ((!typetolookin.isClass())&&(!typetolookin.isArray()))
518 throw new Error("Can't allocate primitive type:"+con.printNode(0));
520 if (!typetolookin.isArray()) {
521 //Array's don't need constructor calls
522 ClassDescriptor classtolookin=typetolookin.getClassDesc();
523 System.out.println("Looking for "+typetolookin.getSymbol());
524 System.out.println(classtolookin.getMethodTable());
526 Set methoddescriptorset=classtolookin.getMethodTable().getSet(typetolookin.getSymbol());
527 MethodDescriptor bestmd=null;
529 for(Iterator methodit=methoddescriptorset.iterator();methodit.hasNext();) {
530 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
531 /* Need correct number of parameters */
532 System.out.println("Examining: "+currmd);
533 if (con.numArgs()!=currmd.numParameters())
535 for(int i=0;i<con.numArgs();i++) {
536 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
539 /* Method okay so far */
543 if (isMoreSpecific(currmd,bestmd)) {
545 } else if (!isMoreSpecific(bestmd, currmd))
546 throw new Error("No method is most specific");
548 /* Is this more specific than bestmd */
552 throw new Error("No method found for "+con.printNode(0));
553 con.setConstructor(bestmd);
558 /** Check to see if md1 is more specific than md2... Informally
559 if md2 could always be called given the arguments passed into
562 boolean isMoreSpecific(MethodDescriptor md1, MethodDescriptor md2) {
563 /* Checks if md1 is more specific than md2 */
564 if (md1.numParameters()!=md2.numParameters())
566 for(int i=0;i<md1.numParameters();i++) {
567 if (!typeutil.isSuperorType(md2.getParamType(i), md1.getParamType(i)))
570 if (!typeutil.isSuperorType(md2.getReturnType(), md1.getReturnType()))
575 ExpressionNode translateNameDescriptorintoExpression(NameDescriptor nd) {
576 String id=nd.getIdentifier();
577 NameDescriptor base=nd.getBase();
579 return new NameNode(nd);
581 return new FieldAccessNode(translateNameDescriptorintoExpression(base),id);
585 void checkMethodInvokeNode(Descriptor md, SymbolTable nametable, MethodInvokeNode min, TypeDescriptor td) {
586 /*Typecheck subexpressions
587 and get types for expressions*/
589 TypeDescriptor[] tdarray=new TypeDescriptor[min.numArgs()];
590 for(int i=0;i<min.numArgs();i++) {
591 ExpressionNode en=min.getArg(i);
592 checkExpressionNode(md,nametable,en,null);
593 tdarray[i]=en.getType();
595 TypeDescriptor typetolookin=null;
596 if (min.getExpression()!=null) {
597 checkExpressionNode(md,nametable,min.getExpression(),null);
598 typetolookin=min.getExpression().getType();
599 } else if (min.getBaseName()!=null) {
600 String rootname=min.getBaseName().getRoot();
601 if (nametable.get(rootname)!=null) {
602 //we have an expression
603 min.setExpression(translateNameDescriptorintoExpression(min.getBaseName()));
604 checkExpressionNode(md, nametable, min.getExpression(), null);
605 typetolookin=min.getExpression().getType();
608 ClassDescriptor cd=typeutil.getClass(min.getBaseName().getSymbol());
610 throw new Error(min.getBaseName()+" undefined");
611 typetolookin=new TypeDescriptor(cd);
613 } else if (md instanceof MethodDescriptor) {
614 typetolookin=new TypeDescriptor(((MethodDescriptor)md).getClassDesc());
616 /* If this a task descriptor we throw an error at this point */
617 throw new Error("Unknown method call to "+min.getMethodName()+"in task"+md.getSymbol());
619 if (!typetolookin.isClass())
621 ClassDescriptor classtolookin=typetolookin.getClassDesc();
622 System.out.println("Method name="+min.getMethodName());
623 Set methoddescriptorset=classtolookin.getMethodTable().getSet(min.getMethodName());
624 MethodDescriptor bestmd=null;
626 for(Iterator methodit=methoddescriptorset.iterator();methodit.hasNext();) {
627 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
628 /* Need correct number of parameters */
629 if (min.numArgs()!=currmd.numParameters())
631 for(int i=0;i<min.numArgs();i++) {
632 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
635 /* Method okay so far */
639 if (isMoreSpecific(currmd,bestmd)) {
641 } else if (!isMoreSpecific(bestmd, currmd))
642 throw new Error("No method is most specific");
644 /* Is this more specific than bestmd */
648 throw new Error("No method found for :"+min.printNode(0));
649 min.setMethod(bestmd);
650 /* Check whether we need to set this parameter to implied this */
651 if (!bestmd.isStatic()) {
652 if (min.getExpression()==null) {
653 ExpressionNode en=new NameNode(new NameDescriptor("this"));
654 min.setExpression(en);
655 checkExpressionNode(md, nametable, min.getExpression(), null);
662 void checkOpNode(Descriptor md, SymbolTable nametable, OpNode on, TypeDescriptor td) {
663 checkExpressionNode(md, nametable, on.getLeft(), null);
664 if (on.getRight()!=null)
665 checkExpressionNode(md, nametable, on.getRight(), null);
666 TypeDescriptor ltd=on.getLeft().getType();
667 TypeDescriptor rtd=on.getRight()!=null?on.getRight().getType():null;
668 TypeDescriptor lefttype=null;
669 TypeDescriptor righttype=null;
670 Operation op=on.getOp();
673 case Operation.LOGIC_OR:
674 case Operation.LOGIC_AND:
675 if (!(ltd.isBoolean()&&rtd.isBoolean()))
679 on.setRightType(rtd);
680 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
683 case Operation.BIT_OR:
684 case Operation.BIT_XOR:
685 case Operation.BIT_AND:
686 // 5.6.2 Binary Numeric Promotion
687 //TODO unboxing of reference objects
688 if (ltd.isDouble()||rtd.isDouble())
690 else if (ltd.isFloat()||rtd.isFloat())
692 else if (ltd.isLong()||rtd.isLong())
693 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
695 lefttype=new TypeDescriptor(TypeDescriptor.INT);
698 on.setLeftType(lefttype);
699 on.setRightType(righttype);
700 on.setType(lefttype);
703 case Operation.EQUAL:
704 case Operation.NOTEQUAL:
705 // 5.6.2 Binary Numeric Promotion
706 //TODO unboxing of reference objects
707 if (ltd.isBoolean()||rtd.isBoolean()) {
708 if (!(ltd.isBoolean()&&rtd.isBoolean()))
710 righttype=lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
711 } else if (ltd.isPtr()||ltd.isArray()||rtd.isPtr()||rtd.isArray()) {
712 if (!((ltd.isPtr()||ltd.isArray())&&(rtd.isPtr()||rtd.isArray())))
716 } else if (ltd.isDouble()||rtd.isDouble())
717 righttype=lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
718 else if (ltd.isFloat()||rtd.isFloat())
719 righttype=lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
720 else if (ltd.isLong()||rtd.isLong())
721 righttype=lefttype=new TypeDescriptor(TypeDescriptor.LONG);
723 righttype=lefttype=new TypeDescriptor(TypeDescriptor.INT);
725 on.setLeftType(lefttype);
726 on.setRightType(righttype);
727 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
736 // 5.6.2 Binary Numeric Promotion
737 //TODO unboxing of reference objects
738 if (!ltd.isNumber()||!rtd.isNumber())
741 if (ltd.isDouble()||rtd.isDouble())
742 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
743 else if (ltd.isFloat()||rtd.isFloat())
744 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
745 else if (ltd.isLong()||rtd.isLong())
746 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
748 lefttype=new TypeDescriptor(TypeDescriptor.INT);
750 on.setLeftType(lefttype);
751 on.setRightType(righttype);
752 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
756 //TODO: Need special case for strings eventually
763 // 5.6.2 Binary Numeric Promotion
764 //TODO unboxing of reference objects
765 if (!ltd.isNumber()||!rtd.isNumber())
766 throw new Error("Error in "+on.printNode(0));
768 if (ltd.isDouble()||rtd.isDouble())
769 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
770 else if (ltd.isFloat()||rtd.isFloat())
771 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
772 else if (ltd.isLong()||rtd.isLong())
773 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
775 lefttype=new TypeDescriptor(TypeDescriptor.INT);
777 on.setLeftType(lefttype);
778 on.setRightType(righttype);
779 on.setType(lefttype);
782 case Operation.LEFTSHIFT:
783 case Operation.RIGHTSHIFT:
784 if (!rtd.isIntegerType())
786 //5.6.1 Unary Numeric Promotion
787 if (rtd.isByte()||rtd.isShort()||rtd.isInt())
788 righttype=new TypeDescriptor(TypeDescriptor.INT);
792 on.setRightType(righttype);
793 if (!ltd.isIntegerType())
795 case Operation.UNARYPLUS:
796 case Operation.UNARYMINUS:
797 /* case Operation.POSTINC:
798 case Operation.POSTDEC:
799 case Operation.PREINC:
800 case Operation.PREDEC:*/
803 //5.6.1 Unary Numeric Promotion
804 if (ltd.isByte()||ltd.isShort()||ltd.isInt())
805 lefttype=new TypeDescriptor(TypeDescriptor.INT);
808 on.setLeftType(lefttype);
809 on.setType(lefttype);
816 if (!typeutil.isSuperorType(td, on.getType())) {
817 System.out.println(td);
818 System.out.println(on.getType());
819 throw new Error("Type of rside not compatible with type of lside"+on.printNode(0));