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 for(Iterator method_it=cd.getMethods(); method_it.hasNext();) {
68 MethodDescriptor md=(MethodDescriptor)method_it.next();
70 if(md.isDefaultConstructor() && (cd.getSuperDesc() != null)) {
71 // add the construction of it super class, can only be super()
72 NameDescriptor nd=new NameDescriptor("super");
73 MethodInvokeNode min=new MethodInvokeNode(nd);
74 BlockExpressionNode ben=new BlockExpressionNode(min);
75 BlockNode bn = state.getMethodBody(md);
76 bn.addFirstBlockStatement(ben);
82 public void semanticCheck() {
83 SymbolTable classtable=state.getClassSymbolTable();
84 toanalyze.addAll(classtable.getValueSet());
85 toanalyze.addAll(state.getTaskSymbolTable().getValueSet());
88 while(!toanalyze.isEmpty()) {
89 Object obj=toanalyze.iterator().next();
90 if (obj instanceof TaskDescriptor) {
91 toanalyze.remove(obj);
92 TaskDescriptor td=(TaskDescriptor)obj;
96 System.out.println( "Error in "+td );
100 ClassDescriptor cd=(ClassDescriptor)obj;
101 toanalyze.remove(cd);
102 //need to initialize typeutil object here...only place we can
103 //get class descriptors without first calling getclass
104 getClass(cd.getSymbol());
105 for(Iterator method_it=cd.getMethods(); method_it.hasNext();) {
106 MethodDescriptor md=(MethodDescriptor)method_it.next();
108 checkMethodBody(cd,md);
110 System.out.println( "Error in "+md );
118 public void checkTypeDescriptor(TypeDescriptor td) {
119 if (td.isPrimitive())
121 else if (td.isClass()) {
122 String name=td.toString();
123 int index = name.lastIndexOf('.');
125 name = name.substring(index+1);
127 ClassDescriptor field_cd=getClass(name);
129 throw new Error("Undefined class "+name);
130 td.setClassDescriptor(field_cd);
132 } else if (td.isTag())
138 public void checkField(ClassDescriptor cd, FieldDescriptor fd) {
139 checkTypeDescriptor(fd.getType());
142 public void checkConstraintCheck(TaskDescriptor td, SymbolTable nametable, Vector ccs) {
144 return; /* No constraint checks to check */
145 for(int i=0; i<ccs.size(); i++) {
146 ConstraintCheck cc=(ConstraintCheck) ccs.get(i);
148 for(int j=0; j<cc.numArgs(); j++) {
149 ExpressionNode en=cc.getArg(j);
150 checkExpressionNode(td,nametable,en,null);
155 public void checkFlagEffects(TaskDescriptor td, Vector vfe, SymbolTable nametable) {
157 return; /* No flag effects to check */
158 for(int i=0; i<vfe.size(); i++) {
159 FlagEffects fe=(FlagEffects) vfe.get(i);
160 String varname=fe.getName();
161 //Make sure the variable is declared as a parameter to the task
162 VarDescriptor vd=(VarDescriptor)td.getParameterTable().get(varname);
164 throw new Error("Parameter "+varname+" in Flag Effects not declared in "+td);
167 //Make sure it correspods to a class
168 TypeDescriptor type_d=vd.getType();
169 if (!type_d.isClass())
170 throw new Error("Cannot have non-object argument for flag_effect");
172 ClassDescriptor cd=type_d.getClassDesc();
173 for(int j=0; j<fe.numEffects(); j++) {
174 FlagEffect flag=fe.getEffect(j);
175 String name=flag.getName();
176 FlagDescriptor flag_d=(FlagDescriptor)cd.getFlagTable().get(name);
177 //Make sure the flag is declared
179 throw new Error("Flag descriptor "+name+" undefined in class: "+cd.getSymbol());
180 if (flag_d.getExternal())
181 throw new Error("Attempting to modify external flag: "+name);
182 flag.setFlag(flag_d);
184 for(int j=0; j<fe.numTagEffects(); j++) {
185 TagEffect tag=fe.getTagEffect(j);
186 String name=tag.getName();
188 Descriptor d=(Descriptor)nametable.get(name);
190 throw new Error("Tag descriptor "+name+" undeclared");
191 else if (!(d instanceof TagVarDescriptor))
192 throw new Error(name+" is not a tag descriptor");
193 tag.setTag((TagVarDescriptor)d);
198 public void checkTask(TaskDescriptor td) {
199 for(int i=0; i<td.numParameters(); i++) {
200 /* Check that parameter is well typed */
201 TypeDescriptor param_type=td.getParamType(i);
202 checkTypeDescriptor(param_type);
204 /* Check the parameter's flag expression is well formed */
205 FlagExpressionNode fen=td.getFlag(td.getParameter(i));
206 if (!param_type.isClass())
207 throw new Error("Cannot have non-object argument to a task");
208 ClassDescriptor cd=param_type.getClassDesc();
210 checkFlagExpressionNode(cd, fen);
213 checkFlagEffects(td, td.getFlagEffects(),td.getParameterTable());
214 /* Check that the task code is valid */
215 BlockNode bn=state.getMethodBody(td);
216 checkBlockNode(td, td.getParameterTable(),bn);
219 public void checkFlagExpressionNode(ClassDescriptor cd, FlagExpressionNode fen) {
221 case Kind.FlagOpNode:
223 FlagOpNode fon=(FlagOpNode)fen;
224 checkFlagExpressionNode(cd, fon.getLeft());
225 if (fon.getRight()!=null)
226 checkFlagExpressionNode(cd, fon.getRight());
232 FlagNode fn=(FlagNode)fen;
233 String name=fn.getFlagName();
234 FlagDescriptor fd=(FlagDescriptor)cd.getFlagTable().get(name);
236 throw new Error("Undeclared flag: "+name);
242 throw new Error("Unrecognized FlagExpressionNode");
246 public void checkMethod(ClassDescriptor cd, MethodDescriptor md) {
248 // TODO add version for normal Java later
249 /* Check for abstract methods */
250 if(md.isAbstract()) {
251 if(!cd.isAbstract() && !cd.isInterface()) {
252 throw new Error("Error! The non-abstract Class " + cd.getSymbol() + " contains an abstract method " + md.getSymbol());
256 /* Check return type */
257 if (!md.isConstructor() && !md.isStaticBlock())
258 if (!md.getReturnType().isVoid())
259 checkTypeDescriptor(md.getReturnType());
261 for(int i=0; i<md.numParameters(); i++) {
262 TypeDescriptor param_type=md.getParamType(i);
263 checkTypeDescriptor(param_type);
265 /* Link the naming environments */
266 if (!md.isStatic()) /* Fields aren't accessible directly in a static method, so don't link in this table */
267 md.getParameterTable().setParent(cd.getFieldTable());
269 if (!md.isStatic()) {
270 VarDescriptor thisvd=new VarDescriptor(new TypeDescriptor(cd),"this");
275 public void checkMethodBody(ClassDescriptor cd, MethodDescriptor md) {
276 ClassDescriptor superdesc=cd.getSuperDesc();
277 if (superdesc!=null) {
278 Set possiblematches=superdesc.getMethodTable().getSet(md.getSymbol());
279 for(Iterator methodit=possiblematches.iterator(); methodit.hasNext();) {
280 MethodDescriptor matchmd=(MethodDescriptor)methodit.next();
281 if (md.matches(matchmd)) {
282 if (matchmd.getModifiers().isFinal()) {
283 throw new Error("Try to override final method in method:"+md+" declared in "+cd);
288 BlockNode bn=state.getMethodBody(md);
289 checkBlockNode(md, md.getParameterTable(),bn);
292 public void checkBlockNode(Descriptor md, SymbolTable nametable, BlockNode bn) {
293 /* Link in the naming environment */
294 bn.getVarTable().setParent(nametable);
295 for(int i=0; i<bn.size(); i++) {
296 BlockStatementNode bsn=bn.get(i);
297 checkBlockStatementNode(md, bn.getVarTable(),bsn);
301 public void checkBlockStatementNode(Descriptor md, SymbolTable nametable, BlockStatementNode bsn) {
303 case Kind.BlockExpressionNode:
304 checkBlockExpressionNode(md, nametable,(BlockExpressionNode)bsn);
307 case Kind.DeclarationNode:
308 checkDeclarationNode(md, nametable, (DeclarationNode)bsn);
311 case Kind.TagDeclarationNode:
312 checkTagDeclarationNode(md, nametable, (TagDeclarationNode)bsn);
315 case Kind.IfStatementNode:
316 checkIfStatementNode(md, nametable, (IfStatementNode)bsn);
319 case Kind.SwitchStatementNode:
320 checkSwitchStatementNode(md, nametable, (SwitchStatementNode)bsn);
324 checkLoopNode(md, nametable, (LoopNode)bsn);
327 case Kind.ReturnNode:
328 checkReturnNode(md, nametable, (ReturnNode)bsn);
331 case Kind.TaskExitNode:
332 checkTaskExitNode(md, nametable, (TaskExitNode)bsn);
335 case Kind.SubBlockNode:
336 checkSubBlockNode(md, nametable, (SubBlockNode)bsn);
339 case Kind.AtomicNode:
340 checkAtomicNode(md, nametable, (AtomicNode)bsn);
343 case Kind.SynchronizedNode:
344 checkSynchronizedNode(md, nametable, (SynchronizedNode)bsn);
347 case Kind.ContinueBreakNode:
348 checkContinueBreakNode(md, nametable, (ContinueBreakNode) bsn);
352 case Kind.GenReachNode:
353 // do nothing, no semantic check for SESEs
360 void checkBlockExpressionNode(Descriptor md, SymbolTable nametable, BlockExpressionNode ben) {
361 checkExpressionNode(md, nametable, ben.getExpression(), null);
364 void checkDeclarationNode(Descriptor md, SymbolTable nametable, DeclarationNode dn) {
365 VarDescriptor vd=dn.getVarDescriptor();
366 checkTypeDescriptor(vd.getType());
367 Descriptor d=nametable.get(vd.getSymbol());
369 (d instanceof FieldDescriptor)) {
372 throw new Error(vd.getSymbol()+" in "+md+" defined a second time");
373 if (dn.getExpression()!=null)
374 checkExpressionNode(md, nametable, dn.getExpression(), vd.getType());
377 void checkTagDeclarationNode(Descriptor md, SymbolTable nametable, TagDeclarationNode dn) {
378 TagVarDescriptor vd=dn.getTagVarDescriptor();
379 Descriptor d=nametable.get(vd.getSymbol());
381 (d instanceof FieldDescriptor)) {
384 throw new Error(vd.getSymbol()+" defined a second time");
387 void checkSubBlockNode(Descriptor md, SymbolTable nametable, SubBlockNode sbn) {
388 checkBlockNode(md, nametable, sbn.getBlockNode());
391 void checkAtomicNode(Descriptor md, SymbolTable nametable, AtomicNode sbn) {
392 checkBlockNode(md, nametable, sbn.getBlockNode());
395 void checkSynchronizedNode(Descriptor md, SymbolTable nametable, SynchronizedNode sbn) {
396 checkBlockNode(md, nametable, sbn.getBlockNode());
397 //todo this could be Object
398 checkExpressionNode(md, nametable, sbn.getExpr(), null);
401 void checkContinueBreakNode(Descriptor md, SymbolTable nametable, ContinueBreakNode cbn) {
402 if (loopstack.empty())
403 throw new Error("continue/break outside of loop");
404 Object o = loopstack.peek();
405 if(o instanceof LoopNode) {
406 LoopNode ln=(LoopNode)o;
411 void checkReturnNode(Descriptor d, SymbolTable nametable, ReturnNode rn) {
412 if (d instanceof TaskDescriptor)
413 throw new Error("Illegal return appears in Task: "+d.getSymbol());
414 MethodDescriptor md=(MethodDescriptor)d;
415 if (rn.getReturnExpression()!=null)
416 if (md.getReturnType()==null)
417 throw new Error("Constructor can't return something.");
418 else if (md.getReturnType().isVoid())
419 throw new Error(md+" is void");
421 checkExpressionNode(md, nametable, rn.getReturnExpression(), md.getReturnType());
423 if (md.getReturnType()!=null&&!md.getReturnType().isVoid())
424 throw new Error("Need to return something for "+md);
427 void checkTaskExitNode(Descriptor md, SymbolTable nametable, TaskExitNode ten) {
428 if (md instanceof MethodDescriptor)
429 throw new Error("Illegal taskexit appears in Method: "+md.getSymbol());
430 checkFlagEffects((TaskDescriptor)md, ten.getFlagEffects(),nametable);
431 checkConstraintCheck((TaskDescriptor) md, nametable, ten.getChecks());
434 void checkIfStatementNode(Descriptor md, SymbolTable nametable, IfStatementNode isn) {
435 checkExpressionNode(md, nametable, isn.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
436 checkBlockNode(md, nametable, isn.getTrueBlock());
437 if (isn.getFalseBlock()!=null)
438 checkBlockNode(md, nametable, isn.getFalseBlock());
441 void checkSwitchStatementNode(Descriptor md, SymbolTable nametable, SwitchStatementNode ssn) {
442 checkExpressionNode(md, nametable, ssn.getCondition(), new TypeDescriptor(TypeDescriptor.INT));
444 BlockNode sbn = ssn.getSwitchBody();
445 boolean hasdefault = false;
446 for(int i = 0; i < sbn.size(); i++) {
447 boolean containdefault = checkSwitchBlockNode(md, nametable, (SwitchBlockNode)sbn.get(i));
448 if(hasdefault && containdefault) {
449 throw new Error("Error: duplicate default branch in switch-case statement in Method: " + md.getSymbol());
451 hasdefault = containdefault;
455 boolean checkSwitchBlockNode(Descriptor md, SymbolTable nametable, SwitchBlockNode sbn) {
456 Vector<SwitchLabelNode> slnv = sbn.getSwitchConditions();
458 for(int i = 0; i < slnv.size(); i++) {
459 if(slnv.elementAt(i).isdefault) {
462 checkConstantExpressionNode(md, nametable, slnv.elementAt(i).getCondition(), new TypeDescriptor(TypeDescriptor.INT));
466 throw new Error("Error: duplicate default branch in switch-case statement in Method: " + md.getSymbol());
469 checkBlockNode(md, nametable, sbn.getSwitchBlockStatement());
471 return (defaultb > 0);
475 void checkConstantExpressionNode(Descriptor md, SymbolTable nametable, ExpressionNode en, TypeDescriptor td) {
477 case Kind.FieldAccessNode:
478 checkFieldAccessNode(md,nametable,(FieldAccessNode)en,td);
481 case Kind.LiteralNode:
482 checkLiteralNode(md,nametable,(LiteralNode)en,td);
486 checkNameNode(md,nametable,(NameNode)en,td);
490 checkOpNode(md, nametable, (OpNode)en, td);
496 void checkExpressionNode(Descriptor md, SymbolTable nametable, ExpressionNode en, TypeDescriptor td) {
498 case Kind.AssignmentNode:
499 checkAssignmentNode(md,nametable,(AssignmentNode)en,td);
503 checkCastNode(md,nametable,(CastNode)en,td);
506 case Kind.CreateObjectNode:
507 checkCreateObjectNode(md,nametable,(CreateObjectNode)en,td);
510 case Kind.FieldAccessNode:
511 checkFieldAccessNode(md,nametable,(FieldAccessNode)en,td);
514 case Kind.ArrayAccessNode:
515 checkArrayAccessNode(md,nametable,(ArrayAccessNode)en,td);
518 case Kind.LiteralNode:
519 checkLiteralNode(md,nametable,(LiteralNode)en,td);
522 case Kind.MethodInvokeNode:
523 checkMethodInvokeNode(md,nametable,(MethodInvokeNode)en,td);
527 checkNameNode(md,nametable,(NameNode)en,td);
531 checkOpNode(md,nametable,(OpNode)en,td);
534 case Kind.OffsetNode:
535 checkOffsetNode(md, nametable, (OffsetNode)en, td);
538 case Kind.TertiaryNode:
539 checkTertiaryNode(md, nametable, (TertiaryNode)en, td);
542 case Kind.InstanceOfNode:
543 checkInstanceOfNode(md, nametable, (InstanceOfNode) en, td);
546 case Kind.ArrayInitializerNode:
547 checkArrayInitializerNode(md, nametable, (ArrayInitializerNode) en, td);
550 case Kind.ClassTypeNode:
551 checkClassTypeNode(md, nametable, (ClassTypeNode) en, td);
557 void checkClassTypeNode(Descriptor md, SymbolTable nametable, ClassTypeNode tn, TypeDescriptor td) {
558 checkTypeDescriptor(tn.getType());
561 void checkCastNode(Descriptor md, SymbolTable nametable, CastNode cn, TypeDescriptor td) {
562 /* Get type descriptor */
563 if (cn.getType()==null) {
564 NameDescriptor typenamed=cn.getTypeName().getName();
565 String typename=typenamed.toString();
566 TypeDescriptor ntd=new TypeDescriptor(getClass(typename));
570 /* Check the type descriptor */
571 TypeDescriptor cast_type=cn.getType();
572 checkTypeDescriptor(cast_type);
576 if (!typeutil.isSuperorType(td,cast_type))
577 throw new Error("Cast node returns "+cast_type+", but need "+td);
580 ExpressionNode en=cn.getExpression();
581 checkExpressionNode(md, nametable, en, null);
582 TypeDescriptor etd=en.getType();
583 if (typeutil.isSuperorType(cast_type,etd)) /* Cast trivially succeeds */
586 if (typeutil.isSuperorType(etd,cast_type)) /* Cast may succeed */
588 if (typeutil.isCastable(etd, cast_type))
591 /* Different branches */
592 /* TODO: change if add interfaces */
593 throw new Error("Cast will always fail\n"+cn.printNode(0));
596 void checkFieldAccessNode(Descriptor md, SymbolTable nametable, FieldAccessNode fan, TypeDescriptor td) {
597 ExpressionNode left=fan.getExpression();
598 checkExpressionNode(md,nametable,left,null);
599 TypeDescriptor ltd=left.getType();
600 String fieldname=fan.getFieldName();
602 FieldDescriptor fd=null;
603 if (ltd.isArray()&&fieldname.equals("length"))
604 fd=FieldDescriptor.arrayLength;
606 fd=(FieldDescriptor) ltd.getClassDesc().getFieldTable().get(fieldname);
608 // TODO add version for normal Java later
609 if(ltd.isClassNameRef()) {
610 // the field access is using a class name directly
611 if(ltd.getClassDesc().isEnum()) {
612 int value = ltd.getClassDesc().getEnumConstant(fieldname);
614 // check if this field is an enum constant
615 throw new Error(fieldname + " is not an enum constant in "+fan.printNode(0)+" in "+md);
617 fd = new FieldDescriptor(new Modifiers(Modifiers.PUBLIC|Modifiers.FINAL), new TypeDescriptor(TypeDescriptor.INT), fieldname, null, false);
619 fd.setEnumValue(value);
620 } else if(fd.isStatic()) {
621 // check if this field is a static field
622 if(fd.getExpressionNode() != null) {
623 checkExpressionNode(md,nametable,fd.getExpressionNode(),null);
626 throw new Error("Dereference of the non-static field "+ fieldname + " in "+fan.printNode(0)+" in "+md);
631 throw new Error("Unknown field "+fieldname + " in "+fan.printNode(0)+" in "+md);
633 if (fd.getType().iswrapper()) {
634 FieldAccessNode fan2=new FieldAccessNode(left, fieldname);
637 fan.fieldname="value";
639 ExpressionNode leftwr=fan.getExpression();
640 TypeDescriptor ltdwr=leftwr.getType();
641 String fieldnamewr=fan.getFieldName();
642 FieldDescriptor fdwr=(FieldDescriptor) ltdwr.getClassDesc().getFieldTable().get(fieldnamewr);
645 throw new Error("Unknown field "+fieldnamewr + " in "+fan.printNode(0)+" in "+md);
650 if (!typeutil.isSuperorType(td,fan.getType()))
651 throw new Error("Field node returns "+fan.getType()+", but need "+td);
655 void checkArrayAccessNode(Descriptor md, SymbolTable nametable, ArrayAccessNode aan, TypeDescriptor td) {
656 ExpressionNode left=aan.getExpression();
657 checkExpressionNode(md,nametable,left,null);
659 checkExpressionNode(md,nametable,aan.getIndex(),new TypeDescriptor(TypeDescriptor.INT));
660 TypeDescriptor ltd=left.getType();
661 if (ltd.dereference().iswrapper()) {
662 aan.wrappertype=((FieldDescriptor)ltd.dereference().getClassDesc().getFieldTable().get("value")).getType();
666 if (!typeutil.isSuperorType(td,aan.getType()))
667 throw new Error("Field node returns "+aan.getType()+", but need "+td);
670 void checkLiteralNode(Descriptor md, SymbolTable nametable, LiteralNode ln, TypeDescriptor td) {
671 /* Resolve the type */
672 Object o=ln.getValue();
673 if (ln.getTypeString().equals("null")) {
674 ln.setType(new TypeDescriptor(TypeDescriptor.NULL));
675 } else if (o instanceof Integer) {
676 ln.setType(new TypeDescriptor(TypeDescriptor.INT));
677 } else if (o instanceof Long) {
678 ln.setType(new TypeDescriptor(TypeDescriptor.LONG));
679 } else if (o instanceof Float) {
680 ln.setType(new TypeDescriptor(TypeDescriptor.FLOAT));
681 } else if (o instanceof Boolean) {
682 ln.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
683 } else if (o instanceof Double) {
684 ln.setType(new TypeDescriptor(TypeDescriptor.DOUBLE));
685 } else if (o instanceof Character) {
686 ln.setType(new TypeDescriptor(TypeDescriptor.CHAR));
687 } else if (o instanceof String) {
688 ln.setType(new TypeDescriptor(getClass(TypeUtil.StringClass)));
692 if (!typeutil.isSuperorType(td,ln.getType())) {
693 Long l = ln.evaluate();
694 if((ln.getType().isByte() || ln.getType().isShort()
695 || ln.getType().isChar() || ln.getType().isInt())
697 && (td.isByte() || td.isShort() || td.isChar()
698 || td.isInt() || td.isLong())) {
699 long lnvalue = l.longValue();
700 if((td.isByte() && ((lnvalue > 127) || (lnvalue < -128)))
701 || (td.isShort() && ((lnvalue > 32767) || (lnvalue < -32768)))
702 || (td.isChar() && ((lnvalue > 65535) || (lnvalue < 0)))
703 || (td.isInt() && ((lnvalue > 2147483647) || (lnvalue < -2147483648)))
704 || (td.isLong() && ((lnvalue > 9223372036854775807L) || (lnvalue < -9223372036854775808L)))) {
705 throw new Error("Field node returns "+ln.getType()+", but need "+td+" in "+md);
708 throw new Error("Field node returns "+ln.getType()+", but need "+td+" in "+md);
713 void checkNameNode(Descriptor md, SymbolTable nametable, NameNode nn, TypeDescriptor td) {
714 NameDescriptor nd=nn.getName();
715 if (nd.getBase()!=null) {
717 /* Rewrite NameNode */
718 ExpressionNode en=translateNameDescriptorintoExpression(nd);
719 nn.setExpression(en);
720 checkExpressionNode(md,nametable,en,td);
722 String varname=nd.toString();
723 if(varname.equals("this")) {
725 nn.setVar((VarDescriptor)nametable.get("this"));
728 Descriptor d=(Descriptor)nametable.get(varname);
731 // TODO add version for normal Java later
732 ClassDescriptor cd = null;
733 if(((MethodDescriptor)md).isStaticBlock()) {
734 // this is a static block, all the accessed fields should be static field
735 cd = ((MethodDescriptor)md).getClassDesc();
736 SymbolTable fieldtbl = cd.getFieldTable();
737 FieldDescriptor fd=(FieldDescriptor)fieldtbl.get(varname);
738 if((fd == null) || (!fd.isStatic())){
739 // no such field in the class, check if this is a class
740 if(varname.equals("this")) {
741 throw new Error("Error: access this obj in a static block");
743 cd=getClass(varname);
745 // this is a class name
749 throw new Error("Name "+varname+" should not be used in static block: "+md);
752 // this is a static field
758 // check if the var is a static field of the class
759 if(md instanceof MethodDescriptor) {
760 cd = ((MethodDescriptor)md).getClassDesc();
761 FieldDescriptor fd = (FieldDescriptor)cd.getFieldTable().get(varname);
762 if((fd != null) && (fd.isStatic())) {
766 if (!typeutil.isSuperorType(td,nn.getType()))
767 throw new Error("Field node returns "+nn.getType()+", but need "+td);
769 } else if(fd != null) {
770 throw new Error("Name "+varname+" should not be used in " + md);
773 cd=getClass(varname);
775 // this is a class name
779 throw new Error("Name "+varname+" undefined in: "+md);
783 throw new Error("Name "+varname+" undefined in: "+md);
786 if (d instanceof VarDescriptor) {
788 } else if (d instanceof FieldDescriptor) {
789 FieldDescriptor fd=(FieldDescriptor)d;
790 if (fd.getType().iswrapper()) {
791 String id=nd.getIdentifier();
792 NameDescriptor base=nd.getBase();
793 NameNode n=new NameNode(nn.getName());
795 n.setVar((VarDescriptor)nametable.get("this")); /* Need a pointer to this */
796 FieldAccessNode fan=new FieldAccessNode(n,"value");
797 FieldDescriptor fdval=(FieldDescriptor) fd.getType().getClassDesc().getFieldTable().get("value");
799 nn.setExpression(fan);
802 nn.setVar((VarDescriptor)nametable.get("this")); /* Need a pointer to this */
804 } else if (d instanceof TagVarDescriptor) {
806 } else throw new Error("Wrong type of descriptor");
808 if (!typeutil.isSuperorType(td,nn.getType()))
809 throw new Error("Field node returns "+nn.getType()+", but need "+td);
813 void checkOffsetNode(Descriptor md, SymbolTable nameTable, OffsetNode ofn, TypeDescriptor td) {
814 TypeDescriptor ltd=ofn.td;
815 checkTypeDescriptor(ltd);
817 String fieldname = ofn.fieldname;
818 FieldDescriptor fd=null;
819 if (ltd.isArray()&&fieldname.equals("length")) {
820 fd=FieldDescriptor.arrayLength;
822 fd=(FieldDescriptor) ltd.getClassDesc().getFieldTable().get(fieldname);
826 checkField(ltd.getClassDesc(), fd);
829 throw new Error("Unknown field "+fieldname + " in "+ofn.printNode(1)+" in "+md);
832 if (!typeutil.isSuperorType(td, ofn.getType())) {
833 System.out.println(td);
834 System.out.println(ofn.getType());
835 throw new Error("Type of rside not compatible with type of lside"+ofn.printNode(0));
841 void checkTertiaryNode(Descriptor md, SymbolTable nametable, TertiaryNode tn, TypeDescriptor td) {
842 checkExpressionNode(md, nametable, tn.getCond(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
843 checkExpressionNode(md, nametable, tn.getTrueExpr(), td );
844 checkExpressionNode(md, nametable, tn.getFalseExpr(), td );
847 void checkInstanceOfNode(Descriptor md, SymbolTable nametable, InstanceOfNode tn, TypeDescriptor td) {
848 if (td!=null&&!td.isBoolean())
849 throw new Error("Expecting type "+td+"for instanceof expression");
851 checkTypeDescriptor(tn.getExprType());
852 checkExpressionNode(md, nametable, tn.getExpr(), null);
855 void checkArrayInitializerNode(Descriptor md, SymbolTable nametable, ArrayInitializerNode ain, TypeDescriptor td) {
856 Vector<TypeDescriptor> vec_type = new Vector<TypeDescriptor>();
857 for( int i = 0; i < ain.numVarInitializers(); ++i ) {
858 checkExpressionNode(md, nametable, ain.getVarInitializer(i), td==null?td:td.dereference());
859 vec_type.add(ain.getVarInitializer(i).getType());
861 // descide the type of this variableInitializerNode
862 TypeDescriptor out_type = vec_type.elementAt(0);
863 for(int i = 1; i < vec_type.size(); i++) {
864 TypeDescriptor tmp_type = vec_type.elementAt(i);
865 if(out_type == null) {
866 if(tmp_type != null) {
869 } else if(out_type.isNull()) {
870 if(!tmp_type.isNull() ) {
871 if(!tmp_type.isArray()) {
872 throw new Error("Error: mixed type in var initializer list");
877 } else if(out_type.isArray()) {
878 if(tmp_type.isArray()) {
879 if(tmp_type.getArrayCount() > out_type.getArrayCount()) {
882 } else if((tmp_type != null) && (!tmp_type.isNull())) {
883 throw new Error("Error: mixed type in var initializer list");
885 } else if(out_type.isInt()) {
886 if(!tmp_type.isInt()) {
887 throw new Error("Error: mixed type in var initializer list");
889 } else if(out_type.isString()) {
890 if(!tmp_type.isString()) {
891 throw new Error("Error: mixed type in var initializer list");
895 if(out_type != null) {
896 out_type = out_type.makeArray(state);
897 //out_type.setStatic();
899 ain.setType(out_type);
902 void checkAssignmentNode(Descriptor md, SymbolTable nametable, AssignmentNode an, TypeDescriptor td) {
903 boolean postinc=true;
904 if (an.getOperation().getBaseOp()==null||
905 (an.getOperation().getBaseOp().getOp()!=Operation.POSTINC&&
906 an.getOperation().getBaseOp().getOp()!=Operation.POSTDEC))
909 checkExpressionNode(md, nametable, an.getSrc(),td);
910 //TODO: Need check on validity of operation here
911 if (!((an.getDest() instanceof FieldAccessNode)||
912 (an.getDest() instanceof ArrayAccessNode)||
913 (an.getDest() instanceof NameNode)))
914 throw new Error("Bad lside in "+an.printNode(0));
915 checkExpressionNode(md, nametable, an.getDest(), null);
917 /* We want parameter variables to tasks to be immutable */
918 if (md instanceof TaskDescriptor) {
919 if (an.getDest() instanceof NameNode) {
920 NameNode nn=(NameNode)an.getDest();
921 if (nn.getVar()!=null) {
922 if (((TaskDescriptor)md).getParameterTable().contains(nn.getVar().getSymbol()))
923 throw new Error("Can't modify parameter "+nn.getVar()+ " to task "+td.getSymbol());
928 if (an.getDest().getType().isString()&&an.getOperation().getOp()==AssignOperation.PLUSEQ) {
930 ClassDescriptor stringcl=getClass(TypeUtil.StringClass);
931 TypeDescriptor stringtd=new TypeDescriptor(stringcl);
932 NameDescriptor nd=new NameDescriptor("String");
933 NameDescriptor valuend=new NameDescriptor(nd, "valueOf");
935 if (!(an.getSrc().getType().isString()&&(an.getSrc() instanceof OpNode))) {
936 MethodInvokeNode rightmin=new MethodInvokeNode(valuend);
937 rightmin.addArgument(an.getSrc());
939 checkExpressionNode(md, nametable, an.getSrc(), null);
943 if (!postinc&&!typeutil.isSuperorType(an.getDest().getType(),an.getSrc().getType())) {
944 TypeDescriptor dt = an.getDest().getType();
945 TypeDescriptor st = an.getSrc().getType();
946 if(an.getSrc().kind() == Kind.ArrayInitializerNode) {
947 if(dt.getArrayCount() != st.getArrayCount()) {
948 throw new Error("Type of rside ("+an.getSrc().getType().toPrettyString()+") not compatible with type of lside ("+an.getDest().getType().toPrettyString()+")"+an.printNode(0));
951 dt = dt.dereference();
952 st = st.dereference();
953 } while(dt.isArray());
954 if((st.isByte() || st.isShort() || st.isChar() || st.isInt())
955 && (dt.isByte() || dt.isShort() || dt.isChar() || dt.isInt() || dt.isLong())) {
958 throw new Error("Type of rside ("+an.getSrc().getType().toPrettyString()+") not compatible with type of lside ("+an.getDest().getType().toPrettyString()+")"+an.printNode(0));
962 Long l = an.getSrc().evaluate();
963 if((st.isByte() || st.isShort() || st.isChar() || st.isInt())
965 && (dt.isByte() || dt.isShort() || dt.isChar() || dt.isInt() || dt.isLong())) {
966 long lnvalue = l.longValue();
967 if((dt.isByte() && ((lnvalue > 127) || (lnvalue < -128)))
968 || (dt.isShort() && ((lnvalue > 32767) || (lnvalue < -32768)))
969 || (dt.isChar() && ((lnvalue > 65535) || (lnvalue < 0)))
970 || (dt.isInt() && ((lnvalue > 2147483647) || (lnvalue < -2147483648)))
971 || (dt.isLong() && ((lnvalue > 9223372036854775807L) || (lnvalue < -9223372036854775808L)))) {
972 throw new Error("Type of rside ("+an.getSrc().getType().toPrettyString()+") not compatible with type of lside ("+an.getDest().getType().toPrettyString()+")"+an.printNode(0));
975 throw new Error("Type of rside ("+an.getSrc().getType().toPrettyString()+") not compatible with type of lside ("+an.getDest().getType().toPrettyString()+")"+an.printNode(0));
981 void checkLoopNode(Descriptor md, SymbolTable nametable, LoopNode ln) {
983 if (ln.getType()==LoopNode.WHILELOOP||ln.getType()==LoopNode.DOWHILELOOP) {
984 checkExpressionNode(md, nametable, ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
985 checkBlockNode(md, nametable, ln.getBody());
988 /* Link in the initializer naming environment */
989 BlockNode bn=ln.getInitializer();
990 bn.getVarTable().setParent(nametable);
991 for(int i=0; i<bn.size(); i++) {
992 BlockStatementNode bsn=bn.get(i);
993 checkBlockStatementNode(md, bn.getVarTable(),bsn);
995 //check the condition
996 checkExpressionNode(md, bn.getVarTable(), ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
997 checkBlockNode(md, bn.getVarTable(), ln.getBody());
998 checkBlockNode(md, bn.getVarTable(), ln.getUpdate());
1004 void checkCreateObjectNode(Descriptor md, SymbolTable nametable, CreateObjectNode con, TypeDescriptor td) {
1005 TypeDescriptor[] tdarray=new TypeDescriptor[con.numArgs()];
1006 for(int i=0; i<con.numArgs(); i++) {
1007 ExpressionNode en=con.getArg(i);
1008 checkExpressionNode(md,nametable,en,null);
1009 tdarray[i]=en.getType();
1012 TypeDescriptor typetolookin=con.getType();
1013 checkTypeDescriptor(typetolookin);
1015 if (td!=null&&!typeutil.isSuperorType(td, typetolookin))
1016 throw new Error(typetolookin + " isn't a "+td);
1018 /* Check Array Initializers */
1019 if(state.MGC && (con.getArrayInitializer() != null)) {
1020 checkArrayInitializerNode(md, nametable, con.getArrayInitializer(), td);
1023 /* Check flag effects */
1024 if (con.getFlagEffects()!=null) {
1025 FlagEffects fe=con.getFlagEffects();
1026 ClassDescriptor cd=typetolookin.getClassDesc();
1028 for(int j=0; j<fe.numEffects(); j++) {
1029 FlagEffect flag=fe.getEffect(j);
1030 String name=flag.getName();
1031 FlagDescriptor flag_d=(FlagDescriptor)cd.getFlagTable().get(name);
1032 //Make sure the flag is declared
1034 throw new Error("Flag descriptor "+name+" undefined in class: "+cd.getSymbol());
1035 if (flag_d.getExternal())
1036 throw new Error("Attempting to modify external flag: "+name);
1037 flag.setFlag(flag_d);
1039 for(int j=0; j<fe.numTagEffects(); j++) {
1040 TagEffect tag=fe.getTagEffect(j);
1041 String name=tag.getName();
1043 Descriptor d=(Descriptor)nametable.get(name);
1045 throw new Error("Tag descriptor "+name+" undeclared");
1046 else if (!(d instanceof TagVarDescriptor))
1047 throw new Error(name+" is not a tag descriptor");
1048 tag.setTag((TagVarDescriptor)d);
1052 if ((!typetolookin.isClass())&&(!typetolookin.isArray()))
1053 throw new Error("Can't allocate primitive type:"+con.printNode(0));
1055 if (!typetolookin.isArray()) {
1056 //Array's don't need constructor calls
1057 ClassDescriptor classtolookin=typetolookin.getClassDesc();
1059 Set methoddescriptorset=classtolookin.getMethodTable().getSet(typetolookin.getSymbol());
1060 MethodDescriptor bestmd=null;
1062 for(Iterator methodit=methoddescriptorset.iterator(); methodit.hasNext();) {
1063 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
1064 /* Need correct number of parameters */
1065 if (con.numArgs()!=currmd.numParameters())
1067 for(int i=0; i<con.numArgs(); i++) {
1068 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
1069 continue NextMethod;
1071 /* Local allocations can't call global allocator */
1072 if (!con.isGlobal()&&currmd.isGlobal())
1075 /* Method okay so far */
1079 if (typeutil.isMoreSpecific(currmd,bestmd)) {
1081 } else if (con.isGlobal()&&match(currmd, bestmd)) {
1082 if (currmd.isGlobal()&&!bestmd.isGlobal())
1084 else if (currmd.isGlobal()&&bestmd.isGlobal())
1086 } else if (!typeutil.isMoreSpecific(bestmd, currmd)) {
1087 throw new Error("No method is most specific");
1090 /* Is this more specific than bestmd */
1094 throw new Error("No method found for "+con.printNode(0)+" in "+md);
1095 con.setConstructor(bestmd);
1100 /** Check to see if md1 is the same specificity as md2.*/
1102 boolean match(MethodDescriptor md1, MethodDescriptor md2) {
1103 /* Checks if md1 is more specific than md2 */
1104 if (md1.numParameters()!=md2.numParameters())
1106 for(int i=0; i<md1.numParameters(); i++) {
1107 if (!md2.getParamType(i).equals(md1.getParamType(i)))
1110 if (!md2.getReturnType().equals(md1.getReturnType()))
1113 if (!md2.getClassDesc().equals(md1.getClassDesc()))
1121 ExpressionNode translateNameDescriptorintoExpression(NameDescriptor nd) {
1122 String id=nd.getIdentifier();
1123 NameDescriptor base=nd.getBase();
1125 return new NameNode(nd);
1127 return new FieldAccessNode(translateNameDescriptorintoExpression(base),id);
1131 void checkMethodInvokeNode(Descriptor md, SymbolTable nametable, MethodInvokeNode min, TypeDescriptor td) {
1132 /*Typecheck subexpressions
1133 and get types for expressions*/
1135 boolean isstatic = false;
1137 if((md instanceof MethodDescriptor) && ((MethodDescriptor)md).isStatic()) {
1141 TypeDescriptor[] tdarray=new TypeDescriptor[min.numArgs()];
1142 for(int i=0; i<min.numArgs(); i++) {
1143 ExpressionNode en=min.getArg(i);
1144 checkExpressionNode(md,nametable,en,null);
1145 tdarray[i]=en.getType();
1146 if(state.MGC && en.getType().isClass() && en.getType().getClassDesc().isEnum()) {
1147 tdarray[i] = new TypeDescriptor(TypeDescriptor.INT);
1150 TypeDescriptor typetolookin=null;
1151 if (min.getExpression()!=null) {
1152 checkExpressionNode(md,nametable,min.getExpression(),null);
1153 typetolookin=min.getExpression().getType();
1154 //if (typetolookin==null)
1155 //throw new Error(md+" has null return type");
1157 } else if (min.getBaseName()!=null) {
1158 String rootname=min.getBaseName().getRoot();
1159 if (rootname.equals("super")) {
1160 ClassDescriptor supercd=((MethodDescriptor)md).getClassDesc().getSuperDesc();
1161 typetolookin=new TypeDescriptor(supercd);
1162 } else if (rootname.equals("this")) {
1164 throw new Error("use this object in static method md = "+ md.toString());
1166 ClassDescriptor cd=((MethodDescriptor)md).getClassDesc();
1167 typetolookin=new TypeDescriptor(cd);
1168 } else if (nametable.get(rootname)!=null) {
1169 //we have an expression
1170 min.setExpression(translateNameDescriptorintoExpression(min.getBaseName()));
1171 checkExpressionNode(md, nametable, min.getExpression(), null);
1172 typetolookin=min.getExpression().getType();
1175 if(!min.getBaseName().getSymbol().equals("System.out")) {
1176 ExpressionNode nn = translateNameDescriptorintoExpression(min.getBaseName());
1177 checkExpressionNode(md, nametable, nn, null);
1178 typetolookin = nn.getType();
1179 if(!((nn.kind()== Kind.NameNode) && (((NameNode)nn).getField() == null)
1180 && (((NameNode)nn).getVar() == null) && (((NameNode)nn).getExpression() == null))) {
1181 // this is not a pure class name, need to add to
1182 min.setExpression(nn);
1186 ClassDescriptor cd = null;
1187 //if (min.getBaseName().getSymbol().equals("System.out"))
1188 cd=getClass("System");
1190 cd=getClass(min.getBaseName().getSymbol());
1193 throw new Error("md = "+ md.toString()+ " "+min.getBaseName()+" undefined");
1194 typetolookin=new TypeDescriptor(cd);
1198 ClassDescriptor cd = null;
1199 if (min.getBaseName().getSymbol().equals("System.out"))
1200 cd=getClass("System");
1202 cd=getClass(min.getBaseName().getSymbol());
1205 throw new Error("md = "+ md.toString()+ " "+min.getBaseName()+" undefined");
1206 typetolookin=new TypeDescriptor(cd);
1209 } else if ((md instanceof MethodDescriptor)&&min.getMethodName().equals("super")) {
1210 ClassDescriptor supercd=((MethodDescriptor)md).getClassDesc().getSuperDesc();
1211 min.methodid=supercd.getSymbol();
1212 typetolookin=new TypeDescriptor(supercd);
1213 } else if (md instanceof MethodDescriptor) {
1214 typetolookin=new TypeDescriptor(((MethodDescriptor)md).getClassDesc());
1216 /* If this a task descriptor we throw an error at this point */
1217 throw new Error("Unknown method call to "+min.getMethodName()+"in task"+md.getSymbol());
1219 if (!typetolookin.isClass())
1220 throw new Error("Error with method call to "+min.getMethodName());
1221 ClassDescriptor classtolookin=typetolookin.getClassDesc();
1222 //System.out.println("Method name="+min.getMethodName());
1224 Set methoddescriptorset=classtolookin.getMethodTable().getSet(min.getMethodName());
1225 MethodDescriptor bestmd=null;
1227 for(Iterator methodit=methoddescriptorset.iterator(); methodit.hasNext();) {
1228 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
1229 /* Need correct number of parameters */
1230 if (min.numArgs()!=currmd.numParameters())
1232 for(int i=0; i<min.numArgs(); i++) {
1233 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
1234 if(state.MGC && ((!tdarray[i].isArray() &&( tdarray[i].isInt() || tdarray[i].isLong()))
1235 && currmd.getParamType(i).isClass() && currmd.getParamType(i).getClassDesc().getSymbol().equals("Object"))) {
1236 // primitive parameters vs object
1238 continue NextMethod;
1241 /* Method okay so far */
1245 if (typeutil.isMoreSpecific(currmd,bestmd)) {
1247 } else if (!typeutil.isMoreSpecific(bestmd, currmd))
1248 throw new Error("No method is most specific");
1250 /* Is this more specific than bestmd */
1254 throw new Error("No method found for :"+min.printNode(0)+" in class: " + classtolookin+" in "+md);
1255 min.setMethod(bestmd);
1257 if ((td!=null)&&(min.getType()!=null)&&!typeutil.isSuperorType(td, min.getType()))
1258 throw new Error(min.getType()+ " is not equal to or a subclass of "+td);
1259 /* Check whether we need to set this parameter to implied this */
1260 if (! isstatic && !bestmd.isStatic()) {
1261 if (min.getExpression()==null) {
1262 ExpressionNode en=new NameNode(new NameDescriptor("this"));
1263 min.setExpression(en);
1264 checkExpressionNode(md, nametable, min.getExpression(), null);
1269 /* Check if we need to wrap primitive paratmeters to objects */
1270 for(int i=0; i<min.numArgs(); i++) {
1271 if(!tdarray[i].isArray() && (tdarray[i].isInt() || tdarray[i].isLong())
1272 && min.getMethod().getParamType(i).isClass() && min.getMethod().getParamType(i).getClassDesc().getSymbol().equals("Object")) {
1273 // Shall wrap this primitive parameter as a object
1274 ExpressionNode exp = min.getArg(i);
1275 TypeDescriptor ptd = null;
1276 NameDescriptor nd=null;
1277 if(exp.getType().isInt()) {
1278 nd = new NameDescriptor("Integer");
1279 ptd = state.getTypeDescriptor(nd);
1280 } else if(exp.getType().isLong()) {
1281 nd = new NameDescriptor("Long");
1282 ptd = state.getTypeDescriptor(nd);
1284 boolean isglobal = false;
1285 String disjointId = null;
1286 CreateObjectNode con=new CreateObjectNode(ptd, isglobal, disjointId);
1287 con.addArgument(exp);
1288 checkExpressionNode(md, nametable, con, null);
1289 min.setArgument(con, i);
1296 void checkOpNode(Descriptor md, SymbolTable nametable, OpNode on, TypeDescriptor td) {
1297 checkExpressionNode(md, nametable, on.getLeft(), null);
1298 if (on.getRight()!=null)
1299 checkExpressionNode(md, nametable, on.getRight(), null);
1300 TypeDescriptor ltd=on.getLeft().getType();
1301 TypeDescriptor rtd=on.getRight()!=null ? on.getRight().getType() : null;
1302 TypeDescriptor lefttype=null;
1303 TypeDescriptor righttype=null;
1304 Operation op=on.getOp();
1306 switch(op.getOp()) {
1307 case Operation.LOGIC_OR:
1308 case Operation.LOGIC_AND:
1309 if (!(rtd.isBoolean()))
1311 on.setRightType(rtd);
1313 case Operation.LOGIC_NOT:
1314 if (!(ltd.isBoolean()))
1317 on.setLeftType(ltd);
1319 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1322 case Operation.COMP:
1323 // 5.6.2 Binary Numeric Promotion
1324 //TODO unboxing of reference objects
1327 else if (ltd.isFloat())
1329 else if (ltd.isLong())
1330 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1332 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1333 on.setLeftType(lefttype);
1334 on.setType(lefttype);
1337 case Operation.BIT_OR:
1338 case Operation.BIT_XOR:
1339 case Operation.BIT_AND:
1340 // 5.6.2 Binary Numeric Promotion
1341 //TODO unboxing of reference objects
1342 if (ltd.isDouble()||rtd.isDouble())
1344 else if (ltd.isFloat()||rtd.isFloat())
1346 else if (ltd.isLong()||rtd.isLong())
1347 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1348 // 090205 hack for boolean
1349 else if (ltd.isBoolean()||rtd.isBoolean())
1350 lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
1352 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1355 on.setLeftType(lefttype);
1356 on.setRightType(righttype);
1357 on.setType(lefttype);
1360 case Operation.ISAVAILABLE:
1361 if (!(ltd.isPtr())) {
1362 throw new Error("Can't use isavailable on non-pointers/non-parameters.");
1365 on.setLeftType(lefttype);
1366 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1369 case Operation.EQUAL:
1370 case Operation.NOTEQUAL:
1371 // 5.6.2 Binary Numeric Promotion
1372 //TODO unboxing of reference objects
1373 if (ltd.isBoolean()||rtd.isBoolean()) {
1374 if (!(ltd.isBoolean()&&rtd.isBoolean()))
1376 righttype=lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
1377 } else if (ltd.isPtr()||rtd.isPtr()) {
1378 if (!(ltd.isPtr()&&rtd.isPtr())) {
1385 } else if (ltd.isDouble()||rtd.isDouble())
1386 righttype=lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
1387 else if (ltd.isFloat()||rtd.isFloat())
1388 righttype=lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
1389 else if (ltd.isLong()||rtd.isLong())
1390 righttype=lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1392 righttype=lefttype=new TypeDescriptor(TypeDescriptor.INT);
1394 on.setLeftType(lefttype);
1395 on.setRightType(righttype);
1396 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1405 // 5.6.2 Binary Numeric Promotion
1406 //TODO unboxing of reference objects
1407 if (!ltd.isNumber()||!rtd.isNumber()) {
1408 if (!ltd.isNumber())
1409 throw new Error("Leftside is not number"+on.printNode(0)+"type="+ltd.toPrettyString());
1410 if (!rtd.isNumber())
1411 throw new Error("Rightside is not number"+on.printNode(0));
1414 if (ltd.isDouble()||rtd.isDouble())
1415 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
1416 else if (ltd.isFloat()||rtd.isFloat())
1417 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
1418 else if (ltd.isLong()||rtd.isLong())
1419 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1421 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1423 on.setLeftType(lefttype);
1424 on.setRightType(righttype);
1425 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1429 if (ltd.isString()||rtd.isString()) {
1430 ClassDescriptor stringcl=getClass(TypeUtil.StringClass);
1431 TypeDescriptor stringtd=new TypeDescriptor(stringcl);
1432 NameDescriptor nd=new NameDescriptor("String");
1433 NameDescriptor valuend=new NameDescriptor(nd, "valueOf");
1434 if (!(ltd.isString()&&(on.getLeft() instanceof OpNode))) {
1435 MethodInvokeNode leftmin=new MethodInvokeNode(valuend);
1436 leftmin.addArgument(on.getLeft());
1438 checkExpressionNode(md, nametable, on.getLeft(), null);
1441 if (!(rtd.isString()&&(on.getRight() instanceof OpNode))) {
1442 MethodInvokeNode rightmin=new MethodInvokeNode(valuend);
1443 rightmin.addArgument(on.getRight());
1445 checkExpressionNode(md, nametable, on.getRight(), null);
1448 on.setLeftType(stringtd);
1449 on.setRightType(stringtd);
1450 on.setType(stringtd);
1455 case Operation.MULT:
1458 // 5.6.2 Binary Numeric Promotion
1459 //TODO unboxing of reference objects
1460 if (ltd.isArray()||rtd.isArray()||!ltd.isNumber()||!rtd.isNumber())
1461 throw new Error("Error in "+on.printNode(0));
1463 if (ltd.isDouble()||rtd.isDouble())
1464 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
1465 else if (ltd.isFloat()||rtd.isFloat())
1466 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
1467 else if (ltd.isLong()||rtd.isLong())
1468 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1470 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1472 on.setLeftType(lefttype);
1473 on.setRightType(righttype);
1474 on.setType(lefttype);
1477 case Operation.LEFTSHIFT:
1478 case Operation.RIGHTSHIFT:
1479 case Operation.URIGHTSHIFT:
1480 if (!rtd.isIntegerType())
1482 //5.6.1 Unary Numeric Promotion
1483 if (rtd.isByte()||rtd.isShort()||rtd.isInt())
1484 righttype=new TypeDescriptor(TypeDescriptor.INT);
1488 on.setRightType(righttype);
1489 if (!ltd.isIntegerType())
1492 case Operation.UNARYPLUS:
1493 case Operation.UNARYMINUS:
1494 /* case Operation.POSTINC:
1495 case Operation.POSTDEC:
1496 case Operation.PREINC:
1497 case Operation.PREDEC:*/
1498 if (!ltd.isNumber())
1500 //5.6.1 Unary Numeric Promotion
1501 if (ltd.isByte()||ltd.isShort()||ltd.isInt())
1502 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1505 on.setLeftType(lefttype);
1506 on.setType(lefttype);
1510 throw new Error(op.toString());
1514 if (!typeutil.isSuperorType(td, on.getType())) {
1515 System.out.println(td);
1516 System.out.println(on.getType());
1517 throw new Error("Type of rside not compatible with type of lside"+on.printNode(0));