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 if (md.getReturnType().isVoid())
266 throw new Error(md+" is void");
268 checkExpressionNode(md, nametable, rn.getReturnExpression(), md.getReturnType());
270 if (md.getReturnType()!=null&&!md.getReturnType().isVoid())
271 throw new Error("Need to return something for "+md);
274 void checkTaskExitNode(Descriptor md, SymbolTable nametable, TaskExitNode ten) {
275 if (md instanceof MethodDescriptor)
276 throw new Error("Illegal taskexit appears in Method: "+md.getSymbol());
277 checkFlagEffects((TaskDescriptor)md, ten.getFlagEffects());
278 checkConstraintCheck((TaskDescriptor) md, nametable, ten.getChecks());
281 void checkIfStatementNode(Descriptor md, SymbolTable nametable, IfStatementNode isn) {
282 checkExpressionNode(md, nametable, isn.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
283 checkBlockNode(md, nametable, isn.getTrueBlock());
284 if (isn.getFalseBlock()!=null)
285 checkBlockNode(md, nametable, isn.getFalseBlock());
288 void checkExpressionNode(Descriptor md, SymbolTable nametable, ExpressionNode en, TypeDescriptor td) {
290 case Kind.AssignmentNode:
291 checkAssignmentNode(md,nametable,(AssignmentNode)en,td);
294 checkCastNode(md,nametable,(CastNode)en,td);
296 case Kind.CreateObjectNode:
297 checkCreateObjectNode(md,nametable,(CreateObjectNode)en,td);
299 case Kind.FieldAccessNode:
300 checkFieldAccessNode(md,nametable,(FieldAccessNode)en,td);
302 case Kind.ArrayAccessNode:
303 checkArrayAccessNode(md,nametable,(ArrayAccessNode)en,td);
305 case Kind.LiteralNode:
306 checkLiteralNode(md,nametable,(LiteralNode)en,td);
308 case Kind.MethodInvokeNode:
309 checkMethodInvokeNode(md,nametable,(MethodInvokeNode)en,td);
312 checkNameNode(md,nametable,(NameNode)en,td);
315 checkOpNode(md,nametable,(OpNode)en,td);
321 void checkCastNode(Descriptor md, SymbolTable nametable, CastNode cn, TypeDescriptor td) {
322 /* Get type descriptor */
323 if (cn.getType()==null) {
324 NameDescriptor typenamed=cn.getTypeName().getName();
325 String typename=typenamed.toString();
326 TypeDescriptor ntd=new TypeDescriptor(typeutil.getClass(typename));
330 /* Check the type descriptor */
331 TypeDescriptor cast_type=cn.getType();
332 checkTypeDescriptor(cast_type);
336 if (!typeutil.isSuperorType(td,cast_type))
337 throw new Error("Cast node returns "+cast_type+", but need "+td);
340 ExpressionNode en=cn.getExpression();
341 checkExpressionNode(md, nametable, en, null);
342 TypeDescriptor etd=en.getType();
343 if (typeutil.isSuperorType(cast_type,etd)) /* Cast trivially succeeds */
346 if (typeutil.isSuperorType(etd,cast_type)) /* Cast may succeed */
349 /* Different branches */
350 /* TODO: change if add interfaces */
351 throw new Error("Cast will always fail\n"+cn.printNode(0));
354 void checkFieldAccessNode(Descriptor md, SymbolTable nametable, FieldAccessNode fan, TypeDescriptor td) {
355 ExpressionNode left=fan.getExpression();
356 checkExpressionNode(md,nametable,left,null);
357 TypeDescriptor ltd=left.getType();
358 String fieldname=fan.getFieldName();
360 FieldDescriptor fd=null;
361 if (ltd.isArray()&&fieldname.equals("length"))
362 fd=FieldDescriptor.arrayLength;
364 fd=(FieldDescriptor) ltd.getClassDesc().getFieldTable().get(fieldname);
366 throw new Error("Unknown field "+fieldname);
369 if (!typeutil.isSuperorType(td,fan.getType()))
370 throw new Error("Field node returns "+fan.getType()+", but need "+td);
373 void checkArrayAccessNode(Descriptor md, SymbolTable nametable, ArrayAccessNode aan, TypeDescriptor td) {
374 ExpressionNode left=aan.getExpression();
375 checkExpressionNode(md,nametable,left,null);
377 checkExpressionNode(md,nametable,aan.getIndex(),new TypeDescriptor(TypeDescriptor.INT));
378 TypeDescriptor ltd=left.getType();
381 if (!typeutil.isSuperorType(td,aan.getType()))
382 throw new Error("Field node returns "+aan.getType()+", but need "+td);
385 void checkLiteralNode(Descriptor md, SymbolTable nametable, LiteralNode ln, TypeDescriptor td) {
386 /* Resolve the type */
387 Object o=ln.getValue();
388 if (ln.getTypeString().equals("null")) {
389 ln.setType(new TypeDescriptor(TypeDescriptor.NULL));
390 } else if (o instanceof Integer) {
391 ln.setType(new TypeDescriptor(TypeDescriptor.INT));
392 } else if (o instanceof Long) {
393 ln.setType(new TypeDescriptor(TypeDescriptor.LONG));
394 } else if (o instanceof Float) {
395 ln.setType(new TypeDescriptor(TypeDescriptor.FLOAT));
396 } else if (o instanceof Boolean) {
397 ln.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
398 } else if (o instanceof Double) {
399 ln.setType(new TypeDescriptor(TypeDescriptor.DOUBLE));
400 } else if (o instanceof Character) {
401 ln.setType(new TypeDescriptor(TypeDescriptor.CHAR));
402 } else if (o instanceof String) {
403 ln.setType(new TypeDescriptor(typeutil.getClass(TypeUtil.StringClass)));
407 if (!typeutil.isSuperorType(td,ln.getType()))
408 throw new Error("Field node returns "+ln.getType()+", but need "+td);
411 void checkNameNode(Descriptor md, SymbolTable nametable, NameNode nn, TypeDescriptor td) {
412 NameDescriptor nd=nn.getName();
413 if (nd.getBase()!=null) {
415 /* Rewrite NameNode */
416 ExpressionNode en=translateNameDescriptorintoExpression(nd);
417 nn.setExpression(en);
418 checkExpressionNode(md,nametable,en,td);
420 String varname=nd.toString();
421 Descriptor d=(Descriptor)nametable.get(varname);
423 throw new Error("Name "+varname+" undefined");
425 if (d instanceof VarDescriptor) {
426 nn.setVar((VarDescriptor)d);
427 } else if (d instanceof FieldDescriptor) {
428 nn.setField((FieldDescriptor)d);
429 nn.setVar((VarDescriptor)nametable.get("this")); /* Need a pointer to this */
432 if (!typeutil.isSuperorType(td,nn.getType()))
433 throw new Error("Field node returns "+nn.getType()+", but need "+td);
437 void checkAssignmentNode(Descriptor md, SymbolTable nametable, AssignmentNode an, TypeDescriptor td) {
438 boolean postinc=true;
439 if (an.getOperation().getBaseOp()==null||
440 (an.getOperation().getBaseOp().getOp()!=Operation.POSTINC&&
441 an.getOperation().getBaseOp().getOp()!=Operation.POSTDEC))
445 checkExpressionNode(md, nametable, an.getSrc() ,td);
446 //TODO: Need check on validity of operation here
447 if (!((an.getDest() instanceof FieldAccessNode)||
448 (an.getDest() instanceof ArrayAccessNode)||
449 (an.getDest() instanceof NameNode)))
450 throw new Error("Bad lside in "+an.printNode(0));
451 checkExpressionNode(md, nametable, an.getDest(), null);
453 /* We want parameter variables to tasks to be immutable */
454 if (md instanceof TaskDescriptor) {
455 if (an.getDest() instanceof NameNode) {
456 NameNode nn=(NameNode)an.getDest();
457 if (nn.getVar()!=null) {
458 if (((TaskDescriptor)md).getParameterTable().contains(nn.getVar().getSymbol()))
459 throw new Error("Can't modify parameter "+nn.getVar()+ " to task "+td.getSymbol());
464 if (!postinc&&!typeutil.isSuperorType(an.getDest().getType(),an.getSrc().getType())) {
465 throw new Error("Type of rside ("+an.getSrc().getType()+") not compatible with type of lside ("+an.getDest().getType()+")"+an.printNode(0));
469 void checkLoopNode(Descriptor md, SymbolTable nametable, LoopNode ln) {
470 if (ln.getType()==LoopNode.WHILELOOP||ln.getType()==LoopNode.DOWHILELOOP) {
471 checkExpressionNode(md, nametable, ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
472 checkBlockNode(md, nametable, ln.getBody());
475 /* Link in the initializer naming environment */
476 BlockNode bn=ln.getInitializer();
477 bn.getVarTable().setParent(nametable);
478 for(int i=0;i<bn.size();i++) {
479 BlockStatementNode bsn=bn.get(i);
480 checkBlockStatementNode(md, bn.getVarTable(),bsn);
482 //check the condition
483 checkExpressionNode(md, bn.getVarTable(), ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
484 checkBlockNode(md, bn.getVarTable(), ln.getBody());
485 checkBlockNode(md, bn.getVarTable(), ln.getUpdate());
490 void checkCreateObjectNode(Descriptor md, SymbolTable nametable, CreateObjectNode con, TypeDescriptor td) {
491 TypeDescriptor[] tdarray=new TypeDescriptor[con.numArgs()];
492 for(int i=0;i<con.numArgs();i++) {
493 ExpressionNode en=con.getArg(i);
494 checkExpressionNode(md,nametable,en,null);
495 tdarray[i]=en.getType();
498 TypeDescriptor typetolookin=con.getType();
499 checkTypeDescriptor(typetolookin);
501 /* Check flag effects */
502 if (con.getFlagEffects()!=null) {
503 FlagEffects fe=con.getFlagEffects();
504 ClassDescriptor cd=typetolookin.getClassDesc();
506 for(int j=0;j<fe.numEffects();j++) {
507 FlagEffect flag=fe.getEffect(j);
508 String name=flag.getName();
509 FlagDescriptor flag_d=(FlagDescriptor)cd.getFlagTable().get(name);
510 //Make sure the flag is declared
512 throw new Error("Flag descriptor "+name+" undefined in class: "+cd.getSymbol());
513 if (flag_d.getExternal())
514 throw new Error("Attempting to modify external flag: "+name);
515 flag.setFlag(flag_d);
520 if ((!typetolookin.isClass())&&(!typetolookin.isArray()))
521 throw new Error("Can't allocate primitive type:"+con.printNode(0));
523 if (!typetolookin.isArray()) {
524 //Array's don't need constructor calls
525 ClassDescriptor classtolookin=typetolookin.getClassDesc();
526 System.out.println("Looking for "+typetolookin.getSymbol());
527 System.out.println(classtolookin.getMethodTable());
529 Set methoddescriptorset=classtolookin.getMethodTable().getSet(typetolookin.getSymbol());
530 MethodDescriptor bestmd=null;
532 for(Iterator methodit=methoddescriptorset.iterator();methodit.hasNext();) {
533 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
534 /* Need correct number of parameters */
535 System.out.println("Examining: "+currmd);
536 if (con.numArgs()!=currmd.numParameters())
538 for(int i=0;i<con.numArgs();i++) {
539 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
542 /* Method okay so far */
546 if (isMoreSpecific(currmd,bestmd)) {
548 } else if (!isMoreSpecific(bestmd, currmd))
549 throw new Error("No method is most specific");
551 /* Is this more specific than bestmd */
555 throw new Error("No method found for "+con.printNode(0));
556 con.setConstructor(bestmd);
561 /** Check to see if md1 is more specific than md2... Informally
562 if md2 could always be called given the arguments passed into
565 boolean isMoreSpecific(MethodDescriptor md1, MethodDescriptor md2) {
566 /* Checks if md1 is more specific than md2 */
567 if (md1.numParameters()!=md2.numParameters())
569 for(int i=0;i<md1.numParameters();i++) {
570 if (!typeutil.isSuperorType(md2.getParamType(i), md1.getParamType(i)))
573 if (!typeutil.isSuperorType(md2.getReturnType(), md1.getReturnType()))
578 ExpressionNode translateNameDescriptorintoExpression(NameDescriptor nd) {
579 String id=nd.getIdentifier();
580 NameDescriptor base=nd.getBase();
582 return new NameNode(nd);
584 return new FieldAccessNode(translateNameDescriptorintoExpression(base),id);
588 void checkMethodInvokeNode(Descriptor md, SymbolTable nametable, MethodInvokeNode min, TypeDescriptor td) {
589 /*Typecheck subexpressions
590 and get types for expressions*/
592 TypeDescriptor[] tdarray=new TypeDescriptor[min.numArgs()];
593 for(int i=0;i<min.numArgs();i++) {
594 ExpressionNode en=min.getArg(i);
595 checkExpressionNode(md,nametable,en,null);
596 tdarray[i]=en.getType();
598 TypeDescriptor typetolookin=null;
599 if (min.getExpression()!=null) {
600 checkExpressionNode(md,nametable,min.getExpression(),null);
601 typetolookin=min.getExpression().getType();
602 } else if (min.getBaseName()!=null) {
603 String rootname=min.getBaseName().getRoot();
604 if (nametable.get(rootname)!=null) {
605 //we have an expression
606 min.setExpression(translateNameDescriptorintoExpression(min.getBaseName()));
607 checkExpressionNode(md, nametable, min.getExpression(), null);
608 typetolookin=min.getExpression().getType();
611 ClassDescriptor cd=typeutil.getClass(min.getBaseName().getSymbol());
613 throw new Error(min.getBaseName()+" undefined");
614 typetolookin=new TypeDescriptor(cd);
616 } else if (md instanceof MethodDescriptor) {
617 typetolookin=new TypeDescriptor(((MethodDescriptor)md).getClassDesc());
619 /* If this a task descriptor we throw an error at this point */
620 throw new Error("Unknown method call to "+min.getMethodName()+"in task"+md.getSymbol());
622 if (!typetolookin.isClass())
624 ClassDescriptor classtolookin=typetolookin.getClassDesc();
625 System.out.println("Method name="+min.getMethodName());
626 Set methoddescriptorset=classtolookin.getMethodTable().getSet(min.getMethodName());
627 MethodDescriptor bestmd=null;
629 for(Iterator methodit=methoddescriptorset.iterator();methodit.hasNext();) {
630 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
631 /* Need correct number of parameters */
632 if (min.numArgs()!=currmd.numParameters())
634 for(int i=0;i<min.numArgs();i++) {
635 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
638 /* Method okay so far */
642 if (isMoreSpecific(currmd,bestmd)) {
644 } else if (!isMoreSpecific(bestmd, currmd))
645 throw new Error("No method is most specific");
647 /* Is this more specific than bestmd */
651 throw new Error("No method found for :"+min.printNode(0));
652 min.setMethod(bestmd);
653 /* Check whether we need to set this parameter to implied this */
654 if (!bestmd.isStatic()) {
655 if (min.getExpression()==null) {
656 ExpressionNode en=new NameNode(new NameDescriptor("this"));
657 min.setExpression(en);
658 checkExpressionNode(md, nametable, min.getExpression(), null);
665 void checkOpNode(Descriptor md, SymbolTable nametable, OpNode on, TypeDescriptor td) {
666 checkExpressionNode(md, nametable, on.getLeft(), null);
667 if (on.getRight()!=null)
668 checkExpressionNode(md, nametable, on.getRight(), null);
669 TypeDescriptor ltd=on.getLeft().getType();
670 TypeDescriptor rtd=on.getRight()!=null?on.getRight().getType():null;
671 TypeDescriptor lefttype=null;
672 TypeDescriptor righttype=null;
673 Operation op=on.getOp();
676 case Operation.LOGIC_OR:
677 case Operation.LOGIC_AND:
678 if (!(ltd.isBoolean()&&rtd.isBoolean()))
682 on.setRightType(rtd);
683 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
686 case Operation.BIT_OR:
687 case Operation.BIT_XOR:
688 case Operation.BIT_AND:
689 // 5.6.2 Binary Numeric Promotion
690 //TODO unboxing of reference objects
691 if (ltd.isDouble()||rtd.isDouble())
693 else if (ltd.isFloat()||rtd.isFloat())
695 else if (ltd.isLong()||rtd.isLong())
696 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
698 lefttype=new TypeDescriptor(TypeDescriptor.INT);
701 on.setLeftType(lefttype);
702 on.setRightType(righttype);
703 on.setType(lefttype);
706 case Operation.EQUAL:
707 case Operation.NOTEQUAL:
708 // 5.6.2 Binary Numeric Promotion
709 //TODO unboxing of reference objects
710 if (ltd.isBoolean()||rtd.isBoolean()) {
711 if (!(ltd.isBoolean()&&rtd.isBoolean()))
713 righttype=lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
714 } else if (ltd.isPtr()||ltd.isArray()||rtd.isPtr()||rtd.isArray()) {
715 if (!((ltd.isPtr()||ltd.isArray())&&(rtd.isPtr()||rtd.isArray())))
719 } else if (ltd.isDouble()||rtd.isDouble())
720 righttype=lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
721 else if (ltd.isFloat()||rtd.isFloat())
722 righttype=lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
723 else if (ltd.isLong()||rtd.isLong())
724 righttype=lefttype=new TypeDescriptor(TypeDescriptor.LONG);
726 righttype=lefttype=new TypeDescriptor(TypeDescriptor.INT);
728 on.setLeftType(lefttype);
729 on.setRightType(righttype);
730 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
739 // 5.6.2 Binary Numeric Promotion
740 //TODO unboxing of reference objects
741 if (!ltd.isNumber()||!rtd.isNumber())
744 if (ltd.isDouble()||rtd.isDouble())
745 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
746 else if (ltd.isFloat()||rtd.isFloat())
747 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
748 else if (ltd.isLong()||rtd.isLong())
749 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
751 lefttype=new TypeDescriptor(TypeDescriptor.INT);
753 on.setLeftType(lefttype);
754 on.setRightType(righttype);
755 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
759 //TODO: Need special case for strings eventually
766 // 5.6.2 Binary Numeric Promotion
767 //TODO unboxing of reference objects
768 if (!ltd.isNumber()||!rtd.isNumber())
769 throw new Error("Error in "+on.printNode(0));
771 if (ltd.isDouble()||rtd.isDouble())
772 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
773 else if (ltd.isFloat()||rtd.isFloat())
774 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
775 else if (ltd.isLong()||rtd.isLong())
776 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
778 lefttype=new TypeDescriptor(TypeDescriptor.INT);
780 on.setLeftType(lefttype);
781 on.setRightType(righttype);
782 on.setType(lefttype);
785 case Operation.LEFTSHIFT:
786 case Operation.RIGHTSHIFT:
787 if (!rtd.isIntegerType())
789 //5.6.1 Unary Numeric Promotion
790 if (rtd.isByte()||rtd.isShort()||rtd.isInt())
791 righttype=new TypeDescriptor(TypeDescriptor.INT);
795 on.setRightType(righttype);
796 if (!ltd.isIntegerType())
798 case Operation.UNARYPLUS:
799 case Operation.UNARYMINUS:
800 /* case Operation.POSTINC:
801 case Operation.POSTDEC:
802 case Operation.PREINC:
803 case Operation.PREDEC:*/
806 //5.6.1 Unary Numeric Promotion
807 if (ltd.isByte()||ltd.isShort()||ltd.isInt())
808 lefttype=new TypeDescriptor(TypeDescriptor.INT);
811 on.setLeftType(lefttype);
812 on.setType(lefttype);
819 if (!typeutil.isSuperorType(td, on.getType())) {
820 System.out.println(td);
821 System.out.println(on.getType());
822 throw new Error("Type of rside not compatible with type of lside"+on.printNode(0));