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 if (cd.isInnerClass()) {
98 Modifiers fdmodifiers=new Modifiers();
99 FieldDescriptor enclosingfd=new FieldDescriptor(fdmodifiers,new TypeDescriptor(cd.getSurroundingDesc()),"this___enclosing",null,false);
100 cd.addField(enclosingfd);
103 /* Check to see that fields are well typed */
104 for(Iterator field_it=cd.getFields(); field_it.hasNext(); ) {
105 FieldDescriptor fd=(FieldDescriptor)field_it.next();
109 System.out.println("Class/Field in "+cd+":"+fd);
113 for(Iterator method_it=cd.getMethods(); method_it.hasNext(); ) {
114 MethodDescriptor md=(MethodDescriptor)method_it.next();
118 System.out.println("Class/Method in "+cd+":"+md);
126 public void semanticCheck() {
127 SymbolTable classtable = state.getClassSymbolTable();
128 toanalyze.addAll(classtable.getValueSet());
129 toanalyze.addAll(state.getTaskSymbolTable().getValueSet());
132 while (!toanalyze.isEmpty()) {
133 Object obj = toanalyze.iterator().next();
134 if (obj instanceof TaskDescriptor) {
135 toanalyze.remove(obj);
136 TaskDescriptor td = (TaskDescriptor) obj;
140 System.out.println("Error in " + td);
144 ClassDescriptor cd = (ClassDescriptor) obj;
145 toanalyze.remove(cd);
147 // need to initialize typeutil object here...only place we can
148 // get class descriptors without first calling getclass
149 getClass(cd, cd.getSymbol());
150 for (Iterator method_it = cd.getMethods(); method_it.hasNext(); ) {
151 MethodDescriptor md = (MethodDescriptor) method_it.next();
153 checkMethodBody(cd, md);
155 System.out.println("Error in " + md);
163 private void checkTypeDescriptor(ClassDescriptor cd, TypeDescriptor td) {
164 if (td.isPrimitive())
166 else if (td.isClass()) {
167 String name=td.toString();
168 ClassDescriptor field_cd=checkAll?getClass(cd, name):getClass(cd, name, REFERENCE);
171 throw new Error("Undefined class "+name);
172 td.setClassDescriptor(field_cd);
174 } else if (td.isTag())
180 public void checkField(ClassDescriptor cd, FieldDescriptor fd) {
181 checkTypeDescriptor(cd, fd.getType());
184 public void checkConstraintCheck(TaskDescriptor td, SymbolTable nametable, Vector ccs) {
186 return; /* No constraint checks to check */
187 for(int i=0; i<ccs.size(); i++) {
188 ConstraintCheck cc=(ConstraintCheck) ccs.get(i);
190 for(int j=0; j<cc.numArgs(); j++) {
191 ExpressionNode en=cc.getArg(j);
192 checkExpressionNode(td,nametable,en,null);
197 public void checkFlagEffects(TaskDescriptor td, Vector vfe, SymbolTable nametable) {
199 return; /* No flag effects to check */
200 for(int i=0; i<vfe.size(); i++) {
201 FlagEffects fe=(FlagEffects) vfe.get(i);
202 String varname=fe.getName();
203 //Make sure the variable is declared as a parameter to the task
204 VarDescriptor vd=(VarDescriptor)td.getParameterTable().get(varname);
206 throw new Error("Parameter "+varname+" in Flag Effects not declared in "+td);
209 //Make sure it correspods to a class
210 TypeDescriptor type_d=vd.getType();
211 if (!type_d.isClass())
212 throw new Error("Cannot have non-object argument for flag_effect");
214 ClassDescriptor cd=type_d.getClassDesc();
215 for(int j=0; j<fe.numEffects(); j++) {
216 FlagEffect flag=fe.getEffect(j);
217 String name=flag.getName();
218 FlagDescriptor flag_d=(FlagDescriptor)cd.getFlagTable().get(name);
219 //Make sure the flag is declared
221 throw new Error("Flag descriptor "+name+" undefined in class: "+cd.getSymbol());
222 if (flag_d.getExternal())
223 throw new Error("Attempting to modify external flag: "+name);
224 flag.setFlag(flag_d);
226 for(int j=0; j<fe.numTagEffects(); j++) {
227 TagEffect tag=fe.getTagEffect(j);
228 String name=tag.getName();
230 Descriptor d=(Descriptor)nametable.get(name);
232 throw new Error("Tag descriptor "+name+" undeclared");
233 else if (!(d instanceof TagVarDescriptor))
234 throw new Error(name+" is not a tag descriptor");
235 tag.setTag((TagVarDescriptor)d);
240 public void checkTask(TaskDescriptor td) {
241 for(int i=0; i<td.numParameters(); i++) {
242 /* Check that parameter is well typed */
243 TypeDescriptor param_type=td.getParamType(i);
244 checkTypeDescriptor(null, param_type);
246 /* Check the parameter's flag expression is well formed */
247 FlagExpressionNode fen=td.getFlag(td.getParameter(i));
248 if (!param_type.isClass())
249 throw new Error("Cannot have non-object argument to a task");
250 ClassDescriptor cd=param_type.getClassDesc();
252 checkFlagExpressionNode(cd, fen);
255 checkFlagEffects(td, td.getFlagEffects(),td.getParameterTable());
256 /* Check that the task code is valid */
257 BlockNode bn=state.getMethodBody(td);
258 checkBlockNode(td, td.getParameterTable(),bn);
261 public void checkFlagExpressionNode(ClassDescriptor cd, FlagExpressionNode fen) {
263 case Kind.FlagOpNode:
265 FlagOpNode fon=(FlagOpNode)fen;
266 checkFlagExpressionNode(cd, fon.getLeft());
267 if (fon.getRight()!=null)
268 checkFlagExpressionNode(cd, fon.getRight());
274 FlagNode fn=(FlagNode)fen;
275 String name=fn.getFlagName();
276 FlagDescriptor fd=(FlagDescriptor)cd.getFlagTable().get(name);
278 throw new Error("Undeclared flag: "+name);
284 throw new Error("Unrecognized FlagExpressionNode");
288 public void checkMethod(ClassDescriptor cd, MethodDescriptor md) {
289 /* Check for abstract methods */
290 if(md.isAbstract()) {
291 if(!cd.isAbstract() && !cd.isInterface()) {
292 throw new Error("Error! The non-abstract Class " + cd.getSymbol() + " contains an abstract method " + md.getSymbol());
296 /* Check return type */
297 if (!md.isConstructor() && !md.isStaticBlock())
298 if (!md.getReturnType().isVoid()) {
299 checkTypeDescriptor(cd, md.getReturnType());
301 for(int i=0; i<md.numParameters(); i++) {
302 TypeDescriptor param_type=md.getParamType(i);
303 checkTypeDescriptor(cd, param_type);
305 /* Link the naming environments */
306 if (!md.isStatic()) /* Fields aren't accessible directly in a static method, so don't link in this table */
307 md.getParameterTable().setParent(cd.getFieldTable());
309 if (!md.isStatic()) {
310 VarDescriptor thisvd=new VarDescriptor(new TypeDescriptor(cd),"this");
313 if(md.isDefaultConstructor() && (cd.getSuperDesc() != null)) {
314 // add the construction of it super class, can only be super()
315 NameDescriptor nd=new NameDescriptor("super");
316 MethodInvokeNode min=new MethodInvokeNode(nd);
317 BlockExpressionNode ben=new BlockExpressionNode(min);
318 BlockNode bn = state.getMethodBody(md);
319 bn.addFirstBlockStatement(ben);
323 public void checkMethodBody(ClassDescriptor cd, MethodDescriptor md) {
324 ClassDescriptor superdesc=cd.getSuperDesc();
325 if (superdesc!=null) {
326 Set possiblematches=superdesc.getMethodTable().getSet(md.getSymbol());
327 for(Iterator methodit=possiblematches.iterator(); methodit.hasNext(); ) {
328 MethodDescriptor matchmd=(MethodDescriptor)methodit.next();
329 if (md.matches(matchmd)) {
330 if (matchmd.getModifiers().isFinal()) {
331 throw new Error("Try to override final method in method:"+md+" declared in "+cd);
336 BlockNode bn=state.getMethodBody(md);
337 checkBlockNode(md, md.getParameterTable(),bn);
340 public void checkBlockNode(Descriptor md, SymbolTable nametable, BlockNode bn) {
341 /* Link in the naming environment */
342 bn.getVarTable().setParent(nametable);
343 for(int i=0; i<bn.size(); i++) {
344 BlockStatementNode bsn=bn.get(i);
345 checkBlockStatementNode(md, bn.getVarTable(),bsn);
349 public void checkBlockStatementNode(Descriptor md, SymbolTable nametable, BlockStatementNode bsn) {
351 case Kind.BlockExpressionNode:
352 checkBlockExpressionNode(md, nametable,(BlockExpressionNode)bsn);
355 case Kind.DeclarationNode:
356 checkDeclarationNode(md, nametable, (DeclarationNode)bsn);
359 case Kind.TagDeclarationNode:
360 checkTagDeclarationNode(md, nametable, (TagDeclarationNode)bsn);
363 case Kind.IfStatementNode:
364 checkIfStatementNode(md, nametable, (IfStatementNode)bsn);
367 case Kind.SwitchStatementNode:
368 checkSwitchStatementNode(md, nametable, (SwitchStatementNode)bsn);
372 checkLoopNode(md, nametable, (LoopNode)bsn);
375 case Kind.ReturnNode:
376 checkReturnNode(md, nametable, (ReturnNode)bsn);
379 case Kind.TaskExitNode:
380 checkTaskExitNode(md, nametable, (TaskExitNode)bsn);
383 case Kind.SubBlockNode:
384 checkSubBlockNode(md, nametable, (SubBlockNode)bsn);
387 case Kind.AtomicNode:
388 checkAtomicNode(md, nametable, (AtomicNode)bsn);
391 case Kind.SynchronizedNode:
392 checkSynchronizedNode(md, nametable, (SynchronizedNode)bsn);
395 case Kind.ContinueBreakNode:
396 checkContinueBreakNode(md, nametable, (ContinueBreakNode) bsn);
400 case Kind.GenReachNode:
401 case Kind.GenDefReachNode:
402 // do nothing, no semantic check for SESEs
409 void checkBlockExpressionNode(Descriptor md, SymbolTable nametable, BlockExpressionNode ben) {
410 checkExpressionNode(md, nametable, ben.getExpression(), null);
413 void checkDeclarationNode(Descriptor md, SymbolTable nametable, DeclarationNode dn) {
414 VarDescriptor vd=dn.getVarDescriptor();
415 checkTypeDescriptor(((md instanceof MethodDescriptor)?((MethodDescriptor)md).getClassDesc():null), vd.getType());
416 Descriptor d=nametable.get(vd.getSymbol());
418 (d instanceof FieldDescriptor)) {
421 throw new Error(vd.getSymbol()+" in "+md+" defined a second time");
422 if (dn.getExpression()!=null)
423 checkExpressionNode(md, nametable, dn.getExpression(), vd.getType());
426 void checkTagDeclarationNode(Descriptor md, SymbolTable nametable, TagDeclarationNode dn) {
427 TagVarDescriptor vd=dn.getTagVarDescriptor();
428 Descriptor d=nametable.get(vd.getSymbol());
430 (d instanceof FieldDescriptor)) {
433 throw new Error(vd.getSymbol()+" defined a second time");
436 void checkSubBlockNode(Descriptor md, SymbolTable nametable, SubBlockNode sbn) {
437 checkBlockNode(md, nametable, sbn.getBlockNode());
440 void checkAtomicNode(Descriptor md, SymbolTable nametable, AtomicNode sbn) {
441 checkBlockNode(md, nametable, sbn.getBlockNode());
444 void checkSynchronizedNode(Descriptor md, SymbolTable nametable, SynchronizedNode sbn) {
445 checkBlockNode(md, nametable, sbn.getBlockNode());
446 //todo this could be Object
447 checkExpressionNode(md, nametable, sbn.getExpr(), null);
450 void checkContinueBreakNode(Descriptor md, SymbolTable nametable, ContinueBreakNode cbn) {
451 if (loopstack.empty())
452 throw new Error("continue/break outside of loop");
453 Object o = loopstack.peek();
454 if(o instanceof LoopNode) {
455 LoopNode ln=(LoopNode)o;
460 void checkReturnNode(Descriptor d, SymbolTable nametable, ReturnNode rn) {
461 if (d instanceof TaskDescriptor)
462 throw new Error("Illegal return appears in Task: "+d.getSymbol());
463 MethodDescriptor md=(MethodDescriptor)d;
464 if (rn.getReturnExpression()!=null)
465 if (md.getReturnType()==null)
466 throw new Error("Constructor can't return something.");
467 else if (md.getReturnType().isVoid())
468 throw new Error(md+" is void");
470 checkExpressionNode(md, nametable, rn.getReturnExpression(), md.getReturnType());
472 if (md.getReturnType()!=null&&!md.getReturnType().isVoid())
473 throw new Error("Need to return something for "+md);
476 void checkTaskExitNode(Descriptor md, SymbolTable nametable, TaskExitNode ten) {
477 if (md instanceof MethodDescriptor)
478 throw new Error("Illegal taskexit appears in Method: "+md.getSymbol());
479 checkFlagEffects((TaskDescriptor)md, ten.getFlagEffects(),nametable);
480 checkConstraintCheck((TaskDescriptor) md, nametable, ten.getChecks());
483 void checkIfStatementNode(Descriptor md, SymbolTable nametable, IfStatementNode isn) {
484 checkExpressionNode(md, nametable, isn.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
485 checkBlockNode(md, nametable, isn.getTrueBlock());
486 if (isn.getFalseBlock()!=null)
487 checkBlockNode(md, nametable, isn.getFalseBlock());
490 void checkSwitchStatementNode(Descriptor md, SymbolTable nametable, SwitchStatementNode ssn) {
491 checkExpressionNode(md, nametable, ssn.getCondition(), new TypeDescriptor(TypeDescriptor.INT));
493 BlockNode sbn = ssn.getSwitchBody();
494 boolean hasdefault = false;
495 for(int i = 0; i < sbn.size(); i++) {
496 boolean containdefault = checkSwitchBlockNode(md, nametable, (SwitchBlockNode)sbn.get(i));
497 if(hasdefault && containdefault) {
498 throw new Error("Error: duplicate default branch in switch-case statement in Method: " + md.getSymbol());
500 hasdefault = containdefault;
504 boolean checkSwitchBlockNode(Descriptor md, SymbolTable nametable, SwitchBlockNode sbn) {
505 Vector<SwitchLabelNode> slnv = sbn.getSwitchConditions();
507 for(int i = 0; i < slnv.size(); i++) {
508 if(slnv.elementAt(i).isdefault) {
511 checkConstantExpressionNode(md, nametable, slnv.elementAt(i).getCondition(), new TypeDescriptor(TypeDescriptor.INT));
515 throw new Error("Error: duplicate default branch in switch-case statement in Method: " + md.getSymbol());
518 checkBlockNode(md, nametable, sbn.getSwitchBlockStatement());
520 return (defaultb > 0);
524 void checkConstantExpressionNode(Descriptor md, SymbolTable nametable, ExpressionNode en, TypeDescriptor td) {
526 case Kind.FieldAccessNode:
527 checkFieldAccessNode(md,nametable,(FieldAccessNode)en,td);
530 case Kind.LiteralNode:
531 checkLiteralNode(md,nametable,(LiteralNode)en,td);
535 checkNameNode(md,nametable,(NameNode)en,td);
539 checkOpNode(md, nametable, (OpNode)en, td);
545 void checkExpressionNode(Descriptor md, SymbolTable nametable, ExpressionNode en, TypeDescriptor td) {
547 case Kind.AssignmentNode:
548 checkAssignmentNode(md,nametable,(AssignmentNode)en,td);
552 checkCastNode(md,nametable,(CastNode)en,td);
555 case Kind.CreateObjectNode:
556 checkCreateObjectNode(md,nametable,(CreateObjectNode)en,td);
559 case Kind.FieldAccessNode:
560 checkFieldAccessNode(md,nametable,(FieldAccessNode)en,td);
563 case Kind.ArrayAccessNode:
564 checkArrayAccessNode(md,nametable,(ArrayAccessNode)en,td);
567 case Kind.LiteralNode:
568 checkLiteralNode(md,nametable,(LiteralNode)en,td);
571 case Kind.MethodInvokeNode:
572 checkMethodInvokeNode(md,nametable,(MethodInvokeNode)en,td);
576 checkNameNode(md,nametable,(NameNode)en,td);
580 checkOpNode(md,nametable,(OpNode)en,td);
583 case Kind.OffsetNode:
584 checkOffsetNode(md, nametable, (OffsetNode)en, td);
587 case Kind.TertiaryNode:
588 checkTertiaryNode(md, nametable, (TertiaryNode)en, td);
591 case Kind.InstanceOfNode:
592 checkInstanceOfNode(md, nametable, (InstanceOfNode) en, td);
595 case Kind.ArrayInitializerNode:
596 checkArrayInitializerNode(md, nametable, (ArrayInitializerNode) en, td);
599 case Kind.ClassTypeNode:
600 checkClassTypeNode(md, nametable, (ClassTypeNode) en, td);
606 void checkClassTypeNode(Descriptor md, SymbolTable nametable, ClassTypeNode tn, TypeDescriptor td) {
607 checkTypeDescriptor(((md instanceof MethodDescriptor)?((MethodDescriptor)md).getClassDesc():null), tn.getType());
610 void checkCastNode(Descriptor md, SymbolTable nametable, CastNode cn, TypeDescriptor td) {
611 /* Get type descriptor */
612 if (cn.getType()==null) {
613 NameDescriptor typenamed=cn.getTypeName().getName();
614 TypeDescriptor ntd=new TypeDescriptor(getClass(((md instanceof MethodDescriptor)?((MethodDescriptor)md).getClassDesc():null), typenamed.toString()));
618 /* Check the type descriptor */
619 TypeDescriptor cast_type=cn.getType();
620 checkTypeDescriptor(((md instanceof MethodDescriptor)?((MethodDescriptor)md).getClassDesc():null),cast_type);
624 if (!typeutil.isSuperorType(td,cast_type))
625 throw new Error("Cast node returns "+cast_type+", but need "+td);
628 ExpressionNode en=cn.getExpression();
629 checkExpressionNode(md, nametable, en, null);
630 TypeDescriptor etd=en.getType();
631 if (typeutil.isSuperorType(cast_type,etd)) /* Cast trivially succeeds */
634 if (typeutil.isSuperorType(etd,cast_type)) /* Cast may succeed */
636 if (typeutil.isCastable(etd, cast_type))
639 //rough hack to handle interfaces...should clean up
640 if (etd.isClass()&&cast_type.isClass()) {
641 ClassDescriptor cdetd=etd.getClassDesc();
642 ClassDescriptor cdcast_type=cast_type.getClassDesc();
644 if (cdetd.isInterface()&&!cdcast_type.getModifier().isFinal())
647 if (cdcast_type.isInterface()&&!cdetd.getModifier().isFinal())
650 /* Different branches */
651 /* TODO: change if add interfaces */
652 throw new Error("Cast will always fail\n"+cn.printNode(0));
655 //FieldDescriptor checkFieldAccessNodeForParentNode( Descriptor md, SymbolTable na )
656 void checkFieldAccessNode(Descriptor md, SymbolTable nametable, FieldAccessNode fan, TypeDescriptor td) {
657 ExpressionNode left=fan.getExpression();
658 checkExpressionNode(md,nametable,left,null);
659 TypeDescriptor ltd=left.getType();
661 checkClass(ltd.getClassDesc(), INIT);
662 String fieldname=fan.getFieldName();
664 FieldDescriptor fd=null;
665 if (ltd.isArray()&&fieldname.equals("length"))
666 fd=FieldDescriptor.arrayLength;
668 fd=(FieldDescriptor) ltd.getClassDesc().getFieldTable().get(fieldname);
670 if(ltd.isClassNameRef()) {
671 // the field access is using a class name directly
673 // check if it is to access a surrounding class in an inner class
674 if(fieldname.equals("this")) {
675 ClassDescriptor icd = ((VarDescriptor)nametable.get("this")).getType().getClassDesc();
676 if(icd.isInnerClass()) {
677 NameNode nn = new NameNode(new NameDescriptor("this"));
678 nn.setVar((VarDescriptor)nametable.get("this"));
679 fan.setExpression(nn);
680 if(icd.getSurroundingDesc()==ltd.getClassDesc()) {
681 // this is a surrounding class access inside an inner class
682 fan.setExpression(nn);
683 fan.setFieldName("this$0");
684 fd = (FieldDescriptor)icd.getFieldTable().get("this$0");
685 } else if(icd==ltd.getClassDesc()) {
686 // this is an inner class this operation
687 fd = new FieldDescriptor(new Modifiers(),new TypeDescriptor(icd),"this",null,false);
693 ClassDescriptor surroundingCls=ltd.getClassDesc().getSurroundingDesc();
695 while(surroundingCls!=null) {
696 fd=(FieldDescriptor) surroundingCls.getFieldTable().get(fieldname);
698 fan.left=new ClassTypeNode(new TypeDescriptor(surroundingCls));
701 surroundingCls=surroundingCls.getSurroundingDesc();
705 if(ltd.getClassDesc().isEnum()) {
706 int value = ltd.getClassDesc().getEnumConstant(fieldname);
708 // check if this field is an enum constant
709 throw new Error(fieldname + " is not an enum constant in "+fan.printNode(0)+" in "+md);
711 fd = new FieldDescriptor(new Modifiers(Modifiers.PUBLIC|Modifiers.FINAL), new TypeDescriptor(TypeDescriptor.INT), fieldname, null, false);
713 fd.setEnumValue(value);
714 } else if(fd == null) {
715 throw new Error("Could not find field "+ fieldname + " in "+fan.printNode(0)+" in "+md + " (Line: "+fan.getNumLine()+")");
716 } else if(fd.isStatic()) {
717 // check if this field is a static field
718 if(fd.getExpressionNode() != null) {
719 checkExpressionNode(md,nametable,fd.getExpressionNode(),null);
722 throw new Error("Dereference of the non-static field "+ fieldname + " in "+fan.printNode(0)+" in "+md);
727 if((md instanceof MethodDescriptor) && false == ((MethodDescriptor)md).isStaticBlock()) {
728 ClassDescriptor cd = ((MethodDescriptor)md).getClassDesc();
729 FieldAccessNode theFieldNode = fieldAccessExpression( cd, fieldname, fan.getNumLine() );
730 if( null != theFieldNode ) {
731 //fan = theFieldNode;
732 checkFieldAccessNode( md, nametable, theFieldNode, td );
733 fan.setField( theFieldNode.getField() );
734 fan.setExpression( theFieldNode.getExpression() );
735 //TypeDescriptor td1 = fan.getType();
740 throw new Error("Unknown field "+fieldname + " in "+fan.printNode(0)+" in "+md);
743 ClassDescriptor surroundingCls=ltd.getClassDesc().getSurroundingDesc();
745 while(surroundingCls!=null) {
746 fd=(FieldDescriptor)surroundingCls.getFieldTable().get(fieldname);
748 surroundingCls=ltd.getClassDesc().getSurroundingDesc();
749 FieldAccessNode ftmp=fan;
750 for(;numencloses>0;numencloses--) {
751 FieldAccessNode fnew=new FieldAccessNode(ftmp.left, "this___enclosing");
752 fnew.setField((FieldDescriptor)surroundingCls.getFieldTable().get("this___enclosing"));
755 surroundingCls=surroundingCls.getSurroundingDesc();
759 surroundingCls=surroundingCls.getSurroundingDesc();
764 throw new Error("Unknown field "+fieldname + " in "+fan.printNode(0)+" in "+md);
768 if (fd.getType().iswrapper()) {
769 FieldAccessNode fan2=new FieldAccessNode(left, fieldname);
770 fan2.setNumLine(left.getNumLine());
773 fan.fieldname="value";
775 ExpressionNode leftwr=fan.getExpression();
776 TypeDescriptor ltdwr=leftwr.getType();
777 String fieldnamewr=fan.getFieldName();
778 FieldDescriptor fdwr=(FieldDescriptor) ltdwr.getClassDesc().getFieldTable().get(fieldnamewr);
781 throw new Error("Unknown field "+fieldnamewr + " in "+fan.printNode(0)+" in "+md);
786 if (!typeutil.isSuperorType(td,fan.getType()))
787 throw new Error("Field node returns "+fan.getType()+", but need "+td);
791 void checkArrayAccessNode(Descriptor md, SymbolTable nametable, ArrayAccessNode aan, TypeDescriptor td) {
792 ExpressionNode left=aan.getExpression();
793 checkExpressionNode(md,nametable,left,null);
795 checkExpressionNode(md,nametable,aan.getIndex(),new TypeDescriptor(TypeDescriptor.INT));
796 TypeDescriptor ltd=left.getType();
797 if (ltd.dereference().iswrapper()) {
798 aan.wrappertype=((FieldDescriptor)ltd.dereference().getClassDesc().getFieldTable().get("value")).getType();
802 if (!typeutil.isSuperorType(td,aan.getType()))
803 throw new Error("Field node returns "+aan.getType()+", but need "+td);
806 void checkLiteralNode(Descriptor md, SymbolTable nametable, LiteralNode ln, TypeDescriptor td) {
807 /* Resolve the type */
808 Object o=ln.getValue();
809 if (ln.getTypeString().equals("null")) {
810 ln.setType(new TypeDescriptor(TypeDescriptor.NULL));
811 } else if (o instanceof Integer) {
812 ln.setType(new TypeDescriptor(TypeDescriptor.INT));
813 } else if (o instanceof Long) {
814 ln.setType(new TypeDescriptor(TypeDescriptor.LONG));
815 } else if (o instanceof Float) {
816 ln.setType(new TypeDescriptor(TypeDescriptor.FLOAT));
817 } else if (o instanceof Boolean) {
818 ln.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
819 } else if (o instanceof Double) {
820 ln.setType(new TypeDescriptor(TypeDescriptor.DOUBLE));
821 } else if (o instanceof Character) {
822 ln.setType(new TypeDescriptor(TypeDescriptor.CHAR));
823 } else if (o instanceof String) {
824 ln.setType(new TypeDescriptor(getClass(null, TypeUtil.StringClass)));
828 if (!typeutil.isSuperorType(td,ln.getType())) {
829 Long l = ln.evaluate();
830 if((ln.getType().isByte() || ln.getType().isShort()
831 || ln.getType().isChar() || ln.getType().isInt())
833 && (td.isByte() || td.isShort() || td.isChar()
834 || td.isInt() || td.isLong())) {
835 long lnvalue = l.longValue();
836 if((td.isByte() && ((lnvalue > 127) || (lnvalue < -128)))
837 || (td.isShort() && ((lnvalue > 32767) || (lnvalue < -32768)))
838 || (td.isChar() && ((lnvalue > 65535) || (lnvalue < 0)))
839 || (td.isInt() && ((lnvalue > 2147483647) || (lnvalue < -2147483648)))
840 || (td.isLong() && ((lnvalue > 9223372036854775807L) || (lnvalue < -9223372036854775808L)))) {
841 throw new Error("Field node returns "+ln.getType()+", but need "+td+" in "+md);
844 throw new Error("Field node returns "+ln.getType()+", but need "+td+" in "+md);
849 FieldDescriptor recurseSurroundingClasses( ClassDescriptor icd, String varname ) {
850 if( null == icd || false == icd.isInnerClass() )
853 ClassDescriptor surroundingDesc = icd.getSurroundingDesc();
854 if( null == surroundingDesc )
857 SymbolTable fieldTable = surroundingDesc.getFieldTable();
858 FieldDescriptor fd = ( FieldDescriptor ) fieldTable.get( varname );
861 return recurseSurroundingClasses( surroundingDesc, varname );
864 FieldAccessNode fieldAccessExpression( ClassDescriptor icd, String varname, int linenum ) {
865 FieldDescriptor fd = recurseSurroundingClasses( icd, varname );
869 ClassDescriptor cd = fd.getClassDescriptor();
871 int startingDepth = icd.getInnerDepth();
873 if( true == cd.isInnerClass() )
874 depth = cd.getInnerDepth();
876 String composed = "this";
877 NameDescriptor runningDesc = new NameDescriptor( "this" );;
879 for ( int index = startingDepth; index > depth; --index ) {
880 composed = "this$" + String.valueOf( index - 1 );
881 runningDesc = new NameDescriptor( runningDesc, composed );
883 if( false == cd.isInnerClass() )
884 runningDesc = new NameDescriptor( runningDesc, "this$" + String.valueOf(0) ); //all the way up.
885 NameDescriptor idDesc = new NameDescriptor( runningDesc, varname );
888 FieldAccessNode theFieldNode = ( FieldAccessNode )translateNameDescriptorintoExpression( idDesc, linenum );
892 void checkNameNode(Descriptor md, SymbolTable nametable, NameNode nn, TypeDescriptor td) {
893 NameDescriptor nd=nn.getName();
894 if (nd.getBase()!=null) {
896 /* Rewrite NameNode */
897 ExpressionNode en=translateNameDescriptorintoExpression(nd,nn.getNumLine());
898 nn.setExpression(en);
899 checkExpressionNode(md,nametable,en,td);
901 String varname=nd.toString();
902 if(varname.equals("this")) {
904 nn.setVar((VarDescriptor)nametable.get("this"));
907 Descriptor d=(Descriptor)nametable.get(varname);
909 ClassDescriptor cd = null;
910 //check the inner class case first.
911 if((md instanceof MethodDescriptor) && false == ((MethodDescriptor)md).isStaticBlock()) {
912 cd = ((MethodDescriptor)md).getClassDesc();
913 FieldAccessNode theFieldNode = fieldAccessExpression( cd, varname, nn.getNumLine() );
914 if( null != theFieldNode ) {
915 nn.setExpression(( ExpressionNode )theFieldNode);
916 checkExpressionNode(md,nametable,( ExpressionNode )theFieldNode,td);
920 if((md instanceof MethodDescriptor) && ((MethodDescriptor)md).isStaticBlock()) {
921 // this is a static block, all the accessed fields should be static field
922 cd = ((MethodDescriptor)md).getClassDesc();
923 SymbolTable fieldtbl = cd.getFieldTable();
924 FieldDescriptor fd=(FieldDescriptor)fieldtbl.get(varname);
925 if((fd == null) || (!fd.isStatic())) {
926 // no such field in the class, check if this is a class
927 if(varname.equals("this")) {
928 throw new Error("Error: access this obj in a static block");
930 cd=getClass(((md instanceof MethodDescriptor)?((MethodDescriptor)md).getClassDesc():null), varname);
932 // this is a class name
936 throw new Error("Name "+varname+" should not be used in static block: "+md);
939 // this is a static field
945 // check if the var is a static field of the class
946 if(md instanceof MethodDescriptor) {
947 cd = ((MethodDescriptor)md).getClassDesc();
948 FieldDescriptor fd = (FieldDescriptor)cd.getFieldTable().get(varname);
949 if((fd != null) && (fd.isStatic())) {
953 if (!typeutil.isSuperorType(td,nn.getType()))
954 throw new Error("Field node returns "+nn.getType()+", but need "+td);
956 } else if(fd != null) {
957 throw new Error("Name "+varname+" should not be used in " + md);
960 cd=getClass(((md instanceof MethodDescriptor)?((MethodDescriptor)md).getClassDesc():null), varname);
962 // this is a class name
966 throw new Error("Name "+varname+" undefined in: "+md);
970 if (d instanceof VarDescriptor) {
972 } else if (d instanceof FieldDescriptor) {
973 FieldDescriptor fd=(FieldDescriptor)d;
974 if (fd.getType().iswrapper()) {
975 String id=nd.getIdentifier();
976 NameDescriptor base=nd.getBase();
977 NameNode n=new NameNode(nn.getName());
978 n.setNumLine(nn.getNumLine());
980 n.setVar((VarDescriptor)nametable.get("this")); /* Need a pointer to this */
981 FieldAccessNode fan=new FieldAccessNode(n,"value");
982 fan.setNumLine(n.getNumLine());
983 FieldDescriptor fdval=(FieldDescriptor) fd.getType().getClassDesc().getFieldTable().get("value");
985 nn.setExpression(fan);
988 nn.setVar((VarDescriptor)nametable.get("this")); /* Need a pointer to this */
990 } else if (d instanceof TagVarDescriptor) {
992 } else throw new Error("Wrong type of descriptor");
994 if (!typeutil.isSuperorType(td,nn.getType()))
995 throw new Error("Field node returns "+nn.getType()+", but need "+td);
999 void checkOffsetNode(Descriptor md, SymbolTable nameTable, OffsetNode ofn, TypeDescriptor td) {
1000 TypeDescriptor ltd=ofn.td;
1001 checkTypeDescriptor(((md instanceof MethodDescriptor)?((MethodDescriptor)md).getClassDesc():null),ltd);
1003 String fieldname = ofn.fieldname;
1004 FieldDescriptor fd=null;
1005 if (ltd.isArray()&&fieldname.equals("length")) {
1006 fd=FieldDescriptor.arrayLength;
1008 fd=(FieldDescriptor) ltd.getClassDesc().getFieldTable().get(fieldname);
1012 checkField(ltd.getClassDesc(), fd);
1015 throw new Error("Unknown field "+fieldname + " in "+ofn.printNode(1)+" in "+md);
1018 if (!typeutil.isSuperorType(td, ofn.getType())) {
1019 throw new Error("Type of rside not compatible with type of lside"+ofn.printNode(0));
1025 void checkTertiaryNode(Descriptor md, SymbolTable nametable, TertiaryNode tn, TypeDescriptor td) {
1026 checkExpressionNode(md, nametable, tn.getCond(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
1027 checkExpressionNode(md, nametable, tn.getTrueExpr(), td);
1028 checkExpressionNode(md, nametable, tn.getFalseExpr(), td);
1031 void checkInstanceOfNode(Descriptor md, SymbolTable nametable, InstanceOfNode tn, TypeDescriptor td) {
1032 if (td!=null&&!td.isBoolean())
1033 throw new Error("Expecting type "+td+"for instanceof expression");
1035 checkTypeDescriptor(((md instanceof MethodDescriptor)?((MethodDescriptor)md).getClassDesc():null), tn.getExprType());
1036 checkExpressionNode(md, nametable, tn.getExpr(), null);
1039 void checkArrayInitializerNode(Descriptor md, SymbolTable nametable, ArrayInitializerNode ain, TypeDescriptor td) {
1040 Vector<TypeDescriptor> vec_type = new Vector<TypeDescriptor>();
1041 for( int i = 0; i < ain.numVarInitializers(); ++i ) {
1042 checkExpressionNode(md, nametable, ain.getVarInitializer(i), td.dereference());
1043 vec_type.add(ain.getVarInitializer(i).getType());
1051 void checkAssignmentNode(Descriptor md, SymbolTable nametable, AssignmentNode an, TypeDescriptor td) {
1052 boolean postinc=true;
1053 if (an.getOperation().getBaseOp()==null||
1054 (an.getOperation().getBaseOp().getOp()!=Operation.POSTINC&&
1055 an.getOperation().getBaseOp().getOp()!=Operation.POSTDEC))
1058 checkExpressionNode(md, nametable, an.getSrc(),td);
1059 //TODO: Need check on validity of operation here
1060 if (!((an.getDest() instanceof FieldAccessNode)||
1061 (an.getDest() instanceof ArrayAccessNode)||
1062 (an.getDest() instanceof NameNode)))
1063 throw new Error("Bad lside in "+an.printNode(0));
1064 checkExpressionNode(md, nametable, an.getDest(), null);
1066 /* We want parameter variables to tasks to be immutable */
1067 if (md instanceof TaskDescriptor) {
1068 if (an.getDest() instanceof NameNode) {
1069 NameNode nn=(NameNode)an.getDest();
1070 if (nn.getVar()!=null) {
1071 if (((TaskDescriptor)md).getParameterTable().contains(nn.getVar().getSymbol()))
1072 throw new Error("Can't modify parameter "+nn.getVar()+ " to task "+td.getSymbol());
1077 if (an.getDest().getType().isString()&&an.getOperation().getOp()==AssignOperation.PLUSEQ) {
1079 ClassDescriptor stringcl=getClass(null, TypeUtil.StringClass);
1080 TypeDescriptor stringtd=new TypeDescriptor(stringcl);
1081 NameDescriptor nd=new NameDescriptor("String");
1082 NameDescriptor valuend=new NameDescriptor(nd, "valueOf");
1084 if (!(an.getSrc().getType().isString()&&(an.getSrc() instanceof OpNode))) {
1085 MethodInvokeNode rightmin=new MethodInvokeNode(valuend);
1086 rightmin.setNumLine(an.getSrc().getNumLine());
1087 rightmin.addArgument(an.getSrc());
1089 checkExpressionNode(md, nametable, an.getSrc(), null);
1093 if (!postinc&&!typeutil.isSuperorType(an.getDest().getType(),an.getSrc().getType())) {
1094 TypeDescriptor dt = an.getDest().getType();
1095 TypeDescriptor st = an.getSrc().getType();
1096 if(an.getSrc().kind() == Kind.ArrayInitializerNode) {
1097 if(dt.getArrayCount() != st.getArrayCount()) {
1098 throw new Error("Type of rside ("+an.getSrc().getType().toPrettyString()+") not compatible with type of lside ("+an.getDest().getType().toPrettyString()+")"+an.printNode(0));
1101 dt = dt.dereference();
1102 st = st.dereference();
1103 } while(dt.isArray());
1104 if((st.isByte() || st.isShort() || st.isChar() || st.isInt())
1105 && (dt.isByte() || dt.isShort() || dt.isChar() || dt.isInt() || dt.isLong())) {
1108 throw new Error("Type of rside ("+an.getSrc().getType().toPrettyString()+") not compatible with type of lside ("+an.getDest().getType().toPrettyString()+")"+an.printNode(0));
1112 Long l = an.getSrc().evaluate();
1113 if((st.isByte() || st.isShort() || st.isChar() || st.isInt())
1115 && (dt.isByte() || dt.isShort() || dt.isChar() || dt.isInt() || dt.isLong())) {
1116 long lnvalue = l.longValue();
1117 if((dt.isByte() && ((lnvalue > 127) || (lnvalue < -128)))
1118 || (dt.isShort() && ((lnvalue > 32767) || (lnvalue < -32768)))
1119 || (dt.isChar() && ((lnvalue > 65535) || (lnvalue < 0)))
1120 || (dt.isInt() && ((lnvalue > 2147483647) || (lnvalue < -2147483648)))
1121 || (dt.isLong() && ((lnvalue > 9223372036854775807L) || (lnvalue < -9223372036854775808L)))) {
1122 throw new Error("Type of rside ("+an.getSrc().getType().toPrettyString()+") not compatible with type of lside ("+an.getDest().getType().toPrettyString()+")"+an.printNode(0));
1125 throw new Error("Type of rside ("+an.getSrc().getType().toPrettyString()+") not compatible with type of lside ("+an.getDest().getType().toPrettyString()+")"+an.printNode(0));
1131 void checkLoopNode(Descriptor md, SymbolTable nametable, LoopNode ln) {
1133 if (ln.getType()==LoopNode.WHILELOOP||ln.getType()==LoopNode.DOWHILELOOP) {
1134 checkExpressionNode(md, nametable, ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
1135 checkBlockNode(md, nametable, ln.getBody());
1138 /* Link in the initializer naming environment */
1139 BlockNode bn=ln.getInitializer();
1140 bn.getVarTable().setParent(nametable);
1141 for(int i=0; i<bn.size(); i++) {
1142 BlockStatementNode bsn=bn.get(i);
1143 checkBlockStatementNode(md, bn.getVarTable(),bsn);
1145 //check the condition
1146 checkExpressionNode(md, bn.getVarTable(), ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
1147 checkBlockNode(md, bn.getVarTable(), ln.getBody());
1148 checkBlockNode(md, bn.getVarTable(), ln.getUpdate());
1153 void InnerClassAddParamToCtor( MethodDescriptor md, ClassDescriptor cd, SymbolTable nametable,
1154 CreateObjectNode con, TypeDescriptor td ) {
1156 TypeDescriptor cdsType = new TypeDescriptor( cd );
1157 ExpressionNode conExp = con.getSurroundingClassExpression();
1158 //System.out.println( "The surrounding class expression si " + con );
1159 if( null == conExp ) {
1160 if( md.isStatic() ) {
1161 throw new Error("trying to instantiate inner class: " + con.getType() + " in a static scope" );
1163 VarDescriptor thisVD = md.getThis();
1164 if( null == thisVD ) {
1165 throw new Error( "this pointer is not defined in a non static scope" );
1167 if( cdsType.equals( thisVD.getType() ) == false ) {
1168 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" );
1170 //make this into an expression node.
1171 NameNode nThis=new NameNode( new NameDescriptor( "this" ) );
1172 con.addArgument( nThis );
1175 //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.
1176 con.addArgument( conExp );
1178 //System.out.println( " the modified createObjectNode is " + con.printNode( 0 ) + "\n" );
1180 void checkCreateObjectNode(Descriptor md, SymbolTable nametable, CreateObjectNode con,
1181 TypeDescriptor td) {
1182 TypeDescriptor[] tdarray = new TypeDescriptor[con.numArgs()];
1183 for (int i = 0; i < con.numArgs(); i++) {
1184 ExpressionNode en = con.getArg(i);
1185 checkExpressionNode(md, nametable, en, null);
1186 tdarray[i] = en.getType();
1189 TypeDescriptor typetolookin = con.getType();
1190 checkTypeDescriptor(((md instanceof MethodDescriptor)?((MethodDescriptor)md).getClassDesc():null), typetolookin);
1192 if (td != null && !typeutil.isSuperorType(td, typetolookin))
1193 throw new Error(typetolookin + " isn't a " + td);
1195 /* Check Array Initializers */
1196 if ((con.getArrayInitializer() != null)) {
1197 checkArrayInitializerNode(md, nametable, con.getArrayInitializer(), typetolookin);
1200 /* Check flag effects */
1201 if (con.getFlagEffects() != null) {
1202 FlagEffects fe = con.getFlagEffects();
1203 ClassDescriptor cd = typetolookin.getClassDesc();
1205 for (int j = 0; j < fe.numEffects(); j++) {
1206 FlagEffect flag = fe.getEffect(j);
1207 String name = flag.getName();
1208 FlagDescriptor flag_d = (FlagDescriptor) cd.getFlagTable().get(name);
1209 // Make sure the flag is declared
1211 throw new Error("Flag descriptor " + name + " undefined in class: " + cd.getSymbol());
1212 if (flag_d.getExternal())
1213 throw new Error("Attempting to modify external flag: " + name);
1214 flag.setFlag(flag_d);
1216 for (int j = 0; j < fe.numTagEffects(); j++) {
1217 TagEffect tag = fe.getTagEffect(j);
1218 String name = tag.getName();
1220 Descriptor d = (Descriptor) nametable.get(name);
1222 throw new Error("Tag descriptor " + name + " undeclared");
1223 else if (!(d instanceof TagVarDescriptor))
1224 throw new Error(name + " is not a tag descriptor");
1225 tag.setTag((TagVarDescriptor) d);
1229 if ((!typetolookin.isClass()) && (!typetolookin.isArray()))
1230 throw new Error("Can't allocate primitive type:" + con.printNode(0));
1232 if (!typetolookin.isArray()) {
1233 // Array's don't need constructor calls
1234 ClassDescriptor classtolookin = typetolookin.getClassDesc();
1235 checkClass(classtolookin, INIT);
1236 if( classtolookin.isInnerClass() ) {
1237 InnerClassAddParamToCtor( (MethodDescriptor)md, ((MethodDescriptor)md).getClassDesc() , nametable, con, td );
1238 tdarray = new TypeDescriptor[con.numArgs()];
1239 for (int i = 0; i < con.numArgs(); i++) {
1240 ExpressionNode en = con.getArg(i);
1241 checkExpressionNode(md, nametable, en, null);
1242 tdarray[i] = en.getType();
1245 Set methoddescriptorset = classtolookin.getMethodTable().getSet(classtolookin.getSymbol());
1246 MethodDescriptor bestmd = null;
1247 NextMethod: for (Iterator methodit = methoddescriptorset.iterator(); methodit.hasNext(); ) {
1248 MethodDescriptor currmd = (MethodDescriptor) methodit.next();
1249 /* Need correct number of parameters */
1250 if (con.numArgs() != currmd.numParameters())
1252 for (int i = 0; i < con.numArgs(); i++) {
1253 if (!typeutil.isSuperorType(currmd.getParamType(i), tdarray[i]))
1254 continue NextMethod;
1256 /* Local allocations can't call global allocator */
1257 if (!con.isGlobal() && currmd.isGlobal())
1260 /* Method okay so far */
1264 if (typeutil.isMoreSpecific(currmd, bestmd)) {
1266 } else if (con.isGlobal() && match(currmd, bestmd)) {
1267 if (currmd.isGlobal() && !bestmd.isGlobal())
1269 else if (currmd.isGlobal() && bestmd.isGlobal())
1271 } else if (!typeutil.isMoreSpecific(bestmd, currmd)) {
1272 throw new Error("No method is most specific:" + bestmd + " and " + currmd);
1275 /* Is this more specific than bestmd */
1278 if (bestmd == null) {
1279 throw new Error("No method found for " + con.printNode(0) + " in " + md);
1281 con.setConstructor(bestmd);
1286 /** Check to see if md1 is the same specificity as md2.*/
1288 boolean match(MethodDescriptor md1, MethodDescriptor md2) {
1289 /* Checks if md1 is more specific than md2 */
1290 if (md1.numParameters()!=md2.numParameters())
1292 for(int i=0; i<md1.numParameters(); i++) {
1293 if (!md2.getParamType(i).equals(md1.getParamType(i)))
1296 if (!md2.getReturnType().equals(md1.getReturnType()))
1299 if (!md2.getClassDesc().equals(md1.getClassDesc()))
1307 ExpressionNode translateNameDescriptorintoExpression(NameDescriptor nd, int numLine) {
1308 String id=nd.getIdentifier();
1309 NameDescriptor base=nd.getBase();
1311 NameNode nn=new NameNode(nd);
1312 nn.setNumLine(numLine);
1315 FieldAccessNode fan=new FieldAccessNode(translateNameDescriptorintoExpression(base,numLine),id);
1316 fan.setNumLine(numLine);
1321 MethodDescriptor recurseSurroundingClassesM( ClassDescriptor icd, String varname ) {
1322 if( null == icd || false == icd.isInnerClass() )
1325 ClassDescriptor surroundingDesc = icd.getSurroundingDesc();
1326 if( null == surroundingDesc )
1329 SymbolTable methodTable = surroundingDesc.getMethodTable();
1330 MethodDescriptor md = ( MethodDescriptor ) methodTable.get( varname );
1333 return recurseSurroundingClassesM( surroundingDesc, varname );
1336 ExpressionNode methodInvocationExpression( ClassDescriptor icd, MethodDescriptor md, int linenum ) {
1337 ClassDescriptor cd = md.getClassDesc();
1339 int startingDepth = icd.getInnerDepth();
1341 if( true == cd.isInnerClass() )
1342 depth = cd.getInnerDepth();
1344 String composed = "this";
1345 NameDescriptor runningDesc = new NameDescriptor( "this" );;
1347 for ( int index = startingDepth; index > depth; --index ) {
1348 composed = "this$" + String.valueOf( index - 1 );
1349 runningDesc = new NameDescriptor( runningDesc, composed );
1351 if( false == cd.isInnerClass() )
1352 runningDesc = new NameDescriptor( runningDesc, "this$" + String.valueOf(0) ); //all the way up.
1354 return new NameNode(runningDesc);
1357 void checkMethodInvokeNode(Descriptor md, SymbolTable nametable, MethodInvokeNode min, TypeDescriptor td) {
1358 /*Typecheck subexpressions
1359 and get types for expressions*/
1361 boolean isstatic = false;
1362 if((md instanceof MethodDescriptor) && ((MethodDescriptor)md).isStatic()) {
1365 TypeDescriptor[] tdarray=new TypeDescriptor[min.numArgs()];
1366 for(int i=0; i<min.numArgs(); i++) {
1367 ExpressionNode en=min.getArg(i);
1368 checkExpressionNode(md,nametable,en,null);
1369 tdarray[i]=en.getType();
1371 if(en.getType().isClass() && en.getType().getClassDesc().isEnum()) {
1372 tdarray[i] = new TypeDescriptor(TypeDescriptor.INT);
1375 TypeDescriptor typetolookin=null;
1376 if (min.getExpression()!=null) {
1377 checkExpressionNode(md,nametable,min.getExpression(),null);
1378 typetolookin=min.getExpression().getType();
1379 } else if (min.getBaseName()!=null) {
1380 String rootname=min.getBaseName().getRoot();
1381 if (rootname.equals("super")) {
1382 ClassDescriptor supercd=((MethodDescriptor)md).getClassDesc().getSuperDesc();
1383 typetolookin=new TypeDescriptor(supercd);
1385 } else if (rootname.equals("this")) {
1387 throw new Error("use this object in static method md = "+ md.toString());
1389 ClassDescriptor cd=((MethodDescriptor)md).getClassDesc();
1390 typetolookin=new TypeDescriptor(cd);
1391 } else if (nametable.get(rootname)!=null) {
1392 //we have an expression
1393 min.setExpression(translateNameDescriptorintoExpression(min.getBaseName(),min.getNumLine()));
1394 checkExpressionNode(md, nametable, min.getExpression(), null);
1395 typetolookin=min.getExpression().getType();
1397 if(!min.getBaseName().getSymbol().equals("System.out")||state.JNI) {
1398 ExpressionNode nn = translateNameDescriptorintoExpression(min.getBaseName(),min.getNumLine());
1399 checkExpressionNode(md, nametable, nn, null);
1400 typetolookin = nn.getType();
1401 if(!((nn.kind()== Kind.NameNode) && (((NameNode)nn).getField() == null)
1402 && (((NameNode)nn).getVar() == null) && (((NameNode)nn).getExpression() == null))) {
1403 // this is not a pure class name, need to add to
1404 min.setExpression(nn);
1408 ClassDescriptor cd = getClass(null, "System");
1411 throw new Error("md = "+ md.toString()+ " "+min.getBaseName()+" undefined");
1412 typetolookin=new TypeDescriptor(cd);
1415 } else if ((md instanceof MethodDescriptor)&&min.getMethodName().equals("super")) {
1416 ClassDescriptor supercd=((MethodDescriptor)md).getClassDesc().getSuperDesc();
1417 min.methodid=supercd.getSymbol();
1418 typetolookin=new TypeDescriptor(supercd);
1419 } else if (md instanceof MethodDescriptor) {
1420 typetolookin=new TypeDescriptor(((MethodDescriptor)md).getClassDesc());
1422 /* If this a task descriptor we throw an error at this point */
1423 throw new Error("Unknown method call to "+min.getMethodName()+"in task"+md.getSymbol());
1425 if (!typetolookin.isClass())
1426 throw new Error("Error with method call to "+min.getMethodName()+" in class "+typetolookin);
1427 ClassDescriptor classtolookin=typetolookin.getClassDesc();
1428 checkClass(classtolookin, REFERENCE);
1429 Set methoddescriptorset=classtolookin.getMethodTable().getSet(min.getMethodName());
1430 MethodDescriptor bestmd=null;
1432 for(Iterator methodit=methoddescriptorset.iterator(); methodit.hasNext(); ) {
1433 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
1434 /* Need correct number of parameters */
1435 if (min.numArgs()!=currmd.numParameters())
1437 for(int i=0; i<min.numArgs(); i++) {
1438 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
1439 if(((!tdarray[i].isArray() &&(tdarray[i].isInt() || tdarray[i].isLong()))
1440 && currmd.getParamType(i).isClass() && currmd.getParamType(i).getClassDesc().getSymbol().equals("Object"))) {
1441 // primitive parameters vs object
1443 continue NextMethod;
1446 /* Method okay so far */
1450 if (typeutil.isMoreSpecific(currmd,bestmd)) {
1452 } else if (!typeutil.isMoreSpecific(bestmd, currmd))
1453 throw new Error("No method is most specific:"+bestmd+" and "+currmd);
1455 /* Is this more specific than bestmd */
1459 // if this is an inner class, need to check the method table for the surrounding class
1460 bestmd = recurseSurroundingClassesM(classtolookin, min.getMethodName());
1462 throw new Error("No method found for :"+min.printNode(0)+" in class: " + classtolookin+" in "+md);
1464 // set the correct "this" expression here
1465 ExpressionNode en=methodInvocationExpression(classtolookin, bestmd, min.getNumLine());
1466 min.setExpression(en);
1467 checkExpressionNode(md, nametable, min.getExpression(), null);
1470 min.setMethod(bestmd);
1472 if ((td!=null)&&(min.getType()!=null)&&!typeutil.isSuperorType(td, min.getType()))
1473 throw new Error(min.getType()+ " is not equal to or a subclass of "+td);
1474 /* Check whether we need to set this parameter to implied this */
1475 if (!isstatic && !bestmd.isStatic()) {
1476 if (min.getExpression()==null) {
1477 ExpressionNode en=new NameNode(new NameDescriptor("this"));
1478 min.setExpression(en);
1479 checkExpressionNode(md, nametable, min.getExpression(), null);
1483 /* Check if we need to wrap primitive paratmeters to objects */
1484 for(int i=0; i<min.numArgs(); i++) {
1485 if(!tdarray[i].isArray() && (tdarray[i].isInt() || tdarray[i].isLong())
1486 && min.getMethod().getParamType(i).isClass() && min.getMethod().getParamType(i).getClassDesc().getSymbol().equals("Object")) {
1487 // Shall wrap this primitive parameter as a object
1488 ExpressionNode exp = min.getArg(i);
1489 TypeDescriptor ptd = null;
1490 NameDescriptor nd=null;
1491 if(exp.getType().isInt()) {
1492 nd = new NameDescriptor("Integer");
1493 ptd = state.getTypeDescriptor(nd);
1494 } else if(exp.getType().isLong()) {
1495 nd = new NameDescriptor("Long");
1496 ptd = state.getTypeDescriptor(nd);
1498 boolean isglobal = false;
1499 String disjointId = null;
1500 CreateObjectNode con=new CreateObjectNode(ptd, isglobal, disjointId);
1501 con.addArgument(exp);
1502 checkExpressionNode(md, nametable, con, null);
1503 min.setArgument(con, i);
1509 void checkOpNode(Descriptor md, SymbolTable nametable, OpNode on, TypeDescriptor td) {
1510 checkExpressionNode(md, nametable, on.getLeft(), null);
1511 if (on.getRight()!=null)
1512 checkExpressionNode(md, nametable, on.getRight(), null);
1513 TypeDescriptor ltd=on.getLeft().getType();
1514 TypeDescriptor rtd=on.getRight()!=null?on.getRight().getType():null;
1515 TypeDescriptor lefttype=null;
1516 TypeDescriptor righttype=null;
1517 Operation op=on.getOp();
1519 switch(op.getOp()) {
1520 case Operation.LOGIC_OR:
1521 case Operation.LOGIC_AND:
1522 if (!(rtd.isBoolean()))
1524 on.setRightType(rtd);
1526 case Operation.LOGIC_NOT:
1527 if (!(ltd.isBoolean()))
1530 on.setLeftType(ltd);
1532 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1535 case Operation.COMP:
1536 // 5.6.2 Binary Numeric Promotion
1537 //TODO unboxing of reference objects
1540 else if (ltd.isFloat())
1542 else if (ltd.isLong())
1543 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1545 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1546 on.setLeftType(lefttype);
1547 on.setType(lefttype);
1550 case Operation.BIT_OR:
1551 case Operation.BIT_XOR:
1552 case Operation.BIT_AND:
1553 // 5.6.2 Binary Numeric Promotion
1554 //TODO unboxing of reference objects
1555 if (ltd.isDouble()||rtd.isDouble())
1557 else if (ltd.isFloat()||rtd.isFloat())
1559 else if (ltd.isLong()||rtd.isLong())
1560 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1561 // 090205 hack for boolean
1562 else if (ltd.isBoolean()||rtd.isBoolean())
1563 lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
1565 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1568 on.setLeftType(lefttype);
1569 on.setRightType(righttype);
1570 on.setType(lefttype);
1573 case Operation.ISAVAILABLE:
1574 if (!(ltd.isPtr())) {
1575 throw new Error("Can't use isavailable on non-pointers/non-parameters.");
1578 on.setLeftType(lefttype);
1579 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1582 case Operation.EQUAL:
1583 case Operation.NOTEQUAL:
1584 // 5.6.2 Binary Numeric Promotion
1585 //TODO unboxing of reference objects
1586 if (ltd.isBoolean()||rtd.isBoolean()) {
1587 if (!(ltd.isBoolean()&&rtd.isBoolean()))
1589 righttype=lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
1590 } else if (ltd.isPtr()||rtd.isPtr()) {
1591 if (!(ltd.isPtr()&&rtd.isPtr())) {
1598 } else if (ltd.isDouble()||rtd.isDouble())
1599 righttype=lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
1600 else if (ltd.isFloat()||rtd.isFloat())
1601 righttype=lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
1602 else if (ltd.isLong()||rtd.isLong())
1603 righttype=lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1605 righttype=lefttype=new TypeDescriptor(TypeDescriptor.INT);
1607 on.setLeftType(lefttype);
1608 on.setRightType(righttype);
1609 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1618 // 5.6.2 Binary Numeric Promotion
1619 //TODO unboxing of reference objects
1620 if (!ltd.isNumber()||!rtd.isNumber()) {
1621 if (!ltd.isNumber())
1622 throw new Error("Leftside is not number"+on.printNode(0)+"type="+ltd.toPrettyString());
1623 if (!rtd.isNumber())
1624 throw new Error("Rightside is not number"+on.printNode(0));
1627 if (ltd.isDouble()||rtd.isDouble())
1628 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
1629 else if (ltd.isFloat()||rtd.isFloat())
1630 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
1631 else if (ltd.isLong()||rtd.isLong())
1632 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1634 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1636 on.setLeftType(lefttype);
1637 on.setRightType(righttype);
1638 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1642 if (ltd.isString()||rtd.isString()) {
1643 ClassDescriptor stringcl=getClass(null, TypeUtil.StringClass);
1644 TypeDescriptor stringtd=new TypeDescriptor(stringcl);
1645 NameDescriptor nd=new NameDescriptor("String");
1646 NameDescriptor valuend=new NameDescriptor(nd, "valueOf");
1647 if (!(ltd.isString()&&(on.getLeft() instanceof OpNode))) {
1648 MethodInvokeNode leftmin=new MethodInvokeNode(valuend);
1649 leftmin.setNumLine(on.getLeft().getNumLine());
1650 leftmin.addArgument(on.getLeft());
1652 checkExpressionNode(md, nametable, on.getLeft(), null);
1655 if (!(rtd.isString()&&(on.getRight() instanceof OpNode))) {
1656 MethodInvokeNode rightmin=new MethodInvokeNode(valuend);
1657 rightmin.setNumLine(on.getRight().getNumLine());
1658 rightmin.addArgument(on.getRight());
1660 checkExpressionNode(md, nametable, on.getRight(), null);
1663 on.setLeftType(stringtd);
1664 on.setRightType(stringtd);
1665 on.setType(stringtd);
1670 case Operation.MULT:
1673 // 5.6.2 Binary Numeric Promotion
1674 //TODO unboxing of reference objects
1675 if (ltd.isArray()||rtd.isArray()||!ltd.isNumber()||!rtd.isNumber())
1676 throw new Error("Error in "+on.printNode(0));
1678 if (ltd.isDouble()||rtd.isDouble())
1679 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
1680 else if (ltd.isFloat()||rtd.isFloat())
1681 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
1682 else if (ltd.isLong()||rtd.isLong())
1683 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1685 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1687 on.setLeftType(lefttype);
1688 on.setRightType(righttype);
1689 on.setType(lefttype);
1692 case Operation.LEFTSHIFT:
1693 case Operation.RIGHTSHIFT:
1694 case Operation.URIGHTSHIFT:
1695 if (!rtd.isIntegerType())
1697 //5.6.1 Unary Numeric Promotion
1698 if (rtd.isByte()||rtd.isShort()||rtd.isInt())
1699 righttype=new TypeDescriptor(TypeDescriptor.INT);
1703 on.setRightType(righttype);
1704 if (!ltd.isIntegerType())
1707 case Operation.UNARYPLUS:
1708 case Operation.UNARYMINUS:
1709 /* case Operation.POSTINC:
1710 case Operation.POSTDEC:
1711 case Operation.PREINC:
1712 case Operation.PREDEC:*/
1713 if (!ltd.isNumber())
1715 //5.6.1 Unary Numeric Promotion
1716 if (ltd.isByte()||ltd.isShort()||ltd.isInt())
1717 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1720 on.setLeftType(lefttype);
1721 on.setType(lefttype);
1725 throw new Error(op.toString());
1729 if (!typeutil.isSuperorType(td, on.getType())) {
1730 throw new Error("Type of rside not compatible with type of lside"+on.printNode(0));