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());
45 // Link together Field, Method tables do classes inherit these from
46 // their ancestor interfaces
47 Vector<String> sifv = cd.getSuperInterface();
48 for(int i = 0; i < sifv.size(); i++) {
49 ClassDescriptor superif = getClass(sifv.elementAt(i));
50 if(!superif.isInterface()) {
51 throw new Error("Error! Class " + cd.getSymbol() + " implements non-interface " + superif.getSymbol());
53 cd.addSuperInterfaces(superif);
54 cd.getFieldTable().addParentIF(superif.getFieldTable());
55 cd.getMethodTable().addParentIF(superif.getMethodTable());
58 /* Check to see that fields are well typed */
59 for(Iterator field_it=cd.getFields(); field_it.hasNext();) {
60 FieldDescriptor fd=(FieldDescriptor)field_it.next();
64 for(Iterator method_it=cd.getMethods(); method_it.hasNext();) {
65 MethodDescriptor md=(MethodDescriptor)method_it.next();
67 if(md.isDefaultConstructor() && (cd.getSuperDesc() != null)) {
68 // add the construction of it super class, can only be super()
69 NameDescriptor nd=new NameDescriptor("super");
70 MethodInvokeNode min=new MethodInvokeNode(nd);
71 BlockExpressionNode ben=new BlockExpressionNode(min);
72 BlockNode bn = state.getMethodBody(md);
73 bn.addFirstBlockStatement(ben);
79 public void semanticCheck() {
80 SymbolTable classtable=state.getClassSymbolTable();
81 toanalyze.addAll(classtable.getValueSet());
82 toanalyze.addAll(state.getTaskSymbolTable().getValueSet());
85 while(!toanalyze.isEmpty()) {
86 Object obj=toanalyze.iterator().next();
87 if (obj instanceof TaskDescriptor) {
88 toanalyze.remove(obj);
89 TaskDescriptor td=(TaskDescriptor)obj;
93 System.out.println( "Error in "+td );
97 ClassDescriptor cd=(ClassDescriptor)obj;
99 //need to initialize typeutil object here...only place we can
100 //get class descriptors without first calling getclass
101 getClass(cd.getSymbol());
102 for(Iterator method_it=cd.getMethods(); method_it.hasNext();) {
103 MethodDescriptor md=(MethodDescriptor)method_it.next();
105 checkMethodBody(cd,md);
107 System.out.println( "Error in "+md );
115 public void checkTypeDescriptor(TypeDescriptor td) {
116 if (td.isPrimitive())
118 else if (td.isClass()) {
119 String name=td.toString();
120 int index = name.lastIndexOf('.');
122 name = name.substring(index+1);
124 ClassDescriptor field_cd=getClass(name);
126 throw new Error("Undefined class "+name);
127 td.setClassDescriptor(field_cd);
129 } else if (td.isTag())
135 public void checkField(ClassDescriptor cd, FieldDescriptor fd) {
136 checkTypeDescriptor(fd.getType());
139 public void checkConstraintCheck(TaskDescriptor td, SymbolTable nametable, Vector ccs) {
141 return; /* No constraint checks to check */
142 for(int i=0; i<ccs.size(); i++) {
143 ConstraintCheck cc=(ConstraintCheck) ccs.get(i);
145 for(int j=0; j<cc.numArgs(); j++) {
146 ExpressionNode en=cc.getArg(j);
147 checkExpressionNode(td,nametable,en,null);
152 public void checkFlagEffects(TaskDescriptor td, Vector vfe, SymbolTable nametable) {
154 return; /* No flag effects to check */
155 for(int i=0; i<vfe.size(); i++) {
156 FlagEffects fe=(FlagEffects) vfe.get(i);
157 String varname=fe.getName();
158 //Make sure the variable is declared as a parameter to the task
159 VarDescriptor vd=(VarDescriptor)td.getParameterTable().get(varname);
161 throw new Error("Parameter "+varname+" in Flag Effects not declared in "+td);
164 //Make sure it correspods to a class
165 TypeDescriptor type_d=vd.getType();
166 if (!type_d.isClass())
167 throw new Error("Cannot have non-object argument for flag_effect");
169 ClassDescriptor cd=type_d.getClassDesc();
170 for(int j=0; j<fe.numEffects(); j++) {
171 FlagEffect flag=fe.getEffect(j);
172 String name=flag.getName();
173 FlagDescriptor flag_d=(FlagDescriptor)cd.getFlagTable().get(name);
174 //Make sure the flag is declared
176 throw new Error("Flag descriptor "+name+" undefined in class: "+cd.getSymbol());
177 if (flag_d.getExternal())
178 throw new Error("Attempting to modify external flag: "+name);
179 flag.setFlag(flag_d);
181 for(int j=0; j<fe.numTagEffects(); j++) {
182 TagEffect tag=fe.getTagEffect(j);
183 String name=tag.getName();
185 Descriptor d=(Descriptor)nametable.get(name);
187 throw new Error("Tag descriptor "+name+" undeclared");
188 else if (!(d instanceof TagVarDescriptor))
189 throw new Error(name+" is not a tag descriptor");
190 tag.setTag((TagVarDescriptor)d);
195 public void checkTask(TaskDescriptor td) {
196 for(int i=0; i<td.numParameters(); i++) {
197 /* Check that parameter is well typed */
198 TypeDescriptor param_type=td.getParamType(i);
199 checkTypeDescriptor(param_type);
201 /* Check the parameter's flag expression is well formed */
202 FlagExpressionNode fen=td.getFlag(td.getParameter(i));
203 if (!param_type.isClass())
204 throw new Error("Cannot have non-object argument to a task");
205 ClassDescriptor cd=param_type.getClassDesc();
207 checkFlagExpressionNode(cd, fen);
210 checkFlagEffects(td, td.getFlagEffects(),td.getParameterTable());
211 /* Check that the task code is valid */
212 BlockNode bn=state.getMethodBody(td);
213 checkBlockNode(td, td.getParameterTable(),bn);
216 public void checkFlagExpressionNode(ClassDescriptor cd, FlagExpressionNode fen) {
218 case Kind.FlagOpNode:
220 FlagOpNode fon=(FlagOpNode)fen;
221 checkFlagExpressionNode(cd, fon.getLeft());
222 if (fon.getRight()!=null)
223 checkFlagExpressionNode(cd, fon.getRight());
229 FlagNode fn=(FlagNode)fen;
230 String name=fn.getFlagName();
231 FlagDescriptor fd=(FlagDescriptor)cd.getFlagTable().get(name);
233 throw new Error("Undeclared flag: "+name);
239 throw new Error("Unrecognized FlagExpressionNode");
243 public void checkMethod(ClassDescriptor cd, MethodDescriptor md) {
244 /* Check for abstract methods */
245 if(md.isAbstract()) {
246 if(!cd.isAbstract() && !cd.isInterface()) {
247 throw new Error("Error! The non-abstract Class " + cd.getSymbol() + " contains an abstract method " + md.getSymbol());
251 /* Check return type */
252 if (!md.isConstructor() && !md.isStaticBlock())
253 if (!md.getReturnType().isVoid())
254 checkTypeDescriptor(md.getReturnType());
256 for(int i=0; i<md.numParameters(); i++) {
257 TypeDescriptor param_type=md.getParamType(i);
258 checkTypeDescriptor(param_type);
260 /* Link the naming environments */
261 if (!md.isStatic()) /* Fields aren't accessible directly in a static method, so don't link in this table */
262 md.getParameterTable().setParent(cd.getFieldTable());
264 if (!md.isStatic()) {
265 VarDescriptor thisvd=new VarDescriptor(new TypeDescriptor(cd),"this");
270 public void checkMethodBody(ClassDescriptor cd, MethodDescriptor md) {
271 ClassDescriptor superdesc=cd.getSuperDesc();
272 if (superdesc!=null) {
273 Set possiblematches=superdesc.getMethodTable().getSet(md.getSymbol());
274 for(Iterator methodit=possiblematches.iterator(); methodit.hasNext();) {
275 MethodDescriptor matchmd=(MethodDescriptor)methodit.next();
276 if (md.matches(matchmd)) {
277 if (matchmd.getModifiers().isFinal()) {
278 throw new Error("Try to override final method in method:"+md+" declared in "+cd);
283 BlockNode bn=state.getMethodBody(md);
284 checkBlockNode(md, md.getParameterTable(),bn);
287 public void checkBlockNode(Descriptor md, SymbolTable nametable, BlockNode bn) {
288 /* Link in the naming environment */
289 bn.getVarTable().setParent(nametable);
290 for(int i=0; i<bn.size(); i++) {
291 BlockStatementNode bsn=bn.get(i);
292 checkBlockStatementNode(md, bn.getVarTable(),bsn);
296 public void checkBlockStatementNode(Descriptor md, SymbolTable nametable, BlockStatementNode bsn) {
298 case Kind.BlockExpressionNode:
299 checkBlockExpressionNode(md, nametable,(BlockExpressionNode)bsn);
302 case Kind.DeclarationNode:
303 checkDeclarationNode(md, nametable, (DeclarationNode)bsn);
306 case Kind.TagDeclarationNode:
307 checkTagDeclarationNode(md, nametable, (TagDeclarationNode)bsn);
310 case Kind.IfStatementNode:
311 checkIfStatementNode(md, nametable, (IfStatementNode)bsn);
314 case Kind.SwitchStatementNode:
315 checkSwitchStatementNode(md, nametable, (SwitchStatementNode)bsn);
319 checkLoopNode(md, nametable, (LoopNode)bsn);
322 case Kind.ReturnNode:
323 checkReturnNode(md, nametable, (ReturnNode)bsn);
326 case Kind.TaskExitNode:
327 checkTaskExitNode(md, nametable, (TaskExitNode)bsn);
330 case Kind.SubBlockNode:
331 checkSubBlockNode(md, nametable, (SubBlockNode)bsn);
334 case Kind.AtomicNode:
335 checkAtomicNode(md, nametable, (AtomicNode)bsn);
338 case Kind.SynchronizedNode:
339 checkSynchronizedNode(md, nametable, (SynchronizedNode)bsn);
342 case Kind.ContinueBreakNode:
343 checkContinueBreakNode(md, nametable, (ContinueBreakNode) bsn);
347 case Kind.GenReachNode:
348 // do nothing, no semantic check for SESEs
355 void checkBlockExpressionNode(Descriptor md, SymbolTable nametable, BlockExpressionNode ben) {
356 checkExpressionNode(md, nametable, ben.getExpression(), null);
359 void checkDeclarationNode(Descriptor md, SymbolTable nametable, DeclarationNode dn) {
360 VarDescriptor vd=dn.getVarDescriptor();
361 checkTypeDescriptor(vd.getType());
362 Descriptor d=nametable.get(vd.getSymbol());
364 (d instanceof FieldDescriptor)) {
367 throw new Error(vd.getSymbol()+" in "+md+" defined a second time");
368 if (dn.getExpression()!=null)
369 checkExpressionNode(md, nametable, dn.getExpression(), vd.getType());
372 void checkTagDeclarationNode(Descriptor md, SymbolTable nametable, TagDeclarationNode dn) {
373 TagVarDescriptor vd=dn.getTagVarDescriptor();
374 Descriptor d=nametable.get(vd.getSymbol());
376 (d instanceof FieldDescriptor)) {
379 throw new Error(vd.getSymbol()+" defined a second time");
382 void checkSubBlockNode(Descriptor md, SymbolTable nametable, SubBlockNode sbn) {
383 checkBlockNode(md, nametable, sbn.getBlockNode());
386 void checkAtomicNode(Descriptor md, SymbolTable nametable, AtomicNode sbn) {
387 checkBlockNode(md, nametable, sbn.getBlockNode());
390 void checkSynchronizedNode(Descriptor md, SymbolTable nametable, SynchronizedNode sbn) {
391 checkBlockNode(md, nametable, sbn.getBlockNode());
392 //todo this could be Object
393 checkExpressionNode(md, nametable, sbn.getExpr(), null);
396 void checkContinueBreakNode(Descriptor md, SymbolTable nametable, ContinueBreakNode cbn) {
397 if (loopstack.empty())
398 throw new Error("continue/break outside of loop");
399 Object o = loopstack.peek();
400 if(o instanceof LoopNode) {
401 LoopNode ln=(LoopNode)o;
406 void checkReturnNode(Descriptor d, SymbolTable nametable, ReturnNode rn) {
407 if (d instanceof TaskDescriptor)
408 throw new Error("Illegal return appears in Task: "+d.getSymbol());
409 MethodDescriptor md=(MethodDescriptor)d;
410 if (rn.getReturnExpression()!=null)
411 if (md.getReturnType()==null)
412 throw new Error("Constructor can't return something.");
413 else if (md.getReturnType().isVoid())
414 throw new Error(md+" is void");
416 checkExpressionNode(md, nametable, rn.getReturnExpression(), md.getReturnType());
418 if (md.getReturnType()!=null&&!md.getReturnType().isVoid())
419 throw new Error("Need to return something for "+md);
422 void checkTaskExitNode(Descriptor md, SymbolTable nametable, TaskExitNode ten) {
423 if (md instanceof MethodDescriptor)
424 throw new Error("Illegal taskexit appears in Method: "+md.getSymbol());
425 checkFlagEffects((TaskDescriptor)md, ten.getFlagEffects(),nametable);
426 checkConstraintCheck((TaskDescriptor) md, nametable, ten.getChecks());
429 void checkIfStatementNode(Descriptor md, SymbolTable nametable, IfStatementNode isn) {
430 checkExpressionNode(md, nametable, isn.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
431 checkBlockNode(md, nametable, isn.getTrueBlock());
432 if (isn.getFalseBlock()!=null)
433 checkBlockNode(md, nametable, isn.getFalseBlock());
436 void checkSwitchStatementNode(Descriptor md, SymbolTable nametable, SwitchStatementNode ssn) {
437 checkExpressionNode(md, nametable, ssn.getCondition(), new TypeDescriptor(TypeDescriptor.INT));
439 BlockNode sbn = ssn.getSwitchBody();
440 boolean hasdefault = false;
441 for(int i = 0; i < sbn.size(); i++) {
442 boolean containdefault = checkSwitchBlockNode(md, nametable, (SwitchBlockNode)sbn.get(i));
443 if(hasdefault && containdefault) {
444 throw new Error("Error: duplicate default branch in switch-case statement in Method: " + md.getSymbol());
446 hasdefault = containdefault;
450 boolean checkSwitchBlockNode(Descriptor md, SymbolTable nametable, SwitchBlockNode sbn) {
451 Vector<SwitchLabelNode> slnv = sbn.getSwitchConditions();
453 for(int i = 0; i < slnv.size(); i++) {
454 if(slnv.elementAt(i).isdefault) {
457 checkConstantExpressionNode(md, nametable, slnv.elementAt(i).getCondition(), new TypeDescriptor(TypeDescriptor.INT));
461 throw new Error("Error: duplicate default branch in switch-case statement in Method: " + md.getSymbol());
464 checkBlockNode(md, nametable, sbn.getSwitchBlockStatement());
466 return (defaultb > 0);
470 void checkConstantExpressionNode(Descriptor md, SymbolTable nametable, ExpressionNode en, TypeDescriptor td) {
472 case Kind.FieldAccessNode:
473 checkFieldAccessNode(md,nametable,(FieldAccessNode)en,td);
476 case Kind.LiteralNode:
477 checkLiteralNode(md,nametable,(LiteralNode)en,td);
481 checkNameNode(md,nametable,(NameNode)en,td);
485 checkOpNode(md, nametable, (OpNode)en, td);
491 void checkExpressionNode(Descriptor md, SymbolTable nametable, ExpressionNode en, TypeDescriptor td) {
493 case Kind.AssignmentNode:
494 checkAssignmentNode(md,nametable,(AssignmentNode)en,td);
498 checkCastNode(md,nametable,(CastNode)en,td);
501 case Kind.CreateObjectNode:
502 checkCreateObjectNode(md,nametable,(CreateObjectNode)en,td);
505 case Kind.FieldAccessNode:
506 checkFieldAccessNode(md,nametable,(FieldAccessNode)en,td);
509 case Kind.ArrayAccessNode:
510 checkArrayAccessNode(md,nametable,(ArrayAccessNode)en,td);
513 case Kind.LiteralNode:
514 checkLiteralNode(md,nametable,(LiteralNode)en,td);
517 case Kind.MethodInvokeNode:
518 checkMethodInvokeNode(md,nametable,(MethodInvokeNode)en,td);
522 checkNameNode(md,nametable,(NameNode)en,td);
526 checkOpNode(md,nametable,(OpNode)en,td);
529 case Kind.OffsetNode:
530 checkOffsetNode(md, nametable, (OffsetNode)en, td);
533 case Kind.TertiaryNode:
534 checkTertiaryNode(md, nametable, (TertiaryNode)en, td);
537 case Kind.InstanceOfNode:
538 checkInstanceOfNode(md, nametable, (InstanceOfNode) en, td);
541 case Kind.ArrayInitializerNode:
542 checkArrayInitializerNode(md, nametable, (ArrayInitializerNode) en, td);
545 case Kind.ClassTypeNode:
546 checkClassTypeNode(md, nametable, (ClassTypeNode) en, td);
552 void checkClassTypeNode(Descriptor md, SymbolTable nametable, ClassTypeNode tn, TypeDescriptor td) {
553 checkTypeDescriptor(tn.getType());
556 void checkCastNode(Descriptor md, SymbolTable nametable, CastNode cn, TypeDescriptor td) {
557 /* Get type descriptor */
558 if (cn.getType()==null) {
559 NameDescriptor typenamed=cn.getTypeName().getName();
560 String typename=typenamed.toString();
561 TypeDescriptor ntd=new TypeDescriptor(getClass(typename));
565 /* Check the type descriptor */
566 TypeDescriptor cast_type=cn.getType();
567 checkTypeDescriptor(cast_type);
571 if (!typeutil.isSuperorType(td,cast_type))
572 throw new Error("Cast node returns "+cast_type+", but need "+td);
575 ExpressionNode en=cn.getExpression();
576 checkExpressionNode(md, nametable, en, null);
577 TypeDescriptor etd=en.getType();
578 if (typeutil.isSuperorType(cast_type,etd)) /* Cast trivially succeeds */
581 if (typeutil.isSuperorType(etd,cast_type)) /* Cast may succeed */
583 if (typeutil.isCastable(etd, cast_type))
586 /* Different branches */
587 /* TODO: change if add interfaces */
588 throw new Error("Cast will always fail\n"+cn.printNode(0));
591 void checkFieldAccessNode(Descriptor md, SymbolTable nametable, FieldAccessNode fan, TypeDescriptor td) {
592 ExpressionNode left=fan.getExpression();
593 checkExpressionNode(md,nametable,left,null);
594 TypeDescriptor ltd=left.getType();
595 String fieldname=fan.getFieldName();
597 FieldDescriptor fd=null;
598 if (ltd.isArray()&&fieldname.equals("length"))
599 fd=FieldDescriptor.arrayLength;
601 fd=(FieldDescriptor) ltd.getClassDesc().getFieldTable().get(fieldname);
603 if(ltd.isClassNameRef()) {
604 // the field access is using a class name directly
605 if(ltd.getClassDesc().isEnum()) {
606 int value = ltd.getClassDesc().getEnumConstant(fieldname);
608 // check if this field is an enum constant
609 throw new Error(fieldname + " is not an enum constant in "+fan.printNode(0)+" in "+md);
611 fd = new FieldDescriptor(new Modifiers(Modifiers.PUBLIC|Modifiers.FINAL), new TypeDescriptor(TypeDescriptor.INT), fieldname, null, false);
613 fd.setEnumValue(value);
614 } else if(fd == null) {
615 throw new Error("Could not find field "+ fieldname + " in "+fan.printNode(0)+" in "+md + " (Line: "+fan.getNumLine()+")");
616 } else if(fd.isStatic()) {
617 // check if this field is a static field
618 if(fd.getExpressionNode() != null) {
619 checkExpressionNode(md,nametable,fd.getExpressionNode(),null);
622 throw new Error("Dereference of the non-static field "+ fieldname + " in "+fan.printNode(0)+" in "+md);
627 throw new Error("Unknown field "+fieldname + " in "+fan.printNode(0)+" in "+md);
629 if (fd.getType().iswrapper()) {
630 FieldAccessNode fan2=new FieldAccessNode(left, fieldname);
631 fan2.setNumLine(left.getNumLine());
634 fan.fieldname="value";
636 ExpressionNode leftwr=fan.getExpression();
637 TypeDescriptor ltdwr=leftwr.getType();
638 String fieldnamewr=fan.getFieldName();
639 FieldDescriptor fdwr=(FieldDescriptor) ltdwr.getClassDesc().getFieldTable().get(fieldnamewr);
642 throw new Error("Unknown field "+fieldnamewr + " in "+fan.printNode(0)+" in "+md);
647 if (!typeutil.isSuperorType(td,fan.getType()))
648 throw new Error("Field node returns "+fan.getType()+", but need "+td);
652 void checkArrayAccessNode(Descriptor md, SymbolTable nametable, ArrayAccessNode aan, TypeDescriptor td) {
653 ExpressionNode left=aan.getExpression();
654 checkExpressionNode(md,nametable,left,null);
656 checkExpressionNode(md,nametable,aan.getIndex(),new TypeDescriptor(TypeDescriptor.INT));
657 TypeDescriptor ltd=left.getType();
658 if (ltd.dereference().iswrapper()) {
659 aan.wrappertype=((FieldDescriptor)ltd.dereference().getClassDesc().getFieldTable().get("value")).getType();
663 if (!typeutil.isSuperorType(td,aan.getType()))
664 throw new Error("Field node returns "+aan.getType()+", but need "+td);
667 void checkLiteralNode(Descriptor md, SymbolTable nametable, LiteralNode ln, TypeDescriptor td) {
668 /* Resolve the type */
669 Object o=ln.getValue();
670 if (ln.getTypeString().equals("null")) {
671 ln.setType(new TypeDescriptor(TypeDescriptor.NULL));
672 } else if (o instanceof Integer) {
673 ln.setType(new TypeDescriptor(TypeDescriptor.INT));
674 } else if (o instanceof Long) {
675 ln.setType(new TypeDescriptor(TypeDescriptor.LONG));
676 } else if (o instanceof Float) {
677 ln.setType(new TypeDescriptor(TypeDescriptor.FLOAT));
678 } else if (o instanceof Boolean) {
679 ln.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
680 } else if (o instanceof Double) {
681 ln.setType(new TypeDescriptor(TypeDescriptor.DOUBLE));
682 } else if (o instanceof Character) {
683 ln.setType(new TypeDescriptor(TypeDescriptor.CHAR));
684 } else if (o instanceof String) {
685 ln.setType(new TypeDescriptor(getClass(TypeUtil.StringClass)));
689 if (!typeutil.isSuperorType(td,ln.getType())) {
690 Long l = ln.evaluate();
691 if((ln.getType().isByte() || ln.getType().isShort()
692 || ln.getType().isChar() || ln.getType().isInt())
694 && (td.isByte() || td.isShort() || td.isChar()
695 || td.isInt() || td.isLong())) {
696 long lnvalue = l.longValue();
697 if((td.isByte() && ((lnvalue > 127) || (lnvalue < -128)))
698 || (td.isShort() && ((lnvalue > 32767) || (lnvalue < -32768)))
699 || (td.isChar() && ((lnvalue > 65535) || (lnvalue < 0)))
700 || (td.isInt() && ((lnvalue > 2147483647) || (lnvalue < -2147483648)))
701 || (td.isLong() && ((lnvalue > 9223372036854775807L) || (lnvalue < -9223372036854775808L)))) {
702 throw new Error("Field node returns "+ln.getType()+", but need "+td+" in "+md);
705 throw new Error("Field node returns "+ln.getType()+", but need "+td+" in "+md);
710 void checkNameNode(Descriptor md, SymbolTable nametable, NameNode nn, TypeDescriptor td) {
711 NameDescriptor nd=nn.getName();
712 if (nd.getBase()!=null) {
714 /* Rewrite NameNode */
715 ExpressionNode en=translateNameDescriptorintoExpression(nd,nn.getNumLine());
716 nn.setExpression(en);
717 checkExpressionNode(md,nametable,en,td);
719 String varname=nd.toString();
720 if(varname.equals("this")) {
722 nn.setVar((VarDescriptor)nametable.get("this"));
725 Descriptor d=(Descriptor)nametable.get(varname);
727 ClassDescriptor cd = null;
728 if((md instanceof MethodDescriptor) && ((MethodDescriptor)md).isStaticBlock()) {
729 // this is a static block, all the accessed fields should be static field
730 cd = ((MethodDescriptor)md).getClassDesc();
731 SymbolTable fieldtbl = cd.getFieldTable();
732 FieldDescriptor fd=(FieldDescriptor)fieldtbl.get(varname);
733 if((fd == null) || (!fd.isStatic())){
734 // no such field in the class, check if this is a class
735 if(varname.equals("this")) {
736 throw new Error("Error: access this obj in a static block");
738 cd=getClass(varname);
740 // this is a class name
744 throw new Error("Name "+varname+" should not be used in static block: "+md);
747 // this is a static field
753 // check if the var is a static field of the class
754 if(md instanceof MethodDescriptor) {
755 cd = ((MethodDescriptor)md).getClassDesc();
756 FieldDescriptor fd = (FieldDescriptor)cd.getFieldTable().get(varname);
757 if((fd != null) && (fd.isStatic())) {
761 if (!typeutil.isSuperorType(td,nn.getType()))
762 throw new Error("Field node returns "+nn.getType()+", but need "+td);
764 } else if(fd != null) {
765 throw new Error("Name "+varname+" should not be used in " + md);
768 cd=getClass(varname);
770 // this is a class name
774 throw new Error("Name "+varname+" undefined in: "+md);
778 if (d instanceof VarDescriptor) {
780 } else if (d instanceof FieldDescriptor) {
781 FieldDescriptor fd=(FieldDescriptor)d;
782 if (fd.getType().iswrapper()) {
783 String id=nd.getIdentifier();
784 NameDescriptor base=nd.getBase();
785 NameNode n=new NameNode(nn.getName());
786 n.setNumLine(nn.getNumLine());
788 n.setVar((VarDescriptor)nametable.get("this")); /* Need a pointer to this */
789 FieldAccessNode fan=new FieldAccessNode(n,"value");
790 fan.setNumLine(n.getNumLine());
791 FieldDescriptor fdval=(FieldDescriptor) fd.getType().getClassDesc().getFieldTable().get("value");
793 nn.setExpression(fan);
796 nn.setVar((VarDescriptor)nametable.get("this")); /* Need a pointer to this */
798 } else if (d instanceof TagVarDescriptor) {
800 } else throw new Error("Wrong type of descriptor");
802 if (!typeutil.isSuperorType(td,nn.getType()))
803 throw new Error("Field node returns "+nn.getType()+", but need "+td);
807 void checkOffsetNode(Descriptor md, SymbolTable nameTable, OffsetNode ofn, TypeDescriptor td) {
808 TypeDescriptor ltd=ofn.td;
809 checkTypeDescriptor(ltd);
811 String fieldname = ofn.fieldname;
812 FieldDescriptor fd=null;
813 if (ltd.isArray()&&fieldname.equals("length")) {
814 fd=FieldDescriptor.arrayLength;
816 fd=(FieldDescriptor) ltd.getClassDesc().getFieldTable().get(fieldname);
820 checkField(ltd.getClassDesc(), fd);
823 throw new Error("Unknown field "+fieldname + " in "+ofn.printNode(1)+" in "+md);
826 if (!typeutil.isSuperorType(td, ofn.getType())) {
827 System.out.println(td);
828 System.out.println(ofn.getType());
829 throw new Error("Type of rside not compatible with type of lside"+ofn.printNode(0));
835 void checkTertiaryNode(Descriptor md, SymbolTable nametable, TertiaryNode tn, TypeDescriptor td) {
836 checkExpressionNode(md, nametable, tn.getCond(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
837 checkExpressionNode(md, nametable, tn.getTrueExpr(), td );
838 checkExpressionNode(md, nametable, tn.getFalseExpr(), td );
841 void checkInstanceOfNode(Descriptor md, SymbolTable nametable, InstanceOfNode tn, TypeDescriptor td) {
842 if (td!=null&&!td.isBoolean())
843 throw new Error("Expecting type "+td+"for instanceof expression");
845 checkTypeDescriptor(tn.getExprType());
846 checkExpressionNode(md, nametable, tn.getExpr(), null);
849 void checkArrayInitializerNode(Descriptor md, SymbolTable nametable, ArrayInitializerNode ain, TypeDescriptor td) {
850 Vector<TypeDescriptor> vec_type = new Vector<TypeDescriptor>();
851 for( int i = 0; i < ain.numVarInitializers(); ++i ) {
852 checkExpressionNode(md, nametable, ain.getVarInitializer(i), td==null?td:td.dereference());
853 vec_type.add(ain.getVarInitializer(i).getType());
855 // descide the type of this variableInitializerNode
856 TypeDescriptor out_type = vec_type.elementAt(0);
857 for(int i = 1; i < vec_type.size(); i++) {
858 TypeDescriptor tmp_type = vec_type.elementAt(i);
859 if(out_type == null) {
860 if(tmp_type != null) {
863 } else if(out_type.isNull()) {
864 if(!tmp_type.isNull() ) {
865 if(!tmp_type.isArray()) {
866 throw new Error("Error: mixed type in var initializer list");
871 } else if(out_type.isArray()) {
872 if(tmp_type.isArray()) {
873 if(tmp_type.getArrayCount() > out_type.getArrayCount()) {
876 } else if((tmp_type != null) && (!tmp_type.isNull())) {
877 throw new Error("Error: mixed type in var initializer list");
879 } else if(out_type.isInt()) {
880 if(!tmp_type.isInt()) {
881 throw new Error("Error: mixed type in var initializer list");
883 } else if(out_type.isString()) {
884 if(!tmp_type.isString()) {
885 throw new Error("Error: mixed type in var initializer list");
889 if(out_type != null) {
890 out_type = out_type.makeArray(state);
891 //out_type.setStatic();
893 ain.setType(out_type);
896 void checkAssignmentNode(Descriptor md, SymbolTable nametable, AssignmentNode an, TypeDescriptor td) {
897 boolean postinc=true;
898 if (an.getOperation().getBaseOp()==null||
899 (an.getOperation().getBaseOp().getOp()!=Operation.POSTINC&&
900 an.getOperation().getBaseOp().getOp()!=Operation.POSTDEC))
903 checkExpressionNode(md, nametable, an.getSrc(),td);
904 //TODO: Need check on validity of operation here
905 if (!((an.getDest() instanceof FieldAccessNode)||
906 (an.getDest() instanceof ArrayAccessNode)||
907 (an.getDest() instanceof NameNode)))
908 throw new Error("Bad lside in "+an.printNode(0));
909 checkExpressionNode(md, nametable, an.getDest(), null);
911 /* We want parameter variables to tasks to be immutable */
912 if (md instanceof TaskDescriptor) {
913 if (an.getDest() instanceof NameNode) {
914 NameNode nn=(NameNode)an.getDest();
915 if (nn.getVar()!=null) {
916 if (((TaskDescriptor)md).getParameterTable().contains(nn.getVar().getSymbol()))
917 throw new Error("Can't modify parameter "+nn.getVar()+ " to task "+td.getSymbol());
922 if (an.getDest().getType().isString()&&an.getOperation().getOp()==AssignOperation.PLUSEQ) {
924 ClassDescriptor stringcl=getClass(TypeUtil.StringClass);
925 TypeDescriptor stringtd=new TypeDescriptor(stringcl);
926 NameDescriptor nd=new NameDescriptor("String");
927 NameDescriptor valuend=new NameDescriptor(nd, "valueOf");
929 if (!(an.getSrc().getType().isString()&&(an.getSrc() instanceof OpNode))) {
930 MethodInvokeNode rightmin=new MethodInvokeNode(valuend);
931 rightmin.setNumLine(an.getSrc().getNumLine());
932 rightmin.addArgument(an.getSrc());
934 checkExpressionNode(md, nametable, an.getSrc(), null);
938 if (!postinc&&!typeutil.isSuperorType(an.getDest().getType(),an.getSrc().getType())) {
939 TypeDescriptor dt = an.getDest().getType();
940 TypeDescriptor st = an.getSrc().getType();
941 if(an.getSrc().kind() == Kind.ArrayInitializerNode) {
942 if(dt.getArrayCount() != st.getArrayCount()) {
943 throw new Error("Type of rside ("+an.getSrc().getType().toPrettyString()+") not compatible with type of lside ("+an.getDest().getType().toPrettyString()+")"+an.printNode(0));
946 dt = dt.dereference();
947 st = st.dereference();
948 } while(dt.isArray());
949 if((st.isByte() || st.isShort() || st.isChar() || st.isInt())
950 && (dt.isByte() || dt.isShort() || dt.isChar() || dt.isInt() || dt.isLong())) {
953 throw new Error("Type of rside ("+an.getSrc().getType().toPrettyString()+") not compatible with type of lside ("+an.getDest().getType().toPrettyString()+")"+an.printNode(0));
957 Long l = an.getSrc().evaluate();
958 if((st.isByte() || st.isShort() || st.isChar() || st.isInt())
960 && (dt.isByte() || dt.isShort() || dt.isChar() || dt.isInt() || dt.isLong())) {
961 long lnvalue = l.longValue();
962 if((dt.isByte() && ((lnvalue > 127) || (lnvalue < -128)))
963 || (dt.isShort() && ((lnvalue > 32767) || (lnvalue < -32768)))
964 || (dt.isChar() && ((lnvalue > 65535) || (lnvalue < 0)))
965 || (dt.isInt() && ((lnvalue > 2147483647) || (lnvalue < -2147483648)))
966 || (dt.isLong() && ((lnvalue > 9223372036854775807L) || (lnvalue < -9223372036854775808L)))) {
967 throw new Error("Type of rside ("+an.getSrc().getType().toPrettyString()+") not compatible with type of lside ("+an.getDest().getType().toPrettyString()+")"+an.printNode(0));
970 throw new Error("Type of rside ("+an.getSrc().getType().toPrettyString()+") not compatible with type of lside ("+an.getDest().getType().toPrettyString()+")"+an.printNode(0));
976 void checkLoopNode(Descriptor md, SymbolTable nametable, LoopNode ln) {
978 if (ln.getType()==LoopNode.WHILELOOP||ln.getType()==LoopNode.DOWHILELOOP) {
979 checkExpressionNode(md, nametable, ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
980 checkBlockNode(md, nametable, ln.getBody());
983 /* Link in the initializer naming environment */
984 BlockNode bn=ln.getInitializer();
985 bn.getVarTable().setParent(nametable);
986 for(int i=0; i<bn.size(); i++) {
987 BlockStatementNode bsn=bn.get(i);
988 checkBlockStatementNode(md, bn.getVarTable(),bsn);
990 //check the condition
991 checkExpressionNode(md, bn.getVarTable(), ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
992 checkBlockNode(md, bn.getVarTable(), ln.getBody());
993 checkBlockNode(md, bn.getVarTable(), ln.getUpdate());
999 void checkCreateObjectNode(Descriptor md, SymbolTable nametable, CreateObjectNode con, TypeDescriptor td) {
1000 TypeDescriptor[] tdarray=new TypeDescriptor[con.numArgs()];
1001 for(int i=0; i<con.numArgs(); i++) {
1002 ExpressionNode en=con.getArg(i);
1003 checkExpressionNode(md,nametable,en,null);
1004 tdarray[i]=en.getType();
1007 TypeDescriptor typetolookin=con.getType();
1008 checkTypeDescriptor(typetolookin);
1010 if (td!=null&&!typeutil.isSuperorType(td, typetolookin))
1011 throw new Error(typetolookin + " isn't a "+td);
1013 /* Check Array Initializers */
1014 if((con.getArrayInitializer() != null)) {
1015 checkArrayInitializerNode(md, nametable, con.getArrayInitializer(), td);
1018 /* Check flag effects */
1019 if (con.getFlagEffects()!=null) {
1020 FlagEffects fe=con.getFlagEffects();
1021 ClassDescriptor cd=typetolookin.getClassDesc();
1023 for(int j=0; j<fe.numEffects(); j++) {
1024 FlagEffect flag=fe.getEffect(j);
1025 String name=flag.getName();
1026 FlagDescriptor flag_d=(FlagDescriptor)cd.getFlagTable().get(name);
1027 //Make sure the flag is declared
1029 throw new Error("Flag descriptor "+name+" undefined in class: "+cd.getSymbol());
1030 if (flag_d.getExternal())
1031 throw new Error("Attempting to modify external flag: "+name);
1032 flag.setFlag(flag_d);
1034 for(int j=0; j<fe.numTagEffects(); j++) {
1035 TagEffect tag=fe.getTagEffect(j);
1036 String name=tag.getName();
1038 Descriptor d=(Descriptor)nametable.get(name);
1040 throw new Error("Tag descriptor "+name+" undeclared");
1041 else if (!(d instanceof TagVarDescriptor))
1042 throw new Error(name+" is not a tag descriptor");
1043 tag.setTag((TagVarDescriptor)d);
1047 if ((!typetolookin.isClass())&&(!typetolookin.isArray()))
1048 throw new Error("Can't allocate primitive type:"+con.printNode(0));
1050 if (!typetolookin.isArray()) {
1051 //Array's don't need constructor calls
1052 ClassDescriptor classtolookin=typetolookin.getClassDesc();
1054 Set methoddescriptorset=classtolookin.getMethodTable().getSet(typetolookin.getSymbol());
1055 MethodDescriptor bestmd=null;
1057 for(Iterator methodit=methoddescriptorset.iterator(); methodit.hasNext();) {
1058 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
1059 /* Need correct number of parameters */
1060 if (con.numArgs()!=currmd.numParameters())
1062 for(int i=0; i<con.numArgs(); i++) {
1063 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
1064 continue NextMethod;
1066 /* Local allocations can't call global allocator */
1067 if (!con.isGlobal()&&currmd.isGlobal())
1070 /* Method okay so far */
1074 if (typeutil.isMoreSpecific(currmd,bestmd)) {
1076 } else if (con.isGlobal()&&match(currmd, bestmd)) {
1077 if (currmd.isGlobal()&&!bestmd.isGlobal())
1079 else if (currmd.isGlobal()&&bestmd.isGlobal())
1081 } else if (!typeutil.isMoreSpecific(bestmd, currmd)) {
1082 throw new Error("No method is most specific:"+bestmd+" and "+currmd);
1085 /* Is this more specific than bestmd */
1089 throw new Error("No method found for "+con.printNode(0)+" in "+md);
1090 con.setConstructor(bestmd);
1095 /** Check to see if md1 is the same specificity as md2.*/
1097 boolean match(MethodDescriptor md1, MethodDescriptor md2) {
1098 /* Checks if md1 is more specific than md2 */
1099 if (md1.numParameters()!=md2.numParameters())
1101 for(int i=0; i<md1.numParameters(); i++) {
1102 if (!md2.getParamType(i).equals(md1.getParamType(i)))
1105 if (!md2.getReturnType().equals(md1.getReturnType()))
1108 if (!md2.getClassDesc().equals(md1.getClassDesc()))
1116 ExpressionNode translateNameDescriptorintoExpression(NameDescriptor nd, int numLine) {
1117 String id=nd.getIdentifier();
1118 NameDescriptor base=nd.getBase();
1120 NameNode nn=new NameNode(nd);
1121 nn.setNumLine(numLine);
1124 FieldAccessNode fan=new FieldAccessNode(translateNameDescriptorintoExpression(base,numLine),id);
1125 fan.setNumLine(numLine);
1131 void checkMethodInvokeNode(Descriptor md, SymbolTable nametable, MethodInvokeNode min, TypeDescriptor td) {
1132 /*Typecheck subexpressions
1133 and get types for expressions*/
1135 boolean isstatic = false;
1136 if((md instanceof MethodDescriptor) && ((MethodDescriptor)md).isStatic()) {
1139 TypeDescriptor[] tdarray=new TypeDescriptor[min.numArgs()];
1140 for(int i=0; i<min.numArgs(); i++) {
1141 ExpressionNode en=min.getArg(i);
1142 checkExpressionNode(md,nametable,en,null);
1143 tdarray[i]=en.getType();
1144 if(en.getType().isClass() && en.getType().getClassDesc().isEnum()) {
1145 tdarray[i] = new TypeDescriptor(TypeDescriptor.INT);
1148 TypeDescriptor typetolookin=null;
1149 if (min.getExpression()!=null) {
1150 checkExpressionNode(md,nametable,min.getExpression(),null);
1151 typetolookin=min.getExpression().getType();
1152 //if (typetolookin==null)
1153 //throw new Error(md+" has null return type");
1155 } else if (min.getBaseName()!=null) {
1156 String rootname=min.getBaseName().getRoot();
1157 if (rootname.equals("super")) {
1158 ClassDescriptor supercd=((MethodDescriptor)md).getClassDesc().getSuperDesc();
1159 typetolookin=new TypeDescriptor(supercd);
1161 } else if (rootname.equals("this")) {
1163 throw new Error("use this object in static method md = "+ md.toString());
1165 ClassDescriptor cd=((MethodDescriptor)md).getClassDesc();
1166 typetolookin=new TypeDescriptor(cd);
1167 } else if (nametable.get(rootname)!=null) {
1168 //we have an expression
1169 min.setExpression(translateNameDescriptorintoExpression(min.getBaseName(),min.getNumLine()));
1170 checkExpressionNode(md, nametable, min.getExpression(), null);
1171 typetolookin=min.getExpression().getType();
1173 if(!min.getBaseName().getSymbol().equals("System.out")) {
1174 ExpressionNode nn = translateNameDescriptorintoExpression(min.getBaseName(),min.getNumLine());
1175 checkExpressionNode(md, nametable, nn, null);
1176 typetolookin = nn.getType();
1177 if(!((nn.kind()== Kind.NameNode) && (((NameNode)nn).getField() == null)
1178 && (((NameNode)nn).getVar() == null) && (((NameNode)nn).getExpression() == null))) {
1179 // this is not a pure class name, need to add to
1180 min.setExpression(nn);
1184 ClassDescriptor cd = null;
1185 //if (min.getBaseName().getSymbol().equals("System.out"))
1186 cd=getClass("System");
1188 cd=getClass(min.getBaseName().getSymbol());
1191 throw new Error("md = "+ md.toString()+ " "+min.getBaseName()+" undefined");
1192 typetolookin=new TypeDescriptor(cd);
1195 } else if ((md instanceof MethodDescriptor)&&min.getMethodName().equals("super")) {
1196 ClassDescriptor supercd=((MethodDescriptor)md).getClassDesc().getSuperDesc();
1197 min.methodid=supercd.getSymbol();
1198 typetolookin=new TypeDescriptor(supercd);
1199 } else if (md instanceof MethodDescriptor) {
1200 typetolookin=new TypeDescriptor(((MethodDescriptor)md).getClassDesc());
1202 /* If this a task descriptor we throw an error at this point */
1203 throw new Error("Unknown method call to "+min.getMethodName()+"in task"+md.getSymbol());
1205 if (!typetolookin.isClass())
1206 throw new Error("Error with method call to "+min.getMethodName());
1207 ClassDescriptor classtolookin=typetolookin.getClassDesc();
1208 //System.out.println("Method name="+min.getMethodName());
1210 Set methoddescriptorset=classtolookin.getMethodTable().getSet(min.getMethodName());
1211 MethodDescriptor bestmd=null;
1213 for(Iterator methodit=methoddescriptorset.iterator(); methodit.hasNext();) {
1214 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
1215 /* Need correct number of parameters */
1216 if (min.numArgs()!=currmd.numParameters())
1218 for(int i=0; i<min.numArgs(); i++) {
1219 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
1220 if(((!tdarray[i].isArray() &&( tdarray[i].isInt() || tdarray[i].isLong()))
1221 && currmd.getParamType(i).isClass() && currmd.getParamType(i).getClassDesc().getSymbol().equals("Object"))) {
1222 // primitive parameters vs object
1224 continue NextMethod;
1227 /* Method okay so far */
1231 if (typeutil.isMoreSpecific(currmd,bestmd)) {
1233 } else if (!typeutil.isMoreSpecific(bestmd, currmd))
1234 throw new Error("No method is most specific:"+bestmd+" and "+currmd);
1236 /* Is this more specific than bestmd */
1240 throw new Error("No method found for :"+min.printNode(0)+" in class: " + classtolookin+" in "+md);
1241 min.setMethod(bestmd);
1243 if ((td!=null)&&(min.getType()!=null)&&!typeutil.isSuperorType(td, min.getType()))
1244 throw new Error(min.getType()+ " is not equal to or a subclass of "+td);
1245 /* Check whether we need to set this parameter to implied this */
1246 if (! isstatic && !bestmd.isStatic()) {
1247 if (min.getExpression()==null) {
1248 ExpressionNode en=new NameNode(new NameDescriptor("this"));
1249 min.setExpression(en);
1250 checkExpressionNode(md, nametable, min.getExpression(), null);
1254 /* Check if we need to wrap primitive paratmeters to objects */
1255 for(int i=0; i<min.numArgs(); i++) {
1256 if(!tdarray[i].isArray() && (tdarray[i].isInt() || tdarray[i].isLong())
1257 && min.getMethod().getParamType(i).isClass() && min.getMethod().getParamType(i).getClassDesc().getSymbol().equals("Object")) {
1258 // Shall wrap this primitive parameter as a object
1259 ExpressionNode exp = min.getArg(i);
1260 TypeDescriptor ptd = null;
1261 NameDescriptor nd=null;
1262 if(exp.getType().isInt()) {
1263 nd = new NameDescriptor("Integer");
1264 ptd = state.getTypeDescriptor(nd);
1265 } else if(exp.getType().isLong()) {
1266 nd = new NameDescriptor("Long");
1267 ptd = state.getTypeDescriptor(nd);
1269 boolean isglobal = false;
1270 String disjointId = null;
1271 CreateObjectNode con=new CreateObjectNode(ptd, isglobal, disjointId);
1272 con.addArgument(exp);
1273 checkExpressionNode(md, nametable, con, null);
1274 min.setArgument(con, i);
1280 void checkOpNode(Descriptor md, SymbolTable nametable, OpNode on, TypeDescriptor td) {
1281 checkExpressionNode(md, nametable, on.getLeft(), null);
1282 if (on.getRight()!=null)
1283 checkExpressionNode(md, nametable, on.getRight(), null);
1284 TypeDescriptor ltd=on.getLeft().getType();
1285 TypeDescriptor rtd=on.getRight()!=null ? on.getRight().getType() : null;
1286 TypeDescriptor lefttype=null;
1287 TypeDescriptor righttype=null;
1288 Operation op=on.getOp();
1290 switch(op.getOp()) {
1291 case Operation.LOGIC_OR:
1292 case Operation.LOGIC_AND:
1293 if (!(rtd.isBoolean()))
1295 on.setRightType(rtd);
1297 case Operation.LOGIC_NOT:
1298 if (!(ltd.isBoolean()))
1301 on.setLeftType(ltd);
1303 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1306 case Operation.COMP:
1307 // 5.6.2 Binary Numeric Promotion
1308 //TODO unboxing of reference objects
1311 else if (ltd.isFloat())
1313 else if (ltd.isLong())
1314 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1316 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1317 on.setLeftType(lefttype);
1318 on.setType(lefttype);
1321 case Operation.BIT_OR:
1322 case Operation.BIT_XOR:
1323 case Operation.BIT_AND:
1324 // 5.6.2 Binary Numeric Promotion
1325 //TODO unboxing of reference objects
1326 if (ltd.isDouble()||rtd.isDouble())
1328 else if (ltd.isFloat()||rtd.isFloat())
1330 else if (ltd.isLong()||rtd.isLong())
1331 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1332 // 090205 hack for boolean
1333 else if (ltd.isBoolean()||rtd.isBoolean())
1334 lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
1336 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1339 on.setLeftType(lefttype);
1340 on.setRightType(righttype);
1341 on.setType(lefttype);
1344 case Operation.ISAVAILABLE:
1345 if (!(ltd.isPtr())) {
1346 throw new Error("Can't use isavailable on non-pointers/non-parameters.");
1349 on.setLeftType(lefttype);
1350 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1353 case Operation.EQUAL:
1354 case Operation.NOTEQUAL:
1355 // 5.6.2 Binary Numeric Promotion
1356 //TODO unboxing of reference objects
1357 if (ltd.isBoolean()||rtd.isBoolean()) {
1358 if (!(ltd.isBoolean()&&rtd.isBoolean()))
1360 righttype=lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
1361 } else if (ltd.isPtr()||rtd.isPtr()) {
1362 if (!(ltd.isPtr()&&rtd.isPtr())) {
1369 } else if (ltd.isDouble()||rtd.isDouble())
1370 righttype=lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
1371 else if (ltd.isFloat()||rtd.isFloat())
1372 righttype=lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
1373 else if (ltd.isLong()||rtd.isLong())
1374 righttype=lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1376 righttype=lefttype=new TypeDescriptor(TypeDescriptor.INT);
1378 on.setLeftType(lefttype);
1379 on.setRightType(righttype);
1380 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1389 // 5.6.2 Binary Numeric Promotion
1390 //TODO unboxing of reference objects
1391 if (!ltd.isNumber()||!rtd.isNumber()) {
1392 if (!ltd.isNumber())
1393 throw new Error("Leftside is not number"+on.printNode(0)+"type="+ltd.toPrettyString());
1394 if (!rtd.isNumber())
1395 throw new Error("Rightside is not number"+on.printNode(0));
1398 if (ltd.isDouble()||rtd.isDouble())
1399 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
1400 else if (ltd.isFloat()||rtd.isFloat())
1401 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
1402 else if (ltd.isLong()||rtd.isLong())
1403 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1405 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1407 on.setLeftType(lefttype);
1408 on.setRightType(righttype);
1409 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1413 if (ltd.isString()||rtd.isString()) {
1414 ClassDescriptor stringcl=getClass(TypeUtil.StringClass);
1415 TypeDescriptor stringtd=new TypeDescriptor(stringcl);
1416 NameDescriptor nd=new NameDescriptor("String");
1417 NameDescriptor valuend=new NameDescriptor(nd, "valueOf");
1418 if (!(ltd.isString()&&(on.getLeft() instanceof OpNode))) {
1419 MethodInvokeNode leftmin=new MethodInvokeNode(valuend);
1420 leftmin.setNumLine(on.getLeft().getNumLine());
1421 leftmin.addArgument(on.getLeft());
1423 checkExpressionNode(md, nametable, on.getLeft(), null);
1426 if (!(rtd.isString()&&(on.getRight() instanceof OpNode))) {
1427 MethodInvokeNode rightmin=new MethodInvokeNode(valuend);
1428 rightmin.setNumLine(on.getRight().getNumLine());
1429 rightmin.addArgument(on.getRight());
1431 checkExpressionNode(md, nametable, on.getRight(), null);
1434 on.setLeftType(stringtd);
1435 on.setRightType(stringtd);
1436 on.setType(stringtd);
1441 case Operation.MULT:
1444 // 5.6.2 Binary Numeric Promotion
1445 //TODO unboxing of reference objects
1446 if (ltd.isArray()||rtd.isArray()||!ltd.isNumber()||!rtd.isNumber())
1447 throw new Error("Error in "+on.printNode(0));
1449 if (ltd.isDouble()||rtd.isDouble())
1450 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
1451 else if (ltd.isFloat()||rtd.isFloat())
1452 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
1453 else if (ltd.isLong()||rtd.isLong())
1454 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1456 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1458 on.setLeftType(lefttype);
1459 on.setRightType(righttype);
1460 on.setType(lefttype);
1463 case Operation.LEFTSHIFT:
1464 case Operation.RIGHTSHIFT:
1465 case Operation.URIGHTSHIFT:
1466 if (!rtd.isIntegerType())
1468 //5.6.1 Unary Numeric Promotion
1469 if (rtd.isByte()||rtd.isShort()||rtd.isInt())
1470 righttype=new TypeDescriptor(TypeDescriptor.INT);
1474 on.setRightType(righttype);
1475 if (!ltd.isIntegerType())
1478 case Operation.UNARYPLUS:
1479 case Operation.UNARYMINUS:
1480 /* case Operation.POSTINC:
1481 case Operation.POSTDEC:
1482 case Operation.PREINC:
1483 case Operation.PREDEC:*/
1484 if (!ltd.isNumber())
1486 //5.6.1 Unary Numeric Promotion
1487 if (ltd.isByte()||ltd.isShort()||ltd.isInt())
1488 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1491 on.setLeftType(lefttype);
1492 on.setType(lefttype);
1496 throw new Error(op.toString());
1500 if (!typeutil.isSuperorType(td, on.getType())) {
1501 System.out.println(td);
1502 System.out.println(on.getType());
1503 throw new Error("Type of rside not compatible with type of lside"+on.printNode(0));