7 public class SemanticCheck {
15 public SemanticCheck(State state, TypeUtil tu) {
18 this.loopstack=new Stack();
19 this.toanalyze=new HashSet();
20 this.completed=new HashSet();
23 public ClassDescriptor getClass(String classname) {
24 ClassDescriptor cd=typeutil.getClass(classname, toanalyze);
29 private void checkClass(ClassDescriptor cd) {
30 if (!completed.contains(cd)) {
33 //Set superclass link up
34 if (cd.getSuper()!=null) {
35 cd.setSuper(getClass(cd.getSuper()));
36 // Link together Field, Method, and Flag tables so classes
37 // inherit these from their superclasses
38 cd.getFieldTable().setParent(cd.getSuperDesc().getFieldTable());
39 cd.getMethodTable().setParent(cd.getSuperDesc().getMethodTable());
40 cd.getFlagTable().setParent(cd.getSuperDesc().getFlagTable());
43 // TODO add version for normal Java later
44 // Link together Field, Method tables do classes inherit these from
45 // their ancestor interfaces
46 Vector<String> sifv = cd.getSuperInterface();
47 for(int i = 0; i < sifv.size(); i++) {
48 ClassDescriptor superif = getClass(sifv.elementAt(i));
49 cd.addSuperInterfaces(superif);
50 cd.getFieldTable().addParentIF(superif.getFieldTable());
51 cd.getMethodTable().addParentIF(superif.getMethodTable());
55 /* Check to see that fields are well typed */
56 for(Iterator field_it=cd.getFields(); field_it.hasNext();) {
57 FieldDescriptor fd=(FieldDescriptor)field_it.next();
61 boolean hasConstructor = false;
62 for(Iterator method_it=cd.getMethods(); method_it.hasNext();) {
63 MethodDescriptor md=(MethodDescriptor)method_it.next();
65 hasConstructor |= md.isConstructor();
68 // add a default constructor for this class
69 MethodDescriptor md = new MethodDescriptor(new Modifiers(Modifiers.PUBLIC),
70 cd.getSymbol(), false);
71 BlockNode bn=new BlockNode();
72 state.addTreeCode(md,bn);
79 public void semanticCheck() {
80 SymbolTable classtable=state.getClassSymbolTable();
81 toanalyze.addAll(classtable.getValueSet());
82 toanalyze.addAll(state.getTaskSymbolTable().getValueSet());
85 while(!toanalyze.isEmpty()) {
86 Object obj=toanalyze.iterator().next();
87 if (obj instanceof TaskDescriptor) {
88 toanalyze.remove(obj);
89 TaskDescriptor td=(TaskDescriptor)obj;
93 System.out.println( "Error in "+td );
97 ClassDescriptor cd=(ClassDescriptor)obj;
99 //need to initialize typeutil object here...only place we can
100 //get class descriptors without first calling getclass
101 getClass(cd.getSymbol());
102 for(Iterator method_it=cd.getMethods(); method_it.hasNext();) {
103 MethodDescriptor md=(MethodDescriptor)method_it.next();
105 checkMethodBody(cd,md);
107 System.out.println( "Error in "+md );
115 public void checkTypeDescriptor(TypeDescriptor td) {
116 if (td.isPrimitive())
118 else if (td.isClass()) {
119 String name=td.toString();
120 int index = name.lastIndexOf('.');
122 name = name.substring(index+1);
124 ClassDescriptor field_cd=getClass(name);
126 throw new Error("Undefined class "+name);
127 td.setClassDescriptor(field_cd);
129 } else if (td.isTag())
135 public void checkField(ClassDescriptor cd, FieldDescriptor fd) {
136 checkTypeDescriptor(fd.getType());
139 public void checkConstraintCheck(TaskDescriptor td, SymbolTable nametable, Vector ccs) {
141 return; /* No constraint checks to check */
142 for(int i=0; i<ccs.size(); i++) {
143 ConstraintCheck cc=(ConstraintCheck) ccs.get(i);
145 for(int j=0; j<cc.numArgs(); j++) {
146 ExpressionNode en=cc.getArg(j);
147 checkExpressionNode(td,nametable,en,null);
152 public void checkFlagEffects(TaskDescriptor td, Vector vfe, SymbolTable nametable) {
154 return; /* No flag effects to check */
155 for(int i=0; i<vfe.size(); i++) {
156 FlagEffects fe=(FlagEffects) vfe.get(i);
157 String varname=fe.getName();
158 //Make sure the variable is declared as a parameter to the task
159 VarDescriptor vd=(VarDescriptor)td.getParameterTable().get(varname);
161 throw new Error("Parameter "+varname+" in Flag Effects not declared in "+td);
164 //Make sure it correspods to a class
165 TypeDescriptor type_d=vd.getType();
166 if (!type_d.isClass())
167 throw new Error("Cannot have non-object argument for flag_effect");
169 ClassDescriptor cd=type_d.getClassDesc();
170 for(int j=0; j<fe.numEffects(); j++) {
171 FlagEffect flag=fe.getEffect(j);
172 String name=flag.getName();
173 FlagDescriptor flag_d=(FlagDescriptor)cd.getFlagTable().get(name);
174 //Make sure the flag is declared
176 throw new Error("Flag descriptor "+name+" undefined in class: "+cd.getSymbol());
177 if (flag_d.getExternal())
178 throw new Error("Attempting to modify external flag: "+name);
179 flag.setFlag(flag_d);
181 for(int j=0; j<fe.numTagEffects(); j++) {
182 TagEffect tag=fe.getTagEffect(j);
183 String name=tag.getName();
185 Descriptor d=(Descriptor)nametable.get(name);
187 throw new Error("Tag descriptor "+name+" undeclared");
188 else if (!(d instanceof TagVarDescriptor))
189 throw new Error(name+" is not a tag descriptor");
190 tag.setTag((TagVarDescriptor)d);
195 public void checkTask(TaskDescriptor td) {
196 for(int i=0; i<td.numParameters(); i++) {
197 /* Check that parameter is well typed */
198 TypeDescriptor param_type=td.getParamType(i);
199 checkTypeDescriptor(param_type);
201 /* Check the parameter's flag expression is well formed */
202 FlagExpressionNode fen=td.getFlag(td.getParameter(i));
203 if (!param_type.isClass())
204 throw new Error("Cannot have non-object argument to a task");
205 ClassDescriptor cd=param_type.getClassDesc();
207 checkFlagExpressionNode(cd, fen);
210 checkFlagEffects(td, td.getFlagEffects(),td.getParameterTable());
211 /* Check that the task code is valid */
212 BlockNode bn=state.getMethodBody(td);
213 checkBlockNode(td, td.getParameterTable(),bn);
216 public void checkFlagExpressionNode(ClassDescriptor cd, FlagExpressionNode fen) {
218 case Kind.FlagOpNode:
220 FlagOpNode fon=(FlagOpNode)fen;
221 checkFlagExpressionNode(cd, fon.getLeft());
222 if (fon.getRight()!=null)
223 checkFlagExpressionNode(cd, fon.getRight());
229 FlagNode fn=(FlagNode)fen;
230 String name=fn.getFlagName();
231 FlagDescriptor fd=(FlagDescriptor)cd.getFlagTable().get(name);
233 throw new Error("Undeclared flag: "+name);
239 throw new Error("Unrecognized FlagExpressionNode");
243 public void checkMethod(ClassDescriptor cd, MethodDescriptor md) {
245 // TODO add version for normal Java later
246 /* Check for abstract methods */
247 if(md.isAbstract()) {
248 if(!cd.isAbstract() && !cd.isInterface()) {
249 throw new Error("Error! The non-abstract Class " + cd.getSymbol() + " contains an abstract method " + md.getSymbol());
253 /* Check return type */
254 if (!md.isConstructor() && !md.isStaticBlock())
255 if (!md.getReturnType().isVoid())
256 checkTypeDescriptor(md.getReturnType());
258 for(int i=0; i<md.numParameters(); i++) {
259 TypeDescriptor param_type=md.getParamType(i);
260 checkTypeDescriptor(param_type);
262 /* Link the naming environments */
263 if (!md.isStatic()) /* Fields aren't accessible directly in a static method, so don't link in this table */
264 md.getParameterTable().setParent(cd.getFieldTable());
266 if (!md.isStatic()) {
267 VarDescriptor thisvd=new VarDescriptor(new TypeDescriptor(cd),"this");
272 public void checkMethodBody(ClassDescriptor cd, MethodDescriptor md) {
273 ClassDescriptor superdesc=cd.getSuperDesc();
274 if (superdesc!=null) {
275 Set possiblematches=superdesc.getMethodTable().getSet(md.getSymbol());
276 for(Iterator methodit=possiblematches.iterator(); methodit.hasNext();) {
277 MethodDescriptor matchmd=(MethodDescriptor)methodit.next();
278 if (md.matches(matchmd)) {
279 if (matchmd.getModifiers().isFinal()) {
280 throw new Error("Try to override final method in method:"+md+" declared in "+cd);
285 BlockNode bn=state.getMethodBody(md);
286 checkBlockNode(md, md.getParameterTable(),bn);
289 public void checkBlockNode(Descriptor md, SymbolTable nametable, BlockNode bn) {
290 /* Link in the naming environment */
291 bn.getVarTable().setParent(nametable);
292 for(int i=0; i<bn.size(); i++) {
293 BlockStatementNode bsn=bn.get(i);
294 checkBlockStatementNode(md, bn.getVarTable(),bsn);
298 public void checkBlockStatementNode(Descriptor md, SymbolTable nametable, BlockStatementNode bsn) {
300 case Kind.BlockExpressionNode:
301 checkBlockExpressionNode(md, nametable,(BlockExpressionNode)bsn);
304 case Kind.DeclarationNode:
305 checkDeclarationNode(md, nametable, (DeclarationNode)bsn);
308 case Kind.TagDeclarationNode:
309 checkTagDeclarationNode(md, nametable, (TagDeclarationNode)bsn);
312 case Kind.IfStatementNode:
313 checkIfStatementNode(md, nametable, (IfStatementNode)bsn);
316 case Kind.SwitchStatementNode:
317 checkSwitchStatementNode(md, nametable, (SwitchStatementNode)bsn);
321 checkLoopNode(md, nametable, (LoopNode)bsn);
324 case Kind.ReturnNode:
325 checkReturnNode(md, nametable, (ReturnNode)bsn);
328 case Kind.TaskExitNode:
329 checkTaskExitNode(md, nametable, (TaskExitNode)bsn);
332 case Kind.SubBlockNode:
333 checkSubBlockNode(md, nametable, (SubBlockNode)bsn);
336 case Kind.AtomicNode:
337 checkAtomicNode(md, nametable, (AtomicNode)bsn);
340 case Kind.SynchronizedNode:
341 checkSynchronizedNode(md, nametable, (SynchronizedNode)bsn);
344 case Kind.ContinueBreakNode:
345 checkContinueBreakNode(md, nametable, (ContinueBreakNode) bsn);
349 case Kind.GenReachNode:
350 // do nothing, no semantic check for SESEs
357 void checkBlockExpressionNode(Descriptor md, SymbolTable nametable, BlockExpressionNode ben) {
358 checkExpressionNode(md, nametable, ben.getExpression(), null);
361 void checkDeclarationNode(Descriptor md, SymbolTable nametable, DeclarationNode dn) {
362 VarDescriptor vd=dn.getVarDescriptor();
363 checkTypeDescriptor(vd.getType());
364 Descriptor d=nametable.get(vd.getSymbol());
366 (d instanceof FieldDescriptor)) {
369 throw new Error(vd.getSymbol()+" in "+md+" defined a second time");
370 if (dn.getExpression()!=null)
371 checkExpressionNode(md, nametable, dn.getExpression(), vd.getType());
374 void checkTagDeclarationNode(Descriptor md, SymbolTable nametable, TagDeclarationNode dn) {
375 TagVarDescriptor vd=dn.getTagVarDescriptor();
376 Descriptor d=nametable.get(vd.getSymbol());
378 (d instanceof FieldDescriptor)) {
381 throw new Error(vd.getSymbol()+" defined a second time");
384 void checkSubBlockNode(Descriptor md, SymbolTable nametable, SubBlockNode sbn) {
385 checkBlockNode(md, nametable, sbn.getBlockNode());
388 void checkAtomicNode(Descriptor md, SymbolTable nametable, AtomicNode sbn) {
389 checkBlockNode(md, nametable, sbn.getBlockNode());
392 void checkSynchronizedNode(Descriptor md, SymbolTable nametable, SynchronizedNode sbn) {
393 checkBlockNode(md, nametable, sbn.getBlockNode());
394 //todo this could be Object
395 checkExpressionNode(md, nametable, sbn.getExpr(), null);
398 void checkContinueBreakNode(Descriptor md, SymbolTable nametable, ContinueBreakNode cbn) {
399 if (loopstack.empty())
400 throw new Error("continue/break outside of loop");
401 Object o = loopstack.peek();
402 if(o instanceof LoopNode) {
403 LoopNode ln=(LoopNode)o;
408 void checkReturnNode(Descriptor d, SymbolTable nametable, ReturnNode rn) {
409 if (d instanceof TaskDescriptor)
410 throw new Error("Illegal return appears in Task: "+d.getSymbol());
411 MethodDescriptor md=(MethodDescriptor)d;
412 if (rn.getReturnExpression()!=null)
413 if (md.getReturnType()==null)
414 throw new Error("Constructor can't return something.");
415 else if (md.getReturnType().isVoid())
416 throw new Error(md+" is void");
418 checkExpressionNode(md, nametable, rn.getReturnExpression(), md.getReturnType());
420 if (md.getReturnType()!=null&&!md.getReturnType().isVoid())
421 throw new Error("Need to return something for "+md);
424 void checkTaskExitNode(Descriptor md, SymbolTable nametable, TaskExitNode ten) {
425 if (md instanceof MethodDescriptor)
426 throw new Error("Illegal taskexit appears in Method: "+md.getSymbol());
427 checkFlagEffects((TaskDescriptor)md, ten.getFlagEffects(),nametable);
428 checkConstraintCheck((TaskDescriptor) md, nametable, ten.getChecks());
431 void checkIfStatementNode(Descriptor md, SymbolTable nametable, IfStatementNode isn) {
432 checkExpressionNode(md, nametable, isn.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
433 checkBlockNode(md, nametable, isn.getTrueBlock());
434 if (isn.getFalseBlock()!=null)
435 checkBlockNode(md, nametable, isn.getFalseBlock());
438 void checkSwitchStatementNode(Descriptor md, SymbolTable nametable, SwitchStatementNode ssn) {
439 checkExpressionNode(md, nametable, ssn.getCondition(), new TypeDescriptor(TypeDescriptor.INT));
441 BlockNode sbn = ssn.getSwitchBody();
442 boolean hasdefault = false;
443 for(int i = 0; i < sbn.size(); i++) {
444 boolean containdefault = checkSwitchBlockNode(md, nametable, (SwitchBlockNode)sbn.get(i));
445 if(hasdefault && containdefault) {
446 throw new Error("Error: duplicate default branch in switch-case statement in Method: " + md.getSymbol());
448 hasdefault = containdefault;
452 boolean checkSwitchBlockNode(Descriptor md, SymbolTable nametable, SwitchBlockNode sbn) {
453 Vector<SwitchLabelNode> slnv = sbn.getSwitchConditions();
455 for(int i = 0; i < slnv.size(); i++) {
456 if(slnv.elementAt(i).isdefault) {
459 checkConstantExpressionNode(md, nametable, slnv.elementAt(i).getCondition(), new TypeDescriptor(TypeDescriptor.INT));
463 throw new Error("Error: duplicate default branch in switch-case statement in Method: " + md.getSymbol());
466 checkBlockNode(md, nametable, sbn.getSwitchBlockStatement());
468 return (defaultb > 0);
472 void checkConstantExpressionNode(Descriptor md, SymbolTable nametable, ExpressionNode en, TypeDescriptor td) {
474 case Kind.FieldAccessNode:
475 checkFieldAccessNode(md,nametable,(FieldAccessNode)en,td);
478 case Kind.LiteralNode:
479 checkLiteralNode(md,nametable,(LiteralNode)en,td);
483 checkNameNode(md,nametable,(NameNode)en,td);
487 checkOpNode(md, nametable, (OpNode)en, td);
493 void checkExpressionNode(Descriptor md, SymbolTable nametable, ExpressionNode en, TypeDescriptor td) {
495 case Kind.AssignmentNode:
496 checkAssignmentNode(md,nametable,(AssignmentNode)en,td);
500 checkCastNode(md,nametable,(CastNode)en,td);
503 case Kind.CreateObjectNode:
504 checkCreateObjectNode(md,nametable,(CreateObjectNode)en,td);
507 case Kind.FieldAccessNode:
508 checkFieldAccessNode(md,nametable,(FieldAccessNode)en,td);
511 case Kind.ArrayAccessNode:
512 checkArrayAccessNode(md,nametable,(ArrayAccessNode)en,td);
515 case Kind.LiteralNode:
516 checkLiteralNode(md,nametable,(LiteralNode)en,td);
519 case Kind.MethodInvokeNode:
520 checkMethodInvokeNode(md,nametable,(MethodInvokeNode)en,td);
524 checkNameNode(md,nametable,(NameNode)en,td);
528 checkOpNode(md,nametable,(OpNode)en,td);
531 case Kind.OffsetNode:
532 checkOffsetNode(md, nametable, (OffsetNode)en, td);
535 case Kind.TertiaryNode:
536 checkTertiaryNode(md, nametable, (TertiaryNode)en, td);
539 case Kind.InstanceOfNode:
540 checkInstanceOfNode(md, nametable, (InstanceOfNode) en, td);
543 case Kind.ArrayInitializerNode:
544 checkArrayInitializerNode(md, nametable, (ArrayInitializerNode) en, td);
547 case Kind.ClassTypeNode:
548 checkClassTypeNode(md, nametable, (ClassTypeNode) en, td);
554 void checkClassTypeNode(Descriptor md, SymbolTable nametable, ClassTypeNode tn, TypeDescriptor td) {
555 checkTypeDescriptor(tn.getType());
558 void checkCastNode(Descriptor md, SymbolTable nametable, CastNode cn, TypeDescriptor td) {
559 /* Get type descriptor */
560 if (cn.getType()==null) {
561 NameDescriptor typenamed=cn.getTypeName().getName();
562 String typename=typenamed.toString();
563 TypeDescriptor ntd=new TypeDescriptor(getClass(typename));
567 /* Check the type descriptor */
568 TypeDescriptor cast_type=cn.getType();
569 checkTypeDescriptor(cast_type);
573 if (!typeutil.isSuperorType(td,cast_type))
574 throw new Error("Cast node returns "+cast_type+", but need "+td);
577 ExpressionNode en=cn.getExpression();
578 checkExpressionNode(md, nametable, en, null);
579 TypeDescriptor etd=en.getType();
580 if (typeutil.isSuperorType(cast_type,etd)) /* Cast trivially succeeds */
583 if (typeutil.isSuperorType(etd,cast_type)) /* Cast may succeed */
585 if (typeutil.isCastable(etd, cast_type))
588 /* Different branches */
589 /* TODO: change if add interfaces */
590 throw new Error("Cast will always fail\n"+cn.printNode(0));
593 void checkFieldAccessNode(Descriptor md, SymbolTable nametable, FieldAccessNode fan, TypeDescriptor td) {
594 ExpressionNode left=fan.getExpression();
595 checkExpressionNode(md,nametable,left,null);
596 TypeDescriptor ltd=left.getType();
597 String fieldname=fan.getFieldName();
599 FieldDescriptor fd=null;
600 if (ltd.isArray()&&fieldname.equals("length"))
601 fd=FieldDescriptor.arrayLength;
603 fd=(FieldDescriptor) ltd.getClassDesc().getFieldTable().get(fieldname);
605 // TODO add version for normal Java later
607 // the field access is using a class name directly
608 if(ltd.getClassDesc().isEnum()) {
609 int value = ltd.getClassDesc().getEnumConstant(fieldname);
611 // check if this field is an enum constant
612 throw new Error(fieldname + " is not an enum constant in "+fan.printNode(0)+" in "+md);
614 fd = new FieldDescriptor(new Modifiers(Modifiers.PUBLIC|Modifiers.FINAL), new TypeDescriptor(TypeDescriptor.INT), fieldname, null, false);
616 fd.setEnumValue(value);
617 } else if(fd.isStatic()) {
618 // check if this field is a static field
619 if(fd.getExpressionNode() != null) {
620 checkExpressionNode(md,nametable,fd.getExpressionNode(),null);
623 throw new Error("Dereference of the non-static field "+ fieldname + " in "+fan.printNode(0)+" in "+md);
628 throw new Error("Unknown field "+fieldname + " in "+fan.printNode(0)+" in "+md);
630 if (fd.getType().iswrapper()) {
631 FieldAccessNode fan2=new FieldAccessNode(left, fieldname);
634 fan.fieldname="value";
636 ExpressionNode leftwr=fan.getExpression();
637 TypeDescriptor ltdwr=leftwr.getType();
638 String fieldnamewr=fan.getFieldName();
639 FieldDescriptor fdwr=(FieldDescriptor) ltdwr.getClassDesc().getFieldTable().get(fieldnamewr);
642 throw new Error("Unknown field "+fieldnamewr + " in "+fan.printNode(0)+" in "+md);
647 if (!typeutil.isSuperorType(td,fan.getType()))
648 throw new Error("Field node returns "+fan.getType()+", but need "+td);
652 void checkArrayAccessNode(Descriptor md, SymbolTable nametable, ArrayAccessNode aan, TypeDescriptor td) {
653 ExpressionNode left=aan.getExpression();
654 checkExpressionNode(md,nametable,left,null);
656 checkExpressionNode(md,nametable,aan.getIndex(),new TypeDescriptor(TypeDescriptor.INT));
657 TypeDescriptor ltd=left.getType();
658 if (ltd.dereference().iswrapper()) {
659 aan.wrappertype=((FieldDescriptor)ltd.dereference().getClassDesc().getFieldTable().get("value")).getType();
663 if (!typeutil.isSuperorType(td,aan.getType()))
664 throw new Error("Field node returns "+aan.getType()+", but need "+td);
667 void checkLiteralNode(Descriptor md, SymbolTable nametable, LiteralNode ln, TypeDescriptor td) {
668 /* Resolve the type */
669 Object o=ln.getValue();
670 if (ln.getTypeString().equals("null")) {
671 ln.setType(new TypeDescriptor(TypeDescriptor.NULL));
672 } else if (o instanceof Integer) {
673 ln.setType(new TypeDescriptor(TypeDescriptor.INT));
674 } else if (o instanceof Long) {
675 ln.setType(new TypeDescriptor(TypeDescriptor.LONG));
676 } else if (o instanceof Float) {
677 ln.setType(new TypeDescriptor(TypeDescriptor.FLOAT));
678 } else if (o instanceof Boolean) {
679 ln.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
680 } else if (o instanceof Double) {
681 ln.setType(new TypeDescriptor(TypeDescriptor.DOUBLE));
682 } else if (o instanceof Character) {
683 ln.setType(new TypeDescriptor(TypeDescriptor.CHAR));
684 } else if (o instanceof String) {
685 ln.setType(new TypeDescriptor(getClass(TypeUtil.StringClass)));
689 if (!typeutil.isSuperorType(td,ln.getType())) {
690 Long l = ln.evaluate();
691 if((ln.getType().isByte() || ln.getType().isShort()
692 || ln.getType().isChar() || ln.getType().isInt())
694 && (td.isByte() || td.isShort() || td.isChar()
695 || td.isInt() || td.isLong())) {
696 long lnvalue = l.longValue();
697 if((td.isByte() && ((lnvalue > 127) || (lnvalue < -128)))
698 || (td.isShort() && ((lnvalue > 32767) || (lnvalue < -32768)))
699 || (td.isChar() && ((lnvalue > 65535) || (lnvalue < 0)))
700 || (td.isInt() && ((lnvalue > 2147483647) || (lnvalue < -2147483648)))
701 || (td.isLong() && ((lnvalue > 9223372036854775807L) || (lnvalue < -9223372036854775808L)))) {
702 throw new Error("Field node returns "+ln.getType()+", but need "+td+" in "+md);
705 throw new Error("Field node returns "+ln.getType()+", but need "+td+" in "+md);
710 void checkNameNode(Descriptor md, SymbolTable nametable, NameNode nn, TypeDescriptor td) {
711 NameDescriptor nd=nn.getName();
712 if (nd.getBase()!=null) {
714 /* Rewrite NameNode */
715 ExpressionNode en=translateNameDescriptorintoExpression(nd);
716 nn.setExpression(en);
717 checkExpressionNode(md,nametable,en,td);
719 String varname=nd.toString();
720 if(varname.equals("this")) {
722 nn.setVar((VarDescriptor)nametable.get("this"));
725 Descriptor d=(Descriptor)nametable.get(varname);
728 // TODO add version for normal Java later
729 ClassDescriptor cd = null;
730 if(((MethodDescriptor)md).isStaticBlock()) {
731 // this is a static block, all the accessed fields should be static field
732 cd = ((MethodDescriptor)md).getClassDesc();
733 SymbolTable fieldtbl = cd.getFieldTable();
734 FieldDescriptor fd=(FieldDescriptor)fieldtbl.get(varname);
735 if((fd == null) || (!fd.isStatic())){
736 // no such field in the class, check if this is a class
737 if(varname.equals("this")) {
738 throw new Error("Error: access this obj in a static block");
740 cd=getClass(varname);
742 // this is a class name
746 throw new Error("Name "+varname+" should not be used in static block: "+md);
749 // this is a static field
755 // check if the var is a static field of the class
756 if(md instanceof MethodDescriptor) {
757 cd = ((MethodDescriptor)md).getClassDesc();
758 FieldDescriptor fd = (FieldDescriptor)cd.getFieldTable().get(varname);
759 if((fd != null) && (fd.isStatic())) {
763 if (!typeutil.isSuperorType(td,nn.getType()))
764 throw new Error("Field node returns "+nn.getType()+", but need "+td);
766 } else if(fd != null) {
767 throw new Error("Name "+varname+" should not be used in " + md);
770 cd=getClass(varname);
772 // this is a class name
776 throw new Error("Name "+varname+" undefined in: "+md);
780 throw new Error("Name "+varname+" undefined in: "+md);
783 if (d instanceof VarDescriptor) {
785 } else if (d instanceof FieldDescriptor) {
786 FieldDescriptor fd=(FieldDescriptor)d;
787 if (fd.getType().iswrapper()) {
788 String id=nd.getIdentifier();
789 NameDescriptor base=nd.getBase();
790 NameNode n=new NameNode(nn.getName());
792 n.setVar((VarDescriptor)nametable.get("this")); /* Need a pointer to this */
793 FieldAccessNode fan=new FieldAccessNode(n,"value");
794 FieldDescriptor fdval=(FieldDescriptor) fd.getType().getClassDesc().getFieldTable().get("value");
796 nn.setExpression(fan);
799 nn.setVar((VarDescriptor)nametable.get("this")); /* Need a pointer to this */
801 } else if (d instanceof TagVarDescriptor) {
803 } else throw new Error("Wrong type of descriptor");
805 if (!typeutil.isSuperorType(td,nn.getType()))
806 throw new Error("Field node returns "+nn.getType()+", but need "+td);
810 void checkOffsetNode(Descriptor md, SymbolTable nameTable, OffsetNode ofn, TypeDescriptor td) {
811 TypeDescriptor ltd=ofn.td;
812 checkTypeDescriptor(ltd);
814 String fieldname = ofn.fieldname;
815 FieldDescriptor fd=null;
816 if (ltd.isArray()&&fieldname.equals("length")) {
817 fd=FieldDescriptor.arrayLength;
819 fd=(FieldDescriptor) ltd.getClassDesc().getFieldTable().get(fieldname);
823 checkField(ltd.getClassDesc(), fd);
826 throw new Error("Unknown field "+fieldname + " in "+ofn.printNode(1)+" in "+md);
829 if (!typeutil.isSuperorType(td, ofn.getType())) {
830 System.out.println(td);
831 System.out.println(ofn.getType());
832 throw new Error("Type of rside not compatible with type of lside"+ofn.printNode(0));
838 void checkTertiaryNode(Descriptor md, SymbolTable nametable, TertiaryNode tn, TypeDescriptor td) {
839 checkExpressionNode(md, nametable, tn.getCond(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
840 checkExpressionNode(md, nametable, tn.getTrueExpr(), td );
841 checkExpressionNode(md, nametable, tn.getFalseExpr(), td );
844 void checkInstanceOfNode(Descriptor md, SymbolTable nametable, InstanceOfNode tn, TypeDescriptor td) {
845 if (td!=null&&!td.isBoolean())
846 throw new Error("Expecting type "+td+"for instanceof expression");
848 checkTypeDescriptor(tn.getExprType());
849 checkExpressionNode(md, nametable, tn.getExpr(), null);
852 void checkArrayInitializerNode(Descriptor md, SymbolTable nametable, ArrayInitializerNode ain, TypeDescriptor td) {
853 Vector<TypeDescriptor> vec_type = new Vector<TypeDescriptor>();
854 for( int i = 0; i < ain.numVarInitializers(); ++i ) {
855 checkExpressionNode(md, nametable, ain.getVarInitializer(i), td==null?td:td.dereference());
856 vec_type.add(ain.getVarInitializer(i).getType());
858 // descide the type of this variableInitializerNode
859 TypeDescriptor out_type = vec_type.elementAt(0);
860 for(int i = 1; i < vec_type.size(); i++) {
861 TypeDescriptor tmp_type = vec_type.elementAt(i);
862 if(out_type == null) {
863 if(tmp_type != null) {
866 } else if(out_type.isNull()) {
867 if(!tmp_type.isNull() ) {
868 if(!tmp_type.isArray()) {
869 throw new Error("Error: mixed type in var initializer list");
874 } else if(out_type.isArray()) {
875 if(tmp_type.isArray()) {
876 if(tmp_type.getArrayCount() > out_type.getArrayCount()) {
879 } else if((tmp_type != null) && (!tmp_type.isNull())) {
880 throw new Error("Error: mixed type in var initializer list");
882 } else if(out_type.isInt()) {
883 if(!tmp_type.isInt()) {
884 throw new Error("Error: mixed type in var initializer list");
886 } else if(out_type.isString()) {
887 if(!tmp_type.isString()) {
888 throw new Error("Error: mixed type in var initializer list");
892 if(out_type != null) {
893 out_type = out_type.makeArray(state);
894 out_type.setStatic();
896 ain.setType(out_type);
899 void checkAssignmentNode(Descriptor md, SymbolTable nametable, AssignmentNode an, TypeDescriptor td) {
900 boolean postinc=true;
901 if (an.getOperation().getBaseOp()==null||
902 (an.getOperation().getBaseOp().getOp()!=Operation.POSTINC&&
903 an.getOperation().getBaseOp().getOp()!=Operation.POSTDEC))
906 checkExpressionNode(md, nametable, an.getSrc(),td);
907 //TODO: Need check on validity of operation here
908 if (!((an.getDest() instanceof FieldAccessNode)||
909 (an.getDest() instanceof ArrayAccessNode)||
910 (an.getDest() instanceof NameNode)))
911 throw new Error("Bad lside in "+an.printNode(0));
912 checkExpressionNode(md, nametable, an.getDest(), null);
914 /* We want parameter variables to tasks to be immutable */
915 if (md instanceof TaskDescriptor) {
916 if (an.getDest() instanceof NameNode) {
917 NameNode nn=(NameNode)an.getDest();
918 if (nn.getVar()!=null) {
919 if (((TaskDescriptor)md).getParameterTable().contains(nn.getVar().getSymbol()))
920 throw new Error("Can't modify parameter "+nn.getVar()+ " to task "+td.getSymbol());
925 if (an.getDest().getType().isString()&&an.getOperation().getOp()==AssignOperation.PLUSEQ) {
927 ClassDescriptor stringcl=getClass(TypeUtil.StringClass);
928 TypeDescriptor stringtd=new TypeDescriptor(stringcl);
929 NameDescriptor nd=new NameDescriptor("String");
930 NameDescriptor valuend=new NameDescriptor(nd, "valueOf");
932 if (!(an.getSrc().getType().isString()&&(an.getSrc() instanceof OpNode))) {
933 MethodInvokeNode rightmin=new MethodInvokeNode(valuend);
934 rightmin.addArgument(an.getSrc());
936 checkExpressionNode(md, nametable, an.getSrc(), null);
940 if (!postinc&&!typeutil.isSuperorType(an.getDest().getType(),an.getSrc().getType())) {
941 TypeDescriptor dt = an.getDest().getType();
942 TypeDescriptor st = an.getSrc().getType();
943 if(an.getSrc().kind() == Kind.ArrayInitializerNode) {
944 if(dt.getArrayCount() != st.getArrayCount()) {
945 throw new Error("Type of rside ("+an.getSrc().getType().toPrettyString()+") not compatible with type of lside ("+an.getDest().getType().toPrettyString()+")"+an.printNode(0));
948 dt = dt.dereference();
949 st = st.dereference();
950 } while(dt.isArray());
951 if((st.isByte() || st.isShort() || st.isChar() || st.isInt())
952 && (dt.isByte() || dt.isShort() || dt.isChar() || dt.isInt() || dt.isLong())) {
955 throw new Error("Type of rside ("+an.getSrc().getType().toPrettyString()+") not compatible with type of lside ("+an.getDest().getType().toPrettyString()+")"+an.printNode(0));
959 Long l = an.getSrc().evaluate();
960 if((st.isByte() || st.isShort() || st.isChar() || st.isInt())
962 && (dt.isByte() || dt.isShort() || dt.isChar() || dt.isInt() || dt.isLong())) {
963 long lnvalue = l.longValue();
964 if((dt.isByte() && ((lnvalue > 127) || (lnvalue < -128)))
965 || (dt.isShort() && ((lnvalue > 32767) || (lnvalue < -32768)))
966 || (dt.isChar() && ((lnvalue > 65535) || (lnvalue < 0)))
967 || (dt.isInt() && ((lnvalue > 2147483647) || (lnvalue < -2147483648)))
968 || (dt.isLong() && ((lnvalue > 9223372036854775807L) || (lnvalue < -9223372036854775808L)))) {
969 throw new Error("Type of rside ("+an.getSrc().getType().toPrettyString()+") not compatible with type of lside ("+an.getDest().getType().toPrettyString()+")"+an.printNode(0));
972 throw new Error("Type of rside ("+an.getSrc().getType().toPrettyString()+") not compatible with type of lside ("+an.getDest().getType().toPrettyString()+")"+an.printNode(0));
978 void checkLoopNode(Descriptor md, SymbolTable nametable, LoopNode ln) {
980 if (ln.getType()==LoopNode.WHILELOOP||ln.getType()==LoopNode.DOWHILELOOP) {
981 checkExpressionNode(md, nametable, ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
982 checkBlockNode(md, nametable, ln.getBody());
985 /* Link in the initializer naming environment */
986 BlockNode bn=ln.getInitializer();
987 bn.getVarTable().setParent(nametable);
988 for(int i=0; i<bn.size(); i++) {
989 BlockStatementNode bsn=bn.get(i);
990 checkBlockStatementNode(md, bn.getVarTable(),bsn);
992 //check the condition
993 checkExpressionNode(md, bn.getVarTable(), ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
994 checkBlockNode(md, bn.getVarTable(), ln.getBody());
995 checkBlockNode(md, bn.getVarTable(), ln.getUpdate());
1001 void checkCreateObjectNode(Descriptor md, SymbolTable nametable, CreateObjectNode con, TypeDescriptor td) {
1002 TypeDescriptor[] tdarray=new TypeDescriptor[con.numArgs()];
1003 for(int i=0; i<con.numArgs(); i++) {
1004 ExpressionNode en=con.getArg(i);
1005 checkExpressionNode(md,nametable,en,null);
1006 tdarray[i]=en.getType();
1009 TypeDescriptor typetolookin=con.getType();
1010 checkTypeDescriptor(typetolookin);
1012 if (td!=null&&!typeutil.isSuperorType(td, typetolookin))
1013 throw new Error(typetolookin + " isn't a "+td);
1015 /* Check Array Initializers */
1016 if(state.MGC && (con.getArrayInitializer() != null)) {
1017 checkArrayInitializerNode(md, nametable, con.getArrayInitializer(), td);
1020 /* Check flag effects */
1021 if (con.getFlagEffects()!=null) {
1022 FlagEffects fe=con.getFlagEffects();
1023 ClassDescriptor cd=typetolookin.getClassDesc();
1025 for(int j=0; j<fe.numEffects(); j++) {
1026 FlagEffect flag=fe.getEffect(j);
1027 String name=flag.getName();
1028 FlagDescriptor flag_d=(FlagDescriptor)cd.getFlagTable().get(name);
1029 //Make sure the flag is declared
1031 throw new Error("Flag descriptor "+name+" undefined in class: "+cd.getSymbol());
1032 if (flag_d.getExternal())
1033 throw new Error("Attempting to modify external flag: "+name);
1034 flag.setFlag(flag_d);
1036 for(int j=0; j<fe.numTagEffects(); j++) {
1037 TagEffect tag=fe.getTagEffect(j);
1038 String name=tag.getName();
1040 Descriptor d=(Descriptor)nametable.get(name);
1042 throw new Error("Tag descriptor "+name+" undeclared");
1043 else if (!(d instanceof TagVarDescriptor))
1044 throw new Error(name+" is not a tag descriptor");
1045 tag.setTag((TagVarDescriptor)d);
1049 if ((!typetolookin.isClass())&&(!typetolookin.isArray()))
1050 throw new Error("Can't allocate primitive type:"+con.printNode(0));
1052 if (!typetolookin.isArray()) {
1053 //Array's don't need constructor calls
1054 ClassDescriptor classtolookin=typetolookin.getClassDesc();
1056 Set methoddescriptorset=classtolookin.getMethodTable().getSet(typetolookin.getSymbol());
1057 MethodDescriptor bestmd=null;
1059 for(Iterator methodit=methoddescriptorset.iterator(); methodit.hasNext();) {
1060 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
1061 /* Need correct number of parameters */
1062 if (con.numArgs()!=currmd.numParameters())
1064 for(int i=0; i<con.numArgs(); i++) {
1065 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
1066 continue NextMethod;
1068 /* Local allocations can't call global allocator */
1069 if (!con.isGlobal()&&currmd.isGlobal())
1072 /* Method okay so far */
1076 if (typeutil.isMoreSpecific(currmd,bestmd)) {
1078 } else if (con.isGlobal()&&match(currmd, bestmd)) {
1079 if (currmd.isGlobal()&&!bestmd.isGlobal())
1081 else if (currmd.isGlobal()&&bestmd.isGlobal())
1083 } else if (!typeutil.isMoreSpecific(bestmd, currmd)) {
1084 throw new Error("No method is most specific");
1087 /* Is this more specific than bestmd */
1091 throw new Error("No method found for "+con.printNode(0)+" in "+md);
1092 con.setConstructor(bestmd);
1097 /** Check to see if md1 is the same specificity as md2.*/
1099 boolean match(MethodDescriptor md1, MethodDescriptor md2) {
1100 /* Checks if md1 is more specific than md2 */
1101 if (md1.numParameters()!=md2.numParameters())
1103 for(int i=0; i<md1.numParameters(); i++) {
1104 if (!md2.getParamType(i).equals(md1.getParamType(i)))
1107 if (!md2.getReturnType().equals(md1.getReturnType()))
1110 if (!md2.getClassDesc().equals(md1.getClassDesc()))
1118 ExpressionNode translateNameDescriptorintoExpression(NameDescriptor nd) {
1119 String id=nd.getIdentifier();
1120 NameDescriptor base=nd.getBase();
1122 return new NameNode(nd);
1124 return new FieldAccessNode(translateNameDescriptorintoExpression(base),id);
1128 void checkMethodInvokeNode(Descriptor md, SymbolTable nametable, MethodInvokeNode min, TypeDescriptor td) {
1129 /*Typecheck subexpressions
1130 and get types for expressions*/
1132 boolean isstatic = false;
1134 if((md instanceof MethodDescriptor) && ((MethodDescriptor)md).isStatic()) {
1138 TypeDescriptor[] tdarray=new TypeDescriptor[min.numArgs()];
1139 for(int i=0; i<min.numArgs(); i++) {
1140 ExpressionNode en=min.getArg(i);
1141 checkExpressionNode(md,nametable,en,null);
1142 tdarray[i]=en.getType();
1143 if(state.MGC && en.getType().isClass() && en.getType().getClassDesc().isEnum()) {
1144 tdarray[i] = new TypeDescriptor(TypeDescriptor.INT);
1147 TypeDescriptor typetolookin=null;
1148 if (min.getExpression()!=null) {
1149 checkExpressionNode(md,nametable,min.getExpression(),null);
1150 typetolookin=min.getExpression().getType();
1151 //if (typetolookin==null)
1152 //throw new Error(md+" has null return type");
1154 } else if (min.getBaseName()!=null) {
1155 String rootname=min.getBaseName().getRoot();
1156 if (rootname.equals("super")) {
1157 ClassDescriptor supercd=((MethodDescriptor)md).getClassDesc().getSuperDesc();
1158 typetolookin=new TypeDescriptor(supercd);
1159 } else if (rootname.equals("this")) {
1161 throw new Error("use this object in static method md = "+ md.toString());
1163 ClassDescriptor cd=((MethodDescriptor)md).getClassDesc();
1164 typetolookin=new TypeDescriptor(cd);
1165 } else if (nametable.get(rootname)!=null) {
1166 //we have an expression
1167 min.setExpression(translateNameDescriptorintoExpression(min.getBaseName()));
1168 checkExpressionNode(md, nametable, min.getExpression(), null);
1169 typetolookin=min.getExpression().getType();
1172 if(!min.getBaseName().getSymbol().equals("System.out")) {
1173 ExpressionNode nn = translateNameDescriptorintoExpression(min.getBaseName());
1174 checkExpressionNode(md, nametable, nn, null);
1175 typetolookin = nn.getType();
1176 if(!((nn.kind()== Kind.NameNode) && (((NameNode)nn).getField() == null)
1177 && (((NameNode)nn).getVar() == null) && (((NameNode)nn).getExpression() == null))) {
1178 // this is not a pure class name, need to add to
1179 min.setExpression(nn);
1183 ClassDescriptor cd = null;
1184 //if (min.getBaseName().getSymbol().equals("System.out"))
1185 cd=getClass("System");
1187 cd=getClass(min.getBaseName().getSymbol());
1190 throw new Error("md = "+ md.toString()+ " "+min.getBaseName()+" undefined");
1191 typetolookin=new TypeDescriptor(cd);
1195 ClassDescriptor cd = null;
1196 if (min.getBaseName().getSymbol().equals("System.out"))
1197 cd=getClass("System");
1199 cd=getClass(min.getBaseName().getSymbol());
1202 throw new Error("md = "+ md.toString()+ " "+min.getBaseName()+" undefined");
1203 typetolookin=new TypeDescriptor(cd);
1206 } else if ((md instanceof MethodDescriptor)&&min.getMethodName().equals("super")) {
1207 ClassDescriptor supercd=((MethodDescriptor)md).getClassDesc().getSuperDesc();
1208 min.methodid=supercd.getSymbol();
1209 typetolookin=new TypeDescriptor(supercd);
1210 } else if (md instanceof MethodDescriptor) {
1211 typetolookin=new TypeDescriptor(((MethodDescriptor)md).getClassDesc());
1213 /* If this a task descriptor we throw an error at this point */
1214 throw new Error("Unknown method call to "+min.getMethodName()+"in task"+md.getSymbol());
1216 if (!typetolookin.isClass())
1217 throw new Error("Error with method call to "+min.getMethodName());
1218 ClassDescriptor classtolookin=typetolookin.getClassDesc();
1219 //System.out.println("Method name="+min.getMethodName());
1221 Set methoddescriptorset=classtolookin.getMethodTable().getSet(min.getMethodName());
1222 MethodDescriptor bestmd=null;
1224 for(Iterator methodit=methoddescriptorset.iterator(); methodit.hasNext();) {
1225 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
1226 /* Need correct number of parameters */
1227 if (min.numArgs()!=currmd.numParameters())
1229 for(int i=0; i<min.numArgs(); i++) {
1230 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
1231 continue NextMethod;
1233 /* Method okay so far */
1237 if (typeutil.isMoreSpecific(currmd,bestmd)) {
1239 } else if (!typeutil.isMoreSpecific(bestmd, currmd))
1240 throw new Error("No method is most specific");
1242 /* Is this more specific than bestmd */
1246 throw new Error("No method found for :"+min.printNode(0)+" in class: " + classtolookin+" in "+md);
1247 min.setMethod(bestmd);
1249 if ((td!=null)&&(min.getType()!=null)&&!typeutil.isSuperorType(td, min.getType()))
1250 throw new Error(min.getType()+ " is not equal to or a subclass of "+td);
1251 /* Check whether we need to set this parameter to implied this */
1252 if (! isstatic && !bestmd.isStatic()) {
1253 if (min.getExpression()==null) {
1254 ExpressionNode en=new NameNode(new NameDescriptor("this"));
1255 min.setExpression(en);
1256 checkExpressionNode(md, nametable, min.getExpression(), null);
1262 void checkOpNode(Descriptor md, SymbolTable nametable, OpNode on, TypeDescriptor td) {
1263 checkExpressionNode(md, nametable, on.getLeft(), null);
1264 if (on.getRight()!=null)
1265 checkExpressionNode(md, nametable, on.getRight(), null);
1266 TypeDescriptor ltd=on.getLeft().getType();
1267 TypeDescriptor rtd=on.getRight()!=null ? on.getRight().getType() : null;
1268 TypeDescriptor lefttype=null;
1269 TypeDescriptor righttype=null;
1270 Operation op=on.getOp();
1272 switch(op.getOp()) {
1273 case Operation.LOGIC_OR:
1274 case Operation.LOGIC_AND:
1275 if (!(rtd.isBoolean()))
1277 on.setRightType(rtd);
1279 case Operation.LOGIC_NOT:
1280 if (!(ltd.isBoolean()))
1283 on.setLeftType(ltd);
1285 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1288 case Operation.COMP:
1289 // 5.6.2 Binary Numeric Promotion
1290 //TODO unboxing of reference objects
1293 else if (ltd.isFloat())
1295 else if (ltd.isLong())
1296 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1298 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1299 on.setLeftType(lefttype);
1300 on.setType(lefttype);
1303 case Operation.BIT_OR:
1304 case Operation.BIT_XOR:
1305 case Operation.BIT_AND:
1306 // 5.6.2 Binary Numeric Promotion
1307 //TODO unboxing of reference objects
1308 if (ltd.isDouble()||rtd.isDouble())
1310 else if (ltd.isFloat()||rtd.isFloat())
1312 else if (ltd.isLong()||rtd.isLong())
1313 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1314 // 090205 hack for boolean
1315 else if (ltd.isBoolean()||rtd.isBoolean())
1316 lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
1318 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1321 on.setLeftType(lefttype);
1322 on.setRightType(righttype);
1323 on.setType(lefttype);
1326 case Operation.ISAVAILABLE:
1327 if (!(ltd.isPtr())) {
1328 throw new Error("Can't use isavailable on non-pointers/non-parameters.");
1331 on.setLeftType(lefttype);
1332 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1335 case Operation.EQUAL:
1336 case Operation.NOTEQUAL:
1337 // 5.6.2 Binary Numeric Promotion
1338 //TODO unboxing of reference objects
1339 if (ltd.isBoolean()||rtd.isBoolean()) {
1340 if (!(ltd.isBoolean()&&rtd.isBoolean()))
1342 righttype=lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
1343 } else if (ltd.isPtr()||rtd.isPtr()) {
1344 if (!(ltd.isPtr()&&rtd.isPtr()))
1348 } else if (ltd.isDouble()||rtd.isDouble())
1349 righttype=lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
1350 else if (ltd.isFloat()||rtd.isFloat())
1351 righttype=lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
1352 else if (ltd.isLong()||rtd.isLong())
1353 righttype=lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1355 righttype=lefttype=new TypeDescriptor(TypeDescriptor.INT);
1357 on.setLeftType(lefttype);
1358 on.setRightType(righttype);
1359 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1368 // 5.6.2 Binary Numeric Promotion
1369 //TODO unboxing of reference objects
1370 if (!ltd.isNumber()||!rtd.isNumber()) {
1371 if (!ltd.isNumber())
1372 throw new Error("Leftside is not number"+on.printNode(0)+"type="+ltd.toPrettyString());
1373 if (!rtd.isNumber())
1374 throw new Error("Rightside is not number"+on.printNode(0));
1377 if (ltd.isDouble()||rtd.isDouble())
1378 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
1379 else if (ltd.isFloat()||rtd.isFloat())
1380 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
1381 else if (ltd.isLong()||rtd.isLong())
1382 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1384 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1386 on.setLeftType(lefttype);
1387 on.setRightType(righttype);
1388 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1392 if (ltd.isString()||rtd.isString()) {
1393 ClassDescriptor stringcl=getClass(TypeUtil.StringClass);
1394 TypeDescriptor stringtd=new TypeDescriptor(stringcl);
1395 NameDescriptor nd=new NameDescriptor("String");
1396 NameDescriptor valuend=new NameDescriptor(nd, "valueOf");
1397 if (!(ltd.isString()&&(on.getLeft() instanceof OpNode))) {
1398 MethodInvokeNode leftmin=new MethodInvokeNode(valuend);
1399 leftmin.addArgument(on.getLeft());
1401 checkExpressionNode(md, nametable, on.getLeft(), null);
1404 if (!(rtd.isString()&&(on.getRight() instanceof OpNode))) {
1405 MethodInvokeNode rightmin=new MethodInvokeNode(valuend);
1406 rightmin.addArgument(on.getRight());
1408 checkExpressionNode(md, nametable, on.getRight(), null);
1411 on.setLeftType(stringtd);
1412 on.setRightType(stringtd);
1413 on.setType(stringtd);
1418 case Operation.MULT:
1421 // 5.6.2 Binary Numeric Promotion
1422 //TODO unboxing of reference objects
1423 if (ltd.isArray()||rtd.isArray()||!ltd.isNumber()||!rtd.isNumber())
1424 throw new Error("Error in "+on.printNode(0));
1426 if (ltd.isDouble()||rtd.isDouble())
1427 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
1428 else if (ltd.isFloat()||rtd.isFloat())
1429 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
1430 else if (ltd.isLong()||rtd.isLong())
1431 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1433 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1435 on.setLeftType(lefttype);
1436 on.setRightType(righttype);
1437 on.setType(lefttype);
1440 case Operation.LEFTSHIFT:
1441 case Operation.RIGHTSHIFT:
1442 case Operation.URIGHTSHIFT:
1443 if (!rtd.isIntegerType())
1445 //5.6.1 Unary Numeric Promotion
1446 if (rtd.isByte()||rtd.isShort()||rtd.isInt())
1447 righttype=new TypeDescriptor(TypeDescriptor.INT);
1451 on.setRightType(righttype);
1452 if (!ltd.isIntegerType())
1455 case Operation.UNARYPLUS:
1456 case Operation.UNARYMINUS:
1457 /* case Operation.POSTINC:
1458 case Operation.POSTDEC:
1459 case Operation.PREINC:
1460 case Operation.PREDEC:*/
1461 if (!ltd.isNumber())
1463 //5.6.1 Unary Numeric Promotion
1464 if (ltd.isByte()||ltd.isShort()||ltd.isInt())
1465 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1468 on.setLeftType(lefttype);
1469 on.setType(lefttype);
1473 throw new Error(op.toString());
1477 if (!typeutil.isSuperorType(td, on.getType())) {
1478 System.out.println(td);
1479 System.out.println(on.getType());
1480 throw new Error("Type of rside not compatible with type of lside"+on.printNode(0));