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 if(cd.getSuperDesc().isInterface()) {
37 throw new Error("Error! Class " + cd.getSymbol() + " extends interface " + cd.getSuper());
39 // Link together Field, Method, and Flag tables so classes
40 // inherit these from their superclasses
41 cd.getFieldTable().setParent(cd.getSuperDesc().getFieldTable());
42 cd.getMethodTable().setParent(cd.getSuperDesc().getMethodTable());
43 cd.getFlagTable().setParent(cd.getSuperDesc().getFlagTable());
46 // TODO add version for normal Java later
47 // Link together Field, Method tables do classes inherit these from
48 // their ancestor interfaces
49 Vector<String> sifv = cd.getSuperInterface();
50 for(int i = 0; i < sifv.size(); i++) {
51 ClassDescriptor superif = getClass(sifv.elementAt(i));
52 if(!superif.isInterface()) {
53 throw new Error("Error! Class " + cd.getSymbol() + " implements non-interface " + superif.getSymbol());
55 cd.addSuperInterfaces(superif);
56 cd.getFieldTable().addParentIF(superif.getFieldTable());
57 cd.getMethodTable().addParentIF(superif.getMethodTable());
61 /* Check to see that fields are well typed */
62 for(Iterator field_it=cd.getFields(); field_it.hasNext();) {
63 FieldDescriptor fd=(FieldDescriptor)field_it.next();
67 boolean hasConstructor = false;
68 for(Iterator method_it=cd.getMethods(); method_it.hasNext();) {
69 MethodDescriptor md=(MethodDescriptor)method_it.next();
71 hasConstructor |= md.isConstructor();
73 if((!hasConstructor) && (!cd.isEnum())) {
74 // add a default constructor for this class
75 MethodDescriptor md = new MethodDescriptor(new Modifiers(Modifiers.PUBLIC),
76 cd.getSymbol(), false);
77 BlockNode bn=new BlockNode();
78 state.addTreeCode(md,bn);
85 public void semanticCheck() {
86 SymbolTable classtable=state.getClassSymbolTable();
87 toanalyze.addAll(classtable.getValueSet());
88 toanalyze.addAll(state.getTaskSymbolTable().getValueSet());
91 while(!toanalyze.isEmpty()) {
92 Object obj=toanalyze.iterator().next();
93 if (obj instanceof TaskDescriptor) {
94 toanalyze.remove(obj);
95 TaskDescriptor td=(TaskDescriptor)obj;
99 System.out.println( "Error in "+td );
103 ClassDescriptor cd=(ClassDescriptor)obj;
104 toanalyze.remove(cd);
105 //need to initialize typeutil object here...only place we can
106 //get class descriptors without first calling getclass
107 getClass(cd.getSymbol());
108 for(Iterator method_it=cd.getMethods(); method_it.hasNext();) {
109 MethodDescriptor md=(MethodDescriptor)method_it.next();
111 checkMethodBody(cd,md);
113 System.out.println( "Error in "+md );
121 public void checkTypeDescriptor(TypeDescriptor td) {
122 if (td.isPrimitive())
124 else if (td.isClass()) {
125 String name=td.toString();
126 int index = name.lastIndexOf('.');
128 name = name.substring(index+1);
130 ClassDescriptor field_cd=getClass(name);
132 throw new Error("Undefined class "+name);
133 td.setClassDescriptor(field_cd);
135 } else if (td.isTag())
141 public void checkField(ClassDescriptor cd, FieldDescriptor fd) {
142 checkTypeDescriptor(fd.getType());
145 public void checkConstraintCheck(TaskDescriptor td, SymbolTable nametable, Vector ccs) {
147 return; /* No constraint checks to check */
148 for(int i=0; i<ccs.size(); i++) {
149 ConstraintCheck cc=(ConstraintCheck) ccs.get(i);
151 for(int j=0; j<cc.numArgs(); j++) {
152 ExpressionNode en=cc.getArg(j);
153 checkExpressionNode(td,nametable,en,null);
158 public void checkFlagEffects(TaskDescriptor td, Vector vfe, SymbolTable nametable) {
160 return; /* No flag effects to check */
161 for(int i=0; i<vfe.size(); i++) {
162 FlagEffects fe=(FlagEffects) vfe.get(i);
163 String varname=fe.getName();
164 //Make sure the variable is declared as a parameter to the task
165 VarDescriptor vd=(VarDescriptor)td.getParameterTable().get(varname);
167 throw new Error("Parameter "+varname+" in Flag Effects not declared in "+td);
170 //Make sure it correspods to a class
171 TypeDescriptor type_d=vd.getType();
172 if (!type_d.isClass())
173 throw new Error("Cannot have non-object argument for flag_effect");
175 ClassDescriptor cd=type_d.getClassDesc();
176 for(int j=0; j<fe.numEffects(); j++) {
177 FlagEffect flag=fe.getEffect(j);
178 String name=flag.getName();
179 FlagDescriptor flag_d=(FlagDescriptor)cd.getFlagTable().get(name);
180 //Make sure the flag is declared
182 throw new Error("Flag descriptor "+name+" undefined in class: "+cd.getSymbol());
183 if (flag_d.getExternal())
184 throw new Error("Attempting to modify external flag: "+name);
185 flag.setFlag(flag_d);
187 for(int j=0; j<fe.numTagEffects(); j++) {
188 TagEffect tag=fe.getTagEffect(j);
189 String name=tag.getName();
191 Descriptor d=(Descriptor)nametable.get(name);
193 throw new Error("Tag descriptor "+name+" undeclared");
194 else if (!(d instanceof TagVarDescriptor))
195 throw new Error(name+" is not a tag descriptor");
196 tag.setTag((TagVarDescriptor)d);
201 public void checkTask(TaskDescriptor td) {
202 for(int i=0; i<td.numParameters(); i++) {
203 /* Check that parameter is well typed */
204 TypeDescriptor param_type=td.getParamType(i);
205 checkTypeDescriptor(param_type);
207 /* Check the parameter's flag expression is well formed */
208 FlagExpressionNode fen=td.getFlag(td.getParameter(i));
209 if (!param_type.isClass())
210 throw new Error("Cannot have non-object argument to a task");
211 ClassDescriptor cd=param_type.getClassDesc();
213 checkFlagExpressionNode(cd, fen);
216 checkFlagEffects(td, td.getFlagEffects(),td.getParameterTable());
217 /* Check that the task code is valid */
218 BlockNode bn=state.getMethodBody(td);
219 checkBlockNode(td, td.getParameterTable(),bn);
222 public void checkFlagExpressionNode(ClassDescriptor cd, FlagExpressionNode fen) {
224 case Kind.FlagOpNode:
226 FlagOpNode fon=(FlagOpNode)fen;
227 checkFlagExpressionNode(cd, fon.getLeft());
228 if (fon.getRight()!=null)
229 checkFlagExpressionNode(cd, fon.getRight());
235 FlagNode fn=(FlagNode)fen;
236 String name=fn.getFlagName();
237 FlagDescriptor fd=(FlagDescriptor)cd.getFlagTable().get(name);
239 throw new Error("Undeclared flag: "+name);
245 throw new Error("Unrecognized FlagExpressionNode");
249 public void checkMethod(ClassDescriptor cd, MethodDescriptor md) {
251 // TODO add version for normal Java later
252 /* Check for abstract methods */
253 if(md.isAbstract()) {
254 if(!cd.isAbstract() && !cd.isInterface()) {
255 throw new Error("Error! The non-abstract Class " + cd.getSymbol() + " contains an abstract method " + md.getSymbol());
259 /* Check return type */
260 if (!md.isConstructor() && !md.isStaticBlock())
261 if (!md.getReturnType().isVoid())
262 checkTypeDescriptor(md.getReturnType());
264 for(int i=0; i<md.numParameters(); i++) {
265 TypeDescriptor param_type=md.getParamType(i);
266 checkTypeDescriptor(param_type);
268 /* Link the naming environments */
269 if (!md.isStatic()) /* Fields aren't accessible directly in a static method, so don't link in this table */
270 md.getParameterTable().setParent(cd.getFieldTable());
272 if (!md.isStatic()) {
273 VarDescriptor thisvd=new VarDescriptor(new TypeDescriptor(cd),"this");
278 public void checkMethodBody(ClassDescriptor cd, MethodDescriptor md) {
279 ClassDescriptor superdesc=cd.getSuperDesc();
280 if (superdesc!=null) {
281 Set possiblematches=superdesc.getMethodTable().getSet(md.getSymbol());
282 for(Iterator methodit=possiblematches.iterator(); methodit.hasNext();) {
283 MethodDescriptor matchmd=(MethodDescriptor)methodit.next();
284 if (md.matches(matchmd)) {
285 if (matchmd.getModifiers().isFinal()) {
286 throw new Error("Try to override final method in method:"+md+" declared in "+cd);
291 BlockNode bn=state.getMethodBody(md);
292 checkBlockNode(md, md.getParameterTable(),bn);
295 public void checkBlockNode(Descriptor md, SymbolTable nametable, BlockNode bn) {
296 /* Link in the naming environment */
297 bn.getVarTable().setParent(nametable);
298 for(int i=0; i<bn.size(); i++) {
299 BlockStatementNode bsn=bn.get(i);
300 checkBlockStatementNode(md, bn.getVarTable(),bsn);
304 public void checkBlockStatementNode(Descriptor md, SymbolTable nametable, BlockStatementNode bsn) {
306 case Kind.BlockExpressionNode:
307 checkBlockExpressionNode(md, nametable,(BlockExpressionNode)bsn);
310 case Kind.DeclarationNode:
311 checkDeclarationNode(md, nametable, (DeclarationNode)bsn);
314 case Kind.TagDeclarationNode:
315 checkTagDeclarationNode(md, nametable, (TagDeclarationNode)bsn);
318 case Kind.IfStatementNode:
319 checkIfStatementNode(md, nametable, (IfStatementNode)bsn);
322 case Kind.SwitchStatementNode:
323 checkSwitchStatementNode(md, nametable, (SwitchStatementNode)bsn);
327 checkLoopNode(md, nametable, (LoopNode)bsn);
330 case Kind.ReturnNode:
331 checkReturnNode(md, nametable, (ReturnNode)bsn);
334 case Kind.TaskExitNode:
335 checkTaskExitNode(md, nametable, (TaskExitNode)bsn);
338 case Kind.SubBlockNode:
339 checkSubBlockNode(md, nametable, (SubBlockNode)bsn);
342 case Kind.AtomicNode:
343 checkAtomicNode(md, nametable, (AtomicNode)bsn);
346 case Kind.SynchronizedNode:
347 checkSynchronizedNode(md, nametable, (SynchronizedNode)bsn);
350 case Kind.ContinueBreakNode:
351 checkContinueBreakNode(md, nametable, (ContinueBreakNode) bsn);
355 case Kind.GenReachNode:
356 // do nothing, no semantic check for SESEs
363 void checkBlockExpressionNode(Descriptor md, SymbolTable nametable, BlockExpressionNode ben) {
364 checkExpressionNode(md, nametable, ben.getExpression(), null);
367 void checkDeclarationNode(Descriptor md, SymbolTable nametable, DeclarationNode dn) {
368 VarDescriptor vd=dn.getVarDescriptor();
369 checkTypeDescriptor(vd.getType());
370 Descriptor d=nametable.get(vd.getSymbol());
372 (d instanceof FieldDescriptor)) {
375 throw new Error(vd.getSymbol()+" in "+md+" defined a second time");
376 if (dn.getExpression()!=null)
377 checkExpressionNode(md, nametable, dn.getExpression(), vd.getType());
380 void checkTagDeclarationNode(Descriptor md, SymbolTable nametable, TagDeclarationNode dn) {
381 TagVarDescriptor vd=dn.getTagVarDescriptor();
382 Descriptor d=nametable.get(vd.getSymbol());
384 (d instanceof FieldDescriptor)) {
387 throw new Error(vd.getSymbol()+" defined a second time");
390 void checkSubBlockNode(Descriptor md, SymbolTable nametable, SubBlockNode sbn) {
391 checkBlockNode(md, nametable, sbn.getBlockNode());
394 void checkAtomicNode(Descriptor md, SymbolTable nametable, AtomicNode sbn) {
395 checkBlockNode(md, nametable, sbn.getBlockNode());
398 void checkSynchronizedNode(Descriptor md, SymbolTable nametable, SynchronizedNode sbn) {
399 checkBlockNode(md, nametable, sbn.getBlockNode());
400 //todo this could be Object
401 checkExpressionNode(md, nametable, sbn.getExpr(), null);
404 void checkContinueBreakNode(Descriptor md, SymbolTable nametable, ContinueBreakNode cbn) {
405 if (loopstack.empty())
406 throw new Error("continue/break outside of loop");
407 Object o = loopstack.peek();
408 if(o instanceof LoopNode) {
409 LoopNode ln=(LoopNode)o;
414 void checkReturnNode(Descriptor d, SymbolTable nametable, ReturnNode rn) {
415 if (d instanceof TaskDescriptor)
416 throw new Error("Illegal return appears in Task: "+d.getSymbol());
417 MethodDescriptor md=(MethodDescriptor)d;
418 if (rn.getReturnExpression()!=null)
419 if (md.getReturnType()==null)
420 throw new Error("Constructor can't return something.");
421 else if (md.getReturnType().isVoid())
422 throw new Error(md+" is void");
424 checkExpressionNode(md, nametable, rn.getReturnExpression(), md.getReturnType());
426 if (md.getReturnType()!=null&&!md.getReturnType().isVoid())
427 throw new Error("Need to return something for "+md);
430 void checkTaskExitNode(Descriptor md, SymbolTable nametable, TaskExitNode ten) {
431 if (md instanceof MethodDescriptor)
432 throw new Error("Illegal taskexit appears in Method: "+md.getSymbol());
433 checkFlagEffects((TaskDescriptor)md, ten.getFlagEffects(),nametable);
434 checkConstraintCheck((TaskDescriptor) md, nametable, ten.getChecks());
437 void checkIfStatementNode(Descriptor md, SymbolTable nametable, IfStatementNode isn) {
438 checkExpressionNode(md, nametable, isn.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
439 checkBlockNode(md, nametable, isn.getTrueBlock());
440 if (isn.getFalseBlock()!=null)
441 checkBlockNode(md, nametable, isn.getFalseBlock());
444 void checkSwitchStatementNode(Descriptor md, SymbolTable nametable, SwitchStatementNode ssn) {
445 checkExpressionNode(md, nametable, ssn.getCondition(), new TypeDescriptor(TypeDescriptor.INT));
447 BlockNode sbn = ssn.getSwitchBody();
448 boolean hasdefault = false;
449 for(int i = 0; i < sbn.size(); i++) {
450 boolean containdefault = checkSwitchBlockNode(md, nametable, (SwitchBlockNode)sbn.get(i));
451 if(hasdefault && containdefault) {
452 throw new Error("Error: duplicate default branch in switch-case statement in Method: " + md.getSymbol());
454 hasdefault = containdefault;
458 boolean checkSwitchBlockNode(Descriptor md, SymbolTable nametable, SwitchBlockNode sbn) {
459 Vector<SwitchLabelNode> slnv = sbn.getSwitchConditions();
461 for(int i = 0; i < slnv.size(); i++) {
462 if(slnv.elementAt(i).isdefault) {
465 checkConstantExpressionNode(md, nametable, slnv.elementAt(i).getCondition(), new TypeDescriptor(TypeDescriptor.INT));
469 throw new Error("Error: duplicate default branch in switch-case statement in Method: " + md.getSymbol());
472 checkBlockNode(md, nametable, sbn.getSwitchBlockStatement());
474 return (defaultb > 0);
478 void checkConstantExpressionNode(Descriptor md, SymbolTable nametable, ExpressionNode en, TypeDescriptor td) {
480 case Kind.FieldAccessNode:
481 checkFieldAccessNode(md,nametable,(FieldAccessNode)en,td);
484 case Kind.LiteralNode:
485 checkLiteralNode(md,nametable,(LiteralNode)en,td);
489 checkNameNode(md,nametable,(NameNode)en,td);
493 checkOpNode(md, nametable, (OpNode)en, td);
499 void checkExpressionNode(Descriptor md, SymbolTable nametable, ExpressionNode en, TypeDescriptor td) {
501 case Kind.AssignmentNode:
502 checkAssignmentNode(md,nametable,(AssignmentNode)en,td);
506 checkCastNode(md,nametable,(CastNode)en,td);
509 case Kind.CreateObjectNode:
510 checkCreateObjectNode(md,nametable,(CreateObjectNode)en,td);
513 case Kind.FieldAccessNode:
514 checkFieldAccessNode(md,nametable,(FieldAccessNode)en,td);
517 case Kind.ArrayAccessNode:
518 checkArrayAccessNode(md,nametable,(ArrayAccessNode)en,td);
521 case Kind.LiteralNode:
522 checkLiteralNode(md,nametable,(LiteralNode)en,td);
525 case Kind.MethodInvokeNode:
526 checkMethodInvokeNode(md,nametable,(MethodInvokeNode)en,td);
530 checkNameNode(md,nametable,(NameNode)en,td);
534 checkOpNode(md,nametable,(OpNode)en,td);
537 case Kind.OffsetNode:
538 checkOffsetNode(md, nametable, (OffsetNode)en, td);
541 case Kind.TertiaryNode:
542 checkTertiaryNode(md, nametable, (TertiaryNode)en, td);
545 case Kind.InstanceOfNode:
546 checkInstanceOfNode(md, nametable, (InstanceOfNode) en, td);
549 case Kind.ArrayInitializerNode:
550 checkArrayInitializerNode(md, nametable, (ArrayInitializerNode) en, td);
553 case Kind.ClassTypeNode:
554 checkClassTypeNode(md, nametable, (ClassTypeNode) en, td);
560 void checkClassTypeNode(Descriptor md, SymbolTable nametable, ClassTypeNode tn, TypeDescriptor td) {
561 checkTypeDescriptor(tn.getType());
564 void checkCastNode(Descriptor md, SymbolTable nametable, CastNode cn, TypeDescriptor td) {
565 /* Get type descriptor */
566 if (cn.getType()==null) {
567 NameDescriptor typenamed=cn.getTypeName().getName();
568 String typename=typenamed.toString();
569 TypeDescriptor ntd=new TypeDescriptor(getClass(typename));
573 /* Check the type descriptor */
574 TypeDescriptor cast_type=cn.getType();
575 checkTypeDescriptor(cast_type);
579 if (!typeutil.isSuperorType(td,cast_type))
580 throw new Error("Cast node returns "+cast_type+", but need "+td);
583 ExpressionNode en=cn.getExpression();
584 checkExpressionNode(md, nametable, en, null);
585 TypeDescriptor etd=en.getType();
586 if (typeutil.isSuperorType(cast_type,etd)) /* Cast trivially succeeds */
589 if (typeutil.isSuperorType(etd,cast_type)) /* Cast may succeed */
591 if (typeutil.isCastable(etd, cast_type))
594 /* Different branches */
595 /* TODO: change if add interfaces */
596 throw new Error("Cast will always fail\n"+cn.printNode(0));
599 void checkFieldAccessNode(Descriptor md, SymbolTable nametable, FieldAccessNode fan, TypeDescriptor td) {
600 ExpressionNode left=fan.getExpression();
601 checkExpressionNode(md,nametable,left,null);
602 TypeDescriptor ltd=left.getType();
603 String fieldname=fan.getFieldName();
605 FieldDescriptor fd=null;
606 if (ltd.isArray()&&fieldname.equals("length"))
607 fd=FieldDescriptor.arrayLength;
609 fd=(FieldDescriptor) ltd.getClassDesc().getFieldTable().get(fieldname);
611 // TODO add version for normal Java later
612 if(ltd.isClassNameRef()) {
613 // the field access is using a class name directly
614 if(ltd.getClassDesc().isEnum()) {
615 int value = ltd.getClassDesc().getEnumConstant(fieldname);
617 // check if this field is an enum constant
618 throw new Error(fieldname + " is not an enum constant in "+fan.printNode(0)+" in "+md);
620 fd = new FieldDescriptor(new Modifiers(Modifiers.PUBLIC|Modifiers.FINAL), new TypeDescriptor(TypeDescriptor.INT), fieldname, null, false);
622 fd.setEnumValue(value);
623 } else if(fd.isStatic()) {
624 // check if this field is a static field
625 if(fd.getExpressionNode() != null) {
626 checkExpressionNode(md,nametable,fd.getExpressionNode(),null);
629 throw new Error("Dereference of the non-static field "+ fieldname + " in "+fan.printNode(0)+" in "+md);
634 throw new Error("Unknown field "+fieldname + " in "+fan.printNode(0)+" in "+md);
636 if (fd.getType().iswrapper()) {
637 FieldAccessNode fan2=new FieldAccessNode(left, fieldname);
640 fan.fieldname="value";
642 ExpressionNode leftwr=fan.getExpression();
643 TypeDescriptor ltdwr=leftwr.getType();
644 String fieldnamewr=fan.getFieldName();
645 FieldDescriptor fdwr=(FieldDescriptor) ltdwr.getClassDesc().getFieldTable().get(fieldnamewr);
648 throw new Error("Unknown field "+fieldnamewr + " in "+fan.printNode(0)+" in "+md);
653 if (!typeutil.isSuperorType(td,fan.getType()))
654 throw new Error("Field node returns "+fan.getType()+", but need "+td);
658 void checkArrayAccessNode(Descriptor md, SymbolTable nametable, ArrayAccessNode aan, TypeDescriptor td) {
659 ExpressionNode left=aan.getExpression();
660 checkExpressionNode(md,nametable,left,null);
662 checkExpressionNode(md,nametable,aan.getIndex(),new TypeDescriptor(TypeDescriptor.INT));
663 TypeDescriptor ltd=left.getType();
664 if (ltd.dereference().iswrapper()) {
665 aan.wrappertype=((FieldDescriptor)ltd.dereference().getClassDesc().getFieldTable().get("value")).getType();
669 if (!typeutil.isSuperorType(td,aan.getType()))
670 throw new Error("Field node returns "+aan.getType()+", but need "+td);
673 void checkLiteralNode(Descriptor md, SymbolTable nametable, LiteralNode ln, TypeDescriptor td) {
674 /* Resolve the type */
675 Object o=ln.getValue();
676 if (ln.getTypeString().equals("null")) {
677 ln.setType(new TypeDescriptor(TypeDescriptor.NULL));
678 } else if (o instanceof Integer) {
679 ln.setType(new TypeDescriptor(TypeDescriptor.INT));
680 } else if (o instanceof Long) {
681 ln.setType(new TypeDescriptor(TypeDescriptor.LONG));
682 } else if (o instanceof Float) {
683 ln.setType(new TypeDescriptor(TypeDescriptor.FLOAT));
684 } else if (o instanceof Boolean) {
685 ln.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
686 } else if (o instanceof Double) {
687 ln.setType(new TypeDescriptor(TypeDescriptor.DOUBLE));
688 } else if (o instanceof Character) {
689 ln.setType(new TypeDescriptor(TypeDescriptor.CHAR));
690 } else if (o instanceof String) {
691 ln.setType(new TypeDescriptor(getClass(TypeUtil.StringClass)));
695 if (!typeutil.isSuperorType(td,ln.getType())) {
696 Long l = ln.evaluate();
697 if((ln.getType().isByte() || ln.getType().isShort()
698 || ln.getType().isChar() || ln.getType().isInt())
700 && (td.isByte() || td.isShort() || td.isChar()
701 || td.isInt() || td.isLong())) {
702 long lnvalue = l.longValue();
703 if((td.isByte() && ((lnvalue > 127) || (lnvalue < -128)))
704 || (td.isShort() && ((lnvalue > 32767) || (lnvalue < -32768)))
705 || (td.isChar() && ((lnvalue > 65535) || (lnvalue < 0)))
706 || (td.isInt() && ((lnvalue > 2147483647) || (lnvalue < -2147483648)))
707 || (td.isLong() && ((lnvalue > 9223372036854775807L) || (lnvalue < -9223372036854775808L)))) {
708 throw new Error("Field node returns "+ln.getType()+", but need "+td+" in "+md);
711 throw new Error("Field node returns "+ln.getType()+", but need "+td+" in "+md);
716 void checkNameNode(Descriptor md, SymbolTable nametable, NameNode nn, TypeDescriptor td) {
717 NameDescriptor nd=nn.getName();
718 if (nd.getBase()!=null) {
720 /* Rewrite NameNode */
721 ExpressionNode en=translateNameDescriptorintoExpression(nd);
722 nn.setExpression(en);
723 checkExpressionNode(md,nametable,en,td);
725 String varname=nd.toString();
726 if(varname.equals("this")) {
728 nn.setVar((VarDescriptor)nametable.get("this"));
731 Descriptor d=(Descriptor)nametable.get(varname);
734 // TODO add version for normal Java later
735 ClassDescriptor cd = null;
736 if(((MethodDescriptor)md).isStaticBlock()) {
737 // this is a static block, all the accessed fields should be static field
738 cd = ((MethodDescriptor)md).getClassDesc();
739 SymbolTable fieldtbl = cd.getFieldTable();
740 FieldDescriptor fd=(FieldDescriptor)fieldtbl.get(varname);
741 if((fd == null) || (!fd.isStatic())){
742 // no such field in the class, check if this is a class
743 if(varname.equals("this")) {
744 throw new Error("Error: access this obj in a static block");
746 cd=getClass(varname);
748 // this is a class name
752 throw new Error("Name "+varname+" should not be used in static block: "+md);
755 // this is a static field
761 // check if the var is a static field of the class
762 if(md instanceof MethodDescriptor) {
763 cd = ((MethodDescriptor)md).getClassDesc();
764 FieldDescriptor fd = (FieldDescriptor)cd.getFieldTable().get(varname);
765 if((fd != null) && (fd.isStatic())) {
769 if (!typeutil.isSuperorType(td,nn.getType()))
770 throw new Error("Field node returns "+nn.getType()+", but need "+td);
772 } else if(fd != null) {
773 throw new Error("Name "+varname+" should not be used in " + md);
776 cd=getClass(varname);
778 // this is a class name
782 throw new Error("Name "+varname+" undefined in: "+md);
786 throw new Error("Name "+varname+" undefined in: "+md);
789 if (d instanceof VarDescriptor) {
791 } else if (d instanceof FieldDescriptor) {
792 FieldDescriptor fd=(FieldDescriptor)d;
793 if (fd.getType().iswrapper()) {
794 String id=nd.getIdentifier();
795 NameDescriptor base=nd.getBase();
796 NameNode n=new NameNode(nn.getName());
798 n.setVar((VarDescriptor)nametable.get("this")); /* Need a pointer to this */
799 FieldAccessNode fan=new FieldAccessNode(n,"value");
800 FieldDescriptor fdval=(FieldDescriptor) fd.getType().getClassDesc().getFieldTable().get("value");
802 nn.setExpression(fan);
805 nn.setVar((VarDescriptor)nametable.get("this")); /* Need a pointer to this */
807 } else if (d instanceof TagVarDescriptor) {
809 } else throw new Error("Wrong type of descriptor");
811 if (!typeutil.isSuperorType(td,nn.getType()))
812 throw new Error("Field node returns "+nn.getType()+", but need "+td);
816 void checkOffsetNode(Descriptor md, SymbolTable nameTable, OffsetNode ofn, TypeDescriptor td) {
817 TypeDescriptor ltd=ofn.td;
818 checkTypeDescriptor(ltd);
820 String fieldname = ofn.fieldname;
821 FieldDescriptor fd=null;
822 if (ltd.isArray()&&fieldname.equals("length")) {
823 fd=FieldDescriptor.arrayLength;
825 fd=(FieldDescriptor) ltd.getClassDesc().getFieldTable().get(fieldname);
829 checkField(ltd.getClassDesc(), fd);
832 throw new Error("Unknown field "+fieldname + " in "+ofn.printNode(1)+" in "+md);
835 if (!typeutil.isSuperorType(td, ofn.getType())) {
836 System.out.println(td);
837 System.out.println(ofn.getType());
838 throw new Error("Type of rside not compatible with type of lside"+ofn.printNode(0));
844 void checkTertiaryNode(Descriptor md, SymbolTable nametable, TertiaryNode tn, TypeDescriptor td) {
845 checkExpressionNode(md, nametable, tn.getCond(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
846 checkExpressionNode(md, nametable, tn.getTrueExpr(), td );
847 checkExpressionNode(md, nametable, tn.getFalseExpr(), td );
850 void checkInstanceOfNode(Descriptor md, SymbolTable nametable, InstanceOfNode tn, TypeDescriptor td) {
851 if (td!=null&&!td.isBoolean())
852 throw new Error("Expecting type "+td+"for instanceof expression");
854 checkTypeDescriptor(tn.getExprType());
855 checkExpressionNode(md, nametable, tn.getExpr(), null);
858 void checkArrayInitializerNode(Descriptor md, SymbolTable nametable, ArrayInitializerNode ain, TypeDescriptor td) {
859 Vector<TypeDescriptor> vec_type = new Vector<TypeDescriptor>();
860 for( int i = 0; i < ain.numVarInitializers(); ++i ) {
861 checkExpressionNode(md, nametable, ain.getVarInitializer(i), td==null?td:td.dereference());
862 vec_type.add(ain.getVarInitializer(i).getType());
864 // descide the type of this variableInitializerNode
865 TypeDescriptor out_type = vec_type.elementAt(0);
866 for(int i = 1; i < vec_type.size(); i++) {
867 TypeDescriptor tmp_type = vec_type.elementAt(i);
868 if(out_type == null) {
869 if(tmp_type != null) {
872 } else if(out_type.isNull()) {
873 if(!tmp_type.isNull() ) {
874 if(!tmp_type.isArray()) {
875 throw new Error("Error: mixed type in var initializer list");
880 } else if(out_type.isArray()) {
881 if(tmp_type.isArray()) {
882 if(tmp_type.getArrayCount() > out_type.getArrayCount()) {
885 } else if((tmp_type != null) && (!tmp_type.isNull())) {
886 throw new Error("Error: mixed type in var initializer list");
888 } else if(out_type.isInt()) {
889 if(!tmp_type.isInt()) {
890 throw new Error("Error: mixed type in var initializer list");
892 } else if(out_type.isString()) {
893 if(!tmp_type.isString()) {
894 throw new Error("Error: mixed type in var initializer list");
898 if(out_type != null) {
899 out_type = out_type.makeArray(state, false);
900 //out_type.setStatic();
902 ain.setType(out_type);
905 void checkAssignmentNode(Descriptor md, SymbolTable nametable, AssignmentNode an, TypeDescriptor td) {
906 boolean postinc=true;
907 if (an.getOperation().getBaseOp()==null||
908 (an.getOperation().getBaseOp().getOp()!=Operation.POSTINC&&
909 an.getOperation().getBaseOp().getOp()!=Operation.POSTDEC))
912 checkExpressionNode(md, nametable, an.getSrc(),td);
913 //TODO: Need check on validity of operation here
914 if (!((an.getDest() instanceof FieldAccessNode)||
915 (an.getDest() instanceof ArrayAccessNode)||
916 (an.getDest() instanceof NameNode)))
917 throw new Error("Bad lside in "+an.printNode(0));
918 checkExpressionNode(md, nametable, an.getDest(), null);
920 /* We want parameter variables to tasks to be immutable */
921 if (md instanceof TaskDescriptor) {
922 if (an.getDest() instanceof NameNode) {
923 NameNode nn=(NameNode)an.getDest();
924 if (nn.getVar()!=null) {
925 if (((TaskDescriptor)md).getParameterTable().contains(nn.getVar().getSymbol()))
926 throw new Error("Can't modify parameter "+nn.getVar()+ " to task "+td.getSymbol());
931 if (an.getDest().getType().isString()&&an.getOperation().getOp()==AssignOperation.PLUSEQ) {
933 ClassDescriptor stringcl=getClass(TypeUtil.StringClass);
934 TypeDescriptor stringtd=new TypeDescriptor(stringcl);
935 NameDescriptor nd=new NameDescriptor("String");
936 NameDescriptor valuend=new NameDescriptor(nd, "valueOf");
938 if (!(an.getSrc().getType().isString()&&(an.getSrc() instanceof OpNode))) {
939 MethodInvokeNode rightmin=new MethodInvokeNode(valuend);
940 rightmin.addArgument(an.getSrc());
942 checkExpressionNode(md, nametable, an.getSrc(), null);
946 if (!postinc&&!typeutil.isSuperorType(an.getDest().getType(),an.getSrc().getType())) {
947 TypeDescriptor dt = an.getDest().getType();
948 TypeDescriptor st = an.getSrc().getType();
949 if(an.getSrc().kind() == Kind.ArrayInitializerNode) {
950 if(dt.getArrayCount() != st.getArrayCount()) {
951 throw new Error("Type of rside ("+an.getSrc().getType().toPrettyString()+") not compatible with type of lside ("+an.getDest().getType().toPrettyString()+")"+an.printNode(0));
954 dt = dt.dereference();
955 st = st.dereference();
956 } while(dt.isArray());
957 if((st.isByte() || st.isShort() || st.isChar() || st.isInt())
958 && (dt.isByte() || dt.isShort() || dt.isChar() || dt.isInt() || dt.isLong())) {
961 throw new Error("Type of rside ("+an.getSrc().getType().toPrettyString()+") not compatible with type of lside ("+an.getDest().getType().toPrettyString()+")"+an.printNode(0));
965 Long l = an.getSrc().evaluate();
966 if((st.isByte() || st.isShort() || st.isChar() || st.isInt())
968 && (dt.isByte() || dt.isShort() || dt.isChar() || dt.isInt() || dt.isLong())) {
969 long lnvalue = l.longValue();
970 if((dt.isByte() && ((lnvalue > 127) || (lnvalue < -128)))
971 || (dt.isShort() && ((lnvalue > 32767) || (lnvalue < -32768)))
972 || (dt.isChar() && ((lnvalue > 65535) || (lnvalue < 0)))
973 || (dt.isInt() && ((lnvalue > 2147483647) || (lnvalue < -2147483648)))
974 || (dt.isLong() && ((lnvalue > 9223372036854775807L) || (lnvalue < -9223372036854775808L)))) {
975 throw new Error("Type of rside ("+an.getSrc().getType().toPrettyString()+") not compatible with type of lside ("+an.getDest().getType().toPrettyString()+")"+an.printNode(0));
978 throw new Error("Type of rside ("+an.getSrc().getType().toPrettyString()+") not compatible with type of lside ("+an.getDest().getType().toPrettyString()+")"+an.printNode(0));
984 void checkLoopNode(Descriptor md, SymbolTable nametable, LoopNode ln) {
986 if (ln.getType()==LoopNode.WHILELOOP||ln.getType()==LoopNode.DOWHILELOOP) {
987 checkExpressionNode(md, nametable, ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
988 checkBlockNode(md, nametable, ln.getBody());
991 /* Link in the initializer naming environment */
992 BlockNode bn=ln.getInitializer();
993 bn.getVarTable().setParent(nametable);
994 for(int i=0; i<bn.size(); i++) {
995 BlockStatementNode bsn=bn.get(i);
996 checkBlockStatementNode(md, bn.getVarTable(),bsn);
998 //check the condition
999 checkExpressionNode(md, bn.getVarTable(), ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
1000 checkBlockNode(md, bn.getVarTable(), ln.getBody());
1001 checkBlockNode(md, bn.getVarTable(), ln.getUpdate());
1007 void checkCreateObjectNode(Descriptor md, SymbolTable nametable, CreateObjectNode con, TypeDescriptor td) {
1008 TypeDescriptor[] tdarray=new TypeDescriptor[con.numArgs()];
1009 for(int i=0; i<con.numArgs(); i++) {
1010 ExpressionNode en=con.getArg(i);
1011 checkExpressionNode(md,nametable,en,null);
1012 tdarray[i]=en.getType();
1015 TypeDescriptor typetolookin=con.getType();
1016 checkTypeDescriptor(typetolookin);
1018 if (td!=null&&!typeutil.isSuperorType(td, typetolookin))
1019 throw new Error(typetolookin + " isn't a "+td);
1021 /* Check Array Initializers */
1022 if(state.MGC && (con.getArrayInitializer() != null)) {
1023 checkArrayInitializerNode(md, nametable, con.getArrayInitializer(), td);
1026 /* Check flag effects */
1027 if (con.getFlagEffects()!=null) {
1028 FlagEffects fe=con.getFlagEffects();
1029 ClassDescriptor cd=typetolookin.getClassDesc();
1031 for(int j=0; j<fe.numEffects(); j++) {
1032 FlagEffect flag=fe.getEffect(j);
1033 String name=flag.getName();
1034 FlagDescriptor flag_d=(FlagDescriptor)cd.getFlagTable().get(name);
1035 //Make sure the flag is declared
1037 throw new Error("Flag descriptor "+name+" undefined in class: "+cd.getSymbol());
1038 if (flag_d.getExternal())
1039 throw new Error("Attempting to modify external flag: "+name);
1040 flag.setFlag(flag_d);
1042 for(int j=0; j<fe.numTagEffects(); j++) {
1043 TagEffect tag=fe.getTagEffect(j);
1044 String name=tag.getName();
1046 Descriptor d=(Descriptor)nametable.get(name);
1048 throw new Error("Tag descriptor "+name+" undeclared");
1049 else if (!(d instanceof TagVarDescriptor))
1050 throw new Error(name+" is not a tag descriptor");
1051 tag.setTag((TagVarDescriptor)d);
1055 if ((!typetolookin.isClass())&&(!typetolookin.isArray()))
1056 throw new Error("Can't allocate primitive type:"+con.printNode(0));
1058 if (!typetolookin.isArray()) {
1059 //Array's don't need constructor calls
1060 ClassDescriptor classtolookin=typetolookin.getClassDesc();
1062 Set methoddescriptorset=classtolookin.getMethodTable().getSet(typetolookin.getSymbol());
1063 MethodDescriptor bestmd=null;
1065 for(Iterator methodit=methoddescriptorset.iterator(); methodit.hasNext();) {
1066 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
1067 /* Need correct number of parameters */
1068 if (con.numArgs()!=currmd.numParameters())
1070 for(int i=0; i<con.numArgs(); i++) {
1071 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
1072 continue NextMethod;
1074 /* Local allocations can't call global allocator */
1075 if (!con.isGlobal()&&currmd.isGlobal())
1078 /* Method okay so far */
1082 if (typeutil.isMoreSpecific(currmd,bestmd)) {
1084 } else if (con.isGlobal()&&match(currmd, bestmd)) {
1085 if (currmd.isGlobal()&&!bestmd.isGlobal())
1087 else if (currmd.isGlobal()&&bestmd.isGlobal())
1089 } else if (!typeutil.isMoreSpecific(bestmd, currmd)) {
1090 throw new Error("No method is most specific");
1093 /* Is this more specific than bestmd */
1097 throw new Error("No method found for "+con.printNode(0)+" in "+md);
1098 con.setConstructor(bestmd);
1103 /** Check to see if md1 is the same specificity as md2.*/
1105 boolean match(MethodDescriptor md1, MethodDescriptor md2) {
1106 /* Checks if md1 is more specific than md2 */
1107 if (md1.numParameters()!=md2.numParameters())
1109 for(int i=0; i<md1.numParameters(); i++) {
1110 if (!md2.getParamType(i).equals(md1.getParamType(i)))
1113 if (!md2.getReturnType().equals(md1.getReturnType()))
1116 if (!md2.getClassDesc().equals(md1.getClassDesc()))
1124 ExpressionNode translateNameDescriptorintoExpression(NameDescriptor nd) {
1125 String id=nd.getIdentifier();
1126 NameDescriptor base=nd.getBase();
1128 return new NameNode(nd);
1130 return new FieldAccessNode(translateNameDescriptorintoExpression(base),id);
1134 void checkMethodInvokeNode(Descriptor md, SymbolTable nametable, MethodInvokeNode min, TypeDescriptor td) {
1135 /*Typecheck subexpressions
1136 and get types for expressions*/
1138 boolean isstatic = false;
1140 if((md instanceof MethodDescriptor) && ((MethodDescriptor)md).isStatic()) {
1144 TypeDescriptor[] tdarray=new TypeDescriptor[min.numArgs()];
1145 for(int i=0; i<min.numArgs(); i++) {
1146 ExpressionNode en=min.getArg(i);
1147 checkExpressionNode(md,nametable,en,null);
1148 tdarray[i]=en.getType();
1149 if(state.MGC && en.getType().isClass() && en.getType().getClassDesc().isEnum()) {
1150 tdarray[i] = new TypeDescriptor(TypeDescriptor.INT);
1153 TypeDescriptor typetolookin=null;
1154 if (min.getExpression()!=null) {
1155 checkExpressionNode(md,nametable,min.getExpression(),null);
1156 typetolookin=min.getExpression().getType();
1157 //if (typetolookin==null)
1158 //throw new Error(md+" has null return type");
1160 } else if (min.getBaseName()!=null) {
1161 String rootname=min.getBaseName().getRoot();
1162 if (rootname.equals("super")) {
1163 ClassDescriptor supercd=((MethodDescriptor)md).getClassDesc().getSuperDesc();
1164 typetolookin=new TypeDescriptor(supercd);
1165 } else if (rootname.equals("this")) {
1167 throw new Error("use this object in static method md = "+ md.toString());
1169 ClassDescriptor cd=((MethodDescriptor)md).getClassDesc();
1170 typetolookin=new TypeDescriptor(cd);
1171 } else if (nametable.get(rootname)!=null) {
1172 //we have an expression
1173 min.setExpression(translateNameDescriptorintoExpression(min.getBaseName()));
1174 checkExpressionNode(md, nametable, min.getExpression(), null);
1175 typetolookin=min.getExpression().getType();
1178 if(!min.getBaseName().getSymbol().equals("System.out")) {
1179 ExpressionNode nn = translateNameDescriptorintoExpression(min.getBaseName());
1180 checkExpressionNode(md, nametable, nn, null);
1181 typetolookin = nn.getType();
1182 if(!((nn.kind()== Kind.NameNode) && (((NameNode)nn).getField() == null)
1183 && (((NameNode)nn).getVar() == null) && (((NameNode)nn).getExpression() == null))) {
1184 // this is not a pure class name, need to add to
1185 min.setExpression(nn);
1189 ClassDescriptor cd = null;
1190 //if (min.getBaseName().getSymbol().equals("System.out"))
1191 cd=getClass("System");
1193 cd=getClass(min.getBaseName().getSymbol());
1196 throw new Error("md = "+ md.toString()+ " "+min.getBaseName()+" undefined");
1197 typetolookin=new TypeDescriptor(cd);
1201 ClassDescriptor cd = null;
1202 if (min.getBaseName().getSymbol().equals("System.out"))
1203 cd=getClass("System");
1205 cd=getClass(min.getBaseName().getSymbol());
1208 throw new Error("md = "+ md.toString()+ " "+min.getBaseName()+" undefined");
1209 typetolookin=new TypeDescriptor(cd);
1212 } else if ((md instanceof MethodDescriptor)&&min.getMethodName().equals("super")) {
1213 ClassDescriptor supercd=((MethodDescriptor)md).getClassDesc().getSuperDesc();
1214 min.methodid=supercd.getSymbol();
1215 typetolookin=new TypeDescriptor(supercd);
1216 } else if (md instanceof MethodDescriptor) {
1217 typetolookin=new TypeDescriptor(((MethodDescriptor)md).getClassDesc());
1219 /* If this a task descriptor we throw an error at this point */
1220 throw new Error("Unknown method call to "+min.getMethodName()+"in task"+md.getSymbol());
1222 if (!typetolookin.isClass())
1223 throw new Error("Error with method call to "+min.getMethodName());
1224 ClassDescriptor classtolookin=typetolookin.getClassDesc();
1225 //System.out.println("Method name="+min.getMethodName());
1227 Set methoddescriptorset=classtolookin.getMethodTable().getSet(min.getMethodName());
1228 MethodDescriptor bestmd=null;
1230 for(Iterator methodit=methoddescriptorset.iterator(); methodit.hasNext();) {
1231 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
1232 /* Need correct number of parameters */
1233 if (min.numArgs()!=currmd.numParameters())
1235 for(int i=0; i<min.numArgs(); i++) {
1236 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
1237 if(state.MGC && ((!tdarray[i].isArray() &&( tdarray[i].isInt() || tdarray[i].isLong()))
1238 && currmd.getParamType(i).isClass() && currmd.getParamType(i).getClassDesc().getSymbol().equals("Object"))) {
1239 // primitive parameters vs object
1241 continue NextMethod;
1244 /* Method okay so far */
1248 if (typeutil.isMoreSpecific(currmd,bestmd)) {
1250 } else if (!typeutil.isMoreSpecific(bestmd, currmd))
1251 throw new Error("No method is most specific");
1253 /* Is this more specific than bestmd */
1257 throw new Error("No method found for :"+min.printNode(0)+" in class: " + classtolookin+" in "+md);
1258 min.setMethod(bestmd);
1260 if ((td!=null)&&(min.getType()!=null)&&!typeutil.isSuperorType(td, min.getType()))
1261 throw new Error(min.getType()+ " is not equal to or a subclass of "+td);
1262 /* Check whether we need to set this parameter to implied this */
1263 if (! isstatic && !bestmd.isStatic()) {
1264 if (min.getExpression()==null) {
1265 ExpressionNode en=new NameNode(new NameDescriptor("this"));
1266 min.setExpression(en);
1267 checkExpressionNode(md, nametable, min.getExpression(), null);
1272 /* Check if we need to wrap primitive paratmeters to objects */
1273 for(int i=0; i<min.numArgs(); i++) {
1274 if(!tdarray[i].isArray() && (tdarray[i].isInt() || tdarray[i].isLong())
1275 && min.getMethod().getParamType(i).isClass() && min.getMethod().getParamType(i).getClassDesc().getSymbol().equals("Object")) {
1276 // Shall wrap this primitive parameter as a object
1277 ExpressionNode exp = min.getArg(i);
1278 TypeDescriptor ptd = null;
1279 NameDescriptor nd=null;
1280 if(exp.getType().isInt()) {
1281 nd = new NameDescriptor("Integer");
1282 ptd = state.getTypeDescriptor(nd);
1283 } else if(exp.getType().isLong()) {
1284 nd = new NameDescriptor("Long");
1285 ptd = state.getTypeDescriptor(nd);
1287 boolean isglobal = false;
1288 String disjointId = null;
1289 CreateObjectNode con=new CreateObjectNode(ptd, isglobal, disjointId);
1290 con.addArgument(exp);
1291 checkExpressionNode(md, nametable, con, null);
1292 min.setArgument(con, i);
1299 void checkOpNode(Descriptor md, SymbolTable nametable, OpNode on, TypeDescriptor td) {
1300 checkExpressionNode(md, nametable, on.getLeft(), null);
1301 if (on.getRight()!=null)
1302 checkExpressionNode(md, nametable, on.getRight(), null);
1303 TypeDescriptor ltd=on.getLeft().getType();
1304 TypeDescriptor rtd=on.getRight()!=null ? on.getRight().getType() : null;
1305 TypeDescriptor lefttype=null;
1306 TypeDescriptor righttype=null;
1307 Operation op=on.getOp();
1309 switch(op.getOp()) {
1310 case Operation.LOGIC_OR:
1311 case Operation.LOGIC_AND:
1312 if (!(rtd.isBoolean()))
1314 on.setRightType(rtd);
1316 case Operation.LOGIC_NOT:
1317 if (!(ltd.isBoolean()))
1320 on.setLeftType(ltd);
1322 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1325 case Operation.COMP:
1326 // 5.6.2 Binary Numeric Promotion
1327 //TODO unboxing of reference objects
1330 else if (ltd.isFloat())
1332 else if (ltd.isLong())
1333 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1335 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1336 on.setLeftType(lefttype);
1337 on.setType(lefttype);
1340 case Operation.BIT_OR:
1341 case Operation.BIT_XOR:
1342 case Operation.BIT_AND:
1343 // 5.6.2 Binary Numeric Promotion
1344 //TODO unboxing of reference objects
1345 if (ltd.isDouble()||rtd.isDouble())
1347 else if (ltd.isFloat()||rtd.isFloat())
1349 else if (ltd.isLong()||rtd.isLong())
1350 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1351 // 090205 hack for boolean
1352 else if (ltd.isBoolean()||rtd.isBoolean())
1353 lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
1355 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1358 on.setLeftType(lefttype);
1359 on.setRightType(righttype);
1360 on.setType(lefttype);
1363 case Operation.ISAVAILABLE:
1364 if (!(ltd.isPtr())) {
1365 throw new Error("Can't use isavailable on non-pointers/non-parameters.");
1368 on.setLeftType(lefttype);
1369 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1372 case Operation.EQUAL:
1373 case Operation.NOTEQUAL:
1374 // 5.6.2 Binary Numeric Promotion
1375 //TODO unboxing of reference objects
1376 if (ltd.isBoolean()||rtd.isBoolean()) {
1377 if (!(ltd.isBoolean()&&rtd.isBoolean()))
1379 righttype=lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
1380 } else if (ltd.isPtr()||rtd.isPtr()) {
1381 if (!(ltd.isPtr()&&rtd.isPtr())) {
1388 } else if (ltd.isDouble()||rtd.isDouble())
1389 righttype=lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
1390 else if (ltd.isFloat()||rtd.isFloat())
1391 righttype=lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
1392 else if (ltd.isLong()||rtd.isLong())
1393 righttype=lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1395 righttype=lefttype=new TypeDescriptor(TypeDescriptor.INT);
1397 on.setLeftType(lefttype);
1398 on.setRightType(righttype);
1399 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1408 // 5.6.2 Binary Numeric Promotion
1409 //TODO unboxing of reference objects
1410 if (!ltd.isNumber()||!rtd.isNumber()) {
1411 if (!ltd.isNumber())
1412 throw new Error("Leftside is not number"+on.printNode(0)+"type="+ltd.toPrettyString());
1413 if (!rtd.isNumber())
1414 throw new Error("Rightside is not number"+on.printNode(0));
1417 if (ltd.isDouble()||rtd.isDouble())
1418 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
1419 else if (ltd.isFloat()||rtd.isFloat())
1420 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
1421 else if (ltd.isLong()||rtd.isLong())
1422 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1424 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1426 on.setLeftType(lefttype);
1427 on.setRightType(righttype);
1428 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1432 if (ltd.isString()||rtd.isString()) {
1433 ClassDescriptor stringcl=getClass(TypeUtil.StringClass);
1434 TypeDescriptor stringtd=new TypeDescriptor(stringcl);
1435 NameDescriptor nd=new NameDescriptor("String");
1436 NameDescriptor valuend=new NameDescriptor(nd, "valueOf");
1437 if (!(ltd.isString()&&(on.getLeft() instanceof OpNode))) {
1438 MethodInvokeNode leftmin=new MethodInvokeNode(valuend);
1439 leftmin.addArgument(on.getLeft());
1441 checkExpressionNode(md, nametable, on.getLeft(), null);
1444 if (!(rtd.isString()&&(on.getRight() instanceof OpNode))) {
1445 MethodInvokeNode rightmin=new MethodInvokeNode(valuend);
1446 rightmin.addArgument(on.getRight());
1448 checkExpressionNode(md, nametable, on.getRight(), null);
1451 on.setLeftType(stringtd);
1452 on.setRightType(stringtd);
1453 on.setType(stringtd);
1458 case Operation.MULT:
1461 // 5.6.2 Binary Numeric Promotion
1462 //TODO unboxing of reference objects
1463 if (ltd.isArray()||rtd.isArray()||!ltd.isNumber()||!rtd.isNumber())
1464 throw new Error("Error in "+on.printNode(0));
1466 if (ltd.isDouble()||rtd.isDouble())
1467 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
1468 else if (ltd.isFloat()||rtd.isFloat())
1469 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
1470 else if (ltd.isLong()||rtd.isLong())
1471 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1473 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1475 on.setLeftType(lefttype);
1476 on.setRightType(righttype);
1477 on.setType(lefttype);
1480 case Operation.LEFTSHIFT:
1481 case Operation.RIGHTSHIFT:
1482 case Operation.URIGHTSHIFT:
1483 if (!rtd.isIntegerType())
1485 //5.6.1 Unary Numeric Promotion
1486 if (rtd.isByte()||rtd.isShort()||rtd.isInt())
1487 righttype=new TypeDescriptor(TypeDescriptor.INT);
1491 on.setRightType(righttype);
1492 if (!ltd.isIntegerType())
1495 case Operation.UNARYPLUS:
1496 case Operation.UNARYMINUS:
1497 /* case Operation.POSTINC:
1498 case Operation.POSTDEC:
1499 case Operation.PREINC:
1500 case Operation.PREDEC:*/
1501 if (!ltd.isNumber())
1503 //5.6.1 Unary Numeric Promotion
1504 if (ltd.isByte()||ltd.isShort()||ltd.isInt())
1505 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1508 on.setLeftType(lefttype);
1509 on.setType(lefttype);
1513 throw new Error(op.toString());
1517 if (!typeutil.isSuperorType(td, on.getType())) {
1518 System.out.println(td);
1519 System.out.println(on.getType());
1520 throw new Error("Type of rside not compatible with type of lside"+on.printNode(0));