7 public class SemanticCheck {
14 //This is the class mappings for a particular file based
15 //on the import names. Maps class to canonical class name.
16 Hashtable classnameMappings;
19 public SemanticCheck(State state, TypeUtil tu) {
22 this.loopstack=new Stack();
23 this.toanalyze=new HashSet();
24 this.completed=new HashSet();
27 public ClassDescriptor getClass(String classname) {
28 ClassDescriptor cd=typeutil.getClass(classname, toanalyze);
33 private void checkClass(ClassDescriptor cd) {
34 if (!completed.contains(cd)) {
37 //Set superclass link up
38 if (cd.getSuper()!=null) {
39 cd.setSuper(getClass(cd.getSuper()));
40 if(cd.getSuperDesc().isInterface()) {
41 throw new Error("Error! Class " + cd.getSymbol() + " extends interface " + cd.getSuper());
43 // Link together Field, Method, and Flag tables so classes
44 // inherit these from their superclasses
45 cd.getFieldTable().setParent(cd.getSuperDesc().getFieldTable());
46 cd.getMethodTable().setParent(cd.getSuperDesc().getMethodTable());
47 cd.getFlagTable().setParent(cd.getSuperDesc().getFlagTable());
49 // Link together Field, Method tables do classes inherit these from
50 // their ancestor interfaces
51 Vector<String> sifv = cd.getSuperInterface();
52 for(int i = 0; i < sifv.size(); i++) {
53 ClassDescriptor superif = getClass(sifv.elementAt(i));
54 if(!superif.isInterface()) {
55 throw new Error("Error! Class " + cd.getSymbol() + " implements non-interface " + superif.getSymbol());
57 cd.addSuperInterfaces(superif);
58 cd.getFieldTable().addParentIF(superif.getFieldTable());
59 cd.getMethodTable().addParentIF(superif.getMethodTable());
62 /* Check to see that fields are well typed */
63 for(Iterator field_it=cd.getFields(); field_it.hasNext();) {
64 FieldDescriptor fd=(FieldDescriptor)field_it.next();
68 for(Iterator method_it=cd.getMethods(); method_it.hasNext();) {
69 MethodDescriptor md=(MethodDescriptor)method_it.next();
71 if(md.isDefaultConstructor() && (cd.getSuperDesc() != null)) {
72 // add the construction of it super class, can only be super()
73 NameDescriptor nd=new NameDescriptor("super");
74 MethodInvokeNode min=new MethodInvokeNode(nd);
75 BlockExpressionNode ben=new BlockExpressionNode(min);
76 BlockNode bn = state.getMethodBody(md);
77 bn.addFirstBlockStatement(ben);
83 public void semanticCheck() {
84 SymbolTable classtable = state.getClassSymbolTable();
85 toanalyze.addAll(classtable.getValueSet());
86 toanalyze.addAll(state.getTaskSymbolTable().getValueSet());
89 while (!toanalyze.isEmpty()) {
90 Object obj = toanalyze.iterator().next();
91 if (obj instanceof TaskDescriptor) {
92 toanalyze.remove(obj);
93 TaskDescriptor td = (TaskDescriptor) obj;
97 System.out.println("Error in " + td);
101 ClassDescriptor cd = (ClassDescriptor) obj;
102 toanalyze.remove(cd);
103 //set the class mappings based on imports.
104 classnameMappings = cd.getSingleImportMappings();
106 // need to initialize typeutil object here...only place we can
107 // get class descriptors without first calling getclass
108 getClass(cd.getSymbol());
109 for (Iterator method_it = cd.getMethods(); method_it.hasNext();) {
110 MethodDescriptor md = (MethodDescriptor) method_it.next();
112 checkMethodBody(cd, md);
114 System.out.println("Error in " + md);
122 public void checkTypeDescriptor(TypeDescriptor td) {
123 if (td.isPrimitive())
125 else if (td.isClass()) {
126 String name=td.toString();
127 int index = name.lastIndexOf('.');
129 name = name.substring(index+1);
131 ClassDescriptor field_cd=getClass(getFullName(name));
133 throw new Error("Undefined class "+name);
134 td.setClassDescriptor(field_cd);
136 } else if (td.isTag())
142 public void checkField(ClassDescriptor cd, FieldDescriptor fd) {
143 checkTypeDescriptor(fd.getType());
146 public void checkConstraintCheck(TaskDescriptor td, SymbolTable nametable, Vector ccs) {
148 return; /* No constraint checks to check */
149 for(int i=0; i<ccs.size(); i++) {
150 ConstraintCheck cc=(ConstraintCheck) ccs.get(i);
152 for(int j=0; j<cc.numArgs(); j++) {
153 ExpressionNode en=cc.getArg(j);
154 checkExpressionNode(td,nametable,en,null);
159 public void checkFlagEffects(TaskDescriptor td, Vector vfe, SymbolTable nametable) {
161 return; /* No flag effects to check */
162 for(int i=0; i<vfe.size(); i++) {
163 FlagEffects fe=(FlagEffects) vfe.get(i);
164 String varname=fe.getName();
165 //Make sure the variable is declared as a parameter to the task
166 VarDescriptor vd=(VarDescriptor)td.getParameterTable().get(varname);
168 throw new Error("Parameter "+varname+" in Flag Effects not declared in "+td);
171 //Make sure it correspods to a class
172 TypeDescriptor type_d=vd.getType();
173 if (!type_d.isClass())
174 throw new Error("Cannot have non-object argument for flag_effect");
176 ClassDescriptor cd=type_d.getClassDesc();
177 for(int j=0; j<fe.numEffects(); j++) {
178 FlagEffect flag=fe.getEffect(j);
179 String name=flag.getName();
180 FlagDescriptor flag_d=(FlagDescriptor)cd.getFlagTable().get(name);
181 //Make sure the flag is declared
183 throw new Error("Flag descriptor "+name+" undefined in class: "+cd.getSymbol());
184 if (flag_d.getExternal())
185 throw new Error("Attempting to modify external flag: "+name);
186 flag.setFlag(flag_d);
188 for(int j=0; j<fe.numTagEffects(); j++) {
189 TagEffect tag=fe.getTagEffect(j);
190 String name=tag.getName();
192 Descriptor d=(Descriptor)nametable.get(name);
194 throw new Error("Tag descriptor "+name+" undeclared");
195 else if (!(d instanceof TagVarDescriptor))
196 throw new Error(name+" is not a tag descriptor");
197 tag.setTag((TagVarDescriptor)d);
202 public void checkTask(TaskDescriptor td) {
203 for(int i=0; i<td.numParameters(); i++) {
204 /* Check that parameter is well typed */
205 TypeDescriptor param_type=td.getParamType(i);
206 checkTypeDescriptor(param_type);
208 /* Check the parameter's flag expression is well formed */
209 FlagExpressionNode fen=td.getFlag(td.getParameter(i));
210 if (!param_type.isClass())
211 throw new Error("Cannot have non-object argument to a task");
212 ClassDescriptor cd=param_type.getClassDesc();
214 checkFlagExpressionNode(cd, fen);
217 checkFlagEffects(td, td.getFlagEffects(),td.getParameterTable());
218 /* Check that the task code is valid */
219 BlockNode bn=state.getMethodBody(td);
220 checkBlockNode(td, td.getParameterTable(),bn);
223 public void checkFlagExpressionNode(ClassDescriptor cd, FlagExpressionNode fen) {
225 case Kind.FlagOpNode:
227 FlagOpNode fon=(FlagOpNode)fen;
228 checkFlagExpressionNode(cd, fon.getLeft());
229 if (fon.getRight()!=null)
230 checkFlagExpressionNode(cd, fon.getRight());
236 FlagNode fn=(FlagNode)fen;
237 String name=fn.getFlagName();
238 FlagDescriptor fd=(FlagDescriptor)cd.getFlagTable().get(name);
240 throw new Error("Undeclared flag: "+name);
246 throw new Error("Unrecognized FlagExpressionNode");
250 public void checkMethod(ClassDescriptor cd, MethodDescriptor md) {
251 /* Check for abstract methods */
252 if(md.isAbstract()) {
253 if(!cd.isAbstract() && !cd.isInterface()) {
254 throw new Error("Error! The non-abstract Class " + cd.getSymbol() + " contains an abstract method " + md.getSymbol());
258 /* Check return type */
259 if (!md.isConstructor() && !md.isStaticBlock())
260 if (!md.getReturnType().isVoid())
261 checkTypeDescriptor(md.getReturnType());
263 for(int i=0; i<md.numParameters(); i++) {
264 TypeDescriptor param_type=md.getParamType(i);
265 checkTypeDescriptor(param_type);
267 /* Link the naming environments */
268 if (!md.isStatic()) /* Fields aren't accessible directly in a static method, so don't link in this table */
269 md.getParameterTable().setParent(cd.getFieldTable());
271 if (!md.isStatic()) {
272 VarDescriptor thisvd=new VarDescriptor(new TypeDescriptor(cd),"this");
277 public void checkMethodBody(ClassDescriptor cd, MethodDescriptor md) {
278 ClassDescriptor superdesc=cd.getSuperDesc();
279 if (superdesc!=null) {
280 Set possiblematches=superdesc.getMethodTable().getSet(md.getSymbol());
281 for(Iterator methodit=possiblematches.iterator(); methodit.hasNext();) {
282 MethodDescriptor matchmd=(MethodDescriptor)methodit.next();
283 if (md.matches(matchmd)) {
284 if (matchmd.getModifiers().isFinal()) {
285 throw new Error("Try to override final method in method:"+md+" declared in "+cd);
290 BlockNode bn=state.getMethodBody(md);
291 checkBlockNode(md, md.getParameterTable(),bn);
294 public void checkBlockNode(Descriptor md, SymbolTable nametable, BlockNode bn) {
295 /* Link in the naming environment */
296 bn.getVarTable().setParent(nametable);
297 for(int i=0; i<bn.size(); i++) {
298 BlockStatementNode bsn=bn.get(i);
299 checkBlockStatementNode(md, bn.getVarTable(),bsn);
303 public void checkBlockStatementNode(Descriptor md, SymbolTable nametable, BlockStatementNode bsn) {
305 case Kind.BlockExpressionNode:
306 checkBlockExpressionNode(md, nametable,(BlockExpressionNode)bsn);
309 case Kind.DeclarationNode:
310 checkDeclarationNode(md, nametable, (DeclarationNode)bsn);
313 case Kind.TagDeclarationNode:
314 checkTagDeclarationNode(md, nametable, (TagDeclarationNode)bsn);
317 case Kind.IfStatementNode:
318 checkIfStatementNode(md, nametable, (IfStatementNode)bsn);
321 case Kind.SwitchStatementNode:
322 checkSwitchStatementNode(md, nametable, (SwitchStatementNode)bsn);
326 checkLoopNode(md, nametable, (LoopNode)bsn);
329 case Kind.ReturnNode:
330 checkReturnNode(md, nametable, (ReturnNode)bsn);
333 case Kind.TaskExitNode:
334 checkTaskExitNode(md, nametable, (TaskExitNode)bsn);
337 case Kind.SubBlockNode:
338 checkSubBlockNode(md, nametable, (SubBlockNode)bsn);
341 case Kind.AtomicNode:
342 checkAtomicNode(md, nametable, (AtomicNode)bsn);
345 case Kind.SynchronizedNode:
346 checkSynchronizedNode(md, nametable, (SynchronizedNode)bsn);
349 case Kind.ContinueBreakNode:
350 checkContinueBreakNode(md, nametable, (ContinueBreakNode) bsn);
354 case Kind.GenReachNode:
355 // do nothing, no semantic check for SESEs
362 void checkBlockExpressionNode(Descriptor md, SymbolTable nametable, BlockExpressionNode ben) {
363 checkExpressionNode(md, nametable, ben.getExpression(), null);
366 void checkDeclarationNode(Descriptor md, SymbolTable nametable, DeclarationNode dn) {
367 VarDescriptor vd=dn.getVarDescriptor();
368 checkTypeDescriptor(vd.getType());
369 Descriptor d=nametable.get(vd.getSymbol());
371 (d instanceof FieldDescriptor)) {
374 throw new Error(vd.getSymbol()+" in "+md+" defined a second time");
375 if (dn.getExpression()!=null)
376 checkExpressionNode(md, nametable, dn.getExpression(), vd.getType());
379 void checkTagDeclarationNode(Descriptor md, SymbolTable nametable, TagDeclarationNode dn) {
380 TagVarDescriptor vd=dn.getTagVarDescriptor();
381 Descriptor d=nametable.get(vd.getSymbol());
383 (d instanceof FieldDescriptor)) {
386 throw new Error(vd.getSymbol()+" defined a second time");
389 void checkSubBlockNode(Descriptor md, SymbolTable nametable, SubBlockNode sbn) {
390 checkBlockNode(md, nametable, sbn.getBlockNode());
393 void checkAtomicNode(Descriptor md, SymbolTable nametable, AtomicNode sbn) {
394 checkBlockNode(md, nametable, sbn.getBlockNode());
397 void checkSynchronizedNode(Descriptor md, SymbolTable nametable, SynchronizedNode sbn) {
398 checkBlockNode(md, nametable, sbn.getBlockNode());
399 //todo this could be Object
400 checkExpressionNode(md, nametable, sbn.getExpr(), null);
403 void checkContinueBreakNode(Descriptor md, SymbolTable nametable, ContinueBreakNode cbn) {
404 if (loopstack.empty())
405 throw new Error("continue/break outside of loop");
406 Object o = loopstack.peek();
407 if(o instanceof LoopNode) {
408 LoopNode ln=(LoopNode)o;
413 void checkReturnNode(Descriptor d, SymbolTable nametable, ReturnNode rn) {
414 if (d instanceof TaskDescriptor)
415 throw new Error("Illegal return appears in Task: "+d.getSymbol());
416 MethodDescriptor md=(MethodDescriptor)d;
417 if (rn.getReturnExpression()!=null)
418 if (md.getReturnType()==null)
419 throw new Error("Constructor can't return something.");
420 else if (md.getReturnType().isVoid())
421 throw new Error(md+" is void");
423 checkExpressionNode(md, nametable, rn.getReturnExpression(), md.getReturnType());
425 if (md.getReturnType()!=null&&!md.getReturnType().isVoid())
426 throw new Error("Need to return something for "+md);
429 void checkTaskExitNode(Descriptor md, SymbolTable nametable, TaskExitNode ten) {
430 if (md instanceof MethodDescriptor)
431 throw new Error("Illegal taskexit appears in Method: "+md.getSymbol());
432 checkFlagEffects((TaskDescriptor)md, ten.getFlagEffects(),nametable);
433 checkConstraintCheck((TaskDescriptor) md, nametable, ten.getChecks());
436 void checkIfStatementNode(Descriptor md, SymbolTable nametable, IfStatementNode isn) {
437 checkExpressionNode(md, nametable, isn.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
438 checkBlockNode(md, nametable, isn.getTrueBlock());
439 if (isn.getFalseBlock()!=null)
440 checkBlockNode(md, nametable, isn.getFalseBlock());
443 void checkSwitchStatementNode(Descriptor md, SymbolTable nametable, SwitchStatementNode ssn) {
444 checkExpressionNode(md, nametable, ssn.getCondition(), new TypeDescriptor(TypeDescriptor.INT));
446 BlockNode sbn = ssn.getSwitchBody();
447 boolean hasdefault = false;
448 for(int i = 0; i < sbn.size(); i++) {
449 boolean containdefault = checkSwitchBlockNode(md, nametable, (SwitchBlockNode)sbn.get(i));
450 if(hasdefault && containdefault) {
451 throw new Error("Error: duplicate default branch in switch-case statement in Method: " + md.getSymbol());
453 hasdefault = containdefault;
457 boolean checkSwitchBlockNode(Descriptor md, SymbolTable nametable, SwitchBlockNode sbn) {
458 Vector<SwitchLabelNode> slnv = sbn.getSwitchConditions();
460 for(int i = 0; i < slnv.size(); i++) {
461 if(slnv.elementAt(i).isdefault) {
464 checkConstantExpressionNode(md, nametable, slnv.elementAt(i).getCondition(), new TypeDescriptor(TypeDescriptor.INT));
468 throw new Error("Error: duplicate default branch in switch-case statement in Method: " + md.getSymbol());
471 checkBlockNode(md, nametable, sbn.getSwitchBlockStatement());
473 return (defaultb > 0);
477 void checkConstantExpressionNode(Descriptor md, SymbolTable nametable, ExpressionNode en, TypeDescriptor td) {
479 case Kind.FieldAccessNode:
480 checkFieldAccessNode(md,nametable,(FieldAccessNode)en,td);
483 case Kind.LiteralNode:
484 checkLiteralNode(md,nametable,(LiteralNode)en,td);
488 checkNameNode(md,nametable,(NameNode)en,td);
492 checkOpNode(md, nametable, (OpNode)en, td);
498 void checkExpressionNode(Descriptor md, SymbolTable nametable, ExpressionNode en, TypeDescriptor td) {
500 case Kind.AssignmentNode:
501 checkAssignmentNode(md,nametable,(AssignmentNode)en,td);
505 checkCastNode(md,nametable,(CastNode)en,td);
508 case Kind.CreateObjectNode:
509 checkCreateObjectNode(md,nametable,(CreateObjectNode)en,td);
512 case Kind.FieldAccessNode:
513 checkFieldAccessNode(md,nametable,(FieldAccessNode)en,td);
516 case Kind.ArrayAccessNode:
517 checkArrayAccessNode(md,nametable,(ArrayAccessNode)en,td);
520 case Kind.LiteralNode:
521 checkLiteralNode(md,nametable,(LiteralNode)en,td);
524 case Kind.MethodInvokeNode:
525 checkMethodInvokeNode(md,nametable,(MethodInvokeNode)en,td);
529 checkNameNode(md,nametable,(NameNode)en,td);
533 checkOpNode(md,nametable,(OpNode)en,td);
536 case Kind.OffsetNode:
537 checkOffsetNode(md, nametable, (OffsetNode)en, td);
540 case Kind.TertiaryNode:
541 checkTertiaryNode(md, nametable, (TertiaryNode)en, td);
544 case Kind.InstanceOfNode:
545 checkInstanceOfNode(md, nametable, (InstanceOfNode) en, td);
548 case Kind.ArrayInitializerNode:
549 checkArrayInitializerNode(md, nametable, (ArrayInitializerNode) en, td);
552 case Kind.ClassTypeNode:
553 checkClassTypeNode(md, nametable, (ClassTypeNode) en, td);
559 void checkClassTypeNode(Descriptor md, SymbolTable nametable, ClassTypeNode tn, TypeDescriptor td) {
560 checkTypeDescriptor(tn.getType());
563 void checkCastNode(Descriptor md, SymbolTable nametable, CastNode cn, TypeDescriptor td) {
564 /* Get type descriptor */
565 if (cn.getType()==null) {
566 NameDescriptor typenamed=cn.getTypeName().getName();
567 String typename=getFullName(typenamed.toString());
568 TypeDescriptor ntd=new TypeDescriptor(getClass(typename));
572 /* Check the type descriptor */
573 TypeDescriptor cast_type=cn.getType();
574 checkTypeDescriptor(cast_type);
578 if (!typeutil.isSuperorType(td,cast_type))
579 throw new Error("Cast node returns "+cast_type+", but need "+td);
582 ExpressionNode en=cn.getExpression();
583 checkExpressionNode(md, nametable, en, null);
584 TypeDescriptor etd=en.getType();
585 if (typeutil.isSuperorType(cast_type,etd)) /* Cast trivially succeeds */
588 if (typeutil.isSuperorType(etd,cast_type)) /* Cast may succeed */
590 if (typeutil.isCastable(etd, cast_type))
593 /* Different branches */
594 /* TODO: change if add interfaces */
595 throw new Error("Cast will always fail\n"+cn.printNode(0));
598 void checkFieldAccessNode(Descriptor md, SymbolTable nametable, FieldAccessNode fan, TypeDescriptor td) {
599 ExpressionNode left=fan.getExpression();
600 checkExpressionNode(md,nametable,left,null);
601 TypeDescriptor ltd=left.getType();
602 String fieldname=fan.getFieldName();
604 FieldDescriptor fd=null;
605 if (ltd.isArray()&&fieldname.equals("length"))
606 fd=FieldDescriptor.arrayLength;
608 fd=(FieldDescriptor) ltd.getClassDesc().getFieldTable().get(fieldname);
610 if(ltd.isClassNameRef()) {
611 // the field access is using a class name directly
612 if(ltd.getClassDesc().isEnum()) {
613 int value = ltd.getClassDesc().getEnumConstant(fieldname);
615 // check if this field is an enum constant
616 throw new Error(fieldname + " is not an enum constant in "+fan.printNode(0)+" in "+md);
618 fd = new FieldDescriptor(new Modifiers(Modifiers.PUBLIC|Modifiers.FINAL), new TypeDescriptor(TypeDescriptor.INT), fieldname, null, false);
620 fd.setEnumValue(value);
621 } else if(fd == null) {
622 throw new Error("Could not find field "+ fieldname + " in "+fan.printNode(0)+" in "+md + " (Line: "+fan.getNumLine()+")");
623 } else if(fd.isStatic()) {
624 // check if this field is a static field
625 if(fd.getExpressionNode() != null) {
626 checkExpressionNode(md,nametable,fd.getExpressionNode(),null);
629 throw new Error("Dereference of the non-static field "+ fieldname + " in "+fan.printNode(0)+" in "+md);
634 throw new Error("Unknown field "+fieldname + " in "+fan.printNode(0)+" in "+md);
636 if (fd.getType().iswrapper()) {
637 FieldAccessNode fan2=new FieldAccessNode(left, fieldname);
638 fan2.setNumLine(left.getNumLine());
641 fan.fieldname="value";
643 ExpressionNode leftwr=fan.getExpression();
644 TypeDescriptor ltdwr=leftwr.getType();
645 String fieldnamewr=fan.getFieldName();
646 FieldDescriptor fdwr=(FieldDescriptor) ltdwr.getClassDesc().getFieldTable().get(fieldnamewr);
649 throw new Error("Unknown field "+fieldnamewr + " in "+fan.printNode(0)+" in "+md);
654 if (!typeutil.isSuperorType(td,fan.getType()))
655 throw new Error("Field node returns "+fan.getType()+", but need "+td);
659 void checkArrayAccessNode(Descriptor md, SymbolTable nametable, ArrayAccessNode aan, TypeDescriptor td) {
660 ExpressionNode left=aan.getExpression();
661 checkExpressionNode(md,nametable,left,null);
663 checkExpressionNode(md,nametable,aan.getIndex(),new TypeDescriptor(TypeDescriptor.INT));
664 TypeDescriptor ltd=left.getType();
665 if (ltd.dereference().iswrapper()) {
666 aan.wrappertype=((FieldDescriptor)ltd.dereference().getClassDesc().getFieldTable().get("value")).getType();
670 if (!typeutil.isSuperorType(td,aan.getType()))
671 throw new Error("Field node returns "+aan.getType()+", but need "+td);
674 void checkLiteralNode(Descriptor md, SymbolTable nametable, LiteralNode ln, TypeDescriptor td) {
675 /* Resolve the type */
676 Object o=ln.getValue();
677 if (ln.getTypeString().equals("null")) {
678 ln.setType(new TypeDescriptor(TypeDescriptor.NULL));
679 } else if (o instanceof Integer) {
680 ln.setType(new TypeDescriptor(TypeDescriptor.INT));
681 } else if (o instanceof Long) {
682 ln.setType(new TypeDescriptor(TypeDescriptor.LONG));
683 } else if (o instanceof Float) {
684 ln.setType(new TypeDescriptor(TypeDescriptor.FLOAT));
685 } else if (o instanceof Boolean) {
686 ln.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
687 } else if (o instanceof Double) {
688 ln.setType(new TypeDescriptor(TypeDescriptor.DOUBLE));
689 } else if (o instanceof Character) {
690 ln.setType(new TypeDescriptor(TypeDescriptor.CHAR));
691 } else if (o instanceof String) {
692 ln.setType(new TypeDescriptor(getClass(TypeUtil.StringClass)));
696 if (!typeutil.isSuperorType(td,ln.getType())) {
697 Long l = ln.evaluate();
698 if((ln.getType().isByte() || ln.getType().isShort()
699 || ln.getType().isChar() || ln.getType().isInt())
701 && (td.isByte() || td.isShort() || td.isChar()
702 || td.isInt() || td.isLong())) {
703 long lnvalue = l.longValue();
704 if((td.isByte() && ((lnvalue > 127) || (lnvalue < -128)))
705 || (td.isShort() && ((lnvalue > 32767) || (lnvalue < -32768)))
706 || (td.isChar() && ((lnvalue > 65535) || (lnvalue < 0)))
707 || (td.isInt() && ((lnvalue > 2147483647) || (lnvalue < -2147483648)))
708 || (td.isLong() && ((lnvalue > 9223372036854775807L) || (lnvalue < -9223372036854775808L)))) {
709 throw new Error("Field node returns "+ln.getType()+", but need "+td+" in "+md);
712 throw new Error("Field node returns "+ln.getType()+", but need "+td+" in "+md);
717 void checkNameNode(Descriptor md, SymbolTable nametable, NameNode nn, TypeDescriptor td) {
718 NameDescriptor nd=nn.getName();
719 if (nd.getBase()!=null) {
721 /* Rewrite NameNode */
722 ExpressionNode en=translateNameDescriptorintoExpression(nd,nn.getNumLine());
723 nn.setExpression(en);
724 checkExpressionNode(md,nametable,en,td);
726 String varname=nd.toString();
727 if(varname.equals("this")) {
729 nn.setVar((VarDescriptor)nametable.get("this"));
732 Descriptor d=(Descriptor)nametable.get(varname);
734 ClassDescriptor cd = null;
735 if((md instanceof MethodDescriptor) && ((MethodDescriptor)md).isStaticBlock()) {
736 // this is a static block, all the accessed fields should be static field
737 cd = ((MethodDescriptor)md).getClassDesc();
738 SymbolTable fieldtbl = cd.getFieldTable();
739 FieldDescriptor fd=(FieldDescriptor)fieldtbl.get(varname);
740 if((fd == null) || (!fd.isStatic())){
741 // no such field in the class, check if this is a class
742 if(varname.equals("this")) {
743 throw new Error("Error: access this obj in a static block");
745 cd=getClass(getFullName(varname));
747 // this is a class name
751 throw new Error("Name "+varname+" should not be used in static block: "+md);
754 // this is a static field
760 // check if the var is a static field of the class
761 if(md instanceof MethodDescriptor) {
762 cd = ((MethodDescriptor)md).getClassDesc();
763 FieldDescriptor fd = (FieldDescriptor)cd.getFieldTable().get(varname);
764 if((fd != null) && (fd.isStatic())) {
768 if (!typeutil.isSuperorType(td,nn.getType()))
769 throw new Error("Field node returns "+nn.getType()+", but need "+td);
771 } else if(fd != null) {
772 throw new Error("Name "+varname+" should not be used in " + md);
775 cd=getClass(varname);
777 // this is a class name
781 throw new Error("Name "+varname+" undefined in: "+md);
785 if (d instanceof VarDescriptor) {
787 } else if (d instanceof FieldDescriptor) {
788 FieldDescriptor fd=(FieldDescriptor)d;
789 if (fd.getType().iswrapper()) {
790 String id=nd.getIdentifier();
791 NameDescriptor base=nd.getBase();
792 NameNode n=new NameNode(nn.getName());
793 n.setNumLine(nn.getNumLine());
795 n.setVar((VarDescriptor)nametable.get("this")); /* Need a pointer to this */
796 FieldAccessNode fan=new FieldAccessNode(n,"value");
797 fan.setNumLine(n.getNumLine());
798 FieldDescriptor fdval=(FieldDescriptor) fd.getType().getClassDesc().getFieldTable().get("value");
800 nn.setExpression(fan);
803 nn.setVar((VarDescriptor)nametable.get("this")); /* Need a pointer to this */
805 } else if (d instanceof TagVarDescriptor) {
807 } else throw new Error("Wrong type of descriptor");
809 if (!typeutil.isSuperorType(td,nn.getType()))
810 throw new Error("Field node returns "+nn.getType()+", but need "+td);
814 void checkOffsetNode(Descriptor md, SymbolTable nameTable, OffsetNode ofn, TypeDescriptor td) {
815 TypeDescriptor ltd=ofn.td;
816 checkTypeDescriptor(ltd);
818 String fieldname = ofn.fieldname;
819 FieldDescriptor fd=null;
820 if (ltd.isArray()&&fieldname.equals("length")) {
821 fd=FieldDescriptor.arrayLength;
823 fd=(FieldDescriptor) ltd.getClassDesc().getFieldTable().get(fieldname);
827 checkField(ltd.getClassDesc(), fd);
830 throw new Error("Unknown field "+fieldname + " in "+ofn.printNode(1)+" in "+md);
833 if (!typeutil.isSuperorType(td, ofn.getType())) {
834 System.out.println(td);
835 System.out.println(ofn.getType());
836 throw new Error("Type of rside not compatible with type of lside"+ofn.printNode(0));
842 void checkTertiaryNode(Descriptor md, SymbolTable nametable, TertiaryNode tn, TypeDescriptor td) {
843 checkExpressionNode(md, nametable, tn.getCond(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
844 checkExpressionNode(md, nametable, tn.getTrueExpr(), td );
845 checkExpressionNode(md, nametable, tn.getFalseExpr(), td );
848 void checkInstanceOfNode(Descriptor md, SymbolTable nametable, InstanceOfNode tn, TypeDescriptor td) {
849 if (td!=null&&!td.isBoolean())
850 throw new Error("Expecting type "+td+"for instanceof expression");
852 checkTypeDescriptor(tn.getExprType());
853 checkExpressionNode(md, nametable, tn.getExpr(), null);
856 void checkArrayInitializerNode(Descriptor md, SymbolTable nametable, ArrayInitializerNode ain, TypeDescriptor td) {
857 Vector<TypeDescriptor> vec_type = new Vector<TypeDescriptor>();
858 for( int i = 0; i < ain.numVarInitializers(); ++i ) {
859 checkExpressionNode(md, nametable, ain.getVarInitializer(i), td==null?td:td.dereference());
860 vec_type.add(ain.getVarInitializer(i).getType());
862 // descide the type of this variableInitializerNode
863 TypeDescriptor out_type = vec_type.elementAt(0);
864 for(int i = 1; i < vec_type.size(); i++) {
865 TypeDescriptor tmp_type = vec_type.elementAt(i);
866 if(out_type == null) {
867 if(tmp_type != null) {
870 } else if(out_type.isNull()) {
871 if(!tmp_type.isNull() ) {
872 if(!tmp_type.isArray()) {
873 throw new Error("Error: mixed type in var initializer list");
878 } else if(out_type.isArray()) {
879 if(tmp_type.isArray()) {
880 if(tmp_type.getArrayCount() > out_type.getArrayCount()) {
883 } else if((tmp_type != null) && (!tmp_type.isNull())) {
884 throw new Error("Error: mixed type in var initializer list");
886 } else if(out_type.isInt()) {
887 if(!tmp_type.isInt()) {
888 throw new Error("Error: mixed type in var initializer list");
890 } else if(out_type.isString()) {
891 if(!tmp_type.isString()) {
892 throw new Error("Error: mixed type in var initializer list");
896 if(out_type != null) {
897 out_type = out_type.makeArray(state);
898 //out_type.setStatic();
900 ain.setType(out_type);
903 void checkAssignmentNode(Descriptor md, SymbolTable nametable, AssignmentNode an, TypeDescriptor td) {
904 boolean postinc=true;
905 if (an.getOperation().getBaseOp()==null||
906 (an.getOperation().getBaseOp().getOp()!=Operation.POSTINC&&
907 an.getOperation().getBaseOp().getOp()!=Operation.POSTDEC))
910 checkExpressionNode(md, nametable, an.getSrc(),td);
911 //TODO: Need check on validity of operation here
912 if (!((an.getDest() instanceof FieldAccessNode)||
913 (an.getDest() instanceof ArrayAccessNode)||
914 (an.getDest() instanceof NameNode)))
915 throw new Error("Bad lside in "+an.printNode(0));
916 checkExpressionNode(md, nametable, an.getDest(), null);
918 /* We want parameter variables to tasks to be immutable */
919 if (md instanceof TaskDescriptor) {
920 if (an.getDest() instanceof NameNode) {
921 NameNode nn=(NameNode)an.getDest();
922 if (nn.getVar()!=null) {
923 if (((TaskDescriptor)md).getParameterTable().contains(nn.getVar().getSymbol()))
924 throw new Error("Can't modify parameter "+nn.getVar()+ " to task "+td.getSymbol());
929 if (an.getDest().getType().isString()&&an.getOperation().getOp()==AssignOperation.PLUSEQ) {
931 ClassDescriptor stringcl=getClass(TypeUtil.StringClass);
932 TypeDescriptor stringtd=new TypeDescriptor(stringcl);
933 NameDescriptor nd=new NameDescriptor("String");
934 NameDescriptor valuend=new NameDescriptor(nd, "valueOf");
936 if (!(an.getSrc().getType().isString()&&(an.getSrc() instanceof OpNode))) {
937 MethodInvokeNode rightmin=new MethodInvokeNode(valuend);
938 rightmin.setNumLine(an.getSrc().getNumLine());
939 rightmin.addArgument(an.getSrc());
941 checkExpressionNode(md, nametable, an.getSrc(), null);
945 if (!postinc&&!typeutil.isSuperorType(an.getDest().getType(),an.getSrc().getType())) {
946 TypeDescriptor dt = an.getDest().getType();
947 TypeDescriptor st = an.getSrc().getType();
948 if(an.getSrc().kind() == Kind.ArrayInitializerNode) {
949 if(dt.getArrayCount() != st.getArrayCount()) {
950 throw new Error("Type of rside ("+an.getSrc().getType().toPrettyString()+") not compatible with type of lside ("+an.getDest().getType().toPrettyString()+")"+an.printNode(0));
953 dt = dt.dereference();
954 st = st.dereference();
955 } while(dt.isArray());
956 if((st.isByte() || st.isShort() || st.isChar() || st.isInt())
957 && (dt.isByte() || dt.isShort() || dt.isChar() || dt.isInt() || dt.isLong())) {
960 throw new Error("Type of rside ("+an.getSrc().getType().toPrettyString()+") not compatible with type of lside ("+an.getDest().getType().toPrettyString()+")"+an.printNode(0));
964 Long l = an.getSrc().evaluate();
965 if((st.isByte() || st.isShort() || st.isChar() || st.isInt())
967 && (dt.isByte() || dt.isShort() || dt.isChar() || dt.isInt() || dt.isLong())) {
968 long lnvalue = l.longValue();
969 if((dt.isByte() && ((lnvalue > 127) || (lnvalue < -128)))
970 || (dt.isShort() && ((lnvalue > 32767) || (lnvalue < -32768)))
971 || (dt.isChar() && ((lnvalue > 65535) || (lnvalue < 0)))
972 || (dt.isInt() && ((lnvalue > 2147483647) || (lnvalue < -2147483648)))
973 || (dt.isLong() && ((lnvalue > 9223372036854775807L) || (lnvalue < -9223372036854775808L)))) {
974 throw new Error("Type of rside ("+an.getSrc().getType().toPrettyString()+") not compatible with type of lside ("+an.getDest().getType().toPrettyString()+")"+an.printNode(0));
977 throw new Error("Type of rside ("+an.getSrc().getType().toPrettyString()+") not compatible with type of lside ("+an.getDest().getType().toPrettyString()+")"+an.printNode(0));
983 void checkLoopNode(Descriptor md, SymbolTable nametable, LoopNode ln) {
985 if (ln.getType()==LoopNode.WHILELOOP||ln.getType()==LoopNode.DOWHILELOOP) {
986 checkExpressionNode(md, nametable, ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
987 checkBlockNode(md, nametable, ln.getBody());
990 /* Link in the initializer naming environment */
991 BlockNode bn=ln.getInitializer();
992 bn.getVarTable().setParent(nametable);
993 for(int i=0; i<bn.size(); i++) {
994 BlockStatementNode bsn=bn.get(i);
995 checkBlockStatementNode(md, bn.getVarTable(),bsn);
997 //check the condition
998 checkExpressionNode(md, bn.getVarTable(), ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
999 checkBlockNode(md, bn.getVarTable(), ln.getBody());
1000 checkBlockNode(md, bn.getVarTable(), ln.getUpdate());
1006 void checkCreateObjectNode(Descriptor md, SymbolTable nametable, CreateObjectNode con,
1007 TypeDescriptor td) {
1008 TypeDescriptor[] tdarray = new TypeDescriptor[con.numArgs()];
1009 for (int i = 0; i < con.numArgs(); i++) {
1010 ExpressionNode en = con.getArg(i);
1011 checkExpressionNode(md, nametable, en, null);
1012 tdarray[i] = en.getType();
1015 TypeDescriptor typetolookin = con.getType();
1016 checkTypeDescriptor(typetolookin);
1018 if (td != null && !typeutil.isSuperorType(td, typetolookin))
1019 throw new Error(typetolookin + " isn't a " + td);
1021 /* Check Array Initializers */
1022 if ((con.getArrayInitializer() != null)) {
1023 checkArrayInitializerNode(md, nametable, con.getArrayInitializer(), td);
1026 /* Check flag effects */
1027 if (con.getFlagEffects() != null) {
1028 FlagEffects fe = con.getFlagEffects();
1029 ClassDescriptor cd = typetolookin.getClassDesc();
1031 for (int j = 0; j < fe.numEffects(); j++) {
1032 FlagEffect flag = fe.getEffect(j);
1033 String name = flag.getName();
1034 FlagDescriptor flag_d = (FlagDescriptor) cd.getFlagTable().get(name);
1035 // Make sure the flag is declared
1037 throw new Error("Flag descriptor " + name + " undefined in class: " + cd.getSymbol());
1038 if (flag_d.getExternal())
1039 throw new Error("Attempting to modify external flag: " + name);
1040 flag.setFlag(flag_d);
1042 for (int j = 0; j < fe.numTagEffects(); j++) {
1043 TagEffect tag = fe.getTagEffect(j);
1044 String name = tag.getName();
1046 Descriptor d = (Descriptor) nametable.get(name);
1048 throw new Error("Tag descriptor " + name + " undeclared");
1049 else if (!(d instanceof TagVarDescriptor))
1050 throw new Error(name + " is not a tag descriptor");
1051 tag.setTag((TagVarDescriptor) d);
1055 if ((!typetolookin.isClass()) && (!typetolookin.isArray()))
1056 throw new Error("Can't allocate primitive type:" + con.printNode(0));
1058 if (!typetolookin.isArray()) {
1059 // Array's don't need constructor calls
1060 ClassDescriptor classtolookin = typetolookin.getClassDesc();
1062 Set methoddescriptorset = classtolookin.getMethodTable().getSet(typetolookin.getSymbol());
1063 MethodDescriptor bestmd = null;
1064 NextMethod: for (Iterator methodit = methoddescriptorset.iterator(); methodit.hasNext();) {
1065 MethodDescriptor currmd = (MethodDescriptor) methodit.next();
1066 /* Need correct number of parameters */
1067 if (con.numArgs() != currmd.numParameters())
1069 for (int i = 0; i < con.numArgs(); i++) {
1070 if (!typeutil.isSuperorType(currmd.getParamType(i), tdarray[i]))
1071 continue NextMethod;
1073 /* Local allocations can't call global allocator */
1074 if (!con.isGlobal() && currmd.isGlobal())
1077 /* Method okay so far */
1081 if (typeutil.isMoreSpecific(currmd, bestmd)) {
1083 } else if (con.isGlobal() && match(currmd, bestmd)) {
1084 if (currmd.isGlobal() && !bestmd.isGlobal())
1086 else if (currmd.isGlobal() && bestmd.isGlobal())
1088 } else if (!typeutil.isMoreSpecific(bestmd, currmd)) {
1089 throw new Error("No method is most specific:" + bestmd + " and " + currmd);
1092 /* Is this more specific than bestmd */
1096 throw new Error("No method found for " + con.printNode(0) + " in " + md);
1097 con.setConstructor(bestmd);
1102 /** Check to see if md1 is the same specificity as md2.*/
1104 boolean match(MethodDescriptor md1, MethodDescriptor md2) {
1105 /* Checks if md1 is more specific than md2 */
1106 if (md1.numParameters()!=md2.numParameters())
1108 for(int i=0; i<md1.numParameters(); i++) {
1109 if (!md2.getParamType(i).equals(md1.getParamType(i)))
1112 if (!md2.getReturnType().equals(md1.getReturnType()))
1115 if (!md2.getClassDesc().equals(md1.getClassDesc()))
1123 ExpressionNode translateNameDescriptorintoExpression(NameDescriptor nd, int numLine) {
1124 String id=nd.getIdentifier();
1125 NameDescriptor base=nd.getBase();
1127 NameNode nn=new NameNode(nd);
1128 nn.setNumLine(numLine);
1131 FieldAccessNode fan=new FieldAccessNode(translateNameDescriptorintoExpression(base,numLine),id);
1132 fan.setNumLine(numLine);
1138 void checkMethodInvokeNode(Descriptor md, SymbolTable nametable, MethodInvokeNode min, TypeDescriptor td) {
1139 /*Typecheck subexpressions
1140 and get types for expressions*/
1142 boolean isstatic = false;
1143 if((md instanceof MethodDescriptor) && ((MethodDescriptor)md).isStatic()) {
1146 TypeDescriptor[] tdarray=new TypeDescriptor[min.numArgs()];
1147 for(int i=0; i<min.numArgs(); i++) {
1148 ExpressionNode en=min.getArg(i);
1149 checkExpressionNode(md,nametable,en,null);
1150 tdarray[i]=en.getType();
1151 if(en.getType().isClass() && en.getType().getClassDesc().isEnum()) {
1152 tdarray[i] = new TypeDescriptor(TypeDescriptor.INT);
1155 TypeDescriptor typetolookin=null;
1156 if (min.getExpression()!=null) {
1157 checkExpressionNode(md,nametable,min.getExpression(),null);
1158 typetolookin=min.getExpression().getType();
1159 //if (typetolookin==null)
1160 //throw new Error(md+" has null return type");
1162 } else if (min.getBaseName()!=null) {
1163 String rootname=min.getBaseName().getRoot();
1164 if (rootname.equals("super")) {
1165 ClassDescriptor supercd=((MethodDescriptor)md).getClassDesc().getSuperDesc();
1166 typetolookin=new TypeDescriptor(supercd);
1168 } else if (rootname.equals("this")) {
1170 throw new Error("use this object in static method md = "+ md.toString());
1172 ClassDescriptor cd=((MethodDescriptor)md).getClassDesc();
1173 typetolookin=new TypeDescriptor(cd);
1174 } else if (nametable.get(rootname)!=null) {
1175 //we have an expression
1176 min.setExpression(translateNameDescriptorintoExpression(min.getBaseName(),min.getNumLine()));
1177 checkExpressionNode(md, nametable, min.getExpression(), null);
1178 typetolookin=min.getExpression().getType();
1180 if(!min.getBaseName().getSymbol().equals("System.out")) {
1181 ExpressionNode nn = translateNameDescriptorintoExpression(min.getBaseName(),min.getNumLine());
1182 checkExpressionNode(md, nametable, nn, null);
1183 typetolookin = nn.getType();
1184 if(!((nn.kind()== Kind.NameNode) && (((NameNode)nn).getField() == null)
1185 && (((NameNode)nn).getVar() == null) && (((NameNode)nn).getExpression() == null))) {
1186 // this is not a pure class name, need to add to
1187 min.setExpression(nn);
1191 ClassDescriptor cd = null;
1192 //if (min.getBaseName().getSymbol().equals("System.out"))
1193 cd=getClass("System");
1195 cd=getClass(min.getBaseName().getSymbol());
1198 throw new Error("md = "+ md.toString()+ " "+min.getBaseName()+" undefined");
1199 typetolookin=new TypeDescriptor(cd);
1202 } else if ((md instanceof MethodDescriptor)&&min.getMethodName().equals("super")) {
1203 ClassDescriptor supercd=((MethodDescriptor)md).getClassDesc().getSuperDesc();
1204 min.methodid=supercd.getSymbol();
1205 typetolookin=new TypeDescriptor(supercd);
1206 } else if (md instanceof MethodDescriptor) {
1207 typetolookin=new TypeDescriptor(((MethodDescriptor)md).getClassDesc());
1209 /* If this a task descriptor we throw an error at this point */
1210 throw new Error("Unknown method call to "+min.getMethodName()+"in task"+md.getSymbol());
1212 if (!typetolookin.isClass())
1213 throw new Error("Error with method call to "+min.getMethodName());
1214 ClassDescriptor classtolookin=typetolookin.getClassDesc();
1215 //System.out.println("Method name="+min.getMethodName());
1217 Set methoddescriptorset=classtolookin.getMethodTable().getSet(min.getMethodName());
1218 MethodDescriptor bestmd=null;
1220 for(Iterator methodit=methoddescriptorset.iterator(); methodit.hasNext();) {
1221 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
1222 /* Need correct number of parameters */
1223 if (min.numArgs()!=currmd.numParameters())
1225 for(int i=0; i<min.numArgs(); i++) {
1226 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
1227 if(((!tdarray[i].isArray() &&(tdarray[i].isInt() || tdarray[i].isLong()))
1228 && currmd.getParamType(i).isClass() && currmd.getParamType(i).getClassDesc().getSymbol().equals("Object"))) {
1229 // primitive parameters vs object
1231 continue NextMethod;
1234 /* Method okay so far */
1238 if (typeutil.isMoreSpecific(currmd,bestmd)) {
1240 } else if (!typeutil.isMoreSpecific(bestmd, currmd))
1241 throw new Error("No method is most specific:"+bestmd+" and "+currmd);
1243 /* Is this more specific than bestmd */
1247 throw new Error("No method found for :"+min.printNode(0)+" in class: " + classtolookin+" in "+md);
1248 min.setMethod(bestmd);
1250 if ((td!=null)&&(min.getType()!=null)&&!typeutil.isSuperorType(td, min.getType()))
1251 throw new Error(min.getType()+ " is not equal to or a subclass of "+td);
1252 /* Check whether we need to set this parameter to implied this */
1253 if (! isstatic && !bestmd.isStatic()) {
1254 if (min.getExpression()==null) {
1255 ExpressionNode en=new NameNode(new NameDescriptor("this"));
1256 min.setExpression(en);
1257 checkExpressionNode(md, nametable, min.getExpression(), null);
1261 /* Check if we need to wrap primitive paratmeters to objects */
1262 for(int i=0; i<min.numArgs(); i++) {
1263 if(!tdarray[i].isArray() && (tdarray[i].isInt() || tdarray[i].isLong())
1264 && min.getMethod().getParamType(i).isClass() && min.getMethod().getParamType(i).getClassDesc().getSymbol().equals("Object")) {
1265 // Shall wrap this primitive parameter as a object
1266 ExpressionNode exp = min.getArg(i);
1267 TypeDescriptor ptd = null;
1268 NameDescriptor nd=null;
1269 if(exp.getType().isInt()) {
1270 nd = new NameDescriptor("Integer");
1271 ptd = state.getTypeDescriptor(nd);
1272 } else if(exp.getType().isLong()) {
1273 nd = new NameDescriptor("Long");
1274 ptd = state.getTypeDescriptor(nd);
1276 boolean isglobal = false;
1277 String disjointId = null;
1278 CreateObjectNode con=new CreateObjectNode(ptd, isglobal, disjointId);
1279 con.addArgument(exp);
1280 checkExpressionNode(md, nametable, con, null);
1281 min.setArgument(con, i);
1287 void checkOpNode(Descriptor md, SymbolTable nametable, OpNode on, TypeDescriptor td) {
1288 checkExpressionNode(md, nametable, on.getLeft(), null);
1289 if (on.getRight()!=null)
1290 checkExpressionNode(md, nametable, on.getRight(), null);
1291 TypeDescriptor ltd=on.getLeft().getType();
1292 TypeDescriptor rtd=on.getRight()!=null ? on.getRight().getType() : null;
1293 TypeDescriptor lefttype=null;
1294 TypeDescriptor righttype=null;
1295 Operation op=on.getOp();
1297 switch(op.getOp()) {
1298 case Operation.LOGIC_OR:
1299 case Operation.LOGIC_AND:
1300 if (!(rtd.isBoolean()))
1302 on.setRightType(rtd);
1304 case Operation.LOGIC_NOT:
1305 if (!(ltd.isBoolean()))
1308 on.setLeftType(ltd);
1310 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1313 case Operation.COMP:
1314 // 5.6.2 Binary Numeric Promotion
1315 //TODO unboxing of reference objects
1318 else if (ltd.isFloat())
1320 else if (ltd.isLong())
1321 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1323 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1324 on.setLeftType(lefttype);
1325 on.setType(lefttype);
1328 case Operation.BIT_OR:
1329 case Operation.BIT_XOR:
1330 case Operation.BIT_AND:
1331 // 5.6.2 Binary Numeric Promotion
1332 //TODO unboxing of reference objects
1333 if (ltd.isDouble()||rtd.isDouble())
1335 else if (ltd.isFloat()||rtd.isFloat())
1337 else if (ltd.isLong()||rtd.isLong())
1338 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1339 // 090205 hack for boolean
1340 else if (ltd.isBoolean()||rtd.isBoolean())
1341 lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
1343 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1346 on.setLeftType(lefttype);
1347 on.setRightType(righttype);
1348 on.setType(lefttype);
1351 case Operation.ISAVAILABLE:
1352 if (!(ltd.isPtr())) {
1353 throw new Error("Can't use isavailable on non-pointers/non-parameters.");
1356 on.setLeftType(lefttype);
1357 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1360 case Operation.EQUAL:
1361 case Operation.NOTEQUAL:
1362 // 5.6.2 Binary Numeric Promotion
1363 //TODO unboxing of reference objects
1364 if (ltd.isBoolean()||rtd.isBoolean()) {
1365 if (!(ltd.isBoolean()&&rtd.isBoolean()))
1367 righttype=lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
1368 } else if (ltd.isPtr()||rtd.isPtr()) {
1369 if (!(ltd.isPtr()&&rtd.isPtr())) {
1376 } else if (ltd.isDouble()||rtd.isDouble())
1377 righttype=lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
1378 else if (ltd.isFloat()||rtd.isFloat())
1379 righttype=lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
1380 else if (ltd.isLong()||rtd.isLong())
1381 righttype=lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1383 righttype=lefttype=new TypeDescriptor(TypeDescriptor.INT);
1385 on.setLeftType(lefttype);
1386 on.setRightType(righttype);
1387 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1396 // 5.6.2 Binary Numeric Promotion
1397 //TODO unboxing of reference objects
1398 if (!ltd.isNumber()||!rtd.isNumber()) {
1399 if (!ltd.isNumber())
1400 throw new Error("Leftside is not number"+on.printNode(0)+"type="+ltd.toPrettyString());
1401 if (!rtd.isNumber())
1402 throw new Error("Rightside is not number"+on.printNode(0));
1405 if (ltd.isDouble()||rtd.isDouble())
1406 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
1407 else if (ltd.isFloat()||rtd.isFloat())
1408 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
1409 else if (ltd.isLong()||rtd.isLong())
1410 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1412 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1414 on.setLeftType(lefttype);
1415 on.setRightType(righttype);
1416 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1420 if (ltd.isString()||rtd.isString()) {
1421 ClassDescriptor stringcl=getClass(TypeUtil.StringClass);
1422 TypeDescriptor stringtd=new TypeDescriptor(stringcl);
1423 NameDescriptor nd=new NameDescriptor("String");
1424 NameDescriptor valuend=new NameDescriptor(nd, "valueOf");
1425 if (!(ltd.isString()&&(on.getLeft() instanceof OpNode))) {
1426 MethodInvokeNode leftmin=new MethodInvokeNode(valuend);
1427 leftmin.setNumLine(on.getLeft().getNumLine());
1428 leftmin.addArgument(on.getLeft());
1430 checkExpressionNode(md, nametable, on.getLeft(), null);
1433 if (!(rtd.isString()&&(on.getRight() instanceof OpNode))) {
1434 MethodInvokeNode rightmin=new MethodInvokeNode(valuend);
1435 rightmin.setNumLine(on.getRight().getNumLine());
1436 rightmin.addArgument(on.getRight());
1438 checkExpressionNode(md, nametable, on.getRight(), null);
1441 on.setLeftType(stringtd);
1442 on.setRightType(stringtd);
1443 on.setType(stringtd);
1448 case Operation.MULT:
1451 // 5.6.2 Binary Numeric Promotion
1452 //TODO unboxing of reference objects
1453 if (ltd.isArray()||rtd.isArray()||!ltd.isNumber()||!rtd.isNumber())
1454 throw new Error("Error in "+on.printNode(0));
1456 if (ltd.isDouble()||rtd.isDouble())
1457 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
1458 else if (ltd.isFloat()||rtd.isFloat())
1459 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
1460 else if (ltd.isLong()||rtd.isLong())
1461 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1463 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1465 on.setLeftType(lefttype);
1466 on.setRightType(righttype);
1467 on.setType(lefttype);
1470 case Operation.LEFTSHIFT:
1471 case Operation.RIGHTSHIFT:
1472 case Operation.URIGHTSHIFT:
1473 if (!rtd.isIntegerType())
1475 //5.6.1 Unary Numeric Promotion
1476 if (rtd.isByte()||rtd.isShort()||rtd.isInt())
1477 righttype=new TypeDescriptor(TypeDescriptor.INT);
1481 on.setRightType(righttype);
1482 if (!ltd.isIntegerType())
1485 case Operation.UNARYPLUS:
1486 case Operation.UNARYMINUS:
1487 /* case Operation.POSTINC:
1488 case Operation.POSTDEC:
1489 case Operation.PREINC:
1490 case Operation.PREDEC:*/
1491 if (!ltd.isNumber())
1493 //5.6.1 Unary Numeric Promotion
1494 if (ltd.isByte()||ltd.isShort()||ltd.isInt())
1495 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1498 on.setLeftType(lefttype);
1499 on.setType(lefttype);
1503 throw new Error(op.toString());
1507 if (!typeutil.isSuperorType(td, on.getType())) {
1508 System.out.println(td);
1509 System.out.println(on.getType());
1510 throw new Error("Type of rside not compatible with type of lside"+on.printNode(0));
1514 public String getFullName(String classIn) {
1515 return (String) ((this.classnameMappings.containsKey(classIn))?classnameMappings.get(classIn):classIn);