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) {
66 else if (td.isClass()) {
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);
140 checkFlagEffects(td, td.getFlagEffects());
141 /* Check that the task code is valid */
142 BlockNode bn=state.getMethodBody(td);
143 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 if (md.getReturnType()==null)
266 throw new Error("Constructor can't return something.");
267 else if (md.getReturnType().isVoid())
268 throw new Error(md+" is void");
270 checkExpressionNode(md, nametable, rn.getReturnExpression(), md.getReturnType());
272 if (md.getReturnType()!=null&&!md.getReturnType().isVoid())
273 throw new Error("Need to return something for "+md);
276 void checkTaskExitNode(Descriptor md, SymbolTable nametable, TaskExitNode ten) {
277 if (md instanceof MethodDescriptor)
278 throw new Error("Illegal taskexit appears in Method: "+md.getSymbol());
279 checkFlagEffects((TaskDescriptor)md, ten.getFlagEffects());
280 checkConstraintCheck((TaskDescriptor) md, nametable, ten.getChecks());
283 void checkIfStatementNode(Descriptor md, SymbolTable nametable, IfStatementNode isn) {
284 checkExpressionNode(md, nametable, isn.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
285 checkBlockNode(md, nametable, isn.getTrueBlock());
286 if (isn.getFalseBlock()!=null)
287 checkBlockNode(md, nametable, isn.getFalseBlock());
290 void checkExpressionNode(Descriptor md, SymbolTable nametable, ExpressionNode en, TypeDescriptor td) {
292 case Kind.AssignmentNode:
293 checkAssignmentNode(md,nametable,(AssignmentNode)en,td);
296 checkCastNode(md,nametable,(CastNode)en,td);
298 case Kind.CreateObjectNode:
299 checkCreateObjectNode(md,nametable,(CreateObjectNode)en,td);
301 case Kind.FieldAccessNode:
302 checkFieldAccessNode(md,nametable,(FieldAccessNode)en,td);
304 case Kind.ArrayAccessNode:
305 checkArrayAccessNode(md,nametable,(ArrayAccessNode)en,td);
307 case Kind.LiteralNode:
308 checkLiteralNode(md,nametable,(LiteralNode)en,td);
310 case Kind.MethodInvokeNode:
311 checkMethodInvokeNode(md,nametable,(MethodInvokeNode)en,td);
314 checkNameNode(md,nametable,(NameNode)en,td);
317 checkOpNode(md,nametable,(OpNode)en,td);
323 void checkCastNode(Descriptor md, SymbolTable nametable, CastNode cn, TypeDescriptor td) {
324 /* Get type descriptor */
325 if (cn.getType()==null) {
326 NameDescriptor typenamed=cn.getTypeName().getName();
327 String typename=typenamed.toString();
328 TypeDescriptor ntd=new TypeDescriptor(typeutil.getClass(typename));
332 /* Check the type descriptor */
333 TypeDescriptor cast_type=cn.getType();
334 checkTypeDescriptor(cast_type);
338 if (!typeutil.isSuperorType(td,cast_type))
339 throw new Error("Cast node returns "+cast_type+", but need "+td);
342 ExpressionNode en=cn.getExpression();
343 checkExpressionNode(md, nametable, en, null);
344 TypeDescriptor etd=en.getType();
345 if (typeutil.isSuperorType(cast_type,etd)) /* Cast trivially succeeds */
348 if (typeutil.isSuperorType(etd,cast_type)) /* Cast may succeed */
350 if (typeutil.isCastable(etd, cast_type))
353 /* Different branches */
354 /* TODO: change if add interfaces */
355 throw new Error("Cast will always fail\n"+cn.printNode(0));
358 void checkFieldAccessNode(Descriptor md, SymbolTable nametable, FieldAccessNode fan, TypeDescriptor td) {
359 ExpressionNode left=fan.getExpression();
360 checkExpressionNode(md,nametable,left,null);
361 TypeDescriptor ltd=left.getType();
362 String fieldname=fan.getFieldName();
364 FieldDescriptor fd=null;
365 if (ltd.isArray()&&fieldname.equals("length"))
366 fd=FieldDescriptor.arrayLength;
368 fd=(FieldDescriptor) ltd.getClassDesc().getFieldTable().get(fieldname);
370 throw new Error("Unknown field "+fieldname);
373 if (!typeutil.isSuperorType(td,fan.getType()))
374 throw new Error("Field node returns "+fan.getType()+", but need "+td);
377 void checkArrayAccessNode(Descriptor md, SymbolTable nametable, ArrayAccessNode aan, TypeDescriptor td) {
378 ExpressionNode left=aan.getExpression();
379 checkExpressionNode(md,nametable,left,null);
381 checkExpressionNode(md,nametable,aan.getIndex(),new TypeDescriptor(TypeDescriptor.INT));
382 TypeDescriptor ltd=left.getType();
385 if (!typeutil.isSuperorType(td,aan.getType()))
386 throw new Error("Field node returns "+aan.getType()+", but need "+td);
389 void checkLiteralNode(Descriptor md, SymbolTable nametable, LiteralNode ln, TypeDescriptor td) {
390 /* Resolve the type */
391 Object o=ln.getValue();
392 if (ln.getTypeString().equals("null")) {
393 ln.setType(new TypeDescriptor(TypeDescriptor.NULL));
394 } else if (o instanceof Integer) {
395 ln.setType(new TypeDescriptor(TypeDescriptor.INT));
396 } else if (o instanceof Long) {
397 ln.setType(new TypeDescriptor(TypeDescriptor.LONG));
398 } else if (o instanceof Float) {
399 ln.setType(new TypeDescriptor(TypeDescriptor.FLOAT));
400 } else if (o instanceof Boolean) {
401 ln.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
402 } else if (o instanceof Double) {
403 ln.setType(new TypeDescriptor(TypeDescriptor.DOUBLE));
404 } else if (o instanceof Character) {
405 ln.setType(new TypeDescriptor(TypeDescriptor.CHAR));
406 } else if (o instanceof String) {
407 ln.setType(new TypeDescriptor(typeutil.getClass(TypeUtil.StringClass)));
411 if (!typeutil.isSuperorType(td,ln.getType()))
412 throw new Error("Field node returns "+ln.getType()+", but need "+td);
415 void checkNameNode(Descriptor md, SymbolTable nametable, NameNode nn, TypeDescriptor td) {
416 NameDescriptor nd=nn.getName();
417 if (nd.getBase()!=null) {
419 /* Rewrite NameNode */
420 ExpressionNode en=translateNameDescriptorintoExpression(nd);
421 nn.setExpression(en);
422 checkExpressionNode(md,nametable,en,td);
424 String varname=nd.toString();
425 Descriptor d=(Descriptor)nametable.get(varname);
427 throw new Error("Name "+varname+" undefined");
429 if (d instanceof VarDescriptor) {
430 nn.setVar((VarDescriptor)d);
431 } else if (d instanceof FieldDescriptor) {
432 nn.setField((FieldDescriptor)d);
433 nn.setVar((VarDescriptor)nametable.get("this")); /* Need a pointer to this */
436 if (!typeutil.isSuperorType(td,nn.getType()))
437 throw new Error("Field node returns "+nn.getType()+", but need "+td);
441 void checkAssignmentNode(Descriptor md, SymbolTable nametable, AssignmentNode an, TypeDescriptor td) {
442 boolean postinc=true;
443 if (an.getOperation().getBaseOp()==null||
444 (an.getOperation().getBaseOp().getOp()!=Operation.POSTINC&&
445 an.getOperation().getBaseOp().getOp()!=Operation.POSTDEC))
449 checkExpressionNode(md, nametable, an.getSrc() ,td);
450 //TODO: Need check on validity of operation here
451 if (!((an.getDest() instanceof FieldAccessNode)||
452 (an.getDest() instanceof ArrayAccessNode)||
453 (an.getDest() instanceof NameNode)))
454 throw new Error("Bad lside in "+an.printNode(0));
455 checkExpressionNode(md, nametable, an.getDest(), null);
457 /* We want parameter variables to tasks to be immutable */
458 if (md instanceof TaskDescriptor) {
459 if (an.getDest() instanceof NameNode) {
460 NameNode nn=(NameNode)an.getDest();
461 if (nn.getVar()!=null) {
462 if (((TaskDescriptor)md).getParameterTable().contains(nn.getVar().getSymbol()))
463 throw new Error("Can't modify parameter "+nn.getVar()+ " to task "+td.getSymbol());
468 if (!postinc&&!typeutil.isSuperorType(an.getDest().getType(),an.getSrc().getType())) {
469 throw new Error("Type of rside ("+an.getSrc().getType()+") not compatible with type of lside ("+an.getDest().getType()+")"+an.printNode(0));
473 void checkLoopNode(Descriptor md, SymbolTable nametable, LoopNode ln) {
474 if (ln.getType()==LoopNode.WHILELOOP||ln.getType()==LoopNode.DOWHILELOOP) {
475 checkExpressionNode(md, nametable, ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
476 checkBlockNode(md, nametable, ln.getBody());
479 /* Link in the initializer naming environment */
480 BlockNode bn=ln.getInitializer();
481 bn.getVarTable().setParent(nametable);
482 for(int i=0;i<bn.size();i++) {
483 BlockStatementNode bsn=bn.get(i);
484 checkBlockStatementNode(md, bn.getVarTable(),bsn);
486 //check the condition
487 checkExpressionNode(md, bn.getVarTable(), ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
488 checkBlockNode(md, bn.getVarTable(), ln.getBody());
489 checkBlockNode(md, bn.getVarTable(), ln.getUpdate());
494 void checkCreateObjectNode(Descriptor md, SymbolTable nametable, CreateObjectNode con, TypeDescriptor td) {
495 TypeDescriptor[] tdarray=new TypeDescriptor[con.numArgs()];
496 for(int i=0;i<con.numArgs();i++) {
497 ExpressionNode en=con.getArg(i);
498 checkExpressionNode(md,nametable,en,null);
499 tdarray[i]=en.getType();
502 TypeDescriptor typetolookin=con.getType();
503 checkTypeDescriptor(typetolookin);
505 /* Check flag effects */
506 if (con.getFlagEffects()!=null) {
507 FlagEffects fe=con.getFlagEffects();
508 ClassDescriptor cd=typetolookin.getClassDesc();
510 for(int j=0;j<fe.numEffects();j++) {
511 FlagEffect flag=fe.getEffect(j);
512 String name=flag.getName();
513 FlagDescriptor flag_d=(FlagDescriptor)cd.getFlagTable().get(name);
514 //Make sure the flag is declared
516 throw new Error("Flag descriptor "+name+" undefined in class: "+cd.getSymbol());
517 if (flag_d.getExternal())
518 throw new Error("Attempting to modify external flag: "+name);
519 flag.setFlag(flag_d);
524 if ((!typetolookin.isClass())&&(!typetolookin.isArray()))
525 throw new Error("Can't allocate primitive type:"+con.printNode(0));
527 if (!typetolookin.isArray()) {
528 //Array's don't need constructor calls
529 ClassDescriptor classtolookin=typetolookin.getClassDesc();
530 //System.out.println("Looking for "+typetolookin.getSymbol());
531 //System.out.println(classtolookin.getMethodTable());
533 Set methoddescriptorset=classtolookin.getMethodTable().getSet(typetolookin.getSymbol());
534 MethodDescriptor bestmd=null;
536 for(Iterator methodit=methoddescriptorset.iterator();methodit.hasNext();) {
537 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
538 /* Need correct number of parameters */
539 //System.out.println("Examining: "+currmd);
540 if (con.numArgs()!=currmd.numParameters())
542 for(int i=0;i<con.numArgs();i++) {
543 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
546 /* Method okay so far */
550 if (isMoreSpecific(currmd,bestmd)) {
552 } else if (!isMoreSpecific(bestmd, currmd))
553 throw new Error("No method is most specific");
555 /* Is this more specific than bestmd */
559 throw new Error("No method found for "+con.printNode(0));
560 con.setConstructor(bestmd);
565 /** Check to see if md1 is more specific than md2... Informally
566 if md2 could always be called given the arguments passed into
569 boolean isMoreSpecific(MethodDescriptor md1, MethodDescriptor md2) {
570 /* Checks if md1 is more specific than md2 */
571 if (md1.numParameters()!=md2.numParameters())
573 for(int i=0;i<md1.numParameters();i++) {
574 if (!typeutil.isSuperorType(md2.getParamType(i), md1.getParamType(i)))
577 if (!typeutil.isSuperorType(md2.getReturnType(), md1.getReturnType()))
582 ExpressionNode translateNameDescriptorintoExpression(NameDescriptor nd) {
583 String id=nd.getIdentifier();
584 NameDescriptor base=nd.getBase();
586 return new NameNode(nd);
588 return new FieldAccessNode(translateNameDescriptorintoExpression(base),id);
592 void checkMethodInvokeNode(Descriptor md, SymbolTable nametable, MethodInvokeNode min, TypeDescriptor td) {
593 /*Typecheck subexpressions
594 and get types for expressions*/
596 TypeDescriptor[] tdarray=new TypeDescriptor[min.numArgs()];
597 for(int i=0;i<min.numArgs();i++) {
598 ExpressionNode en=min.getArg(i);
599 checkExpressionNode(md,nametable,en,null);
600 tdarray[i]=en.getType();
602 TypeDescriptor typetolookin=null;
603 if (min.getExpression()!=null) {
604 checkExpressionNode(md,nametable,min.getExpression(),null);
605 typetolookin=min.getExpression().getType();
606 } else if (min.getBaseName()!=null) {
607 String rootname=min.getBaseName().getRoot();
608 if (nametable.get(rootname)!=null) {
609 //we have an expression
610 min.setExpression(translateNameDescriptorintoExpression(min.getBaseName()));
611 checkExpressionNode(md, nametable, min.getExpression(), null);
612 typetolookin=min.getExpression().getType();
615 ClassDescriptor cd=typeutil.getClass(min.getBaseName().getSymbol());
617 throw new Error(min.getBaseName()+" undefined");
618 typetolookin=new TypeDescriptor(cd);
620 } else if (md instanceof MethodDescriptor) {
621 typetolookin=new TypeDescriptor(((MethodDescriptor)md).getClassDesc());
623 /* If this a task descriptor we throw an error at this point */
624 throw new Error("Unknown method call to "+min.getMethodName()+"in task"+md.getSymbol());
626 if (!typetolookin.isClass())
627 throw new Error("Error with method call to "+min.getMethodName());
628 ClassDescriptor classtolookin=typetolookin.getClassDesc();
629 //System.out.println("Method name="+min.getMethodName());
631 Set methoddescriptorset=classtolookin.getMethodTable().getSet(min.getMethodName());
632 MethodDescriptor bestmd=null;
634 for(Iterator methodit=methoddescriptorset.iterator();methodit.hasNext();) {
635 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
636 /* Need correct number of parameters */
637 if (min.numArgs()!=currmd.numParameters())
639 for(int i=0;i<min.numArgs();i++) {
640 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
643 /* Method okay so far */
647 if (isMoreSpecific(currmd,bestmd)) {
649 } else if (!isMoreSpecific(bestmd, currmd))
650 throw new Error("No method is most specific");
652 /* Is this more specific than bestmd */
656 throw new Error("No method found for :"+min.printNode(0));
657 min.setMethod(bestmd);
658 /* Check whether we need to set this parameter to implied this */
659 if (!bestmd.isStatic()) {
660 if (min.getExpression()==null) {
661 ExpressionNode en=new NameNode(new NameDescriptor("this"));
662 min.setExpression(en);
663 checkExpressionNode(md, nametable, min.getExpression(), null);
670 void checkOpNode(Descriptor md, SymbolTable nametable, OpNode on, TypeDescriptor td) {
671 checkExpressionNode(md, nametable, on.getLeft(), null);
672 if (on.getRight()!=null)
673 checkExpressionNode(md, nametable, on.getRight(), null);
674 TypeDescriptor ltd=on.getLeft().getType();
675 TypeDescriptor rtd=on.getRight()!=null?on.getRight().getType():null;
676 TypeDescriptor lefttype=null;
677 TypeDescriptor righttype=null;
678 Operation op=on.getOp();
681 case Operation.LOGIC_OR:
682 case Operation.LOGIC_AND:
683 if (!(ltd.isBoolean()&&rtd.isBoolean()))
687 on.setRightType(rtd);
688 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
691 case Operation.BIT_OR:
692 case Operation.BIT_XOR:
693 case Operation.BIT_AND:
694 // 5.6.2 Binary Numeric Promotion
695 //TODO unboxing of reference objects
696 if (ltd.isDouble()||rtd.isDouble())
698 else if (ltd.isFloat()||rtd.isFloat())
700 else if (ltd.isLong()||rtd.isLong())
701 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
703 lefttype=new TypeDescriptor(TypeDescriptor.INT);
706 on.setLeftType(lefttype);
707 on.setRightType(righttype);
708 on.setType(lefttype);
711 case Operation.EQUAL:
712 case Operation.NOTEQUAL:
713 // 5.6.2 Binary Numeric Promotion
714 //TODO unboxing of reference objects
715 if (ltd.isBoolean()||rtd.isBoolean()) {
716 if (!(ltd.isBoolean()&&rtd.isBoolean()))
718 righttype=lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
719 } else if (ltd.isPtr()||ltd.isArray()||rtd.isPtr()||rtd.isArray()) {
720 if (!((ltd.isPtr()||ltd.isArray())&&(rtd.isPtr()||rtd.isArray())))
724 } else if (ltd.isDouble()||rtd.isDouble())
725 righttype=lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
726 else if (ltd.isFloat()||rtd.isFloat())
727 righttype=lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
728 else if (ltd.isLong()||rtd.isLong())
729 righttype=lefttype=new TypeDescriptor(TypeDescriptor.LONG);
731 righttype=lefttype=new TypeDescriptor(TypeDescriptor.INT);
733 on.setLeftType(lefttype);
734 on.setRightType(righttype);
735 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
744 // 5.6.2 Binary Numeric Promotion
745 //TODO unboxing of reference objects
746 if (!ltd.isNumber()||!rtd.isNumber())
749 if (ltd.isDouble()||rtd.isDouble())
750 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
751 else if (ltd.isFloat()||rtd.isFloat())
752 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
753 else if (ltd.isLong()||rtd.isLong())
754 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
756 lefttype=new TypeDescriptor(TypeDescriptor.INT);
758 on.setLeftType(lefttype);
759 on.setRightType(righttype);
760 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
764 //TODO: Need special case for strings eventually
771 // 5.6.2 Binary Numeric Promotion
772 //TODO unboxing of reference objects
773 if (ltd.isArray()||rtd.isArray()||!ltd.isNumber()||!rtd.isNumber())
774 throw new Error("Error in "+on.printNode(0));
776 if (ltd.isDouble()||rtd.isDouble())
777 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
778 else if (ltd.isFloat()||rtd.isFloat())
779 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
780 else if (ltd.isLong()||rtd.isLong())
781 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
783 lefttype=new TypeDescriptor(TypeDescriptor.INT);
785 on.setLeftType(lefttype);
786 on.setRightType(righttype);
787 on.setType(lefttype);
790 case Operation.LEFTSHIFT:
791 case Operation.RIGHTSHIFT:
792 if (!rtd.isIntegerType())
794 //5.6.1 Unary Numeric Promotion
795 if (rtd.isByte()||rtd.isShort()||rtd.isInt())
796 righttype=new TypeDescriptor(TypeDescriptor.INT);
800 on.setRightType(righttype);
801 if (!ltd.isIntegerType())
803 case Operation.UNARYPLUS:
804 case Operation.UNARYMINUS:
805 /* case Operation.POSTINC:
806 case Operation.POSTDEC:
807 case Operation.PREINC:
808 case Operation.PREDEC:*/
811 //5.6.1 Unary Numeric Promotion
812 if (ltd.isByte()||ltd.isShort()||ltd.isInt())
813 lefttype=new TypeDescriptor(TypeDescriptor.INT);
816 on.setLeftType(lefttype);
817 on.setType(lefttype);
824 if (!typeutil.isSuperorType(td, on.getType())) {
825 System.out.println(td);
826 System.out.println(on.getType());
827 throw new Error("Type of rside not compatible with type of lside"+on.printNode(0));