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);
41 public ClassDescriptor getClass(ClassDescriptor context, String classname, int fullcheck) {
43 Hashtable remaptable=context.getSingleImportMappings();
44 classname=remaptable.containsKey(classname)?((String)remaptable.get(classname)):classname;
46 ClassDescriptor cd=typeutil.getClass(classname, toanalyze);
47 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 int index = name.lastIndexOf('.');
148 name = name.substring(index+1);
150 ClassDescriptor field_cd=checkAll?getClass(cd, name):getClass(cd, name, REFERENCE);
153 throw new Error("Undefined class "+name);
154 td.setClassDescriptor(field_cd);
156 } else if (td.isTag())
162 public void checkField(ClassDescriptor cd, FieldDescriptor fd) {
163 checkTypeDescriptor(cd, fd.getType());
166 public void checkConstraintCheck(TaskDescriptor td, SymbolTable nametable, Vector ccs) {
168 return; /* No constraint checks to check */
169 for(int i=0; i<ccs.size(); i++) {
170 ConstraintCheck cc=(ConstraintCheck) ccs.get(i);
172 for(int j=0; j<cc.numArgs(); j++) {
173 ExpressionNode en=cc.getArg(j);
174 checkExpressionNode(td,nametable,en,null);
179 public void checkFlagEffects(TaskDescriptor td, Vector vfe, SymbolTable nametable) {
181 return; /* No flag effects to check */
182 for(int i=0; i<vfe.size(); i++) {
183 FlagEffects fe=(FlagEffects) vfe.get(i);
184 String varname=fe.getName();
185 //Make sure the variable is declared as a parameter to the task
186 VarDescriptor vd=(VarDescriptor)td.getParameterTable().get(varname);
188 throw new Error("Parameter "+varname+" in Flag Effects not declared in "+td);
191 //Make sure it correspods to a class
192 TypeDescriptor type_d=vd.getType();
193 if (!type_d.isClass())
194 throw new Error("Cannot have non-object argument for flag_effect");
196 ClassDescriptor cd=type_d.getClassDesc();
197 for(int j=0; j<fe.numEffects(); j++) {
198 FlagEffect flag=fe.getEffect(j);
199 String name=flag.getName();
200 FlagDescriptor flag_d=(FlagDescriptor)cd.getFlagTable().get(name);
201 //Make sure the flag is declared
203 throw new Error("Flag descriptor "+name+" undefined in class: "+cd.getSymbol());
204 if (flag_d.getExternal())
205 throw new Error("Attempting to modify external flag: "+name);
206 flag.setFlag(flag_d);
208 for(int j=0; j<fe.numTagEffects(); j++) {
209 TagEffect tag=fe.getTagEffect(j);
210 String name=tag.getName();
212 Descriptor d=(Descriptor)nametable.get(name);
214 throw new Error("Tag descriptor "+name+" undeclared");
215 else if (!(d instanceof TagVarDescriptor))
216 throw new Error(name+" is not a tag descriptor");
217 tag.setTag((TagVarDescriptor)d);
222 public void checkTask(TaskDescriptor td) {
223 for(int i=0; i<td.numParameters(); i++) {
224 /* Check that parameter is well typed */
225 TypeDescriptor param_type=td.getParamType(i);
226 checkTypeDescriptor(null, param_type);
228 /* Check the parameter's flag expression is well formed */
229 FlagExpressionNode fen=td.getFlag(td.getParameter(i));
230 if (!param_type.isClass())
231 throw new Error("Cannot have non-object argument to a task");
232 ClassDescriptor cd=param_type.getClassDesc();
234 checkFlagExpressionNode(cd, fen);
237 checkFlagEffects(td, td.getFlagEffects(),td.getParameterTable());
238 /* Check that the task code is valid */
239 BlockNode bn=state.getMethodBody(td);
240 checkBlockNode(td, td.getParameterTable(),bn);
243 public void checkFlagExpressionNode(ClassDescriptor cd, FlagExpressionNode fen) {
245 case Kind.FlagOpNode:
247 FlagOpNode fon=(FlagOpNode)fen;
248 checkFlagExpressionNode(cd, fon.getLeft());
249 if (fon.getRight()!=null)
250 checkFlagExpressionNode(cd, fon.getRight());
256 FlagNode fn=(FlagNode)fen;
257 String name=fn.getFlagName();
258 FlagDescriptor fd=(FlagDescriptor)cd.getFlagTable().get(name);
260 throw new Error("Undeclared flag: "+name);
266 throw new Error("Unrecognized FlagExpressionNode");
270 public void checkMethod(ClassDescriptor cd, MethodDescriptor md) {
271 /* Check for abstract methods */
272 if(md.isAbstract()) {
273 if(!cd.isAbstract() && !cd.isInterface()) {
274 throw new Error("Error! The non-abstract Class " + cd.getSymbol() + " contains an abstract method " + md.getSymbol());
278 /* Check return type */
279 if (!md.isConstructor() && !md.isStaticBlock())
280 if (!md.getReturnType().isVoid()) {
281 checkTypeDescriptor(cd, md.getReturnType());
283 for(int i=0; i<md.numParameters(); i++) {
284 TypeDescriptor param_type=md.getParamType(i);
285 checkTypeDescriptor(cd, param_type);
287 /* Link the naming environments */
288 if (!md.isStatic()) /* Fields aren't accessible directly in a static method, so don't link in this table */
289 md.getParameterTable().setParent(cd.getFieldTable());
291 if (!md.isStatic()) {
292 VarDescriptor thisvd=new VarDescriptor(new TypeDescriptor(cd),"this");
295 if(md.isDefaultConstructor() && (cd.getSuperDesc() != null)) {
296 // add the construction of it super class, can only be super()
297 NameDescriptor nd=new NameDescriptor("super");
298 MethodInvokeNode min=new MethodInvokeNode(nd);
299 BlockExpressionNode ben=new BlockExpressionNode(min);
300 BlockNode bn = state.getMethodBody(md);
301 bn.addFirstBlockStatement(ben);
305 public void checkMethodBody(ClassDescriptor cd, MethodDescriptor md) {
306 ClassDescriptor superdesc=cd.getSuperDesc();
307 if (superdesc!=null) {
308 Set possiblematches=superdesc.getMethodTable().getSet(md.getSymbol());
309 for(Iterator methodit=possiblematches.iterator(); methodit.hasNext();) {
310 MethodDescriptor matchmd=(MethodDescriptor)methodit.next();
311 if (md.matches(matchmd)) {
312 if (matchmd.getModifiers().isFinal()) {
313 throw new Error("Try to override final method in method:"+md+" declared in "+cd);
318 BlockNode bn=state.getMethodBody(md);
319 checkBlockNode(md, md.getParameterTable(),bn);
322 public void checkBlockNode(Descriptor md, SymbolTable nametable, BlockNode bn) {
323 /* Link in the naming environment */
324 bn.getVarTable().setParent(nametable);
325 for(int i=0; i<bn.size(); i++) {
326 BlockStatementNode bsn=bn.get(i);
327 checkBlockStatementNode(md, bn.getVarTable(),bsn);
331 public void checkBlockStatementNode(Descriptor md, SymbolTable nametable, BlockStatementNode bsn) {
333 case Kind.BlockExpressionNode:
334 checkBlockExpressionNode(md, nametable,(BlockExpressionNode)bsn);
337 case Kind.DeclarationNode:
338 checkDeclarationNode(md, nametable, (DeclarationNode)bsn);
341 case Kind.TagDeclarationNode:
342 checkTagDeclarationNode(md, nametable, (TagDeclarationNode)bsn);
345 case Kind.IfStatementNode:
346 checkIfStatementNode(md, nametable, (IfStatementNode)bsn);
349 case Kind.SwitchStatementNode:
350 checkSwitchStatementNode(md, nametable, (SwitchStatementNode)bsn);
354 checkLoopNode(md, nametable, (LoopNode)bsn);
357 case Kind.ReturnNode:
358 checkReturnNode(md, nametable, (ReturnNode)bsn);
361 case Kind.TaskExitNode:
362 checkTaskExitNode(md, nametable, (TaskExitNode)bsn);
365 case Kind.SubBlockNode:
366 checkSubBlockNode(md, nametable, (SubBlockNode)bsn);
369 case Kind.AtomicNode:
370 checkAtomicNode(md, nametable, (AtomicNode)bsn);
373 case Kind.SynchronizedNode:
374 checkSynchronizedNode(md, nametable, (SynchronizedNode)bsn);
377 case Kind.ContinueBreakNode:
378 checkContinueBreakNode(md, nametable, (ContinueBreakNode) bsn);
382 case Kind.GenReachNode:
383 // do nothing, no semantic check for SESEs
390 void checkBlockExpressionNode(Descriptor md, SymbolTable nametable, BlockExpressionNode ben) {
391 checkExpressionNode(md, nametable, ben.getExpression(), null);
394 void checkDeclarationNode(Descriptor md, SymbolTable nametable, DeclarationNode dn) {
395 VarDescriptor vd=dn.getVarDescriptor();
396 checkTypeDescriptor(((md instanceof MethodDescriptor)?((MethodDescriptor)md).getClassDesc():null), vd.getType());
397 Descriptor d=nametable.get(vd.getSymbol());
399 (d instanceof FieldDescriptor)) {
402 throw new Error(vd.getSymbol()+" in "+md+" defined a second time");
403 if (dn.getExpression()!=null)
404 checkExpressionNode(md, nametable, dn.getExpression(), vd.getType());
407 void checkTagDeclarationNode(Descriptor md, SymbolTable nametable, TagDeclarationNode dn) {
408 TagVarDescriptor vd=dn.getTagVarDescriptor();
409 Descriptor d=nametable.get(vd.getSymbol());
411 (d instanceof FieldDescriptor)) {
414 throw new Error(vd.getSymbol()+" defined a second time");
417 void checkSubBlockNode(Descriptor md, SymbolTable nametable, SubBlockNode sbn) {
418 checkBlockNode(md, nametable, sbn.getBlockNode());
421 void checkAtomicNode(Descriptor md, SymbolTable nametable, AtomicNode sbn) {
422 checkBlockNode(md, nametable, sbn.getBlockNode());
425 void checkSynchronizedNode(Descriptor md, SymbolTable nametable, SynchronizedNode sbn) {
426 checkBlockNode(md, nametable, sbn.getBlockNode());
427 //todo this could be Object
428 checkExpressionNode(md, nametable, sbn.getExpr(), null);
431 void checkContinueBreakNode(Descriptor md, SymbolTable nametable, ContinueBreakNode cbn) {
432 if (loopstack.empty())
433 throw new Error("continue/break outside of loop");
434 Object o = loopstack.peek();
435 if(o instanceof LoopNode) {
436 LoopNode ln=(LoopNode)o;
441 void checkReturnNode(Descriptor d, SymbolTable nametable, ReturnNode rn) {
442 if (d instanceof TaskDescriptor)
443 throw new Error("Illegal return appears in Task: "+d.getSymbol());
444 MethodDescriptor md=(MethodDescriptor)d;
445 if (rn.getReturnExpression()!=null)
446 if (md.getReturnType()==null)
447 throw new Error("Constructor can't return something.");
448 else if (md.getReturnType().isVoid())
449 throw new Error(md+" is void");
451 checkExpressionNode(md, nametable, rn.getReturnExpression(), md.getReturnType());
453 if (md.getReturnType()!=null&&!md.getReturnType().isVoid())
454 throw new Error("Need to return something for "+md);
457 void checkTaskExitNode(Descriptor md, SymbolTable nametable, TaskExitNode ten) {
458 if (md instanceof MethodDescriptor)
459 throw new Error("Illegal taskexit appears in Method: "+md.getSymbol());
460 checkFlagEffects((TaskDescriptor)md, ten.getFlagEffects(),nametable);
461 checkConstraintCheck((TaskDescriptor) md, nametable, ten.getChecks());
464 void checkIfStatementNode(Descriptor md, SymbolTable nametable, IfStatementNode isn) {
465 checkExpressionNode(md, nametable, isn.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
466 checkBlockNode(md, nametable, isn.getTrueBlock());
467 if (isn.getFalseBlock()!=null)
468 checkBlockNode(md, nametable, isn.getFalseBlock());
471 void checkSwitchStatementNode(Descriptor md, SymbolTable nametable, SwitchStatementNode ssn) {
472 checkExpressionNode(md, nametable, ssn.getCondition(), new TypeDescriptor(TypeDescriptor.INT));
474 BlockNode sbn = ssn.getSwitchBody();
475 boolean hasdefault = false;
476 for(int i = 0; i < sbn.size(); i++) {
477 boolean containdefault = checkSwitchBlockNode(md, nametable, (SwitchBlockNode)sbn.get(i));
478 if(hasdefault && containdefault) {
479 throw new Error("Error: duplicate default branch in switch-case statement in Method: " + md.getSymbol());
481 hasdefault = containdefault;
485 boolean checkSwitchBlockNode(Descriptor md, SymbolTable nametable, SwitchBlockNode sbn) {
486 Vector<SwitchLabelNode> slnv = sbn.getSwitchConditions();
488 for(int i = 0; i < slnv.size(); i++) {
489 if(slnv.elementAt(i).isdefault) {
492 checkConstantExpressionNode(md, nametable, slnv.elementAt(i).getCondition(), new TypeDescriptor(TypeDescriptor.INT));
496 throw new Error("Error: duplicate default branch in switch-case statement in Method: " + md.getSymbol());
499 checkBlockNode(md, nametable, sbn.getSwitchBlockStatement());
501 return (defaultb > 0);
505 void checkConstantExpressionNode(Descriptor md, SymbolTable nametable, ExpressionNode en, TypeDescriptor td) {
507 case Kind.FieldAccessNode:
508 checkFieldAccessNode(md,nametable,(FieldAccessNode)en,td);
511 case Kind.LiteralNode:
512 checkLiteralNode(md,nametable,(LiteralNode)en,td);
516 checkNameNode(md,nametable,(NameNode)en,td);
520 checkOpNode(md, nametable, (OpNode)en, td);
526 void checkExpressionNode(Descriptor md, SymbolTable nametable, ExpressionNode en, TypeDescriptor td) {
528 case Kind.AssignmentNode:
529 checkAssignmentNode(md,nametable,(AssignmentNode)en,td);
533 checkCastNode(md,nametable,(CastNode)en,td);
536 case Kind.CreateObjectNode:
537 checkCreateObjectNode(md,nametable,(CreateObjectNode)en,td);
540 case Kind.FieldAccessNode:
541 checkFieldAccessNode(md,nametable,(FieldAccessNode)en,td);
544 case Kind.ArrayAccessNode:
545 checkArrayAccessNode(md,nametable,(ArrayAccessNode)en,td);
548 case Kind.LiteralNode:
549 checkLiteralNode(md,nametable,(LiteralNode)en,td);
552 case Kind.MethodInvokeNode:
553 checkMethodInvokeNode(md,nametable,(MethodInvokeNode)en,td);
557 checkNameNode(md,nametable,(NameNode)en,td);
561 checkOpNode(md,nametable,(OpNode)en,td);
564 case Kind.OffsetNode:
565 checkOffsetNode(md, nametable, (OffsetNode)en, td);
568 case Kind.TertiaryNode:
569 checkTertiaryNode(md, nametable, (TertiaryNode)en, td);
572 case Kind.InstanceOfNode:
573 checkInstanceOfNode(md, nametable, (InstanceOfNode) en, td);
576 case Kind.ArrayInitializerNode:
577 checkArrayInitializerNode(md, nametable, (ArrayInitializerNode) en, td);
580 case Kind.ClassTypeNode:
581 checkClassTypeNode(md, nametable, (ClassTypeNode) en, td);
587 void checkClassTypeNode(Descriptor md, SymbolTable nametable, ClassTypeNode tn, TypeDescriptor td) {
588 checkTypeDescriptor(((md instanceof MethodDescriptor)?((MethodDescriptor)md).getClassDesc():null), tn.getType());
591 void checkCastNode(Descriptor md, SymbolTable nametable, CastNode cn, TypeDescriptor td) {
592 /* Get type descriptor */
593 if (cn.getType()==null) {
594 NameDescriptor typenamed=cn.getTypeName().getName();
595 TypeDescriptor ntd=new TypeDescriptor(getClass(((md instanceof MethodDescriptor)?((MethodDescriptor)md).getClassDesc():null), typenamed.toString()));
599 /* Check the type descriptor */
600 TypeDescriptor cast_type=cn.getType();
601 checkTypeDescriptor(((md instanceof MethodDescriptor)?((MethodDescriptor)md).getClassDesc():null),cast_type);
605 if (!typeutil.isSuperorType(td,cast_type))
606 throw new Error("Cast node returns "+cast_type+", but need "+td);
609 ExpressionNode en=cn.getExpression();
610 checkExpressionNode(md, nametable, en, null);
611 TypeDescriptor etd=en.getType();
612 if (typeutil.isSuperorType(cast_type,etd)) /* Cast trivially succeeds */
615 if (typeutil.isSuperorType(etd,cast_type)) /* Cast may succeed */
617 if (typeutil.isCastable(etd, cast_type))
620 /* Different branches */
621 /* TODO: change if add interfaces */
622 throw new Error("Cast will always fail\n"+cn.printNode(0));
625 void checkFieldAccessNode(Descriptor md, SymbolTable nametable, FieldAccessNode fan, TypeDescriptor td) {
626 ExpressionNode left=fan.getExpression();
627 checkExpressionNode(md,nametable,left,null);
628 TypeDescriptor ltd=left.getType();
629 String fieldname=fan.getFieldName();
631 FieldDescriptor fd=null;
632 if (ltd.isArray()&&fieldname.equals("length"))
633 fd=FieldDescriptor.arrayLength;
635 fd=(FieldDescriptor) ltd.getClassDesc().getFieldTable().get(fieldname);
637 if(ltd.isClassNameRef()) {
638 // the field access is using a class name directly
639 if(ltd.getClassDesc().isEnum()) {
640 int value = ltd.getClassDesc().getEnumConstant(fieldname);
642 // check if this field is an enum constant
643 throw new Error(fieldname + " is not an enum constant in "+fan.printNode(0)+" in "+md);
645 fd = new FieldDescriptor(new Modifiers(Modifiers.PUBLIC|Modifiers.FINAL), new TypeDescriptor(TypeDescriptor.INT), fieldname, null, false);
647 fd.setEnumValue(value);
648 } else if(fd == null) {
649 throw new Error("Could not find field "+ fieldname + " in "+fan.printNode(0)+" in "+md + " (Line: "+fan.getNumLine()+")");
650 } else if(fd.isStatic()) {
651 // check if this field is a static field
652 if(fd.getExpressionNode() != null) {
653 checkExpressionNode(md,nametable,fd.getExpressionNode(),null);
656 throw new Error("Dereference of the non-static field "+ fieldname + " in "+fan.printNode(0)+" in "+md);
661 throw new Error("Unknown field "+fieldname + " in "+fan.printNode(0)+" in "+md);
663 if (fd.getType().iswrapper()) {
664 FieldAccessNode fan2=new FieldAccessNode(left, fieldname);
665 fan2.setNumLine(left.getNumLine());
668 fan.fieldname="value";
670 ExpressionNode leftwr=fan.getExpression();
671 TypeDescriptor ltdwr=leftwr.getType();
672 String fieldnamewr=fan.getFieldName();
673 FieldDescriptor fdwr=(FieldDescriptor) ltdwr.getClassDesc().getFieldTable().get(fieldnamewr);
676 throw new Error("Unknown field "+fieldnamewr + " in "+fan.printNode(0)+" in "+md);
681 if (!typeutil.isSuperorType(td,fan.getType()))
682 throw new Error("Field node returns "+fan.getType()+", but need "+td);
686 void checkArrayAccessNode(Descriptor md, SymbolTable nametable, ArrayAccessNode aan, TypeDescriptor td) {
687 ExpressionNode left=aan.getExpression();
688 checkExpressionNode(md,nametable,left,null);
690 checkExpressionNode(md,nametable,aan.getIndex(),new TypeDescriptor(TypeDescriptor.INT));
691 TypeDescriptor ltd=left.getType();
692 if (ltd.dereference().iswrapper()) {
693 aan.wrappertype=((FieldDescriptor)ltd.dereference().getClassDesc().getFieldTable().get("value")).getType();
697 if (!typeutil.isSuperorType(td,aan.getType()))
698 throw new Error("Field node returns "+aan.getType()+", but need "+td);
701 void checkLiteralNode(Descriptor md, SymbolTable nametable, LiteralNode ln, TypeDescriptor td) {
702 /* Resolve the type */
703 Object o=ln.getValue();
704 if (ln.getTypeString().equals("null")) {
705 ln.setType(new TypeDescriptor(TypeDescriptor.NULL));
706 } else if (o instanceof Integer) {
707 ln.setType(new TypeDescriptor(TypeDescriptor.INT));
708 } else if (o instanceof Long) {
709 ln.setType(new TypeDescriptor(TypeDescriptor.LONG));
710 } else if (o instanceof Float) {
711 ln.setType(new TypeDescriptor(TypeDescriptor.FLOAT));
712 } else if (o instanceof Boolean) {
713 ln.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
714 } else if (o instanceof Double) {
715 ln.setType(new TypeDescriptor(TypeDescriptor.DOUBLE));
716 } else if (o instanceof Character) {
717 ln.setType(new TypeDescriptor(TypeDescriptor.CHAR));
718 } else if (o instanceof String) {
719 ln.setType(new TypeDescriptor(getClass(null, TypeUtil.StringClass)));
723 if (!typeutil.isSuperorType(td,ln.getType())) {
724 Long l = ln.evaluate();
725 if((ln.getType().isByte() || ln.getType().isShort()
726 || ln.getType().isChar() || ln.getType().isInt())
728 && (td.isByte() || td.isShort() || td.isChar()
729 || td.isInt() || td.isLong())) {
730 long lnvalue = l.longValue();
731 if((td.isByte() && ((lnvalue > 127) || (lnvalue < -128)))
732 || (td.isShort() && ((lnvalue > 32767) || (lnvalue < -32768)))
733 || (td.isChar() && ((lnvalue > 65535) || (lnvalue < 0)))
734 || (td.isInt() && ((lnvalue > 2147483647) || (lnvalue < -2147483648)))
735 || (td.isLong() && ((lnvalue > 9223372036854775807L) || (lnvalue < -9223372036854775808L)))) {
736 throw new Error("Field node returns "+ln.getType()+", but need "+td+" in "+md);
739 throw new Error("Field node returns "+ln.getType()+", but need "+td+" in "+md);
744 void checkNameNode(Descriptor md, SymbolTable nametable, NameNode nn, TypeDescriptor td) {
745 NameDescriptor nd=nn.getName();
746 if (nd.getBase()!=null) {
748 /* Rewrite NameNode */
749 ExpressionNode en=translateNameDescriptorintoExpression(nd,nn.getNumLine());
750 nn.setExpression(en);
751 checkExpressionNode(md,nametable,en,td);
753 String varname=nd.toString();
754 if(varname.equals("this")) {
756 nn.setVar((VarDescriptor)nametable.get("this"));
759 Descriptor d=(Descriptor)nametable.get(varname);
761 ClassDescriptor cd = null;
762 if((md instanceof MethodDescriptor) && ((MethodDescriptor)md).isStaticBlock()) {
763 // this is a static block, all the accessed fields should be static field
764 cd = ((MethodDescriptor)md).getClassDesc();
765 SymbolTable fieldtbl = cd.getFieldTable();
766 FieldDescriptor fd=(FieldDescriptor)fieldtbl.get(varname);
767 if((fd == null) || (!fd.isStatic())){
768 // no such field in the class, check if this is a class
769 if(varname.equals("this")) {
770 throw new Error("Error: access this obj in a static block");
772 cd=getClass(((md instanceof MethodDescriptor)?((MethodDescriptor)md).getClassDesc():null), varname);
774 // this is a class name
778 throw new Error("Name "+varname+" should not be used in static block: "+md);
781 // this is a static field
787 // check if the var is a static field of the class
788 if(md instanceof MethodDescriptor) {
789 cd = ((MethodDescriptor)md).getClassDesc();
790 FieldDescriptor fd = (FieldDescriptor)cd.getFieldTable().get(varname);
791 if((fd != null) && (fd.isStatic())) {
795 if (!typeutil.isSuperorType(td,nn.getType()))
796 throw new Error("Field node returns "+nn.getType()+", but need "+td);
798 } else if(fd != null) {
799 throw new Error("Name "+varname+" should not be used in " + md);
802 cd=getClass(((md instanceof MethodDescriptor)?((MethodDescriptor)md).getClassDesc():null), varname);
804 // this is a class name
808 throw new Error("Name "+varname+" undefined in: "+md);
812 if (d instanceof VarDescriptor) {
814 } else if (d instanceof FieldDescriptor) {
815 FieldDescriptor fd=(FieldDescriptor)d;
816 if (fd.getType().iswrapper()) {
817 String id=nd.getIdentifier();
818 NameDescriptor base=nd.getBase();
819 NameNode n=new NameNode(nn.getName());
820 n.setNumLine(nn.getNumLine());
822 n.setVar((VarDescriptor)nametable.get("this")); /* Need a pointer to this */
823 FieldAccessNode fan=new FieldAccessNode(n,"value");
824 fan.setNumLine(n.getNumLine());
825 FieldDescriptor fdval=(FieldDescriptor) fd.getType().getClassDesc().getFieldTable().get("value");
827 nn.setExpression(fan);
830 nn.setVar((VarDescriptor)nametable.get("this")); /* Need a pointer to this */
832 } else if (d instanceof TagVarDescriptor) {
834 } else throw new Error("Wrong type of descriptor");
836 if (!typeutil.isSuperorType(td,nn.getType()))
837 throw new Error("Field node returns "+nn.getType()+", but need "+td);
841 void checkOffsetNode(Descriptor md, SymbolTable nameTable, OffsetNode ofn, TypeDescriptor td) {
842 TypeDescriptor ltd=ofn.td;
843 checkTypeDescriptor(((md instanceof MethodDescriptor)?((MethodDescriptor)md).getClassDesc():null),ltd);
845 String fieldname = ofn.fieldname;
846 FieldDescriptor fd=null;
847 if (ltd.isArray()&&fieldname.equals("length")) {
848 fd=FieldDescriptor.arrayLength;
850 fd=(FieldDescriptor) ltd.getClassDesc().getFieldTable().get(fieldname);
854 checkField(ltd.getClassDesc(), fd);
857 throw new Error("Unknown field "+fieldname + " in "+ofn.printNode(1)+" in "+md);
860 if (!typeutil.isSuperorType(td, ofn.getType())) {
861 System.out.println(td);
862 System.out.println(ofn.getType());
863 throw new Error("Type of rside not compatible with type of lside"+ofn.printNode(0));
869 void checkTertiaryNode(Descriptor md, SymbolTable nametable, TertiaryNode tn, TypeDescriptor td) {
870 checkExpressionNode(md, nametable, tn.getCond(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
871 checkExpressionNode(md, nametable, tn.getTrueExpr(), td );
872 checkExpressionNode(md, nametable, tn.getFalseExpr(), td );
875 void checkInstanceOfNode(Descriptor md, SymbolTable nametable, InstanceOfNode tn, TypeDescriptor td) {
876 if (td!=null&&!td.isBoolean())
877 throw new Error("Expecting type "+td+"for instanceof expression");
879 checkTypeDescriptor(((md instanceof MethodDescriptor)?((MethodDescriptor)md).getClassDesc():null), tn.getExprType());
880 checkExpressionNode(md, nametable, tn.getExpr(), null);
883 void checkArrayInitializerNode(Descriptor md, SymbolTable nametable, ArrayInitializerNode ain, TypeDescriptor td) {
884 Vector<TypeDescriptor> vec_type = new Vector<TypeDescriptor>();
885 for( int i = 0; i < ain.numVarInitializers(); ++i ) {
886 checkExpressionNode(md, nametable, ain.getVarInitializer(i), td==null?td:td.dereference());
887 vec_type.add(ain.getVarInitializer(i).getType());
889 // descide the type of this variableInitializerNode
890 TypeDescriptor out_type = vec_type.elementAt(0);
891 for(int i = 1; i < vec_type.size(); i++) {
892 TypeDescriptor tmp_type = vec_type.elementAt(i);
893 if(out_type == null) {
894 if(tmp_type != null) {
897 } else if(out_type.isNull()) {
898 if(!tmp_type.isNull() ) {
899 if(!tmp_type.isArray()) {
900 throw new Error("Error: mixed type in var initializer list");
905 } else if(out_type.isArray()) {
906 if(tmp_type.isArray()) {
907 if(tmp_type.getArrayCount() > out_type.getArrayCount()) {
910 } else if((tmp_type != null) && (!tmp_type.isNull())) {
911 throw new Error("Error: mixed type in var initializer list");
913 } else if(out_type.isInt()) {
914 if(!tmp_type.isInt()) {
915 throw new Error("Error: mixed type in var initializer list");
917 } else if(out_type.isString()) {
918 if(!tmp_type.isString()) {
919 throw new Error("Error: mixed type in var initializer list");
923 if(out_type != null) {
924 out_type = out_type.makeArray(state);
925 //out_type.setStatic();
927 ain.setType(out_type);
930 void checkAssignmentNode(Descriptor md, SymbolTable nametable, AssignmentNode an, TypeDescriptor td) {
931 boolean postinc=true;
932 if (an.getOperation().getBaseOp()==null||
933 (an.getOperation().getBaseOp().getOp()!=Operation.POSTINC&&
934 an.getOperation().getBaseOp().getOp()!=Operation.POSTDEC))
937 checkExpressionNode(md, nametable, an.getSrc(),td);
938 //TODO: Need check on validity of operation here
939 if (!((an.getDest() instanceof FieldAccessNode)||
940 (an.getDest() instanceof ArrayAccessNode)||
941 (an.getDest() instanceof NameNode)))
942 throw new Error("Bad lside in "+an.printNode(0));
943 checkExpressionNode(md, nametable, an.getDest(), null);
945 /* We want parameter variables to tasks to be immutable */
946 if (md instanceof TaskDescriptor) {
947 if (an.getDest() instanceof NameNode) {
948 NameNode nn=(NameNode)an.getDest();
949 if (nn.getVar()!=null) {
950 if (((TaskDescriptor)md).getParameterTable().contains(nn.getVar().getSymbol()))
951 throw new Error("Can't modify parameter "+nn.getVar()+ " to task "+td.getSymbol());
956 if (an.getDest().getType().isString()&&an.getOperation().getOp()==AssignOperation.PLUSEQ) {
958 ClassDescriptor stringcl=getClass(null, TypeUtil.StringClass);
959 TypeDescriptor stringtd=new TypeDescriptor(stringcl);
960 NameDescriptor nd=new NameDescriptor("String");
961 NameDescriptor valuend=new NameDescriptor(nd, "valueOf");
963 if (!(an.getSrc().getType().isString()&&(an.getSrc() instanceof OpNode))) {
964 MethodInvokeNode rightmin=new MethodInvokeNode(valuend);
965 rightmin.setNumLine(an.getSrc().getNumLine());
966 rightmin.addArgument(an.getSrc());
968 checkExpressionNode(md, nametable, an.getSrc(), null);
972 if (!postinc&&!typeutil.isSuperorType(an.getDest().getType(),an.getSrc().getType())) {
973 TypeDescriptor dt = an.getDest().getType();
974 TypeDescriptor st = an.getSrc().getType();
975 if(an.getSrc().kind() == Kind.ArrayInitializerNode) {
976 if(dt.getArrayCount() != st.getArrayCount()) {
977 throw new Error("Type of rside ("+an.getSrc().getType().toPrettyString()+") not compatible with type of lside ("+an.getDest().getType().toPrettyString()+")"+an.printNode(0));
980 dt = dt.dereference();
981 st = st.dereference();
982 } while(dt.isArray());
983 if((st.isByte() || st.isShort() || st.isChar() || st.isInt())
984 && (dt.isByte() || dt.isShort() || dt.isChar() || dt.isInt() || dt.isLong())) {
987 throw new Error("Type of rside ("+an.getSrc().getType().toPrettyString()+") not compatible with type of lside ("+an.getDest().getType().toPrettyString()+")"+an.printNode(0));
991 Long l = an.getSrc().evaluate();
992 if((st.isByte() || st.isShort() || st.isChar() || st.isInt())
994 && (dt.isByte() || dt.isShort() || dt.isChar() || dt.isInt() || dt.isLong())) {
995 long lnvalue = l.longValue();
996 if((dt.isByte() && ((lnvalue > 127) || (lnvalue < -128)))
997 || (dt.isShort() && ((lnvalue > 32767) || (lnvalue < -32768)))
998 || (dt.isChar() && ((lnvalue > 65535) || (lnvalue < 0)))
999 || (dt.isInt() && ((lnvalue > 2147483647) || (lnvalue < -2147483648)))
1000 || (dt.isLong() && ((lnvalue > 9223372036854775807L) || (lnvalue < -9223372036854775808L)))) {
1001 throw new Error("Type of rside ("+an.getSrc().getType().toPrettyString()+") not compatible with type of lside ("+an.getDest().getType().toPrettyString()+")"+an.printNode(0));
1004 throw new Error("Type of rside ("+an.getSrc().getType().toPrettyString()+") not compatible with type of lside ("+an.getDest().getType().toPrettyString()+")"+an.printNode(0));
1010 void checkLoopNode(Descriptor md, SymbolTable nametable, LoopNode ln) {
1012 if (ln.getType()==LoopNode.WHILELOOP||ln.getType()==LoopNode.DOWHILELOOP) {
1013 checkExpressionNode(md, nametable, ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
1014 checkBlockNode(md, nametable, ln.getBody());
1017 /* Link in the initializer naming environment */
1018 BlockNode bn=ln.getInitializer();
1019 bn.getVarTable().setParent(nametable);
1020 for(int i=0; i<bn.size(); i++) {
1021 BlockStatementNode bsn=bn.get(i);
1022 checkBlockStatementNode(md, bn.getVarTable(),bsn);
1024 //check the condition
1025 checkExpressionNode(md, bn.getVarTable(), ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
1026 checkBlockNode(md, bn.getVarTable(), ln.getBody());
1027 checkBlockNode(md, bn.getVarTable(), ln.getUpdate());
1033 void checkCreateObjectNode(Descriptor md, SymbolTable nametable, CreateObjectNode con,
1034 TypeDescriptor td) {
1035 TypeDescriptor[] tdarray = new TypeDescriptor[con.numArgs()];
1036 for (int i = 0; i < con.numArgs(); i++) {
1037 ExpressionNode en = con.getArg(i);
1038 checkExpressionNode(md, nametable, en, null);
1039 tdarray[i] = en.getType();
1042 TypeDescriptor typetolookin = con.getType();
1043 checkTypeDescriptor(((md instanceof MethodDescriptor)?((MethodDescriptor)md).getClassDesc():null), typetolookin);
1045 if (td != null && !typeutil.isSuperorType(td, typetolookin))
1046 throw new Error(typetolookin + " isn't a " + td);
1048 /* Check Array Initializers */
1049 if ((con.getArrayInitializer() != null)) {
1050 checkArrayInitializerNode(md, nametable, con.getArrayInitializer(), td);
1053 /* Check flag effects */
1054 if (con.getFlagEffects() != null) {
1055 FlagEffects fe = con.getFlagEffects();
1056 ClassDescriptor cd = typetolookin.getClassDesc();
1058 for (int j = 0; j < fe.numEffects(); j++) {
1059 FlagEffect flag = fe.getEffect(j);
1060 String name = flag.getName();
1061 FlagDescriptor flag_d = (FlagDescriptor) cd.getFlagTable().get(name);
1062 // Make sure the flag is declared
1064 throw new Error("Flag descriptor " + name + " undefined in class: " + cd.getSymbol());
1065 if (flag_d.getExternal())
1066 throw new Error("Attempting to modify external flag: " + name);
1067 flag.setFlag(flag_d);
1069 for (int j = 0; j < fe.numTagEffects(); j++) {
1070 TagEffect tag = fe.getTagEffect(j);
1071 String name = tag.getName();
1073 Descriptor d = (Descriptor) nametable.get(name);
1075 throw new Error("Tag descriptor " + name + " undeclared");
1076 else if (!(d instanceof TagVarDescriptor))
1077 throw new Error(name + " is not a tag descriptor");
1078 tag.setTag((TagVarDescriptor) d);
1082 if ((!typetolookin.isClass()) && (!typetolookin.isArray()))
1083 throw new Error("Can't allocate primitive type:" + con.printNode(0));
1085 if (!typetolookin.isArray()) {
1086 // Array's don't need constructor calls
1087 ClassDescriptor classtolookin = typetolookin.getClassDesc();
1088 checkClass(classtolookin, INIT);
1090 Set methoddescriptorset = classtolookin.getMethodTable().getSet(typetolookin.getSymbol());
1091 MethodDescriptor bestmd = null;
1092 NextMethod: for (Iterator methodit = methoddescriptorset.iterator(); methodit.hasNext();) {
1093 MethodDescriptor currmd = (MethodDescriptor) methodit.next();
1094 /* Need correct number of parameters */
1095 if (con.numArgs() != currmd.numParameters())
1097 for (int i = 0; i < con.numArgs(); i++) {
1098 if (!typeutil.isSuperorType(currmd.getParamType(i), tdarray[i]))
1099 continue NextMethod;
1101 /* Local allocations can't call global allocator */
1102 if (!con.isGlobal() && currmd.isGlobal())
1105 /* Method okay so far */
1109 if (typeutil.isMoreSpecific(currmd, bestmd)) {
1111 } else if (con.isGlobal() && match(currmd, bestmd)) {
1112 if (currmd.isGlobal() && !bestmd.isGlobal())
1114 else if (currmd.isGlobal() && bestmd.isGlobal())
1116 } else if (!typeutil.isMoreSpecific(bestmd, currmd)) {
1117 throw new Error("No method is most specific:" + bestmd + " and " + currmd);
1120 /* Is this more specific than bestmd */
1124 throw new Error("No method found for " + con.printNode(0) + " in " + md);
1125 con.setConstructor(bestmd);
1130 /** Check to see if md1 is the same specificity as md2.*/
1132 boolean match(MethodDescriptor md1, MethodDescriptor md2) {
1133 /* Checks if md1 is more specific than md2 */
1134 if (md1.numParameters()!=md2.numParameters())
1136 for(int i=0; i<md1.numParameters(); i++) {
1137 if (!md2.getParamType(i).equals(md1.getParamType(i)))
1140 if (!md2.getReturnType().equals(md1.getReturnType()))
1143 if (!md2.getClassDesc().equals(md1.getClassDesc()))
1151 ExpressionNode translateNameDescriptorintoExpression(NameDescriptor nd, int numLine) {
1152 String id=nd.getIdentifier();
1153 NameDescriptor base=nd.getBase();
1155 NameNode nn=new NameNode(nd);
1156 nn.setNumLine(numLine);
1159 FieldAccessNode fan=new FieldAccessNode(translateNameDescriptorintoExpression(base,numLine),id);
1160 fan.setNumLine(numLine);
1166 void checkMethodInvokeNode(Descriptor md, SymbolTable nametable, MethodInvokeNode min, TypeDescriptor td) {
1167 /*Typecheck subexpressions
1168 and get types for expressions*/
1170 boolean isstatic = false;
1171 if((md instanceof MethodDescriptor) && ((MethodDescriptor)md).isStatic()) {
1174 TypeDescriptor[] tdarray=new TypeDescriptor[min.numArgs()];
1175 for(int i=0; i<min.numArgs(); i++) {
1176 ExpressionNode en=min.getArg(i);
1177 checkExpressionNode(md,nametable,en,null);
1178 tdarray[i]=en.getType();
1179 if(en.getType().isClass() && en.getType().getClassDesc().isEnum()) {
1180 tdarray[i] = new TypeDescriptor(TypeDescriptor.INT);
1183 TypeDescriptor typetolookin=null;
1184 if (min.getExpression()!=null) {
1185 checkExpressionNode(md,nametable,min.getExpression(),null);
1186 typetolookin=min.getExpression().getType();
1187 //if (typetolookin==null)
1188 //throw new Error(md+" has null return type");
1190 } else if (min.getBaseName()!=null) {
1191 String rootname=min.getBaseName().getRoot();
1192 if (rootname.equals("super")) {
1193 ClassDescriptor supercd=((MethodDescriptor)md).getClassDesc().getSuperDesc();
1194 typetolookin=new TypeDescriptor(supercd);
1196 } else if (rootname.equals("this")) {
1198 throw new Error("use this object in static method md = "+ md.toString());
1200 ClassDescriptor cd=((MethodDescriptor)md).getClassDesc();
1201 typetolookin=new TypeDescriptor(cd);
1202 } else if (nametable.get(rootname)!=null) {
1203 //we have an expression
1204 min.setExpression(translateNameDescriptorintoExpression(min.getBaseName(),min.getNumLine()));
1205 checkExpressionNode(md, nametable, min.getExpression(), null);
1206 typetolookin=min.getExpression().getType();
1208 if(!min.getBaseName().getSymbol().equals("System.out")) {
1209 ExpressionNode nn = translateNameDescriptorintoExpression(min.getBaseName(),min.getNumLine());
1210 checkExpressionNode(md, nametable, nn, null);
1211 typetolookin = nn.getType();
1212 if(!((nn.kind()== Kind.NameNode) && (((NameNode)nn).getField() == null)
1213 && (((NameNode)nn).getVar() == null) && (((NameNode)nn).getExpression() == null))) {
1214 // this is not a pure class name, need to add to
1215 min.setExpression(nn);
1219 ClassDescriptor cd = null;
1220 //if (min.getBaseName().getSymbol().equals("System.out"))
1221 cd=getClass(null, "System");
1223 cd=getClass(min.getBaseName().getSymbol());
1226 throw new Error("md = "+ md.toString()+ " "+min.getBaseName()+" undefined");
1227 typetolookin=new TypeDescriptor(cd);
1230 } else if ((md instanceof MethodDescriptor)&&min.getMethodName().equals("super")) {
1231 ClassDescriptor supercd=((MethodDescriptor)md).getClassDesc().getSuperDesc();
1232 min.methodid=supercd.getSymbol();
1233 typetolookin=new TypeDescriptor(supercd);
1234 } else if (md instanceof MethodDescriptor) {
1235 typetolookin=new TypeDescriptor(((MethodDescriptor)md).getClassDesc());
1237 /* If this a task descriptor we throw an error at this point */
1238 throw new Error("Unknown method call to "+min.getMethodName()+"in task"+md.getSymbol());
1240 if (!typetolookin.isClass())
1241 throw new Error("Error with method call to "+min.getMethodName());
1242 ClassDescriptor classtolookin=typetolookin.getClassDesc();
1243 checkClass(classtolookin, INIT);
1244 //System.out.println("Method name="+min.getMethodName());
1246 Set methoddescriptorset=classtolookin.getMethodTable().getSet(min.getMethodName());
1247 MethodDescriptor bestmd=null;
1249 for(Iterator methodit=methoddescriptorset.iterator(); methodit.hasNext();) {
1250 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
1251 /* Need correct number of parameters */
1252 if (min.numArgs()!=currmd.numParameters())
1254 for(int i=0; i<min.numArgs(); i++) {
1255 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
1256 if(((!tdarray[i].isArray() &&(tdarray[i].isInt() || tdarray[i].isLong()))
1257 && currmd.getParamType(i).isClass() && currmd.getParamType(i).getClassDesc().getSymbol().equals("Object"))) {
1258 // primitive parameters vs object
1260 continue NextMethod;
1263 /* Method okay so far */
1267 if (typeutil.isMoreSpecific(currmd,bestmd)) {
1269 } else if (!typeutil.isMoreSpecific(bestmd, currmd))
1270 throw new Error("No method is most specific:"+bestmd+" and "+currmd);
1272 /* Is this more specific than bestmd */
1276 throw new Error("No method found for :"+min.printNode(0)+" in class: " + classtolookin+" in "+md);
1277 min.setMethod(bestmd);
1279 if ((td!=null)&&(min.getType()!=null)&&!typeutil.isSuperorType(td, min.getType()))
1280 throw new Error(min.getType()+ " is not equal to or a subclass of "+td);
1281 /* Check whether we need to set this parameter to implied this */
1282 if (! isstatic && !bestmd.isStatic()) {
1283 if (min.getExpression()==null) {
1284 ExpressionNode en=new NameNode(new NameDescriptor("this"));
1285 min.setExpression(en);
1286 checkExpressionNode(md, nametable, min.getExpression(), null);
1290 /* Check if we need to wrap primitive paratmeters to objects */
1291 for(int i=0; i<min.numArgs(); i++) {
1292 if(!tdarray[i].isArray() && (tdarray[i].isInt() || tdarray[i].isLong())
1293 && min.getMethod().getParamType(i).isClass() && min.getMethod().getParamType(i).getClassDesc().getSymbol().equals("Object")) {
1294 // Shall wrap this primitive parameter as a object
1295 ExpressionNode exp = min.getArg(i);
1296 TypeDescriptor ptd = null;
1297 NameDescriptor nd=null;
1298 if(exp.getType().isInt()) {
1299 nd = new NameDescriptor("Integer");
1300 ptd = state.getTypeDescriptor(nd);
1301 } else if(exp.getType().isLong()) {
1302 nd = new NameDescriptor("Long");
1303 ptd = state.getTypeDescriptor(nd);
1305 boolean isglobal = false;
1306 String disjointId = null;
1307 CreateObjectNode con=new CreateObjectNode(ptd, isglobal, disjointId);
1308 con.addArgument(exp);
1309 checkExpressionNode(md, nametable, con, null);
1310 min.setArgument(con, i);
1316 void checkOpNode(Descriptor md, SymbolTable nametable, OpNode on, TypeDescriptor td) {
1317 checkExpressionNode(md, nametable, on.getLeft(), null);
1318 if (on.getRight()!=null)
1319 checkExpressionNode(md, nametable, on.getRight(), null);
1320 TypeDescriptor ltd=on.getLeft().getType();
1321 TypeDescriptor rtd=on.getRight()!=null ? on.getRight().getType() : null;
1322 TypeDescriptor lefttype=null;
1323 TypeDescriptor righttype=null;
1324 Operation op=on.getOp();
1326 switch(op.getOp()) {
1327 case Operation.LOGIC_OR:
1328 case Operation.LOGIC_AND:
1329 if (!(rtd.isBoolean()))
1331 on.setRightType(rtd);
1333 case Operation.LOGIC_NOT:
1334 if (!(ltd.isBoolean()))
1337 on.setLeftType(ltd);
1339 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1342 case Operation.COMP:
1343 // 5.6.2 Binary Numeric Promotion
1344 //TODO unboxing of reference objects
1347 else if (ltd.isFloat())
1349 else if (ltd.isLong())
1350 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1352 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1353 on.setLeftType(lefttype);
1354 on.setType(lefttype);
1357 case Operation.BIT_OR:
1358 case Operation.BIT_XOR:
1359 case Operation.BIT_AND:
1360 // 5.6.2 Binary Numeric Promotion
1361 //TODO unboxing of reference objects
1362 if (ltd.isDouble()||rtd.isDouble())
1364 else if (ltd.isFloat()||rtd.isFloat())
1366 else if (ltd.isLong()||rtd.isLong())
1367 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1368 // 090205 hack for boolean
1369 else if (ltd.isBoolean()||rtd.isBoolean())
1370 lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
1372 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1375 on.setLeftType(lefttype);
1376 on.setRightType(righttype);
1377 on.setType(lefttype);
1380 case Operation.ISAVAILABLE:
1381 if (!(ltd.isPtr())) {
1382 throw new Error("Can't use isavailable on non-pointers/non-parameters.");
1385 on.setLeftType(lefttype);
1386 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1389 case Operation.EQUAL:
1390 case Operation.NOTEQUAL:
1391 // 5.6.2 Binary Numeric Promotion
1392 //TODO unboxing of reference objects
1393 if (ltd.isBoolean()||rtd.isBoolean()) {
1394 if (!(ltd.isBoolean()&&rtd.isBoolean()))
1396 righttype=lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
1397 } else if (ltd.isPtr()||rtd.isPtr()) {
1398 if (!(ltd.isPtr()&&rtd.isPtr())) {
1405 } else if (ltd.isDouble()||rtd.isDouble())
1406 righttype=lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
1407 else if (ltd.isFloat()||rtd.isFloat())
1408 righttype=lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
1409 else if (ltd.isLong()||rtd.isLong())
1410 righttype=lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1412 righttype=lefttype=new TypeDescriptor(TypeDescriptor.INT);
1414 on.setLeftType(lefttype);
1415 on.setRightType(righttype);
1416 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1425 // 5.6.2 Binary Numeric Promotion
1426 //TODO unboxing of reference objects
1427 if (!ltd.isNumber()||!rtd.isNumber()) {
1428 if (!ltd.isNumber())
1429 throw new Error("Leftside is not number"+on.printNode(0)+"type="+ltd.toPrettyString());
1430 if (!rtd.isNumber())
1431 throw new Error("Rightside is not number"+on.printNode(0));
1434 if (ltd.isDouble()||rtd.isDouble())
1435 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
1436 else if (ltd.isFloat()||rtd.isFloat())
1437 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
1438 else if (ltd.isLong()||rtd.isLong())
1439 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1441 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1443 on.setLeftType(lefttype);
1444 on.setRightType(righttype);
1445 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1449 if (ltd.isString()||rtd.isString()) {
1450 ClassDescriptor stringcl=getClass(null, TypeUtil.StringClass);
1451 TypeDescriptor stringtd=new TypeDescriptor(stringcl);
1452 NameDescriptor nd=new NameDescriptor("String");
1453 NameDescriptor valuend=new NameDescriptor(nd, "valueOf");
1454 if (!(ltd.isString()&&(on.getLeft() instanceof OpNode))) {
1455 MethodInvokeNode leftmin=new MethodInvokeNode(valuend);
1456 leftmin.setNumLine(on.getLeft().getNumLine());
1457 leftmin.addArgument(on.getLeft());
1459 checkExpressionNode(md, nametable, on.getLeft(), null);
1462 if (!(rtd.isString()&&(on.getRight() instanceof OpNode))) {
1463 MethodInvokeNode rightmin=new MethodInvokeNode(valuend);
1464 rightmin.setNumLine(on.getRight().getNumLine());
1465 rightmin.addArgument(on.getRight());
1467 checkExpressionNode(md, nametable, on.getRight(), null);
1470 on.setLeftType(stringtd);
1471 on.setRightType(stringtd);
1472 on.setType(stringtd);
1477 case Operation.MULT:
1480 // 5.6.2 Binary Numeric Promotion
1481 //TODO unboxing of reference objects
1482 if (ltd.isArray()||rtd.isArray()||!ltd.isNumber()||!rtd.isNumber())
1483 throw new Error("Error in "+on.printNode(0));
1485 if (ltd.isDouble()||rtd.isDouble())
1486 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
1487 else if (ltd.isFloat()||rtd.isFloat())
1488 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
1489 else if (ltd.isLong()||rtd.isLong())
1490 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1492 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1494 on.setLeftType(lefttype);
1495 on.setRightType(righttype);
1496 on.setType(lefttype);
1499 case Operation.LEFTSHIFT:
1500 case Operation.RIGHTSHIFT:
1501 case Operation.URIGHTSHIFT:
1502 if (!rtd.isIntegerType())
1504 //5.6.1 Unary Numeric Promotion
1505 if (rtd.isByte()||rtd.isShort()||rtd.isInt())
1506 righttype=new TypeDescriptor(TypeDescriptor.INT);
1510 on.setRightType(righttype);
1511 if (!ltd.isIntegerType())
1514 case Operation.UNARYPLUS:
1515 case Operation.UNARYMINUS:
1516 /* case Operation.POSTINC:
1517 case Operation.POSTDEC:
1518 case Operation.PREINC:
1519 case Operation.PREDEC:*/
1520 if (!ltd.isNumber())
1522 //5.6.1 Unary Numeric Promotion
1523 if (ltd.isByte()||ltd.isShort()||ltd.isInt())
1524 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1527 on.setLeftType(lefttype);
1528 on.setType(lefttype);
1532 throw new Error(op.toString());
1536 if (!typeutil.isSuperorType(td, on.getType())) {
1537 System.out.println(td);
1538 System.out.println(on.getType());
1539 throw new Error("Type of rside not compatible with type of lside"+on.printNode(0));