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 ClassDescriptor cd=typeutil.getClass(context, classnameIn, toanalyze);
42 checkClass(cd, fullcheck);
46 public void checkClass(ClassDescriptor cd) {
50 public void checkClass(ClassDescriptor cd, int fullcheck) {
51 if (!completed.containsKey(cd)||completed.get(cd)<fullcheck) {
52 int oldstatus=completed.containsKey(cd)?completed.get(cd):0;
53 completed.put(cd, fullcheck);
55 if (fullcheck>=REFERENCE&&oldstatus<INIT) {
56 //Set superclass link up
57 if (cd.getSuper()!=null) {
58 ClassDescriptor superdesc=getClass(cd, cd.getSuper(), fullcheck);
59 if (superdesc.isInnerClass()) {
62 if (superdesc.isInterface()) {
65 cd.getSuperInterface().add(superdesc.getSymbol());
67 throw new Error("Error! Class " + cd.getSymbol() + " extends interface " + cd.getSuper());
70 cd.setSuperDesc(superdesc);
72 // Link together Field, Method, and Flag tables so classes
73 // inherit these from their superclasses
74 if (oldstatus<REFERENCE) {
75 cd.getFieldTable().setParent(cd.getSuperDesc().getFieldTable());
76 cd.getMethodTable().setParent(cd.getSuperDesc().getMethodTable());
77 cd.getFlagTable().setParent(cd.getSuperDesc().getFlagTable());
81 // Link together Field, Method tables do classes inherit these from
82 // their ancestor interfaces
83 Vector<String> sifv = cd.getSuperInterface();
84 for(int i = 0; i < sifv.size(); i++) {
85 ClassDescriptor superif = getClass(cd, sifv.elementAt(i), fullcheck);
86 if(!superif.isInterface()) {
87 throw new Error("Error! Class " + cd.getSymbol() + " implements non-interface " + superif.getSymbol());
89 if (oldstatus<REFERENCE) {
90 cd.addSuperInterfaces(superif);
91 cd.getMethodTable().addParentIF(superif.getMethodTable());
92 cd.getFieldTable().addParentIF(superif.getFieldTable());
96 if (oldstatus<INIT&&fullcheck>=INIT) {
97 /* Check to see that fields are well typed */
98 for(Iterator field_it=cd.getFields(); field_it.hasNext(); ) {
99 FieldDescriptor fd=(FieldDescriptor)field_it.next();
103 System.out.println("Class/Field in "+cd+":"+fd);
107 for(Iterator method_it=cd.getMethods(); method_it.hasNext(); ) {
108 MethodDescriptor md=(MethodDescriptor)method_it.next();
112 System.out.println("Class/Method in "+cd+":"+md);
120 public void semanticCheck() {
121 SymbolTable classtable = state.getClassSymbolTable();
122 toanalyze.addAll(classtable.getValueSet());
123 toanalyze.addAll(state.getTaskSymbolTable().getValueSet());
126 while (!toanalyze.isEmpty()) {
127 Object obj = toanalyze.iterator().next();
128 if (obj instanceof TaskDescriptor) {
129 toanalyze.remove(obj);
130 TaskDescriptor td = (TaskDescriptor) obj;
134 System.out.println("Error in " + td);
138 ClassDescriptor cd = (ClassDescriptor) obj;
139 toanalyze.remove(cd);
141 getClass(cd, cd.getSymbol());
142 // for inline defined anonymous classes, we need to check its
143 // surrounding class first to get its surrounding context
144 ClassDescriptor surroundingcd = cd.getSurroundingDesc();
145 if(toanalyze.contains(surroundingcd)) {
146 toanalyze.remove(surroundingcd);
147 getClass(surroundingcd, surroundingcd.getSymbol());
148 for (Iterator method_it = surroundingcd.getMethods(); method_it.hasNext(); ) {
149 MethodDescriptor md = (MethodDescriptor) method_it.next();
151 checkMethodBody(surroundingcd, md);
153 System.out.println("Error in " + md);
159 // need to initialize typeutil object here...only place we can
160 // get class descriptors without first calling getclass
161 getClass(cd, cd.getSymbol());
164 for (Iterator method_it = cd.getMethods(); method_it.hasNext(); ) {
165 MethodDescriptor md = (MethodDescriptor) method_it.next();
167 checkMethodBody(cd, md);
169 System.out.println("Error in " + md);
177 private void checkTypeDescriptor(ClassDescriptor cd, TypeDescriptor td) {
178 if (td.isPrimitive())
180 else if (td.isClass()) {
181 String name=td.toString();
182 ClassDescriptor field_cd=checkAll?getClass(cd, name):getClass(cd, name, REFERENCE);
185 throw new Error("Undefined class "+name);
186 td.setClassDescriptor(field_cd);
188 } else if (td.isTag())
194 public void checkField(ClassDescriptor cd, FieldDescriptor fd) {
195 checkTypeDescriptor(cd, fd.getType());
198 public void checkConstraintCheck(TaskDescriptor td, SymbolTable nametable, Vector ccs) {
200 return; /* No constraint checks to check */
201 for(int i=0; i<ccs.size(); i++) {
202 ConstraintCheck cc=(ConstraintCheck) ccs.get(i);
204 for(int j=0; j<cc.numArgs(); j++) {
205 ExpressionNode en=cc.getArg(j);
206 checkExpressionNode(td,nametable,en,null);
211 public void checkFlagEffects(TaskDescriptor td, Vector vfe, SymbolTable nametable) {
213 return; /* No flag effects to check */
214 for(int i=0; i<vfe.size(); i++) {
215 FlagEffects fe=(FlagEffects) vfe.get(i);
216 String varname=fe.getName();
217 //Make sure the variable is declared as a parameter to the task
218 VarDescriptor vd=(VarDescriptor)td.getParameterTable().get(varname);
220 throw new Error("Parameter "+varname+" in Flag Effects not declared in "+td);
223 //Make sure it correspods to a class
224 TypeDescriptor type_d=vd.getType();
225 if (!type_d.isClass())
226 throw new Error("Cannot have non-object argument for flag_effect");
228 ClassDescriptor cd=type_d.getClassDesc();
229 for(int j=0; j<fe.numEffects(); j++) {
230 FlagEffect flag=fe.getEffect(j);
231 String name=flag.getName();
232 FlagDescriptor flag_d=(FlagDescriptor)cd.getFlagTable().get(name);
233 //Make sure the flag is declared
235 throw new Error("Flag descriptor "+name+" undefined in class: "+cd.getSymbol());
236 if (flag_d.getExternal())
237 throw new Error("Attempting to modify external flag: "+name);
238 flag.setFlag(flag_d);
240 for(int j=0; j<fe.numTagEffects(); j++) {
241 TagEffect tag=fe.getTagEffect(j);
242 String name=tag.getName();
244 Descriptor d=(Descriptor)nametable.get(name);
246 throw new Error("Tag descriptor "+name+" undeclared");
247 else if (!(d instanceof TagVarDescriptor))
248 throw new Error(name+" is not a tag descriptor");
249 tag.setTag((TagVarDescriptor)d);
254 public void checkTask(TaskDescriptor td) {
255 for(int i=0; i<td.numParameters(); i++) {
256 /* Check that parameter is well typed */
257 TypeDescriptor param_type=td.getParamType(i);
258 checkTypeDescriptor(null, param_type);
260 /* Check the parameter's flag expression is well formed */
261 FlagExpressionNode fen=td.getFlag(td.getParameter(i));
262 if (!param_type.isClass())
263 throw new Error("Cannot have non-object argument to a task");
264 ClassDescriptor cd=param_type.getClassDesc();
266 checkFlagExpressionNode(cd, fen);
269 checkFlagEffects(td, td.getFlagEffects(),td.getParameterTable());
270 /* Check that the task code is valid */
271 BlockNode bn=state.getMethodBody(td);
272 checkBlockNode(td, td.getParameterTable(),bn);
275 public void checkFlagExpressionNode(ClassDescriptor cd, FlagExpressionNode fen) {
277 case Kind.FlagOpNode:
279 FlagOpNode fon=(FlagOpNode)fen;
280 checkFlagExpressionNode(cd, fon.getLeft());
281 if (fon.getRight()!=null)
282 checkFlagExpressionNode(cd, fon.getRight());
288 FlagNode fn=(FlagNode)fen;
289 String name=fn.getFlagName();
290 FlagDescriptor fd=(FlagDescriptor)cd.getFlagTable().get(name);
292 throw new Error("Undeclared flag: "+name);
298 throw new Error("Unrecognized FlagExpressionNode");
302 public void checkMethod(ClassDescriptor cd, MethodDescriptor md) {
303 /* Check for abstract methods */
304 if(md.isAbstract()) {
305 if(!cd.isAbstract() && !cd.isInterface()) {
306 throw new Error("Error! The non-abstract Class " + cd.getSymbol() + " contains an abstract method " + md.getSymbol());
310 /* Check return type */
311 if (!md.isConstructor() && !md.isStaticBlock())
312 if (!md.getReturnType().isVoid()) {
313 checkTypeDescriptor(cd, md.getReturnType());
315 for(int i=0; i<md.numParameters(); i++) {
316 TypeDescriptor param_type=md.getParamType(i);
317 checkTypeDescriptor(cd, param_type);
319 /* Link the naming environments */
320 if (!md.isStatic()) /* Fields aren't accessible directly in a static method, so don't link in this table */
321 md.getParameterTable().setParent(cd.getFieldTable());
323 if (!md.isStatic()) {
324 VarDescriptor thisvd=new VarDescriptor(new TypeDescriptor(cd),"this");
327 if(md.isDefaultConstructor() && (cd.getSuperDesc() != null)) {
328 // add the construction of it super class, can only be super()
329 NameDescriptor nd=new NameDescriptor("super");
330 MethodInvokeNode min=new MethodInvokeNode(nd);
331 BlockExpressionNode ben=new BlockExpressionNode(min);
332 BlockNode bn = state.getMethodBody(md);
333 bn.addFirstBlockStatement(ben);
337 public void checkMethodBody(ClassDescriptor cd, MethodDescriptor md) {
338 ClassDescriptor superdesc=cd.getSuperDesc();
339 if (superdesc!=null) {
340 Set possiblematches=superdesc.getMethodTable().getSet(md.getSymbol());
341 for(Iterator methodit=possiblematches.iterator(); methodit.hasNext(); ) {
342 MethodDescriptor matchmd=(MethodDescriptor)methodit.next();
343 if (md.matches(matchmd)) {
344 if (matchmd.getModifiers().isFinal()) {
345 throw new Error("Try to override final method in method:"+md+" declared in "+cd);
350 BlockNode bn=state.getMethodBody(md);
351 checkBlockNode(md, md.getParameterTable(),bn);
354 public void checkBlockNode(Descriptor md, SymbolTable nametable, BlockNode bn) {
355 /* Link in the naming environment */
356 bn.getVarTable().setParent(nametable);
357 for(int i=0; i<bn.size(); i++) {
358 BlockStatementNode bsn=bn.get(i);
359 checkBlockStatementNode(md, bn.getVarTable(),bsn);
363 public void checkBlockStatementNode(Descriptor md, SymbolTable nametable, BlockStatementNode bsn) {
365 case Kind.BlockExpressionNode:
366 checkBlockExpressionNode(md, nametable,(BlockExpressionNode)bsn);
369 case Kind.DeclarationNode:
370 checkDeclarationNode(md, nametable, (DeclarationNode)bsn);
373 case Kind.TagDeclarationNode:
374 checkTagDeclarationNode(md, nametable, (TagDeclarationNode)bsn);
377 case Kind.IfStatementNode:
378 checkIfStatementNode(md, nametable, (IfStatementNode)bsn);
381 case Kind.SwitchStatementNode:
382 checkSwitchStatementNode(md, nametable, (SwitchStatementNode)bsn);
386 checkLoopNode(md, nametable, (LoopNode)bsn);
389 case Kind.ReturnNode:
390 checkReturnNode(md, nametable, (ReturnNode)bsn);
393 case Kind.TaskExitNode:
394 checkTaskExitNode(md, nametable, (TaskExitNode)bsn);
397 case Kind.SubBlockNode:
398 checkSubBlockNode(md, nametable, (SubBlockNode)bsn);
401 case Kind.AtomicNode:
402 checkAtomicNode(md, nametable, (AtomicNode)bsn);
405 case Kind.SynchronizedNode:
406 checkSynchronizedNode(md, nametable, (SynchronizedNode)bsn);
409 case Kind.ContinueBreakNode:
410 checkContinueBreakNode(md, nametable, (ContinueBreakNode) bsn);
414 case Kind.GenReachNode:
415 case Kind.GenDefReachNode:
416 // do nothing, no semantic check for SESEs
423 void checkBlockExpressionNode(Descriptor md, SymbolTable nametable, BlockExpressionNode ben) {
424 checkExpressionNode(md, nametable, ben.getExpression(), null);
427 void checkDeclarationNode(Descriptor md, SymbolTable nametable, DeclarationNode dn) {
428 VarDescriptor vd=dn.getVarDescriptor();
429 checkTypeDescriptor(((md instanceof MethodDescriptor)?((MethodDescriptor)md).getClassDesc():null), vd.getType());
430 Descriptor d=nametable.get(vd.getSymbol());
432 (d instanceof FieldDescriptor)) {
435 throw new Error(vd.getSymbol()+" in "+md+" defined a second time");
436 if (dn.getExpression()!=null)
437 checkExpressionNode(md, nametable, dn.getExpression(), vd.getType());
440 void checkTagDeclarationNode(Descriptor md, SymbolTable nametable, TagDeclarationNode dn) {
441 TagVarDescriptor vd=dn.getTagVarDescriptor();
442 Descriptor d=nametable.get(vd.getSymbol());
444 (d instanceof FieldDescriptor)) {
447 throw new Error(vd.getSymbol()+" defined a second time");
450 void checkSubBlockNode(Descriptor md, SymbolTable nametable, SubBlockNode sbn) {
451 checkBlockNode(md, nametable, sbn.getBlockNode());
454 void checkAtomicNode(Descriptor md, SymbolTable nametable, AtomicNode sbn) {
455 checkBlockNode(md, nametable, sbn.getBlockNode());
458 void checkSynchronizedNode(Descriptor md, SymbolTable nametable, SynchronizedNode sbn) {
459 checkBlockNode(md, nametable, sbn.getBlockNode());
460 //todo this could be Object
461 checkExpressionNode(md, nametable, sbn.getExpr(), null);
464 void checkContinueBreakNode(Descriptor md, SymbolTable nametable, ContinueBreakNode cbn) {
465 if (loopstack.empty())
466 throw new Error("continue/break outside of loop");
467 Object o = loopstack.peek();
468 if(o instanceof LoopNode) {
469 LoopNode ln=(LoopNode)o;
474 void checkReturnNode(Descriptor d, SymbolTable nametable, ReturnNode rn) {
475 if (d instanceof TaskDescriptor)
476 throw new Error("Illegal return appears in Task: "+d.getSymbol());
477 MethodDescriptor md=(MethodDescriptor)d;
478 if (rn.getReturnExpression()!=null)
479 if (md.getReturnType()==null)
480 throw new Error("Constructor can't return something.");
481 else if (md.getReturnType().isVoid())
482 throw new Error(md+" is void");
484 checkExpressionNode(md, nametable, rn.getReturnExpression(), md.getReturnType());
486 if (md.getReturnType()!=null&&!md.getReturnType().isVoid())
487 throw new Error("Need to return something for "+md);
490 void checkTaskExitNode(Descriptor md, SymbolTable nametable, TaskExitNode ten) {
491 if (md instanceof MethodDescriptor)
492 throw new Error("Illegal taskexit appears in Method: "+md.getSymbol());
493 checkFlagEffects((TaskDescriptor)md, ten.getFlagEffects(),nametable);
494 checkConstraintCheck((TaskDescriptor) md, nametable, ten.getChecks());
497 void checkIfStatementNode(Descriptor md, SymbolTable nametable, IfStatementNode isn) {
498 checkExpressionNode(md, nametable, isn.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
499 checkBlockNode(md, nametable, isn.getTrueBlock());
500 if (isn.getFalseBlock()!=null)
501 checkBlockNode(md, nametable, isn.getFalseBlock());
504 void checkSwitchStatementNode(Descriptor md, SymbolTable nametable, SwitchStatementNode ssn) {
505 checkExpressionNode(md, nametable, ssn.getCondition(), new TypeDescriptor(TypeDescriptor.INT));
507 BlockNode sbn = ssn.getSwitchBody();
508 boolean hasdefault = false;
509 for(int i = 0; i < sbn.size(); i++) {
510 boolean containdefault = checkSwitchBlockNode(md, nametable, (SwitchBlockNode)sbn.get(i));
511 if(hasdefault && containdefault) {
512 throw new Error("Error: duplicate default branch in switch-case statement in Method: " + md.getSymbol());
514 hasdefault = containdefault;
518 boolean checkSwitchBlockNode(Descriptor md, SymbolTable nametable, SwitchBlockNode sbn) {
519 Vector<SwitchLabelNode> slnv = sbn.getSwitchConditions();
521 for(int i = 0; i < slnv.size(); i++) {
522 if(slnv.elementAt(i).isdefault) {
525 checkConstantExpressionNode(md, nametable, slnv.elementAt(i).getCondition(), new TypeDescriptor(TypeDescriptor.INT));
529 throw new Error("Error: duplicate default branch in switch-case statement in Method: " + md.getSymbol());
532 checkBlockNode(md, nametable, sbn.getSwitchBlockStatement());
534 return (defaultb > 0);
538 void checkConstantExpressionNode(Descriptor md, SymbolTable nametable, ExpressionNode en, TypeDescriptor td) {
540 case Kind.FieldAccessNode:
541 checkFieldAccessNode(md,nametable,(FieldAccessNode)en,td);
544 case Kind.LiteralNode:
545 checkLiteralNode(md,nametable,(LiteralNode)en,td);
549 checkNameNode(md,nametable,(NameNode)en,td);
553 checkOpNode(md, nametable, (OpNode)en, td);
559 void checkExpressionNode(Descriptor md, SymbolTable nametable, ExpressionNode en, TypeDescriptor td) {
561 case Kind.AssignmentNode:
562 checkAssignmentNode(md,nametable,(AssignmentNode)en,td);
566 checkCastNode(md,nametable,(CastNode)en,td);
569 case Kind.CreateObjectNode:
570 checkCreateObjectNode(md,nametable,(CreateObjectNode)en,td);
573 case Kind.FieldAccessNode:
574 checkFieldAccessNode(md,nametable,(FieldAccessNode)en,td);
577 case Kind.ArrayAccessNode:
578 checkArrayAccessNode(md,nametable,(ArrayAccessNode)en,td);
581 case Kind.LiteralNode:
582 checkLiteralNode(md,nametable,(LiteralNode)en,td);
585 case Kind.MethodInvokeNode:
586 checkMethodInvokeNode(md,nametable,(MethodInvokeNode)en,td);
590 checkNameNode(md,nametable,(NameNode)en,td);
594 checkOpNode(md,nametable,(OpNode)en,td);
597 case Kind.OffsetNode:
598 checkOffsetNode(md, nametable, (OffsetNode)en, td);
601 case Kind.TertiaryNode:
602 checkTertiaryNode(md, nametable, (TertiaryNode)en, td);
605 case Kind.InstanceOfNode:
606 checkInstanceOfNode(md, nametable, (InstanceOfNode) en, td);
609 case Kind.ArrayInitializerNode:
610 checkArrayInitializerNode(md, nametable, (ArrayInitializerNode) en, td);
613 case Kind.ClassTypeNode:
614 checkClassTypeNode(md, nametable, (ClassTypeNode) en, td);
620 void checkClassTypeNode(Descriptor md, SymbolTable nametable, ClassTypeNode tn, TypeDescriptor td) {
621 checkTypeDescriptor(((md instanceof MethodDescriptor)?((MethodDescriptor)md).getClassDesc():null), tn.getType());
624 void checkCastNode(Descriptor md, SymbolTable nametable, CastNode cn, TypeDescriptor td) {
625 /* Get type descriptor */
626 if (cn.getType()==null) {
627 NameDescriptor typenamed=cn.getTypeName().getName();
628 TypeDescriptor ntd=new TypeDescriptor(getClass(((md instanceof MethodDescriptor)?((MethodDescriptor)md).getClassDesc():null), typenamed.toString()));
632 /* Check the type descriptor */
633 TypeDescriptor cast_type=cn.getType();
634 checkTypeDescriptor(((md instanceof MethodDescriptor)?((MethodDescriptor)md).getClassDesc():null),cast_type);
638 if (!typeutil.isSuperorType(td,cast_type))
639 throw new Error("Cast node returns "+cast_type+", but need "+td);
642 ExpressionNode en=cn.getExpression();
643 checkExpressionNode(md, nametable, en, null);
644 TypeDescriptor etd=en.getType();
645 if (typeutil.isSuperorType(cast_type,etd)) /* Cast trivially succeeds */
648 if (typeutil.isSuperorType(etd,cast_type)) /* Cast may succeed */
650 if (typeutil.isCastable(etd, cast_type))
653 //rough hack to handle interfaces...should clean up
654 if (etd.isClass()&&cast_type.isClass()) {
655 ClassDescriptor cdetd=etd.getClassDesc();
656 ClassDescriptor cdcast_type=cast_type.getClassDesc();
658 if (cdetd.isInterface()&&!cdcast_type.getModifier().isFinal())
661 if (cdcast_type.isInterface()&&!cdetd.getModifier().isFinal())
664 /* Different branches */
665 /* TODO: change if add interfaces */
666 throw new Error("Cast will always fail\n"+cn.printNode(0));
669 //FieldDescriptor checkFieldAccessNodeForParentNode( Descriptor md, SymbolTable na )
670 void checkFieldAccessNode(Descriptor md, SymbolTable nametable, FieldAccessNode fan, TypeDescriptor td) {
671 ExpressionNode left=fan.getExpression();
672 checkExpressionNode(md,nametable,left,null);
673 TypeDescriptor ltd=left.getType();
675 checkClass(ltd.getClassDesc(), INIT);
676 String fieldname=fan.getFieldName();
678 FieldDescriptor fd=null;
679 if (ltd.isArray()&&fieldname.equals("length"))
680 fd=FieldDescriptor.arrayLength;
681 else if(((left instanceof NameNode) && ((NameNode)left).isSuper())
682 ||((left instanceof FieldAccessNode) && ((FieldAccessNode)left).isSuper())){
683 fd = (FieldDescriptor) ltd.getClassDesc().getSuperDesc().getFieldTable().get(fieldname);
685 fd=(FieldDescriptor) ltd.getClassDesc().getFieldTable().get(fieldname);
687 if(ltd.isClassNameRef()) {
688 // the field access is using a class name directly
690 // check if it is to access a surrounding class in an inner class
691 if(fieldname.equals("this") || fieldname.equals("super")) {
692 ClassDescriptor icd = ((VarDescriptor)nametable.get("this")).getType().getClassDesc();
693 if(icd.isInnerClass()) {
694 NameNode nn = new NameNode(new NameDescriptor("this"));
695 nn.setVar((VarDescriptor)nametable.get("this"));
696 fan.setExpression(nn);
697 if(icd.getSurroundingDesc()==ltd.getClassDesc()) {
698 // this is a surrounding class access inside an inner class
699 fan.setExpression(nn);
700 fan.setFieldName("this$0");
701 fd = (FieldDescriptor)icd.getFieldTable().get("this$0");
702 } else if(icd==ltd.getClassDesc()) {
703 // this is an inner class this operation
704 fd = new FieldDescriptor(new Modifiers(),new TypeDescriptor(icd),"this",null,false);
706 if(fieldname.equals("super")) {
713 ClassDescriptor surroundingCls=ltd.getClassDesc().getSurroundingDesc();
715 while(surroundingCls!=null) {
716 fd=(FieldDescriptor) surroundingCls.getFieldTable().get(fieldname);
718 fan.left=new ClassTypeNode(new TypeDescriptor(surroundingCls));
721 surroundingCls=surroundingCls.getSurroundingDesc();
725 if(ltd.getClassDesc().isEnum()) {
726 int value = ltd.getClassDesc().getEnumConstant(fieldname);
728 // check if this field is an enum constant
729 throw new Error(fieldname + " is not an enum constant in "+fan.printNode(0)+" in "+md);
731 fd = new FieldDescriptor(new Modifiers(Modifiers.PUBLIC|Modifiers.FINAL), new TypeDescriptor(TypeDescriptor.INT), fieldname, null, false);
733 fd.setEnumValue(value);
734 } else if(fd == null) {
735 throw new Error("Could not find field "+ fieldname + " in "+fan.printNode(0)+" in "+md + " (Line: "+fan.getNumLine()+")");
736 } else if(fd.isStatic()) {
737 // check if this field is a static field
738 if(fd.getExpressionNode() != null) {
739 checkExpressionNode(md,nametable,fd.getExpressionNode(),null);
742 throw new Error("Dereference of the non-static field "+ fieldname + " in "+fan.printNode(0)+" in "+md);
747 if((md instanceof MethodDescriptor) && false == ((MethodDescriptor)md).isStaticBlock()) {
748 ClassDescriptor cd = ((MethodDescriptor)md).getClassDesc();
749 FieldAccessNode theFieldNode = fieldAccessExpression( cd, fieldname, fan.getNumLine() );
750 if( null != theFieldNode ) {
751 //fan = theFieldNode;
752 checkFieldAccessNode( md, nametable, theFieldNode, td );
753 fan.setField( theFieldNode.getField() );
754 fan.setExpression( theFieldNode.getExpression() );
755 //TypeDescriptor td1 = fan.getType();
760 throw new Error("Unknown field "+fieldname + " in "+fan.printNode(0)+" in "+md);
763 if (fd.getType().iswrapper()) {
764 FieldAccessNode fan2=new FieldAccessNode(left, fieldname);
765 fan2.setNumLine(left.getNumLine());
768 fan.fieldname="value";
770 ExpressionNode leftwr=fan.getExpression();
771 TypeDescriptor ltdwr=leftwr.getType();
772 String fieldnamewr=fan.getFieldName();
773 FieldDescriptor fdwr=(FieldDescriptor) ltdwr.getClassDesc().getFieldTable().get(fieldnamewr);
776 throw new Error("Unknown field "+fieldnamewr + " in "+fan.printNode(0)+" in "+md);
781 if (!typeutil.isSuperorType(td,fan.getType()))
782 throw new Error("Field node returns "+fan.getType()+", but need "+td);
786 void checkArrayAccessNode(Descriptor md, SymbolTable nametable, ArrayAccessNode aan, TypeDescriptor td) {
787 ExpressionNode left=aan.getExpression();
788 checkExpressionNode(md,nametable,left,null);
790 checkExpressionNode(md,nametable,aan.getIndex(),new TypeDescriptor(TypeDescriptor.INT));
791 TypeDescriptor ltd=left.getType();
792 if (ltd.dereference().iswrapper()) {
793 aan.wrappertype=((FieldDescriptor)ltd.dereference().getClassDesc().getFieldTable().get("value")).getType();
797 if (!typeutil.isSuperorType(td,aan.getType()))
798 throw new Error("Field node returns "+aan.getType()+", but need "+td);
801 void checkLiteralNode(Descriptor md, SymbolTable nametable, LiteralNode ln, TypeDescriptor td) {
802 /* Resolve the type */
803 Object o=ln.getValue();
804 if (ln.getTypeString().equals("null")) {
805 ln.setType(new TypeDescriptor(TypeDescriptor.NULL));
806 } else if (o instanceof Integer) {
807 ln.setType(new TypeDescriptor(TypeDescriptor.INT));
808 } else if (o instanceof Long) {
809 ln.setType(new TypeDescriptor(TypeDescriptor.LONG));
810 } else if (o instanceof Float) {
811 ln.setType(new TypeDescriptor(TypeDescriptor.FLOAT));
812 } else if (o instanceof Boolean) {
813 ln.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
814 } else if (o instanceof Double) {
815 ln.setType(new TypeDescriptor(TypeDescriptor.DOUBLE));
816 } else if (o instanceof Character) {
817 ln.setType(new TypeDescriptor(TypeDescriptor.CHAR));
818 } else if (o instanceof String) {
819 ln.setType(new TypeDescriptor(getClass(null, TypeUtil.StringClass)));
823 if (!typeutil.isSuperorType(td,ln.getType())) {
824 Long l = ln.evaluate();
825 if((ln.getType().isByte() || ln.getType().isShort()
826 || ln.getType().isChar() || ln.getType().isInt())
828 && (td.isByte() || td.isShort() || td.isChar()
829 || td.isInt() || td.isLong())) {
830 long lnvalue = l.longValue();
831 if((td.isByte() && ((lnvalue > 127) || (lnvalue < -128)))
832 || (td.isShort() && ((lnvalue > 32767) || (lnvalue < -32768)))
833 || (td.isChar() && ((lnvalue > 65535) || (lnvalue < 0)))
834 || (td.isInt() && ((lnvalue > 2147483647) || (lnvalue < -2147483648)))
835 || (td.isLong() && ((lnvalue > 9223372036854775807L) || (lnvalue < -9223372036854775808L)))) {
836 throw new Error("Field node returns "+ln.getType()+", but need "+td+" in "+md);
839 throw new Error("Field node returns "+ln.getType()+", but need "+td+" in "+md);
844 FieldDescriptor recurseSurroundingClasses( ClassDescriptor icd, String varname ) {
845 if( null == icd || false == icd.isInnerClass() )
848 ClassDescriptor surroundingDesc = icd.getSurroundingDesc();
849 if( null == surroundingDesc )
852 SymbolTable fieldTable = surroundingDesc.getFieldTable();
853 FieldDescriptor fd = ( FieldDescriptor ) fieldTable.get( varname );
856 return recurseSurroundingClasses( surroundingDesc, varname );
859 FieldAccessNode fieldAccessExpression( ClassDescriptor icd, String varname, int linenum ) {
860 FieldDescriptor fd = recurseSurroundingClasses( icd, varname );
864 ClassDescriptor cd = fd.getClassDescriptor();
866 int startingDepth = icd.getInnerDepth();
868 if( true == cd.isInnerClass() )
869 depth = cd.getInnerDepth();
871 String composed = "this";
872 NameDescriptor runningDesc = new NameDescriptor( "this" );;
874 for ( int index = startingDepth; index > depth; --index ) {
875 composed = "this$" + String.valueOf( index - 1 );
876 runningDesc = new NameDescriptor( runningDesc, composed );
878 if( false == cd.isInnerClass() )
879 runningDesc = new NameDescriptor( runningDesc, "this$" + String.valueOf(0) ); //all the way up.
880 NameDescriptor idDesc = new NameDescriptor( runningDesc, varname );
883 FieldAccessNode theFieldNode = ( FieldAccessNode )translateNameDescriptorintoExpression( idDesc, linenum );
887 void checkNameNode(Descriptor md, SymbolTable nametable, NameNode nn, TypeDescriptor td) {
888 NameDescriptor nd=nn.getName();
889 if (nd.getBase()!=null) {
891 /* Rewrite NameNode */
892 ExpressionNode en=translateNameDescriptorintoExpression(nd,nn.getNumLine());
893 nn.setExpression(en);
894 checkExpressionNode(md,nametable,en,td);
896 String varname=nd.toString();
897 if(varname.equals("this") || varname.equals("super")) {
899 nn.setVar((VarDescriptor)nametable.get("this"));
900 if(varname.equals("super")) {
905 Descriptor d=(Descriptor)nametable.get(varname);
907 ClassDescriptor cd = null;
908 //check the inner class case first.
909 if((md instanceof MethodDescriptor) && false == ((MethodDescriptor)md).isStaticBlock()) {
910 cd = ((MethodDescriptor)md).getClassDesc();
911 FieldAccessNode theFieldNode = fieldAccessExpression( cd, varname, nn.getNumLine() );
912 if( null != theFieldNode ) {
913 nn.setExpression(( ExpressionNode )theFieldNode);
914 checkExpressionNode(md,nametable,( ExpressionNode )theFieldNode,td);
918 if((md instanceof MethodDescriptor) && ((MethodDescriptor)md).isStaticBlock()) {
919 // this is a static block, all the accessed fields should be static field
920 cd = ((MethodDescriptor)md).getClassDesc();
921 SymbolTable fieldtbl = cd.getFieldTable();
922 FieldDescriptor fd=(FieldDescriptor)fieldtbl.get(varname);
923 if((fd == null) || (!fd.isStatic())) {
924 // no such field in the class, check if this is a class
925 if(varname.equals("this")) {
926 throw new Error("Error: access this obj in a static block");
928 cd=getClass(((md instanceof MethodDescriptor)?((MethodDescriptor)md).getClassDesc():null), varname);
930 // this is a class name
934 throw new Error("Name "+varname+" should not be used in static block: "+md);
937 // this is a static field
943 // check if the var is a static field of the class
944 if(md instanceof MethodDescriptor) {
945 cd = ((MethodDescriptor)md).getClassDesc();
946 FieldDescriptor fd = (FieldDescriptor)cd.getFieldTable().get(varname);
947 if((fd != null) && (fd.isStatic())) {
951 if (!typeutil.isSuperorType(td,nn.getType()))
952 throw new Error("Field node returns "+nn.getType()+", but need "+td);
954 } else if(fd != null) {
955 throw new Error("Name "+varname+" should not be used in " + md);
958 cd=getClass(((md instanceof MethodDescriptor)?((MethodDescriptor)md).getClassDesc():null), varname);
960 // this is a class name
964 throw new Error("Name "+varname+" undefined in: "+md);
969 if (d instanceof VarDescriptor) {
971 } else if (d instanceof FieldDescriptor) {
972 FieldDescriptor fd=(FieldDescriptor)d;
973 if (fd.getType().iswrapper()) {
974 String id=nd.getIdentifier();
975 NameDescriptor base=nd.getBase();
976 NameNode n=new NameNode(nn.getName());
977 n.setNumLine(nn.getNumLine());
979 n.setVar((VarDescriptor)nametable.get("this")); /* Need a pointer to this */
980 FieldAccessNode fan=new FieldAccessNode(n,"value");
981 fan.setNumLine(n.getNumLine());
982 FieldDescriptor fdval=(FieldDescriptor) fd.getType().getClassDesc().getFieldTable().get("value");
984 nn.setExpression(fan);
987 nn.setVar((VarDescriptor)nametable.get("this")); /* Need a pointer to this */
989 } else if (d instanceof TagVarDescriptor) {
991 } else throw new Error("Wrong type of descriptor");
993 if (!typeutil.isSuperorType(td,nn.getType()))
994 throw new Error("Field node returns "+nn.getType()+", but need "+td);
998 void checkOffsetNode(Descriptor md, SymbolTable nameTable, OffsetNode ofn, TypeDescriptor td) {
999 TypeDescriptor ltd=ofn.td;
1000 checkTypeDescriptor(((md instanceof MethodDescriptor)?((MethodDescriptor)md).getClassDesc():null),ltd);
1002 String fieldname = ofn.fieldname;
1003 FieldDescriptor fd=null;
1004 if (ltd.isArray()&&fieldname.equals("length")) {
1005 fd=FieldDescriptor.arrayLength;
1007 fd=(FieldDescriptor) ltd.getClassDesc().getFieldTable().get(fieldname);
1011 checkField(ltd.getClassDesc(), fd);
1014 throw new Error("Unknown field "+fieldname + " in "+ofn.printNode(1)+" in "+md);
1017 if (!typeutil.isSuperorType(td, ofn.getType())) {
1018 throw new Error("Type of rside not compatible with type of lside"+ofn.printNode(0));
1024 void checkTertiaryNode(Descriptor md, SymbolTable nametable, TertiaryNode tn, TypeDescriptor td) {
1025 checkExpressionNode(md, nametable, tn.getCond(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
1026 checkExpressionNode(md, nametable, tn.getTrueExpr(), td);
1027 checkExpressionNode(md, nametable, tn.getFalseExpr(), td);
1030 void checkInstanceOfNode(Descriptor md, SymbolTable nametable, InstanceOfNode tn, TypeDescriptor td) {
1031 if (td!=null&&!td.isBoolean())
1032 throw new Error("Expecting type "+td+"for instanceof expression");
1034 checkTypeDescriptor(((md instanceof MethodDescriptor)?((MethodDescriptor)md).getClassDesc():null), tn.getExprType());
1035 checkExpressionNode(md, nametable, tn.getExpr(), null);
1038 void checkArrayInitializerNode(Descriptor md, SymbolTable nametable, ArrayInitializerNode ain, TypeDescriptor td) {
1039 Vector<TypeDescriptor> vec_type = new Vector<TypeDescriptor>();
1040 for( int i = 0; i < ain.numVarInitializers(); ++i ) {
1041 checkExpressionNode(md, nametable, ain.getVarInitializer(i), td.dereference());
1042 vec_type.add(ain.getVarInitializer(i).getType());
1050 void checkAssignmentNode(Descriptor md, SymbolTable nametable, AssignmentNode an, TypeDescriptor td) {
1051 boolean postinc=true;
1052 if (an.getOperation().getBaseOp()==null||
1053 (an.getOperation().getBaseOp().getOp()!=Operation.POSTINC&&
1054 an.getOperation().getBaseOp().getOp()!=Operation.POSTDEC))
1057 checkExpressionNode(md, nametable, an.getSrc(),td);
1058 //TODO: Need check on validity of operation here
1059 if (!((an.getDest() instanceof FieldAccessNode)||
1060 (an.getDest() instanceof ArrayAccessNode)||
1061 (an.getDest() instanceof NameNode)))
1062 throw new Error("Bad lside in "+an.printNode(0));
1063 checkExpressionNode(md, nametable, an.getDest(), null);
1065 /* We want parameter variables to tasks to be immutable */
1066 if (md instanceof TaskDescriptor) {
1067 if (an.getDest() instanceof NameNode) {
1068 NameNode nn=(NameNode)an.getDest();
1069 if (nn.getVar()!=null) {
1070 if (((TaskDescriptor)md).getParameterTable().contains(nn.getVar().getSymbol()))
1071 throw new Error("Can't modify parameter "+nn.getVar()+ " to task "+td.getSymbol());
1076 if (an.getDest().getType().isString()&&an.getOperation().getOp()==AssignOperation.PLUSEQ) {
1078 ClassDescriptor stringcl=getClass(null, TypeUtil.StringClass);
1079 TypeDescriptor stringtd=new TypeDescriptor(stringcl);
1080 NameDescriptor nd=new NameDescriptor("String");
1081 NameDescriptor valuend=new NameDescriptor(nd, "valueOf");
1083 if (!(an.getSrc().getType().isString()&&(an.getSrc() instanceof OpNode))) {
1084 MethodInvokeNode rightmin=new MethodInvokeNode(valuend);
1085 rightmin.setNumLine(an.getSrc().getNumLine());
1086 rightmin.addArgument(an.getSrc());
1088 checkExpressionNode(md, nametable, an.getSrc(), null);
1092 if (!postinc&&!typeutil.isSuperorType(an.getDest().getType(),an.getSrc().getType())) {
1093 TypeDescriptor dt = an.getDest().getType();
1094 TypeDescriptor st = an.getSrc().getType();
1095 if(an.getSrc().kind() == Kind.ArrayInitializerNode) {
1096 if(dt.getArrayCount() != st.getArrayCount()) {
1097 throw new Error("Type of rside ("+an.getSrc().getType().toPrettyString()+") not compatible with type of lside ("+an.getDest().getType().toPrettyString()+")"+an.printNode(0));
1100 dt = dt.dereference();
1101 st = st.dereference();
1102 } while(dt.isArray());
1103 if((st.isByte() || st.isShort() || st.isChar() || st.isInt())
1104 && (dt.isByte() || dt.isShort() || dt.isChar() || dt.isInt() || dt.isLong())) {
1107 throw new Error("Type of rside ("+an.getSrc().getType().toPrettyString()+") not compatible with type of lside ("+an.getDest().getType().toPrettyString()+")"+an.printNode(0));
1111 Long l = an.getSrc().evaluate();
1112 if((st.isByte() || st.isShort() || st.isChar() || st.isInt())
1114 && (dt.isByte() || dt.isShort() || dt.isChar() || dt.isInt() || dt.isLong())) {
1115 long lnvalue = l.longValue();
1116 if((dt.isByte() && ((lnvalue > 127) || (lnvalue < -128)))
1117 || (dt.isShort() && ((lnvalue > 32767) || (lnvalue < -32768)))
1118 || (dt.isChar() && ((lnvalue > 65535) || (lnvalue < 0)))
1119 || (dt.isInt() && ((lnvalue > 2147483647) || (lnvalue < -2147483648)))
1120 || (dt.isLong() && ((lnvalue > 9223372036854775807L) || (lnvalue < -9223372036854775808L)))) {
1121 throw new Error("Type of rside ("+an.getSrc().getType().toPrettyString()+") not compatible with type of lside ("+an.getDest().getType().toPrettyString()+")"+an.printNode(0));
1124 throw new Error("Type of rside ("+an.getSrc().getType().toPrettyString()+") not compatible with type of lside ("+an.getDest().getType().toPrettyString()+")"+an.printNode(0));
1130 void checkLoopNode(Descriptor md, SymbolTable nametable, LoopNode ln) {
1132 if (ln.getType()==LoopNode.WHILELOOP||ln.getType()==LoopNode.DOWHILELOOP) {
1133 checkExpressionNode(md, nametable, ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
1134 checkBlockNode(md, nametable, ln.getBody());
1137 /* Link in the initializer naming environment */
1138 BlockNode bn=ln.getInitializer();
1139 bn.getVarTable().setParent(nametable);
1140 for(int i=0; i<bn.size(); i++) {
1141 BlockStatementNode bsn=bn.get(i);
1142 checkBlockStatementNode(md, bn.getVarTable(),bsn);
1144 //check the condition
1145 checkExpressionNode(md, bn.getVarTable(), ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
1146 checkBlockNode(md, bn.getVarTable(), ln.getBody());
1147 checkBlockNode(md, bn.getVarTable(), ln.getUpdate());
1152 void InnerClassAddParamToCtor( MethodDescriptor md, ClassDescriptor cd, SymbolTable nametable,
1153 CreateObjectNode con, TypeDescriptor td ) {
1155 TypeDescriptor cdsType = new TypeDescriptor( cd );
1156 ExpressionNode conExp = con.getSurroundingClassExpression();
1157 //System.out.println( "The surrounding class expression si " + con );
1158 if( null == conExp ) {
1159 if( md.isStatic() ) {
1160 throw new Error("trying to instantiate inner class: " + con.getType() + " in a static scope" );
1162 VarDescriptor thisVD = md.getThis();
1163 if( null == thisVD ) {
1164 throw new Error( "this pointer is not defined in a non static scope" );
1166 if( cdsType.equals( thisVD.getType() ) == false ) {
1167 throw new Error( "the type of this pointer is different than the type expected for inner class constructor. Initializing the inner class: " + con.getType() + " in the wrong scope" );
1169 //make this into an expression node.
1170 NameNode nThis=new NameNode( new NameDescriptor( "this" ) );
1171 con.addArgument( nThis );
1174 //REVISIT : here i am storing the expression as an expressionNode which does not implement type, there is no way for me to semantic check this argument.
1175 con.addArgument( conExp );
1177 //System.out.println( " the modified createObjectNode is " + con.printNode( 0 ) + "\n" );
1180 // add all the vars in the surrounding context of the inline class into the inline
1181 // classes field table and pass them to the inline class' constructors
1182 // TODO: BUGFIX. There need not add all the local vars of the surrounding context
1183 // into the inline class. Should only add those that are referred to inside the inline
1184 // class. Also these local vars should be final. But currently we've lost those
1185 // information, so we cannot have that checked.
1186 void InlineClassAddParamToCtor(MethodDescriptor md, ClassDescriptor cd , SymbolTable nametable, CreateObjectNode con, TypeDescriptor td ) {
1187 Iterator it_values = nametable.getAllValueSet().iterator();
1188 while(it_values.hasNext()) {
1189 Descriptor d = (Descriptor)it_values.next();
1190 if(d instanceof VarDescriptor && !d.getSymbol().equals("this")) {
1191 con.addArgument(new NameNode(new NameDescriptor(d.getSymbol())));
1192 cd.addField(new FieldDescriptor(new Modifiers(Modifiers.PUBLIC), ((VarDescriptor)d).getType(), d.getSymbol(), null, false));
1193 for(Iterator it_methods = cd.getMethods(); it_methods.hasNext();) {
1194 MethodDescriptor imd = (MethodDescriptor)it_methods.next();
1195 if(imd.isConstructor()) {
1196 imd.addParameter(((VarDescriptor)d).getType(), d.getSymbol()+"_p");
1197 // add the initialize statement into this constructor
1198 BlockNode obn = state.getMethodBody(imd);
1199 NameNode nn=new NameNode(new NameDescriptor(d.getSymbol()));
1200 NameNode fn = new NameNode (new NameDescriptor(d.getSymbol()+"_p"));
1201 AssignmentNode an=new AssignmentNode(nn,fn,new AssignOperation(1));
1202 obn.addFirstBlockStatement(new BlockExpressionNode(an));
1203 state.addTreeCode(imd, obn);
1210 void checkCreateObjectNode(Descriptor md, SymbolTable nametable, CreateObjectNode con,
1211 TypeDescriptor td) {
1212 TypeDescriptor[] tdarray = new TypeDescriptor[con.numArgs()];
1213 for (int i = 0; i < con.numArgs(); i++) {
1214 ExpressionNode en = con.getArg(i);
1215 checkExpressionNode(md, nametable, en, null);
1216 tdarray[i] = en.getType();
1219 TypeDescriptor typetolookin = con.getType();
1220 checkTypeDescriptor(((md instanceof MethodDescriptor)?((MethodDescriptor)md).getClassDesc():null), typetolookin);
1222 if (td != null && !typeutil.isSuperorType(td, typetolookin))
1223 throw new Error(typetolookin + " isn't a " + td);
1225 /* Check Array Initializers */
1226 if ((con.getArrayInitializer() != null)) {
1227 checkArrayInitializerNode(md, nametable, con.getArrayInitializer(), typetolookin);
1230 /* Check flag effects */
1231 if (con.getFlagEffects() != null) {
1232 FlagEffects fe = con.getFlagEffects();
1233 ClassDescriptor cd = typetolookin.getClassDesc();
1235 for (int j = 0; j < fe.numEffects(); j++) {
1236 FlagEffect flag = fe.getEffect(j);
1237 String name = flag.getName();
1238 FlagDescriptor flag_d = (FlagDescriptor) cd.getFlagTable().get(name);
1239 // Make sure the flag is declared
1241 throw new Error("Flag descriptor " + name + " undefined in class: " + cd.getSymbol());
1242 if (flag_d.getExternal())
1243 throw new Error("Attempting to modify external flag: " + name);
1244 flag.setFlag(flag_d);
1246 for (int j = 0; j < fe.numTagEffects(); j++) {
1247 TagEffect tag = fe.getTagEffect(j);
1248 String name = tag.getName();
1250 Descriptor d = (Descriptor) nametable.get(name);
1252 throw new Error("Tag descriptor " + name + " undeclared");
1253 else if (!(d instanceof TagVarDescriptor))
1254 throw new Error(name + " is not a tag descriptor");
1255 tag.setTag((TagVarDescriptor) d);
1259 if ((!typetolookin.isClass()) && (!typetolookin.isArray()))
1260 throw new Error("Can't allocate primitive type:" + con.printNode(0));
1262 if (!typetolookin.isArray()) {
1263 // Array's don't need constructor calls
1264 ClassDescriptor classtolookin = typetolookin.getClassDesc();
1265 checkClass(classtolookin, INIT);
1266 if( classtolookin.isInnerClass() ) {
1267 InnerClassAddParamToCtor( (MethodDescriptor)md, ((MethodDescriptor)md).getClassDesc() , nametable, con, td );
1268 if(classtolookin.getInline()) {
1269 // for an inline anonymous inner class, all the local variables are passed as
1270 // parameters of the constructors of the inline class
1271 InlineClassAddParamToCtor( (MethodDescriptor)md, classtolookin, nametable, con, td );
1273 tdarray = new TypeDescriptor[con.numArgs()];
1274 for (int i = 0; i < con.numArgs(); i++) {
1275 ExpressionNode en = con.getArg(i);
1276 checkExpressionNode(md, nametable, en, null);
1277 tdarray[i] = en.getType();
1280 Set methoddescriptorset = classtolookin.getMethodTable().getSet(classtolookin.getSymbol());
1281 MethodDescriptor bestmd = null;
1282 NextMethod: for (Iterator methodit = methoddescriptorset.iterator(); methodit.hasNext(); ) {
1283 MethodDescriptor currmd = (MethodDescriptor) methodit.next();
1284 /* Need correct number of parameters */
1285 if (con.numArgs() != currmd.numParameters())
1287 for (int i = 0; i < con.numArgs(); i++) {
1288 if (!typeutil.isSuperorType(currmd.getParamType(i), tdarray[i]))
1289 continue NextMethod;
1291 /* Local allocations can't call global allocator */
1292 if (!con.isGlobal() && currmd.isGlobal())
1295 /* Method okay so far */
1299 if (typeutil.isMoreSpecific(currmd, bestmd)) {
1301 } else if (con.isGlobal() && match(currmd, bestmd)) {
1302 if (currmd.isGlobal() && !bestmd.isGlobal())
1304 else if (currmd.isGlobal() && bestmd.isGlobal())
1306 } else if (!typeutil.isMoreSpecific(bestmd, currmd)) {
1307 throw new Error("No method is most specific:" + bestmd + " and " + currmd);
1310 /* Is this more specific than bestmd */
1313 if (bestmd == null) {
1314 throw new Error("No method found for " + con.printNode(0) + " in " + md);
1316 con.setConstructor(bestmd);
1321 /** Check to see if md1 is the same specificity as md2.*/
1323 boolean match(MethodDescriptor md1, MethodDescriptor md2) {
1324 /* Checks if md1 is more specific than md2 */
1325 if (md1.numParameters()!=md2.numParameters())
1327 for(int i=0; i<md1.numParameters(); i++) {
1328 if (!md2.getParamType(i).equals(md1.getParamType(i)))
1331 if (!md2.getReturnType().equals(md1.getReturnType()))
1334 if (!md2.getClassDesc().equals(md1.getClassDesc()))
1342 ExpressionNode translateNameDescriptorintoExpression(NameDescriptor nd, int numLine) {
1343 String id=nd.getIdentifier();
1344 NameDescriptor base=nd.getBase();
1346 NameNode nn=new NameNode(nd);
1347 nn.setNumLine(numLine);
1350 FieldAccessNode fan=new FieldAccessNode(translateNameDescriptorintoExpression(base,numLine),id);
1351 fan.setNumLine(numLine);
1356 MethodDescriptor recurseSurroundingClassesM( ClassDescriptor icd, String varname ) {
1357 if( null == icd || false == icd.isInnerClass() )
1360 ClassDescriptor surroundingDesc = icd.getSurroundingDesc();
1361 if( null == surroundingDesc )
1364 SymbolTable methodTable = surroundingDesc.getMethodTable();
1365 MethodDescriptor md = ( MethodDescriptor ) methodTable.get( varname );
1368 return recurseSurroundingClassesM( surroundingDesc, varname );
1371 ExpressionNode methodInvocationExpression( ClassDescriptor icd, MethodDescriptor md, int linenum ) {
1372 ClassDescriptor cd = md.getClassDesc();
1374 int startingDepth = icd.getInnerDepth();
1376 if( true == cd.isInnerClass() )
1377 depth = cd.getInnerDepth();
1379 String composed = "this";
1380 NameDescriptor runningDesc = new NameDescriptor( "this" );;
1382 for ( int index = startingDepth; index > depth; --index ) {
1383 composed = "this$" + String.valueOf( index - 1 );
1384 runningDesc = new NameDescriptor( runningDesc, composed );
1386 if( false == cd.isInnerClass() )
1387 runningDesc = new NameDescriptor( runningDesc, "this$" + String.valueOf(0) ); //all the way up.
1389 return new NameNode(runningDesc);
1392 void checkMethodInvokeNode(Descriptor md, SymbolTable nametable, MethodInvokeNode min, TypeDescriptor td) {
1393 /*Typecheck subexpressions
1394 and get types for expressions*/
1396 boolean isstatic = false;
1397 if((md instanceof MethodDescriptor) && ((MethodDescriptor)md).isStatic()) {
1400 TypeDescriptor[] tdarray=new TypeDescriptor[min.numArgs()];
1401 for(int i=0; i<min.numArgs(); i++) {
1402 ExpressionNode en=min.getArg(i);
1403 checkExpressionNode(md,nametable,en,null);
1404 tdarray[i]=en.getType();
1406 if(en.getType().isClass() && en.getType().getClassDesc().isEnum()) {
1407 tdarray[i] = new TypeDescriptor(TypeDescriptor.INT);
1410 TypeDescriptor typetolookin=null;
1411 if (min.getExpression()!=null) {
1412 checkExpressionNode(md,nametable,min.getExpression(),null);
1413 typetolookin=min.getExpression().getType();
1414 } else if (min.getBaseName()!=null) {
1415 String rootname=min.getBaseName().getRoot();
1416 if (rootname.equals("super")) {
1417 ClassDescriptor supercd=((MethodDescriptor)md).getClassDesc().getSuperDesc();
1418 typetolookin=new TypeDescriptor(supercd);
1420 } else if (rootname.equals("this")) {
1422 throw new Error("use this object in static method md = "+ md.toString());
1424 ClassDescriptor cd=((MethodDescriptor)md).getClassDesc();
1425 typetolookin=new TypeDescriptor(cd);
1426 } else if (nametable.get(rootname)!=null) {
1427 //we have an expression
1428 min.setExpression(translateNameDescriptorintoExpression(min.getBaseName(),min.getNumLine()));
1429 checkExpressionNode(md, nametable, min.getExpression(), null);
1430 typetolookin=min.getExpression().getType();
1432 if(!min.getBaseName().getSymbol().equals("System.out")||state.JNI) {
1433 ExpressionNode nn = translateNameDescriptorintoExpression(min.getBaseName(),min.getNumLine());
1434 checkExpressionNode(md, nametable, nn, null);
1435 typetolookin = nn.getType();
1436 if(!((nn.kind()== Kind.NameNode) && (((NameNode)nn).getField() == null)
1437 && (((NameNode)nn).getVar() == null) && (((NameNode)nn).getExpression() == null))) {
1438 // this is not a pure class name, need to add to
1439 min.setExpression(nn);
1443 ClassDescriptor cd = getClass(null, "System");
1446 throw new Error("md = "+ md.toString()+ " "+min.getBaseName()+" undefined");
1447 typetolookin=new TypeDescriptor(cd);
1450 } else if ((md instanceof MethodDescriptor)&&min.getMethodName().equals("super")) {
1451 ClassDescriptor supercd=((MethodDescriptor)md).getClassDesc().getSuperDesc();
1452 min.methodid=supercd.getSymbol();
1453 typetolookin=new TypeDescriptor(supercd);
1454 } else if (md instanceof MethodDescriptor) {
1455 typetolookin=new TypeDescriptor(((MethodDescriptor)md).getClassDesc());
1457 /* If this a task descriptor we throw an error at this point */
1458 throw new Error("Unknown method call to "+min.getMethodName()+"in task"+md.getSymbol());
1460 if (!typetolookin.isClass())
1461 throw new Error("Error with method call to "+min.getMethodName()+" in class "+typetolookin);
1462 ClassDescriptor classtolookin=typetolookin.getClassDesc();
1463 checkClass(classtolookin, REFERENCE);
1464 Set methoddescriptorset=classtolookin.getMethodTable().getSet(min.getMethodName());
1465 MethodDescriptor bestmd=null;
1467 for(Iterator methodit=methoddescriptorset.iterator(); methodit.hasNext(); ) {
1468 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
1469 /* Need correct number of parameters */
1470 if (min.numArgs()!=currmd.numParameters())
1472 for(int i=0; i<min.numArgs(); i++) {
1473 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
1474 if(((!tdarray[i].isArray() &&(tdarray[i].isInt() || tdarray[i].isLong()))
1475 && currmd.getParamType(i).isClass() && currmd.getParamType(i).getClassDesc().getSymbol().equals("Object"))) {
1476 // primitive parameters vs object
1478 continue NextMethod;
1481 /* Method okay so far */
1485 if (typeutil.isMoreSpecific(currmd,bestmd)) {
1487 } else if (!typeutil.isMoreSpecific(bestmd, currmd))
1488 throw new Error("No method is most specific:"+bestmd+" and "+currmd);
1490 /* Is this more specific than bestmd */
1494 // if this is an inner class, need to check the method table for the surrounding class
1495 bestmd = recurseSurroundingClassesM(classtolookin, min.getMethodName());
1497 throw new Error("No method found for :"+min.printNode(0)+" in class: " + classtolookin+" in "+md);
1499 // set the correct "this" expression here
1500 ExpressionNode en=methodInvocationExpression(classtolookin, bestmd, min.getNumLine());
1501 min.setExpression(en);
1502 checkExpressionNode(md, nametable, min.getExpression(), null);
1505 min.setMethod(bestmd);
1507 if ((td!=null)&&(min.getType()!=null)&&!typeutil.isSuperorType(td, min.getType()))
1508 throw new Error(min.getType()+ " is not equal to or a subclass of "+td);
1509 /* Check whether we need to set this parameter to implied this */
1510 if (!isstatic && !bestmd.isStatic()) {
1511 if (min.getExpression()==null) {
1512 ExpressionNode en=new NameNode(new NameDescriptor("this"));
1513 min.setExpression(en);
1514 checkExpressionNode(md, nametable, min.getExpression(), null);
1518 /* Check if we need to wrap primitive paratmeters to objects */
1519 for(int i=0; i<min.numArgs(); i++) {
1520 if(!tdarray[i].isArray() && (tdarray[i].isInt() || tdarray[i].isLong())
1521 && min.getMethod().getParamType(i).isClass() && min.getMethod().getParamType(i).getClassDesc().getSymbol().equals("Object")) {
1522 // Shall wrap this primitive parameter as a object
1523 ExpressionNode exp = min.getArg(i);
1524 TypeDescriptor ptd = null;
1525 NameDescriptor nd=null;
1526 if(exp.getType().isInt()) {
1527 nd = new NameDescriptor("Integer");
1528 ptd = state.getTypeDescriptor(nd);
1529 } else if(exp.getType().isLong()) {
1530 nd = new NameDescriptor("Long");
1531 ptd = state.getTypeDescriptor(nd);
1533 boolean isglobal = false;
1534 String disjointId = null;
1535 CreateObjectNode con=new CreateObjectNode(ptd, isglobal, disjointId);
1536 con.addArgument(exp);
1537 checkExpressionNode(md, nametable, con, null);
1538 min.setArgument(con, i);
1544 void checkOpNode(Descriptor md, SymbolTable nametable, OpNode on, TypeDescriptor td) {
1545 checkExpressionNode(md, nametable, on.getLeft(), null);
1546 if (on.getRight()!=null)
1547 checkExpressionNode(md, nametable, on.getRight(), null);
1548 TypeDescriptor ltd=on.getLeft().getType();
1549 TypeDescriptor rtd=on.getRight()!=null?on.getRight().getType():null;
1550 TypeDescriptor lefttype=null;
1551 TypeDescriptor righttype=null;
1552 Operation op=on.getOp();
1554 switch(op.getOp()) {
1555 case Operation.LOGIC_OR:
1556 case Operation.LOGIC_AND:
1557 if (!(rtd.isBoolean()))
1559 on.setRightType(rtd);
1561 case Operation.LOGIC_NOT:
1562 if (!(ltd.isBoolean()))
1565 on.setLeftType(ltd);
1567 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1570 case Operation.COMP:
1571 // 5.6.2 Binary Numeric Promotion
1572 //TODO unboxing of reference objects
1575 else if (ltd.isFloat())
1577 else if (ltd.isLong())
1578 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1580 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1581 on.setLeftType(lefttype);
1582 on.setType(lefttype);
1585 case Operation.BIT_OR:
1586 case Operation.BIT_XOR:
1587 case Operation.BIT_AND:
1588 // 5.6.2 Binary Numeric Promotion
1589 //TODO unboxing of reference objects
1590 if (ltd.isDouble()||rtd.isDouble())
1592 else if (ltd.isFloat()||rtd.isFloat())
1594 else if (ltd.isLong()||rtd.isLong())
1595 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1596 // 090205 hack for boolean
1597 else if (ltd.isBoolean()||rtd.isBoolean())
1598 lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
1600 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1603 on.setLeftType(lefttype);
1604 on.setRightType(righttype);
1605 on.setType(lefttype);
1608 case Operation.ISAVAILABLE:
1609 if (!(ltd.isPtr())) {
1610 throw new Error("Can't use isavailable on non-pointers/non-parameters.");
1613 on.setLeftType(lefttype);
1614 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1617 case Operation.EQUAL:
1618 case Operation.NOTEQUAL:
1619 // 5.6.2 Binary Numeric Promotion
1620 //TODO unboxing of reference objects
1621 if (ltd.isBoolean()||rtd.isBoolean()) {
1622 if (!(ltd.isBoolean()&&rtd.isBoolean()))
1624 righttype=lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
1625 } else if (ltd.isPtr()||rtd.isPtr()) {
1626 if (!(ltd.isPtr()&&rtd.isPtr())) {
1633 } else if (ltd.isDouble()||rtd.isDouble())
1634 righttype=lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
1635 else if (ltd.isFloat()||rtd.isFloat())
1636 righttype=lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
1637 else if (ltd.isLong()||rtd.isLong())
1638 righttype=lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1640 righttype=lefttype=new TypeDescriptor(TypeDescriptor.INT);
1642 on.setLeftType(lefttype);
1643 on.setRightType(righttype);
1644 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1653 // 5.6.2 Binary Numeric Promotion
1654 //TODO unboxing of reference objects
1655 if (!ltd.isNumber()||!rtd.isNumber()) {
1656 if (!ltd.isNumber())
1657 throw new Error("Leftside is not number"+on.printNode(0)+"type="+ltd.toPrettyString());
1658 if (!rtd.isNumber())
1659 throw new Error("Rightside is not number"+on.printNode(0));
1662 if (ltd.isDouble()||rtd.isDouble())
1663 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
1664 else if (ltd.isFloat()||rtd.isFloat())
1665 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
1666 else if (ltd.isLong()||rtd.isLong())
1667 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1669 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1671 on.setLeftType(lefttype);
1672 on.setRightType(righttype);
1673 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1677 if (ltd.isString()||rtd.isString()) {
1678 ClassDescriptor stringcl=getClass(null, TypeUtil.StringClass);
1679 TypeDescriptor stringtd=new TypeDescriptor(stringcl);
1680 NameDescriptor nd=new NameDescriptor("String");
1681 NameDescriptor valuend=new NameDescriptor(nd, "valueOf");
1682 if (!(ltd.isString()&&(on.getLeft() instanceof OpNode))) {
1683 MethodInvokeNode leftmin=new MethodInvokeNode(valuend);
1684 leftmin.setNumLine(on.getLeft().getNumLine());
1685 leftmin.addArgument(on.getLeft());
1687 checkExpressionNode(md, nametable, on.getLeft(), null);
1690 if (!(rtd.isString()&&(on.getRight() instanceof OpNode))) {
1691 MethodInvokeNode rightmin=new MethodInvokeNode(valuend);
1692 rightmin.setNumLine(on.getRight().getNumLine());
1693 rightmin.addArgument(on.getRight());
1695 checkExpressionNode(md, nametable, on.getRight(), null);
1698 on.setLeftType(stringtd);
1699 on.setRightType(stringtd);
1700 on.setType(stringtd);
1705 case Operation.MULT:
1708 // 5.6.2 Binary Numeric Promotion
1709 //TODO unboxing of reference objects
1710 if (ltd.isArray()||rtd.isArray()||!ltd.isNumber()||!rtd.isNumber())
1711 throw new Error("Error in "+on.printNode(0));
1713 if (ltd.isDouble()||rtd.isDouble())
1714 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
1715 else if (ltd.isFloat()||rtd.isFloat())
1716 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
1717 else if (ltd.isLong()||rtd.isLong())
1718 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1720 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1722 on.setLeftType(lefttype);
1723 on.setRightType(righttype);
1724 on.setType(lefttype);
1727 case Operation.LEFTSHIFT:
1728 case Operation.RIGHTSHIFT:
1729 case Operation.URIGHTSHIFT:
1730 if (!rtd.isIntegerType())
1732 //5.6.1 Unary Numeric Promotion
1733 if (rtd.isByte()||rtd.isShort()||rtd.isInt())
1734 righttype=new TypeDescriptor(TypeDescriptor.INT);
1738 on.setRightType(righttype);
1739 if (!ltd.isIntegerType())
1742 case Operation.UNARYPLUS:
1743 case Operation.UNARYMINUS:
1744 /* case Operation.POSTINC:
1745 case Operation.POSTDEC:
1746 case Operation.PREINC:
1747 case Operation.PREDEC:*/
1748 if (!ltd.isNumber())
1750 //5.6.1 Unary Numeric Promotion
1751 if (ltd.isByte()||ltd.isShort()||ltd.isInt())
1752 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1755 on.setLeftType(lefttype);
1756 on.setType(lefttype);
1760 throw new Error(op.toString());
1764 if (!typeutil.isSuperorType(td, on.getType())) {
1765 throw new Error("Type of rside not compatible with type of lside"+on.printNode(0));