7 public class SemanticCheck {
12 HashMap<ClassDescriptor, Integer> completed;
14 public static final int NOCHECK=0;
15 public static final int REFERENCE=1;
16 public static final int INIT=2;
20 public boolean hasLayout(ClassDescriptor cd) {
21 return completed.get(cd)!=null&&completed.get(cd)==INIT;
24 public SemanticCheck(State state, TypeUtil tu) {
25 this(state, tu, true);
28 public SemanticCheck(State state, TypeUtil tu, boolean checkAll) {
31 this.loopstack=new Stack();
32 this.toanalyze=new HashSet();
33 this.completed=new HashMap<ClassDescriptor, Integer>();
34 this.checkAll=checkAll;
37 public ClassDescriptor getClass(ClassDescriptor context, String classname) {
38 return getClass(context, classname, INIT);
40 public ClassDescriptor getClass(ClassDescriptor context, String classnameIn, int fullcheck) {
41 String classname = classnameIn;
43 classname = context.getCannonicalImportMapName(classnameIn);
45 ClassDescriptor cd=typeutil.getClass(classname, toanalyze);
46 checkClass(cd, fullcheck);
51 public void checkClass(ClassDescriptor cd) {
55 public void checkClass(ClassDescriptor cd, int fullcheck) {
56 if (!completed.containsKey(cd)||completed.get(cd)<fullcheck) {
57 int oldstatus=completed.containsKey(cd)?completed.get(cd):0;
58 completed.put(cd, fullcheck);
60 if (fullcheck>=REFERENCE&&oldstatus<INIT) {
61 //Set superclass link up
62 if (cd.getSuper()!=null) {
63 cd.setSuper(getClass(cd, cd.getSuper(), fullcheck));
64 if(cd.getSuperDesc().isInterface()) {
65 throw new Error("Error! Class " + cd.getSymbol() + " extends interface " + cd.getSuper());
67 // Link together Field, Method, and Flag tables so classes
68 // inherit these from their superclasses
69 if (oldstatus<REFERENCE) {
70 cd.getFieldTable().setParent(cd.getSuperDesc().getFieldTable());
71 cd.getMethodTable().setParent(cd.getSuperDesc().getMethodTable());
72 cd.getFlagTable().setParent(cd.getSuperDesc().getFlagTable());
75 // Link together Field, Method tables do classes inherit these from
76 // their ancestor interfaces
77 Vector<String> sifv = cd.getSuperInterface();
78 for(int i = 0; i < sifv.size(); i++) {
79 ClassDescriptor superif = getClass(cd, sifv.elementAt(i), fullcheck);
80 if(!superif.isInterface()) {
81 throw new Error("Error! Class " + cd.getSymbol() + " implements non-interface " + superif.getSymbol());
83 if (oldstatus<REFERENCE) {
84 cd.addSuperInterfaces(superif);
85 cd.getFieldTable().addParentIF(superif.getFieldTable());
86 cd.getMethodTable().addParentIF(superif.getMethodTable());
90 if (oldstatus<INIT&&fullcheck>=INIT) {
91 /* Check to see that fields are well typed */
92 for(Iterator field_it=cd.getFields(); field_it.hasNext(); ) {
93 FieldDescriptor fd=(FieldDescriptor)field_it.next();
96 for(Iterator method_it=cd.getMethods(); method_it.hasNext(); ) {
97 MethodDescriptor md=(MethodDescriptor)method_it.next();
104 public void semanticCheck() {
105 SymbolTable classtable = state.getClassSymbolTable();
106 toanalyze.addAll(classtable.getValueSet());
107 toanalyze.addAll(state.getTaskSymbolTable().getValueSet());
110 while (!toanalyze.isEmpty()) {
111 Object obj = toanalyze.iterator().next();
112 if (obj instanceof TaskDescriptor) {
113 toanalyze.remove(obj);
114 TaskDescriptor td = (TaskDescriptor) obj;
118 System.out.println("Error in " + td);
122 ClassDescriptor cd = (ClassDescriptor) obj;
123 toanalyze.remove(cd);
125 // need to initialize typeutil object here...only place we can
126 // get class descriptors without first calling getclass
127 getClass(cd, cd.getSymbol());
128 for (Iterator method_it = cd.getMethods(); method_it.hasNext(); ) {
129 MethodDescriptor md = (MethodDescriptor) method_it.next();
131 checkMethodBody(cd, md);
133 System.out.println("Error in " + md);
141 private void checkTypeDescriptor(ClassDescriptor cd, TypeDescriptor td) {
142 if (td.isPrimitive())
144 else if (td.isClass()) {
145 String name=td.toString();
146 ClassDescriptor field_cd=checkAll?getClass(cd, name):getClass(cd, name, REFERENCE);
149 throw new Error("Undefined class "+name);
150 td.setClassDescriptor(field_cd);
152 } else if (td.isTag())
158 public void checkField(ClassDescriptor cd, FieldDescriptor fd) {
159 checkTypeDescriptor(cd, fd.getType());
162 public void checkConstraintCheck(TaskDescriptor td, SymbolTable nametable, Vector ccs) {
164 return; /* No constraint checks to check */
165 for(int i=0; i<ccs.size(); i++) {
166 ConstraintCheck cc=(ConstraintCheck) ccs.get(i);
168 for(int j=0; j<cc.numArgs(); j++) {
169 ExpressionNode en=cc.getArg(j);
170 checkExpressionNode(td,nametable,en,null);
175 public void checkFlagEffects(TaskDescriptor td, Vector vfe, SymbolTable nametable) {
177 return; /* No flag effects to check */
178 for(int i=0; i<vfe.size(); i++) {
179 FlagEffects fe=(FlagEffects) vfe.get(i);
180 String varname=fe.getName();
181 //Make sure the variable is declared as a parameter to the task
182 VarDescriptor vd=(VarDescriptor)td.getParameterTable().get(varname);
184 throw new Error("Parameter "+varname+" in Flag Effects not declared in "+td);
187 //Make sure it correspods to a class
188 TypeDescriptor type_d=vd.getType();
189 if (!type_d.isClass())
190 throw new Error("Cannot have non-object argument for flag_effect");
192 ClassDescriptor cd=type_d.getClassDesc();
193 for(int j=0; j<fe.numEffects(); j++) {
194 FlagEffect flag=fe.getEffect(j);
195 String name=flag.getName();
196 FlagDescriptor flag_d=(FlagDescriptor)cd.getFlagTable().get(name);
197 //Make sure the flag is declared
199 throw new Error("Flag descriptor "+name+" undefined in class: "+cd.getSymbol());
200 if (flag_d.getExternal())
201 throw new Error("Attempting to modify external flag: "+name);
202 flag.setFlag(flag_d);
204 for(int j=0; j<fe.numTagEffects(); j++) {
205 TagEffect tag=fe.getTagEffect(j);
206 String name=tag.getName();
208 Descriptor d=(Descriptor)nametable.get(name);
210 throw new Error("Tag descriptor "+name+" undeclared");
211 else if (!(d instanceof TagVarDescriptor))
212 throw new Error(name+" is not a tag descriptor");
213 tag.setTag((TagVarDescriptor)d);
218 public void checkTask(TaskDescriptor td) {
219 for(int i=0; i<td.numParameters(); i++) {
220 /* Check that parameter is well typed */
221 TypeDescriptor param_type=td.getParamType(i);
222 checkTypeDescriptor(null, param_type);
224 /* Check the parameter's flag expression is well formed */
225 FlagExpressionNode fen=td.getFlag(td.getParameter(i));
226 if (!param_type.isClass())
227 throw new Error("Cannot have non-object argument to a task");
228 ClassDescriptor cd=param_type.getClassDesc();
230 checkFlagExpressionNode(cd, fen);
233 checkFlagEffects(td, td.getFlagEffects(),td.getParameterTable());
234 /* Check that the task code is valid */
235 BlockNode bn=state.getMethodBody(td);
236 checkBlockNode(td, td.getParameterTable(),bn);
239 public void checkFlagExpressionNode(ClassDescriptor cd, FlagExpressionNode fen) {
241 case Kind.FlagOpNode:
243 FlagOpNode fon=(FlagOpNode)fen;
244 checkFlagExpressionNode(cd, fon.getLeft());
245 if (fon.getRight()!=null)
246 checkFlagExpressionNode(cd, fon.getRight());
252 FlagNode fn=(FlagNode)fen;
253 String name=fn.getFlagName();
254 FlagDescriptor fd=(FlagDescriptor)cd.getFlagTable().get(name);
256 throw new Error("Undeclared flag: "+name);
262 throw new Error("Unrecognized FlagExpressionNode");
266 public void checkMethod(ClassDescriptor cd, MethodDescriptor md) {
267 /* Check for abstract methods */
268 if(md.isAbstract()) {
269 if(!cd.isAbstract() && !cd.isInterface()) {
270 throw new Error("Error! The non-abstract Class " + cd.getSymbol() + " contains an abstract method " + md.getSymbol());
274 /* Check return type */
275 if (!md.isConstructor() && !md.isStaticBlock())
276 if (!md.getReturnType().isVoid()) {
277 checkTypeDescriptor(cd, md.getReturnType());
279 for(int i=0; i<md.numParameters(); i++) {
280 TypeDescriptor param_type=md.getParamType(i);
281 checkTypeDescriptor(cd, param_type);
283 /* Link the naming environments */
284 if (!md.isStatic()) /* Fields aren't accessible directly in a static method, so don't link in this table */
285 md.getParameterTable().setParent(cd.getFieldTable());
287 if (!md.isStatic()) {
288 VarDescriptor thisvd=new VarDescriptor(new TypeDescriptor(cd),"this");
291 if(md.isDefaultConstructor() && (cd.getSuperDesc() != null)) {
292 // add the construction of it super class, can only be super()
293 NameDescriptor nd=new NameDescriptor("super");
294 MethodInvokeNode min=new MethodInvokeNode(nd);
295 BlockExpressionNode ben=new BlockExpressionNode(min);
296 BlockNode bn = state.getMethodBody(md);
297 bn.addFirstBlockStatement(ben);
301 public void checkMethodBody(ClassDescriptor cd, MethodDescriptor md) {
302 ClassDescriptor superdesc=cd.getSuperDesc();
303 if (superdesc!=null) {
304 Set possiblematches=superdesc.getMethodTable().getSet(md.getSymbol());
305 for(Iterator methodit=possiblematches.iterator(); methodit.hasNext(); ) {
306 MethodDescriptor matchmd=(MethodDescriptor)methodit.next();
307 if (md.matches(matchmd)) {
308 if (matchmd.getModifiers().isFinal()) {
309 throw new Error("Try to override final method in method:"+md+" declared in "+cd);
314 BlockNode bn=state.getMethodBody(md);
315 checkBlockNode(md, md.getParameterTable(),bn);
318 public void checkBlockNode(Descriptor md, SymbolTable nametable, BlockNode bn) {
319 /* Link in the naming environment */
320 bn.getVarTable().setParent(nametable);
321 for(int i=0; i<bn.size(); i++) {
322 BlockStatementNode bsn=bn.get(i);
323 checkBlockStatementNode(md, bn.getVarTable(),bsn);
327 public void checkBlockStatementNode(Descriptor md, SymbolTable nametable, BlockStatementNode bsn) {
329 case Kind.BlockExpressionNode:
330 checkBlockExpressionNode(md, nametable,(BlockExpressionNode)bsn);
333 case Kind.DeclarationNode:
334 checkDeclarationNode(md, nametable, (DeclarationNode)bsn);
337 case Kind.TagDeclarationNode:
338 checkTagDeclarationNode(md, nametable, (TagDeclarationNode)bsn);
341 case Kind.IfStatementNode:
342 checkIfStatementNode(md, nametable, (IfStatementNode)bsn);
345 case Kind.SwitchStatementNode:
346 checkSwitchStatementNode(md, nametable, (SwitchStatementNode)bsn);
350 checkLoopNode(md, nametable, (LoopNode)bsn);
353 case Kind.ReturnNode:
354 checkReturnNode(md, nametable, (ReturnNode)bsn);
357 case Kind.TaskExitNode:
358 checkTaskExitNode(md, nametable, (TaskExitNode)bsn);
361 case Kind.SubBlockNode:
362 checkSubBlockNode(md, nametable, (SubBlockNode)bsn);
365 case Kind.AtomicNode:
366 checkAtomicNode(md, nametable, (AtomicNode)bsn);
369 case Kind.SynchronizedNode:
370 checkSynchronizedNode(md, nametable, (SynchronizedNode)bsn);
373 case Kind.ContinueBreakNode:
374 checkContinueBreakNode(md, nametable, (ContinueBreakNode) bsn);
378 case Kind.GenReachNode:
379 // do nothing, no semantic check for SESEs
386 void checkBlockExpressionNode(Descriptor md, SymbolTable nametable, BlockExpressionNode ben) {
387 checkExpressionNode(md, nametable, ben.getExpression(), null);
390 void checkDeclarationNode(Descriptor md, SymbolTable nametable, DeclarationNode dn) {
391 VarDescriptor vd=dn.getVarDescriptor();
392 checkTypeDescriptor(((md instanceof MethodDescriptor)?((MethodDescriptor)md).getClassDesc():null), vd.getType());
393 Descriptor d=nametable.get(vd.getSymbol());
395 (d instanceof FieldDescriptor)) {
398 throw new Error(vd.getSymbol()+" in "+md+" defined a second time");
399 if (dn.getExpression()!=null)
400 checkExpressionNode(md, nametable, dn.getExpression(), vd.getType());
403 void checkTagDeclarationNode(Descriptor md, SymbolTable nametable, TagDeclarationNode dn) {
404 TagVarDescriptor vd=dn.getTagVarDescriptor();
405 Descriptor d=nametable.get(vd.getSymbol());
407 (d instanceof FieldDescriptor)) {
410 throw new Error(vd.getSymbol()+" defined a second time");
413 void checkSubBlockNode(Descriptor md, SymbolTable nametable, SubBlockNode sbn) {
414 checkBlockNode(md, nametable, sbn.getBlockNode());
417 void checkAtomicNode(Descriptor md, SymbolTable nametable, AtomicNode sbn) {
418 checkBlockNode(md, nametable, sbn.getBlockNode());
421 void checkSynchronizedNode(Descriptor md, SymbolTable nametable, SynchronizedNode sbn) {
422 checkBlockNode(md, nametable, sbn.getBlockNode());
423 //todo this could be Object
424 checkExpressionNode(md, nametable, sbn.getExpr(), null);
427 void checkContinueBreakNode(Descriptor md, SymbolTable nametable, ContinueBreakNode cbn) {
428 if (loopstack.empty())
429 throw new Error("continue/break outside of loop");
430 Object o = loopstack.peek();
431 if(o instanceof LoopNode) {
432 LoopNode ln=(LoopNode)o;
437 void checkReturnNode(Descriptor d, SymbolTable nametable, ReturnNode rn) {
438 if (d instanceof TaskDescriptor)
439 throw new Error("Illegal return appears in Task: "+d.getSymbol());
440 MethodDescriptor md=(MethodDescriptor)d;
441 if (rn.getReturnExpression()!=null)
442 if (md.getReturnType()==null)
443 throw new Error("Constructor can't return something.");
444 else if (md.getReturnType().isVoid())
445 throw new Error(md+" is void");
447 checkExpressionNode(md, nametable, rn.getReturnExpression(), md.getReturnType());
449 if (md.getReturnType()!=null&&!md.getReturnType().isVoid())
450 throw new Error("Need to return something for "+md);
453 void checkTaskExitNode(Descriptor md, SymbolTable nametable, TaskExitNode ten) {
454 if (md instanceof MethodDescriptor)
455 throw new Error("Illegal taskexit appears in Method: "+md.getSymbol());
456 checkFlagEffects((TaskDescriptor)md, ten.getFlagEffects(),nametable);
457 checkConstraintCheck((TaskDescriptor) md, nametable, ten.getChecks());
460 void checkIfStatementNode(Descriptor md, SymbolTable nametable, IfStatementNode isn) {
461 checkExpressionNode(md, nametable, isn.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
462 checkBlockNode(md, nametable, isn.getTrueBlock());
463 if (isn.getFalseBlock()!=null)
464 checkBlockNode(md, nametable, isn.getFalseBlock());
467 void checkSwitchStatementNode(Descriptor md, SymbolTable nametable, SwitchStatementNode ssn) {
468 checkExpressionNode(md, nametable, ssn.getCondition(), new TypeDescriptor(TypeDescriptor.INT));
470 BlockNode sbn = ssn.getSwitchBody();
471 boolean hasdefault = false;
472 for(int i = 0; i < sbn.size(); i++) {
473 boolean containdefault = checkSwitchBlockNode(md, nametable, (SwitchBlockNode)sbn.get(i));
474 if(hasdefault && containdefault) {
475 throw new Error("Error: duplicate default branch in switch-case statement in Method: " + md.getSymbol());
477 hasdefault = containdefault;
481 boolean checkSwitchBlockNode(Descriptor md, SymbolTable nametable, SwitchBlockNode sbn) {
482 Vector<SwitchLabelNode> slnv = sbn.getSwitchConditions();
484 for(int i = 0; i < slnv.size(); i++) {
485 if(slnv.elementAt(i).isdefault) {
488 checkConstantExpressionNode(md, nametable, slnv.elementAt(i).getCondition(), new TypeDescriptor(TypeDescriptor.INT));
492 throw new Error("Error: duplicate default branch in switch-case statement in Method: " + md.getSymbol());
495 checkBlockNode(md, nametable, sbn.getSwitchBlockStatement());
497 return (defaultb > 0);
501 void checkConstantExpressionNode(Descriptor md, SymbolTable nametable, ExpressionNode en, TypeDescriptor td) {
503 case Kind.FieldAccessNode:
504 checkFieldAccessNode(md,nametable,(FieldAccessNode)en,td);
507 case Kind.LiteralNode:
508 checkLiteralNode(md,nametable,(LiteralNode)en,td);
512 checkNameNode(md,nametable,(NameNode)en,td);
516 checkOpNode(md, nametable, (OpNode)en, td);
522 void checkExpressionNode(Descriptor md, SymbolTable nametable, ExpressionNode en, TypeDescriptor td) {
524 case Kind.AssignmentNode:
525 checkAssignmentNode(md,nametable,(AssignmentNode)en,td);
529 checkCastNode(md,nametable,(CastNode)en,td);
532 case Kind.CreateObjectNode:
533 checkCreateObjectNode(md,nametable,(CreateObjectNode)en,td);
536 case Kind.FieldAccessNode:
537 checkFieldAccessNode(md,nametable,(FieldAccessNode)en,td);
540 case Kind.ArrayAccessNode:
541 checkArrayAccessNode(md,nametable,(ArrayAccessNode)en,td);
544 case Kind.LiteralNode:
545 checkLiteralNode(md,nametable,(LiteralNode)en,td);
548 case Kind.MethodInvokeNode:
549 checkMethodInvokeNode(md,nametable,(MethodInvokeNode)en,td);
553 checkNameNode(md,nametable,(NameNode)en,td);
557 checkOpNode(md,nametable,(OpNode)en,td);
560 case Kind.OffsetNode:
561 checkOffsetNode(md, nametable, (OffsetNode)en, td);
564 case Kind.TertiaryNode:
565 checkTertiaryNode(md, nametable, (TertiaryNode)en, td);
568 case Kind.InstanceOfNode:
569 checkInstanceOfNode(md, nametable, (InstanceOfNode) en, td);
572 case Kind.ArrayInitializerNode:
573 checkArrayInitializerNode(md, nametable, (ArrayInitializerNode) en, td);
576 case Kind.ClassTypeNode:
577 checkClassTypeNode(md, nametable, (ClassTypeNode) en, td);
583 void checkClassTypeNode(Descriptor md, SymbolTable nametable, ClassTypeNode tn, TypeDescriptor td) {
584 checkTypeDescriptor(((md instanceof MethodDescriptor)?((MethodDescriptor)md).getClassDesc():null), tn.getType());
587 void checkCastNode(Descriptor md, SymbolTable nametable, CastNode cn, TypeDescriptor td) {
588 /* Get type descriptor */
589 if (cn.getType()==null) {
590 NameDescriptor typenamed=cn.getTypeName().getName();
591 TypeDescriptor ntd=new TypeDescriptor(getClass(((md instanceof MethodDescriptor)?((MethodDescriptor)md).getClassDesc():null), typenamed.toString()));
595 /* Check the type descriptor */
596 TypeDescriptor cast_type=cn.getType();
597 checkTypeDescriptor(((md instanceof MethodDescriptor)?((MethodDescriptor)md).getClassDesc():null),cast_type);
601 if (!typeutil.isSuperorType(td,cast_type))
602 throw new Error("Cast node returns "+cast_type+", but need "+td);
605 ExpressionNode en=cn.getExpression();
606 checkExpressionNode(md, nametable, en, null);
607 TypeDescriptor etd=en.getType();
608 if (typeutil.isSuperorType(cast_type,etd)) /* Cast trivially succeeds */
611 if (typeutil.isSuperorType(etd,cast_type)) /* Cast may succeed */
613 if (typeutil.isCastable(etd, cast_type))
616 /* Different branches */
617 /* TODO: change if add interfaces */
618 throw new Error("Cast will always fail\n"+cn.printNode(0));
621 void checkFieldAccessNode(Descriptor md, SymbolTable nametable, FieldAccessNode fan, TypeDescriptor td) {
622 ExpressionNode left=fan.getExpression();
623 checkExpressionNode(md,nametable,left,null);
624 TypeDescriptor ltd=left.getType();
625 String fieldname=fan.getFieldName();
627 FieldDescriptor fd=null;
628 if (ltd.isArray()&&fieldname.equals("length"))
629 fd=FieldDescriptor.arrayLength;
631 fd=(FieldDescriptor) ltd.getClassDesc().getFieldTable().get(fieldname);
633 if(ltd.isClassNameRef()) {
634 // the field access is using a class name directly
635 if(ltd.getClassDesc().isEnum()) {
636 int value = ltd.getClassDesc().getEnumConstant(fieldname);
638 // check if this field is an enum constant
639 throw new Error(fieldname + " is not an enum constant in "+fan.printNode(0)+" in "+md);
641 fd = new FieldDescriptor(new Modifiers(Modifiers.PUBLIC|Modifiers.FINAL), new TypeDescriptor(TypeDescriptor.INT), fieldname, null, false);
643 fd.setEnumValue(value);
644 } else if(fd == null) {
645 throw new Error("Could not find field "+ fieldname + " in "+fan.printNode(0)+" in "+md + " (Line: "+fan.getNumLine()+")");
646 } else if(fd.isStatic()) {
647 // check if this field is a static field
648 if(fd.getExpressionNode() != null) {
649 checkExpressionNode(md,nametable,fd.getExpressionNode(),null);
652 throw new Error("Dereference of the non-static field "+ fieldname + " in "+fan.printNode(0)+" in "+md);
657 throw new Error("Unknown field "+fieldname + " in "+fan.printNode(0)+" in "+md);
659 if (fd.getType().iswrapper()) {
660 FieldAccessNode fan2=new FieldAccessNode(left, fieldname);
661 fan2.setNumLine(left.getNumLine());
664 fan.fieldname="value";
666 ExpressionNode leftwr=fan.getExpression();
667 TypeDescriptor ltdwr=leftwr.getType();
668 String fieldnamewr=fan.getFieldName();
669 FieldDescriptor fdwr=(FieldDescriptor) ltdwr.getClassDesc().getFieldTable().get(fieldnamewr);
672 throw new Error("Unknown field "+fieldnamewr + " in "+fan.printNode(0)+" in "+md);
677 if (!typeutil.isSuperorType(td,fan.getType()))
678 throw new Error("Field node returns "+fan.getType()+", but need "+td);
682 void checkArrayAccessNode(Descriptor md, SymbolTable nametable, ArrayAccessNode aan, TypeDescriptor td) {
683 ExpressionNode left=aan.getExpression();
684 checkExpressionNode(md,nametable,left,null);
686 checkExpressionNode(md,nametable,aan.getIndex(),new TypeDescriptor(TypeDescriptor.INT));
687 TypeDescriptor ltd=left.getType();
688 if (ltd.dereference().iswrapper()) {
689 aan.wrappertype=((FieldDescriptor)ltd.dereference().getClassDesc().getFieldTable().get("value")).getType();
693 if (!typeutil.isSuperorType(td,aan.getType()))
694 throw new Error("Field node returns "+aan.getType()+", but need "+td);
697 void checkLiteralNode(Descriptor md, SymbolTable nametable, LiteralNode ln, TypeDescriptor td) {
698 /* Resolve the type */
699 Object o=ln.getValue();
700 if (ln.getTypeString().equals("null")) {
701 ln.setType(new TypeDescriptor(TypeDescriptor.NULL));
702 } else if (o instanceof Integer) {
703 ln.setType(new TypeDescriptor(TypeDescriptor.INT));
704 } else if (o instanceof Long) {
705 ln.setType(new TypeDescriptor(TypeDescriptor.LONG));
706 } else if (o instanceof Float) {
707 ln.setType(new TypeDescriptor(TypeDescriptor.FLOAT));
708 } else if (o instanceof Boolean) {
709 ln.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
710 } else if (o instanceof Double) {
711 ln.setType(new TypeDescriptor(TypeDescriptor.DOUBLE));
712 } else if (o instanceof Character) {
713 ln.setType(new TypeDescriptor(TypeDescriptor.CHAR));
714 } else if (o instanceof String) {
715 ln.setType(new TypeDescriptor(getClass(null, TypeUtil.StringClass)));
719 if (!typeutil.isSuperorType(td,ln.getType())) {
720 Long l = ln.evaluate();
721 if((ln.getType().isByte() || ln.getType().isShort()
722 || ln.getType().isChar() || ln.getType().isInt())
724 && (td.isByte() || td.isShort() || td.isChar()
725 || td.isInt() || td.isLong())) {
726 long lnvalue = l.longValue();
727 if((td.isByte() && ((lnvalue > 127) || (lnvalue < -128)))
728 || (td.isShort() && ((lnvalue > 32767) || (lnvalue < -32768)))
729 || (td.isChar() && ((lnvalue > 65535) || (lnvalue < 0)))
730 || (td.isInt() && ((lnvalue > 2147483647) || (lnvalue < -2147483648)))
731 || (td.isLong() && ((lnvalue > 9223372036854775807L) || (lnvalue < -9223372036854775808L)))) {
732 throw new Error("Field node returns "+ln.getType()+", but need "+td+" in "+md);
735 throw new Error("Field node returns "+ln.getType()+", but need "+td+" in "+md);
740 void checkNameNode(Descriptor md, SymbolTable nametable, NameNode nn, TypeDescriptor td) {
741 NameDescriptor nd=nn.getName();
742 if (nd.getBase()!=null) {
744 /* Rewrite NameNode */
745 ExpressionNode en=translateNameDescriptorintoExpression(nd,nn.getNumLine());
746 nn.setExpression(en);
747 checkExpressionNode(md,nametable,en,td);
749 String varname=nd.toString();
750 if(varname.equals("this")) {
752 nn.setVar((VarDescriptor)nametable.get("this"));
755 Descriptor d=(Descriptor)nametable.get(varname);
757 ClassDescriptor cd = null;
758 if((md instanceof MethodDescriptor) && ((MethodDescriptor)md).isStaticBlock()) {
759 // this is a static block, all the accessed fields should be static field
760 cd = ((MethodDescriptor)md).getClassDesc();
761 SymbolTable fieldtbl = cd.getFieldTable();
762 FieldDescriptor fd=(FieldDescriptor)fieldtbl.get(varname);
763 if((fd == null) || (!fd.isStatic())) {
764 // no such field in the class, check if this is a class
765 if(varname.equals("this")) {
766 throw new Error("Error: access this obj in a static block");
768 cd=getClass(((md instanceof MethodDescriptor)?((MethodDescriptor)md).getClassDesc():null), varname);
770 // this is a class name
774 throw new Error("Name "+varname+" should not be used in static block: "+md);
777 // this is a static field
783 // check if the var is a static field of the class
784 if(md instanceof MethodDescriptor) {
785 cd = ((MethodDescriptor)md).getClassDesc();
786 FieldDescriptor fd = (FieldDescriptor)cd.getFieldTable().get(varname);
787 if((fd != null) && (fd.isStatic())) {
791 if (!typeutil.isSuperorType(td,nn.getType()))
792 throw new Error("Field node returns "+nn.getType()+", but need "+td);
794 } else if(fd != null) {
795 throw new Error("Name "+varname+" should not be used in " + md);
798 cd=getClass(((md instanceof MethodDescriptor)?((MethodDescriptor)md).getClassDesc():null), varname);
800 // this is a class name
804 throw new Error("Name "+varname+" undefined in: "+md);
808 if (d instanceof VarDescriptor) {
810 } else if (d instanceof FieldDescriptor) {
811 FieldDescriptor fd=(FieldDescriptor)d;
812 if (fd.getType().iswrapper()) {
813 String id=nd.getIdentifier();
814 NameDescriptor base=nd.getBase();
815 NameNode n=new NameNode(nn.getName());
816 n.setNumLine(nn.getNumLine());
818 n.setVar((VarDescriptor)nametable.get("this")); /* Need a pointer to this */
819 FieldAccessNode fan=new FieldAccessNode(n,"value");
820 fan.setNumLine(n.getNumLine());
821 FieldDescriptor fdval=(FieldDescriptor) fd.getType().getClassDesc().getFieldTable().get("value");
823 nn.setExpression(fan);
826 nn.setVar((VarDescriptor)nametable.get("this")); /* Need a pointer to this */
828 } else if (d instanceof TagVarDescriptor) {
830 } else throw new Error("Wrong type of descriptor");
832 if (!typeutil.isSuperorType(td,nn.getType()))
833 throw new Error("Field node returns "+nn.getType()+", but need "+td);
837 void checkOffsetNode(Descriptor md, SymbolTable nameTable, OffsetNode ofn, TypeDescriptor td) {
838 TypeDescriptor ltd=ofn.td;
839 checkTypeDescriptor(((md instanceof MethodDescriptor)?((MethodDescriptor)md).getClassDesc():null),ltd);
841 String fieldname = ofn.fieldname;
842 FieldDescriptor fd=null;
843 if (ltd.isArray()&&fieldname.equals("length")) {
844 fd=FieldDescriptor.arrayLength;
846 fd=(FieldDescriptor) ltd.getClassDesc().getFieldTable().get(fieldname);
850 checkField(ltd.getClassDesc(), fd);
853 throw new Error("Unknown field "+fieldname + " in "+ofn.printNode(1)+" in "+md);
856 if (!typeutil.isSuperorType(td, ofn.getType())) {
857 System.out.println(td);
858 System.out.println(ofn.getType());
859 throw new Error("Type of rside not compatible with type of lside"+ofn.printNode(0));
865 void checkTertiaryNode(Descriptor md, SymbolTable nametable, TertiaryNode tn, TypeDescriptor td) {
866 checkExpressionNode(md, nametable, tn.getCond(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
867 checkExpressionNode(md, nametable, tn.getTrueExpr(), td);
868 checkExpressionNode(md, nametable, tn.getFalseExpr(), td);
871 void checkInstanceOfNode(Descriptor md, SymbolTable nametable, InstanceOfNode tn, TypeDescriptor td) {
872 if (td!=null&&!td.isBoolean())
873 throw new Error("Expecting type "+td+"for instanceof expression");
875 checkTypeDescriptor(((md instanceof MethodDescriptor)?((MethodDescriptor)md).getClassDesc():null), tn.getExprType());
876 checkExpressionNode(md, nametable, tn.getExpr(), null);
879 void checkArrayInitializerNode(Descriptor md, SymbolTable nametable, ArrayInitializerNode ain, TypeDescriptor td) {
880 Vector<TypeDescriptor> vec_type = new Vector<TypeDescriptor>();
881 for( int i = 0; i < ain.numVarInitializers(); ++i ) {
882 checkExpressionNode(md, nametable, ain.getVarInitializer(i), td==null?td:td.dereference());
883 vec_type.add(ain.getVarInitializer(i).getType());
885 // descide the type of this variableInitializerNode
886 TypeDescriptor out_type = vec_type.elementAt(0);
887 for(int i = 1; i < vec_type.size(); i++) {
888 TypeDescriptor tmp_type = vec_type.elementAt(i);
889 if(out_type == null) {
890 if(tmp_type != null) {
893 } else if(out_type.isNull()) {
894 if(!tmp_type.isNull() ) {
895 if(!tmp_type.isArray()) {
896 throw new Error("Error: mixed type in var initializer list");
901 } else if(out_type.isArray()) {
902 if(tmp_type.isArray()) {
903 if(tmp_type.getArrayCount() > out_type.getArrayCount()) {
906 } else if((tmp_type != null) && (!tmp_type.isNull())) {
907 throw new Error("Error: mixed type in var initializer list");
909 } else if(out_type.isInt()) {
910 if(!tmp_type.isInt()) {
911 throw new Error("Error: mixed type in var initializer list");
913 } else if(out_type.isString()) {
914 if(!tmp_type.isString()) {
915 throw new Error("Error: mixed type in var initializer list");
919 if(out_type != null) {
920 out_type = out_type.makeArray(state);
921 //out_type.setStatic();
923 ain.setType(out_type);
926 void checkAssignmentNode(Descriptor md, SymbolTable nametable, AssignmentNode an, TypeDescriptor td) {
927 boolean postinc=true;
928 if (an.getOperation().getBaseOp()==null||
929 (an.getOperation().getBaseOp().getOp()!=Operation.POSTINC&&
930 an.getOperation().getBaseOp().getOp()!=Operation.POSTDEC))
933 checkExpressionNode(md, nametable, an.getSrc(),td);
934 //TODO: Need check on validity of operation here
935 if (!((an.getDest() instanceof FieldAccessNode)||
936 (an.getDest() instanceof ArrayAccessNode)||
937 (an.getDest() instanceof NameNode)))
938 throw new Error("Bad lside in "+an.printNode(0));
939 checkExpressionNode(md, nametable, an.getDest(), null);
941 /* We want parameter variables to tasks to be immutable */
942 if (md instanceof TaskDescriptor) {
943 if (an.getDest() instanceof NameNode) {
944 NameNode nn=(NameNode)an.getDest();
945 if (nn.getVar()!=null) {
946 if (((TaskDescriptor)md).getParameterTable().contains(nn.getVar().getSymbol()))
947 throw new Error("Can't modify parameter "+nn.getVar()+ " to task "+td.getSymbol());
952 if (an.getDest().getType().isString()&&an.getOperation().getOp()==AssignOperation.PLUSEQ) {
954 ClassDescriptor stringcl=getClass(null, TypeUtil.StringClass);
955 TypeDescriptor stringtd=new TypeDescriptor(stringcl);
956 NameDescriptor nd=new NameDescriptor("String");
957 NameDescriptor valuend=new NameDescriptor(nd, "valueOf");
959 if (!(an.getSrc().getType().isString()&&(an.getSrc() instanceof OpNode))) {
960 MethodInvokeNode rightmin=new MethodInvokeNode(valuend);
961 rightmin.setNumLine(an.getSrc().getNumLine());
962 rightmin.addArgument(an.getSrc());
964 checkExpressionNode(md, nametable, an.getSrc(), null);
968 if (!postinc&&!typeutil.isSuperorType(an.getDest().getType(),an.getSrc().getType())) {
969 TypeDescriptor dt = an.getDest().getType();
970 TypeDescriptor st = an.getSrc().getType();
971 if(an.getSrc().kind() == Kind.ArrayInitializerNode) {
972 if(dt.getArrayCount() != st.getArrayCount()) {
973 throw new Error("Type of rside ("+an.getSrc().getType().toPrettyString()+") not compatible with type of lside ("+an.getDest().getType().toPrettyString()+")"+an.printNode(0));
976 dt = dt.dereference();
977 st = st.dereference();
978 } while(dt.isArray());
979 if((st.isByte() || st.isShort() || st.isChar() || st.isInt())
980 && (dt.isByte() || dt.isShort() || dt.isChar() || dt.isInt() || dt.isLong())) {
983 throw new Error("Type of rside ("+an.getSrc().getType().toPrettyString()+") not compatible with type of lside ("+an.getDest().getType().toPrettyString()+")"+an.printNode(0));
987 Long l = an.getSrc().evaluate();
988 if((st.isByte() || st.isShort() || st.isChar() || st.isInt())
990 && (dt.isByte() || dt.isShort() || dt.isChar() || dt.isInt() || dt.isLong())) {
991 long lnvalue = l.longValue();
992 if((dt.isByte() && ((lnvalue > 127) || (lnvalue < -128)))
993 || (dt.isShort() && ((lnvalue > 32767) || (lnvalue < -32768)))
994 || (dt.isChar() && ((lnvalue > 65535) || (lnvalue < 0)))
995 || (dt.isInt() && ((lnvalue > 2147483647) || (lnvalue < -2147483648)))
996 || (dt.isLong() && ((lnvalue > 9223372036854775807L) || (lnvalue < -9223372036854775808L)))) {
997 throw new Error("Type of rside ("+an.getSrc().getType().toPrettyString()+") not compatible with type of lside ("+an.getDest().getType().toPrettyString()+")"+an.printNode(0));
1000 throw new Error("Type of rside ("+an.getSrc().getType().toPrettyString()+") not compatible with type of lside ("+an.getDest().getType().toPrettyString()+")"+an.printNode(0));
1006 void checkLoopNode(Descriptor md, SymbolTable nametable, LoopNode ln) {
1008 if (ln.getType()==LoopNode.WHILELOOP||ln.getType()==LoopNode.DOWHILELOOP) {
1009 checkExpressionNode(md, nametable, ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
1010 checkBlockNode(md, nametable, ln.getBody());
1013 /* Link in the initializer naming environment */
1014 BlockNode bn=ln.getInitializer();
1015 bn.getVarTable().setParent(nametable);
1016 for(int i=0; i<bn.size(); i++) {
1017 BlockStatementNode bsn=bn.get(i);
1018 checkBlockStatementNode(md, bn.getVarTable(),bsn);
1020 //check the condition
1021 checkExpressionNode(md, bn.getVarTable(), ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
1022 checkBlockNode(md, bn.getVarTable(), ln.getBody());
1023 checkBlockNode(md, bn.getVarTable(), ln.getUpdate());
1029 void checkCreateObjectNode(Descriptor md, SymbolTable nametable, CreateObjectNode con,
1030 TypeDescriptor td) {
1031 TypeDescriptor[] tdarray = new TypeDescriptor[con.numArgs()];
1032 for (int i = 0; i < con.numArgs(); i++) {
1033 ExpressionNode en = con.getArg(i);
1034 checkExpressionNode(md, nametable, en, null);
1035 tdarray[i] = en.getType();
1038 TypeDescriptor typetolookin = con.getType();
1039 checkTypeDescriptor(((md instanceof MethodDescriptor)?((MethodDescriptor)md).getClassDesc():null), typetolookin);
1041 if (td != null && !typeutil.isSuperorType(td, typetolookin))
1042 throw new Error(typetolookin + " isn't a " + td);
1044 /* Check Array Initializers */
1045 if ((con.getArrayInitializer() != null)) {
1046 checkArrayInitializerNode(md, nametable, con.getArrayInitializer(), td);
1049 /* Check flag effects */
1050 if (con.getFlagEffects() != null) {
1051 FlagEffects fe = con.getFlagEffects();
1052 ClassDescriptor cd = typetolookin.getClassDesc();
1054 for (int j = 0; j < fe.numEffects(); j++) {
1055 FlagEffect flag = fe.getEffect(j);
1056 String name = flag.getName();
1057 FlagDescriptor flag_d = (FlagDescriptor) cd.getFlagTable().get(name);
1058 // Make sure the flag is declared
1060 throw new Error("Flag descriptor " + name + " undefined in class: " + cd.getSymbol());
1061 if (flag_d.getExternal())
1062 throw new Error("Attempting to modify external flag: " + name);
1063 flag.setFlag(flag_d);
1065 for (int j = 0; j < fe.numTagEffects(); j++) {
1066 TagEffect tag = fe.getTagEffect(j);
1067 String name = tag.getName();
1069 Descriptor d = (Descriptor) nametable.get(name);
1071 throw new Error("Tag descriptor " + name + " undeclared");
1072 else if (!(d instanceof TagVarDescriptor))
1073 throw new Error(name + " is not a tag descriptor");
1074 tag.setTag((TagVarDescriptor) d);
1078 if ((!typetolookin.isClass()) && (!typetolookin.isArray()))
1079 throw new Error("Can't allocate primitive type:" + con.printNode(0));
1081 if (!typetolookin.isArray()) {
1082 // Array's don't need constructor calls
1083 ClassDescriptor classtolookin = typetolookin.getClassDesc();
1084 checkClass(classtolookin, INIT);
1086 Set methoddescriptorset = classtolookin.getMethodTable().getSet(typetolookin.getSymbol());
1087 MethodDescriptor bestmd = null;
1088 NextMethod: for (Iterator methodit = methoddescriptorset.iterator(); methodit.hasNext(); ) {
1089 MethodDescriptor currmd = (MethodDescriptor) methodit.next();
1090 /* Need correct number of parameters */
1091 if (con.numArgs() != currmd.numParameters())
1093 for (int i = 0; i < con.numArgs(); i++) {
1094 if (!typeutil.isSuperorType(currmd.getParamType(i), tdarray[i]))
1095 continue NextMethod;
1097 /* Local allocations can't call global allocator */
1098 if (!con.isGlobal() && currmd.isGlobal())
1101 /* Method okay so far */
1105 if (typeutil.isMoreSpecific(currmd, bestmd)) {
1107 } else if (con.isGlobal() && match(currmd, bestmd)) {
1108 if (currmd.isGlobal() && !bestmd.isGlobal())
1110 else if (currmd.isGlobal() && bestmd.isGlobal())
1112 } else if (!typeutil.isMoreSpecific(bestmd, currmd)) {
1113 throw new Error("No method is most specific:" + bestmd + " and " + currmd);
1116 /* Is this more specific than bestmd */
1120 throw new Error("No method found for " + con.printNode(0) + " in " + md);
1121 con.setConstructor(bestmd);
1126 /** Check to see if md1 is the same specificity as md2.*/
1128 boolean match(MethodDescriptor md1, MethodDescriptor md2) {
1129 /* Checks if md1 is more specific than md2 */
1130 if (md1.numParameters()!=md2.numParameters())
1132 for(int i=0; i<md1.numParameters(); i++) {
1133 if (!md2.getParamType(i).equals(md1.getParamType(i)))
1136 if (!md2.getReturnType().equals(md1.getReturnType()))
1139 if (!md2.getClassDesc().equals(md1.getClassDesc()))
1147 ExpressionNode translateNameDescriptorintoExpression(NameDescriptor nd, int numLine) {
1148 String id=nd.getIdentifier();
1149 NameDescriptor base=nd.getBase();
1151 NameNode nn=new NameNode(nd);
1152 nn.setNumLine(numLine);
1155 FieldAccessNode fan=new FieldAccessNode(translateNameDescriptorintoExpression(base,numLine),id);
1156 fan.setNumLine(numLine);
1162 void checkMethodInvokeNode(Descriptor md, SymbolTable nametable, MethodInvokeNode min, TypeDescriptor td) {
1163 /*Typecheck subexpressions
1164 and get types for expressions*/
1166 boolean isstatic = false;
1167 if((md instanceof MethodDescriptor) && ((MethodDescriptor)md).isStatic()) {
1170 TypeDescriptor[] tdarray=new TypeDescriptor[min.numArgs()];
1171 for(int i=0; i<min.numArgs(); i++) {
1172 ExpressionNode en=min.getArg(i);
1173 checkExpressionNode(md,nametable,en,null);
1174 tdarray[i]=en.getType();
1175 if(en.getType().isClass() && en.getType().getClassDesc().isEnum()) {
1176 tdarray[i] = new TypeDescriptor(TypeDescriptor.INT);
1179 TypeDescriptor typetolookin=null;
1180 if (min.getExpression()!=null) {
1181 checkExpressionNode(md,nametable,min.getExpression(),null);
1182 typetolookin=min.getExpression().getType();
1183 //if (typetolookin==null)
1184 //throw new Error(md+" has null return type");
1186 } else if (min.getBaseName()!=null) {
1187 String rootname=min.getBaseName().getRoot();
1188 if (rootname.equals("super")) {
1189 ClassDescriptor supercd=((MethodDescriptor)md).getClassDesc().getSuperDesc();
1190 typetolookin=new TypeDescriptor(supercd);
1192 } else if (rootname.equals("this")) {
1194 throw new Error("use this object in static method md = "+ md.toString());
1196 ClassDescriptor cd=((MethodDescriptor)md).getClassDesc();
1197 typetolookin=new TypeDescriptor(cd);
1198 } else if (nametable.get(rootname)!=null) {
1199 //we have an expression
1200 min.setExpression(translateNameDescriptorintoExpression(min.getBaseName(),min.getNumLine()));
1201 checkExpressionNode(md, nametable, min.getExpression(), null);
1202 typetolookin=min.getExpression().getType();
1204 if(!min.getBaseName().getSymbol().equals("System.out")||state.JNI) {
1205 ExpressionNode nn = translateNameDescriptorintoExpression(min.getBaseName(),min.getNumLine());
1206 checkExpressionNode(md, nametable, nn, null);
1207 typetolookin = nn.getType();
1208 if(!((nn.kind()== Kind.NameNode) && (((NameNode)nn).getField() == null)
1209 && (((NameNode)nn).getVar() == null) && (((NameNode)nn).getExpression() == null))) {
1210 // this is not a pure class name, need to add to
1211 min.setExpression(nn);
1215 ClassDescriptor cd = getClass(null, "System");
1218 throw new Error("md = "+ md.toString()+ " "+min.getBaseName()+" undefined");
1219 typetolookin=new TypeDescriptor(cd);
1222 } else if ((md instanceof MethodDescriptor)&&min.getMethodName().equals("super")) {
1223 ClassDescriptor supercd=((MethodDescriptor)md).getClassDesc().getSuperDesc();
1224 min.methodid=supercd.getSymbol();
1225 typetolookin=new TypeDescriptor(supercd);
1226 } else if (md instanceof MethodDescriptor) {
1227 typetolookin=new TypeDescriptor(((MethodDescriptor)md).getClassDesc());
1229 /* If this a task descriptor we throw an error at this point */
1230 throw new Error("Unknown method call to "+min.getMethodName()+"in task"+md.getSymbol());
1232 if (!typetolookin.isClass())
1233 throw new Error("Error with method call to "+min.getMethodName());
1234 ClassDescriptor classtolookin=typetolookin.getClassDesc();
1235 checkClass(classtolookin, INIT);
1236 //System.out.println("Method name="+min.getMethodName());
1238 Set methoddescriptorset=classtolookin.getMethodTable().getSet(min.getMethodName());
1239 MethodDescriptor bestmd=null;
1241 for(Iterator methodit=methoddescriptorset.iterator(); methodit.hasNext(); ) {
1242 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
1243 /* Need correct number of parameters */
1244 if (min.numArgs()!=currmd.numParameters())
1246 for(int i=0; i<min.numArgs(); i++) {
1247 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
1248 if(((!tdarray[i].isArray() &&(tdarray[i].isInt() || tdarray[i].isLong()))
1249 && currmd.getParamType(i).isClass() && currmd.getParamType(i).getClassDesc().getSymbol().equals("Object"))) {
1250 // primitive parameters vs object
1252 continue NextMethod;
1255 /* Method okay so far */
1259 if (typeutil.isMoreSpecific(currmd,bestmd)) {
1261 } else if (!typeutil.isMoreSpecific(bestmd, currmd))
1262 throw new Error("No method is most specific:"+bestmd+" and "+currmd);
1264 /* Is this more specific than bestmd */
1268 throw new Error("No method found for :"+min.printNode(0)+" in class: " + classtolookin+" in "+md);
1269 min.setMethod(bestmd);
1271 if ((td!=null)&&(min.getType()!=null)&&!typeutil.isSuperorType(td, min.getType()))
1272 throw new Error(min.getType()+ " is not equal to or a subclass of "+td);
1273 /* Check whether we need to set this parameter to implied this */
1274 if (!isstatic && !bestmd.isStatic()) {
1275 if (min.getExpression()==null) {
1276 ExpressionNode en=new NameNode(new NameDescriptor("this"));
1277 min.setExpression(en);
1278 checkExpressionNode(md, nametable, min.getExpression(), null);
1282 /* Check if we need to wrap primitive paratmeters to objects */
1283 for(int i=0; i<min.numArgs(); i++) {
1284 if(!tdarray[i].isArray() && (tdarray[i].isInt() || tdarray[i].isLong())
1285 && min.getMethod().getParamType(i).isClass() && min.getMethod().getParamType(i).getClassDesc().getSymbol().equals("Object")) {
1286 // Shall wrap this primitive parameter as a object
1287 ExpressionNode exp = min.getArg(i);
1288 TypeDescriptor ptd = null;
1289 NameDescriptor nd=null;
1290 if(exp.getType().isInt()) {
1291 nd = new NameDescriptor("Integer");
1292 ptd = state.getTypeDescriptor(nd);
1293 } else if(exp.getType().isLong()) {
1294 nd = new NameDescriptor("Long");
1295 ptd = state.getTypeDescriptor(nd);
1297 boolean isglobal = false;
1298 String disjointId = null;
1299 CreateObjectNode con=new CreateObjectNode(ptd, isglobal, disjointId);
1300 con.addArgument(exp);
1301 checkExpressionNode(md, nametable, con, null);
1302 min.setArgument(con, i);
1308 void checkOpNode(Descriptor md, SymbolTable nametable, OpNode on, TypeDescriptor td) {
1309 checkExpressionNode(md, nametable, on.getLeft(), null);
1310 if (on.getRight()!=null)
1311 checkExpressionNode(md, nametable, on.getRight(), null);
1312 TypeDescriptor ltd=on.getLeft().getType();
1313 TypeDescriptor rtd=on.getRight()!=null?on.getRight().getType():null;
1314 TypeDescriptor lefttype=null;
1315 TypeDescriptor righttype=null;
1316 Operation op=on.getOp();
1318 switch(op.getOp()) {
1319 case Operation.LOGIC_OR:
1320 case Operation.LOGIC_AND:
1321 if (!(rtd.isBoolean()))
1323 on.setRightType(rtd);
1325 case Operation.LOGIC_NOT:
1326 if (!(ltd.isBoolean()))
1329 on.setLeftType(ltd);
1331 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1334 case Operation.COMP:
1335 // 5.6.2 Binary Numeric Promotion
1336 //TODO unboxing of reference objects
1339 else if (ltd.isFloat())
1341 else if (ltd.isLong())
1342 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1344 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1345 on.setLeftType(lefttype);
1346 on.setType(lefttype);
1349 case Operation.BIT_OR:
1350 case Operation.BIT_XOR:
1351 case Operation.BIT_AND:
1352 // 5.6.2 Binary Numeric Promotion
1353 //TODO unboxing of reference objects
1354 if (ltd.isDouble()||rtd.isDouble())
1356 else if (ltd.isFloat()||rtd.isFloat())
1358 else if (ltd.isLong()||rtd.isLong())
1359 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1360 // 090205 hack for boolean
1361 else if (ltd.isBoolean()||rtd.isBoolean())
1362 lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
1364 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1367 on.setLeftType(lefttype);
1368 on.setRightType(righttype);
1369 on.setType(lefttype);
1372 case Operation.ISAVAILABLE:
1373 if (!(ltd.isPtr())) {
1374 throw new Error("Can't use isavailable on non-pointers/non-parameters.");
1377 on.setLeftType(lefttype);
1378 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1381 case Operation.EQUAL:
1382 case Operation.NOTEQUAL:
1383 // 5.6.2 Binary Numeric Promotion
1384 //TODO unboxing of reference objects
1385 if (ltd.isBoolean()||rtd.isBoolean()) {
1386 if (!(ltd.isBoolean()&&rtd.isBoolean()))
1388 righttype=lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
1389 } else if (ltd.isPtr()||rtd.isPtr()) {
1390 if (!(ltd.isPtr()&&rtd.isPtr())) {
1397 } else if (ltd.isDouble()||rtd.isDouble())
1398 righttype=lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
1399 else if (ltd.isFloat()||rtd.isFloat())
1400 righttype=lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
1401 else if (ltd.isLong()||rtd.isLong())
1402 righttype=lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1404 righttype=lefttype=new TypeDescriptor(TypeDescriptor.INT);
1406 on.setLeftType(lefttype);
1407 on.setRightType(righttype);
1408 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1417 // 5.6.2 Binary Numeric Promotion
1418 //TODO unboxing of reference objects
1419 if (!ltd.isNumber()||!rtd.isNumber()) {
1420 if (!ltd.isNumber())
1421 throw new Error("Leftside is not number"+on.printNode(0)+"type="+ltd.toPrettyString());
1422 if (!rtd.isNumber())
1423 throw new Error("Rightside is not number"+on.printNode(0));
1426 if (ltd.isDouble()||rtd.isDouble())
1427 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
1428 else if (ltd.isFloat()||rtd.isFloat())
1429 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
1430 else if (ltd.isLong()||rtd.isLong())
1431 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1433 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1435 on.setLeftType(lefttype);
1436 on.setRightType(righttype);
1437 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1441 if (ltd.isString()||rtd.isString()) {
1442 ClassDescriptor stringcl=getClass(null, TypeUtil.StringClass);
1443 TypeDescriptor stringtd=new TypeDescriptor(stringcl);
1444 NameDescriptor nd=new NameDescriptor("String");
1445 NameDescriptor valuend=new NameDescriptor(nd, "valueOf");
1446 if (!(ltd.isString()&&(on.getLeft() instanceof OpNode))) {
1447 MethodInvokeNode leftmin=new MethodInvokeNode(valuend);
1448 leftmin.setNumLine(on.getLeft().getNumLine());
1449 leftmin.addArgument(on.getLeft());
1451 checkExpressionNode(md, nametable, on.getLeft(), null);
1454 if (!(rtd.isString()&&(on.getRight() instanceof OpNode))) {
1455 MethodInvokeNode rightmin=new MethodInvokeNode(valuend);
1456 rightmin.setNumLine(on.getRight().getNumLine());
1457 rightmin.addArgument(on.getRight());
1459 checkExpressionNode(md, nametable, on.getRight(), null);
1462 on.setLeftType(stringtd);
1463 on.setRightType(stringtd);
1464 on.setType(stringtd);
1469 case Operation.MULT:
1472 // 5.6.2 Binary Numeric Promotion
1473 //TODO unboxing of reference objects
1474 if (ltd.isArray()||rtd.isArray()||!ltd.isNumber()||!rtd.isNumber())
1475 throw new Error("Error in "+on.printNode(0));
1477 if (ltd.isDouble()||rtd.isDouble())
1478 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
1479 else if (ltd.isFloat()||rtd.isFloat())
1480 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
1481 else if (ltd.isLong()||rtd.isLong())
1482 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1484 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1486 on.setLeftType(lefttype);
1487 on.setRightType(righttype);
1488 on.setType(lefttype);
1491 case Operation.LEFTSHIFT:
1492 case Operation.RIGHTSHIFT:
1493 case Operation.URIGHTSHIFT:
1494 if (!rtd.isIntegerType())
1496 //5.6.1 Unary Numeric Promotion
1497 if (rtd.isByte()||rtd.isShort()||rtd.isInt())
1498 righttype=new TypeDescriptor(TypeDescriptor.INT);
1502 on.setRightType(righttype);
1503 if (!ltd.isIntegerType())
1506 case Operation.UNARYPLUS:
1507 case Operation.UNARYMINUS:
1508 /* case Operation.POSTINC:
1509 case Operation.POSTDEC:
1510 case Operation.PREINC:
1511 case Operation.PREDEC:*/
1512 if (!ltd.isNumber())
1514 //5.6.1 Unary Numeric Promotion
1515 if (ltd.isByte()||ltd.isShort()||ltd.isInt())
1516 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1519 on.setLeftType(lefttype);
1520 on.setType(lefttype);
1524 throw new Error(op.toString());
1528 if (!typeutil.isSuperorType(td, on.getType())) {
1529 System.out.println(td);
1530 System.out.println(on.getType());
1531 throw new Error("Type of rside not compatible with type of lside"+on.printNode(0));