7 public class SemanticCheck {
12 HashMap<ClassDescriptor, Integer> completed;
13 HashMap<ClassDescriptor, Vector<VarDescriptor>> inlineClass2LiveVars;
14 boolean trialcheck = false;
16 public static final int NOCHECK=0;
17 public static final int REFERENCE=1;
18 public static final int INIT=2;
22 public boolean hasLayout(ClassDescriptor cd) {
23 return completed.get(cd)!=null&&completed.get(cd)==INIT;
26 public SemanticCheck(State state, TypeUtil tu) {
27 this(state, tu, true);
30 public SemanticCheck(State state, TypeUtil tu, boolean checkAll) {
33 this.loopstack=new Stack();
34 this.toanalyze=new HashSet();
35 this.completed=new HashMap<ClassDescriptor, Integer>();
36 this.checkAll=checkAll;
37 this.inlineClass2LiveVars = new HashMap<ClassDescriptor, Vector<VarDescriptor>>();
40 public ClassDescriptor getClass(ClassDescriptor context, String classname) {
41 return getClass(context, classname, INIT);
43 public ClassDescriptor getClass(ClassDescriptor context, String classnameIn, int fullcheck) {
44 ClassDescriptor cd=typeutil.getClass(context, classnameIn, toanalyze);
45 checkClass(cd, fullcheck);
49 public void checkClass(ClassDescriptor cd) {
53 public void checkClass(ClassDescriptor cd, int fullcheck) {
54 if (!completed.containsKey(cd)||completed.get(cd)<fullcheck) {
55 int oldstatus=completed.containsKey(cd)?completed.get(cd):0;
56 completed.put(cd, fullcheck);
58 if (fullcheck>=REFERENCE&&oldstatus<INIT) {
59 //Set superclass link up
60 if (cd.getSuper()!=null) {
61 ClassDescriptor superdesc=getClass(cd, cd.getSuper(), fullcheck);
62 if (superdesc.isInnerClass()) {
65 if (superdesc.isInterface()) {
68 cd.getSuperInterface().add(superdesc.getSymbol());
70 throw new Error("Error! Class " + cd.getSymbol() + " extends interface " + cd.getSuper());
73 cd.setSuperDesc(superdesc);
75 // Link together Field, Method, and Flag tables so classes
76 // inherit these from their superclasses
77 if (oldstatus<REFERENCE) {
78 cd.getFieldTable().setParent(cd.getSuperDesc().getFieldTable());
79 cd.getMethodTable().setParent(cd.getSuperDesc().getMethodTable());
80 cd.getFlagTable().setParent(cd.getSuperDesc().getFlagTable());
84 // Link together Field, Method tables do classes inherit these from
85 // their ancestor interfaces
86 Vector<String> sifv = cd.getSuperInterface();
87 for(int i = 0; i < sifv.size(); i++) {
88 ClassDescriptor superif = getClass(cd, sifv.elementAt(i), fullcheck);
89 if(!superif.isInterface()) {
90 throw new Error("Error! Class " + cd.getSymbol() + " implements non-interface " + superif.getSymbol());
92 if (oldstatus<REFERENCE) {
93 cd.addSuperInterfaces(superif);
94 cd.getMethodTable().addParentIF(superif.getMethodTable());
95 cd.getFieldTable().addParentIF(superif.getFieldTable());
99 if (oldstatus<INIT&&fullcheck>=INIT) {
100 /* Check to see that fields are well typed */
101 for(Iterator field_it=cd.getFields(); field_it.hasNext(); ) {
102 FieldDescriptor fd=(FieldDescriptor)field_it.next();
106 System.out.println("Class/Field in "+cd+":"+fd);
110 for(Iterator method_it=cd.getMethods(); method_it.hasNext(); ) {
111 MethodDescriptor md=(MethodDescriptor)method_it.next();
115 System.out.println("Class/Method in "+cd+":"+md);
123 public void semanticCheckClass(ClassDescriptor cd) {
124 // need to initialize typeutil object here...only place we can
125 // get class descriptors without first calling getclass
126 getClass(cd, cd.getSymbol());
129 // for inline defined anonymous classes, we need to check its
130 // surrounding class first to get its surrounding context
131 ClassDescriptor surroundingcd = cd.getSurroundingDesc();
132 if(toanalyze.contains(surroundingcd)) {
133 toanalyze.remove(surroundingcd);
134 semanticCheckClass(surroundingcd);
138 for (Iterator method_it = cd.getMethods(); method_it.hasNext(); ) {
139 MethodDescriptor md = (MethodDescriptor) method_it.next();
141 checkMethodBody(cd, md);
143 System.out.println("Error in " + md);
149 public void semanticCheck() {
150 SymbolTable classtable = state.getClassSymbolTable();
151 toanalyze.addAll(classtable.getValueSet());
152 toanalyze.addAll(state.getTaskSymbolTable().getValueSet());
155 while (!toanalyze.isEmpty()) {
156 Object obj = toanalyze.iterator().next();
157 if (obj instanceof TaskDescriptor) {
158 toanalyze.remove(obj);
159 TaskDescriptor td = (TaskDescriptor) obj;
163 System.out.println("Error in " + td);
167 ClassDescriptor cd = (ClassDescriptor) obj;
168 toanalyze.remove(cd);
169 semanticCheckClass(cd);
174 private void checkTypeDescriptor(ClassDescriptor cd, TypeDescriptor td) {
175 if (td.isPrimitive())
177 else if (td.isClass()) {
178 String name=td.toString();
179 ClassDescriptor field_cd=checkAll?getClass(cd, name):getClass(cd, name, REFERENCE);
182 throw new Error("Undefined class "+name);
183 td.setClassDescriptor(field_cd);
185 } else if (td.isTag())
191 public void checkField(ClassDescriptor cd, FieldDescriptor fd) {
192 checkTypeDescriptor(cd, fd.getType());
195 public void checkConstraintCheck(TaskDescriptor td, SymbolTable nametable, Vector ccs) {
197 return; /* No constraint checks to check */
198 for(int i=0; i<ccs.size(); i++) {
199 ConstraintCheck cc=(ConstraintCheck) ccs.get(i);
201 for(int j=0; j<cc.numArgs(); j++) {
202 ExpressionNode en=cc.getArg(j);
203 checkExpressionNode(td,nametable,en,null);
208 public void checkFlagEffects(TaskDescriptor td, Vector vfe, SymbolTable nametable) {
210 return; /* No flag effects to check */
211 for(int i=0; i<vfe.size(); i++) {
212 FlagEffects fe=(FlagEffects) vfe.get(i);
213 String varname=fe.getName();
214 //Make sure the variable is declared as a parameter to the task
215 VarDescriptor vd=(VarDescriptor)td.getParameterTable().get(varname);
217 throw new Error("Parameter "+varname+" in Flag Effects not declared in "+td);
220 //Make sure it correspods to a class
221 TypeDescriptor type_d=vd.getType();
222 if (!type_d.isClass())
223 throw new Error("Cannot have non-object argument for flag_effect");
225 ClassDescriptor cd=type_d.getClassDesc();
226 for(int j=0; j<fe.numEffects(); j++) {
227 FlagEffect flag=fe.getEffect(j);
228 String name=flag.getName();
229 FlagDescriptor flag_d=(FlagDescriptor)cd.getFlagTable().get(name);
230 //Make sure the flag is declared
232 throw new Error("Flag descriptor "+name+" undefined in class: "+cd.getSymbol());
233 if (flag_d.getExternal())
234 throw new Error("Attempting to modify external flag: "+name);
235 flag.setFlag(flag_d);
237 for(int j=0; j<fe.numTagEffects(); j++) {
238 TagEffect tag=fe.getTagEffect(j);
239 String name=tag.getName();
241 Descriptor d=(Descriptor)nametable.get(name);
243 throw new Error("Tag descriptor "+name+" undeclared");
244 else if (!(d instanceof TagVarDescriptor))
245 throw new Error(name+" is not a tag descriptor");
246 tag.setTag((TagVarDescriptor)d);
251 public void checkTask(TaskDescriptor td) {
252 for(int i=0; i<td.numParameters(); i++) {
253 /* Check that parameter is well typed */
254 TypeDescriptor param_type=td.getParamType(i);
255 checkTypeDescriptor(null, param_type);
257 /* Check the parameter's flag expression is well formed */
258 FlagExpressionNode fen=td.getFlag(td.getParameter(i));
259 if (!param_type.isClass())
260 throw new Error("Cannot have non-object argument to a task");
261 ClassDescriptor cd=param_type.getClassDesc();
263 checkFlagExpressionNode(cd, fen);
266 checkFlagEffects(td, td.getFlagEffects(),td.getParameterTable());
267 /* Check that the task code is valid */
268 BlockNode bn=state.getMethodBody(td);
269 checkBlockNode(td, td.getParameterTable(),bn);
272 public void checkFlagExpressionNode(ClassDescriptor cd, FlagExpressionNode fen) {
274 case Kind.FlagOpNode:
276 FlagOpNode fon=(FlagOpNode)fen;
277 checkFlagExpressionNode(cd, fon.getLeft());
278 if (fon.getRight()!=null)
279 checkFlagExpressionNode(cd, fon.getRight());
285 FlagNode fn=(FlagNode)fen;
286 String name=fn.getFlagName();
287 FlagDescriptor fd=(FlagDescriptor)cd.getFlagTable().get(name);
289 throw new Error("Undeclared flag: "+name);
295 throw new Error("Unrecognized FlagExpressionNode");
299 public void checkMethod(ClassDescriptor cd, MethodDescriptor md) {
300 /* Check for abstract methods */
301 if(md.isAbstract()) {
302 if(!cd.isAbstract() && !cd.isInterface()) {
303 throw new Error("Error! The non-abstract Class " + cd.getSymbol() + " contains an abstract method " + md.getSymbol());
307 /* Check return type */
308 if (!md.isConstructor() && !md.isStaticBlock())
309 if (!md.getReturnType().isVoid()) {
310 checkTypeDescriptor(cd, md.getReturnType());
312 for(int i=0; i<md.numParameters(); i++) {
313 TypeDescriptor param_type=md.getParamType(i);
314 checkTypeDescriptor(cd, param_type);
316 /* Link the naming environments */
317 if (!md.isStatic()) /* Fields aren't accessible directly in a static method, so don't link in this table */
318 md.getParameterTable().setParent(cd.getFieldTable());
320 if (!md.isStatic()) {
321 VarDescriptor thisvd=new VarDescriptor(new TypeDescriptor(cd),"this");
324 if(md.isDefaultConstructor() && (cd.getSuperDesc() != null) && !cd.getInline()) {
325 // add the construction of it super class, can only be super()
326 // NOTE: inline class should be treated differently
327 NameDescriptor nd=new NameDescriptor("super");
328 MethodInvokeNode min=new MethodInvokeNode(nd);
329 BlockExpressionNode ben=new BlockExpressionNode(min);
330 BlockNode bn = state.getMethodBody(md);
331 bn.addFirstBlockStatement(ben);
335 public void checkMethodBody(ClassDescriptor cd, MethodDescriptor md) {
336 ClassDescriptor superdesc=cd.getSuperDesc();
337 if (superdesc!=null) {
338 Set possiblematches=superdesc.getMethodTable().getSet(md.getSymbol());
339 for(Iterator methodit=possiblematches.iterator(); methodit.hasNext(); ) {
340 MethodDescriptor matchmd=(MethodDescriptor)methodit.next();
341 if (md.matches(matchmd)) {
342 if (matchmd.getModifiers().isFinal()) {
343 throw new Error("Try to override final method in method:"+md+" declared in "+cd);
348 BlockNode bn=state.getMethodBody(md);
349 checkBlockNode(md, md.getParameterTable(),bn);
352 public void checkBlockNode(Descriptor md, SymbolTable nametable, BlockNode bn) {
353 /* Link in the naming environment */
354 bn.getVarTable().setParent(nametable);
355 for(int i=0; i<bn.size(); i++) {
356 BlockStatementNode bsn=bn.get(i);
357 checkBlockStatementNode(md, bn.getVarTable(),bsn);
361 public void checkBlockStatementNode(Descriptor md, SymbolTable nametable, BlockStatementNode bsn) {
363 case Kind.BlockExpressionNode:
364 checkBlockExpressionNode(md, nametable,(BlockExpressionNode)bsn);
367 case Kind.DeclarationNode:
368 checkDeclarationNode(md, nametable, (DeclarationNode)bsn);
371 case Kind.TagDeclarationNode:
372 checkTagDeclarationNode(md, nametable, (TagDeclarationNode)bsn);
375 case Kind.IfStatementNode:
376 checkIfStatementNode(md, nametable, (IfStatementNode)bsn);
379 case Kind.SwitchStatementNode:
380 checkSwitchStatementNode(md, nametable, (SwitchStatementNode)bsn);
384 checkLoopNode(md, nametable, (LoopNode)bsn);
387 case Kind.ReturnNode:
388 checkReturnNode(md, nametable, (ReturnNode)bsn);
391 case Kind.TaskExitNode:
392 checkTaskExitNode(md, nametable, (TaskExitNode)bsn);
395 case Kind.SubBlockNode:
396 checkSubBlockNode(md, nametable, (SubBlockNode)bsn);
399 case Kind.AtomicNode:
400 checkAtomicNode(md, nametable, (AtomicNode)bsn);
403 case Kind.SynchronizedNode:
404 checkSynchronizedNode(md, nametable, (SynchronizedNode)bsn);
407 case Kind.ContinueBreakNode:
408 checkContinueBreakNode(md, nametable, (ContinueBreakNode) bsn);
412 case Kind.GenReachNode:
413 case Kind.GenDefReachNode:
414 // do nothing, no semantic check for SESEs
421 void checkBlockExpressionNode(Descriptor md, SymbolTable nametable, BlockExpressionNode ben) {
422 checkExpressionNode(md, nametable, ben.getExpression(), null);
425 void checkDeclarationNode(Descriptor md, SymbolTable nametable, DeclarationNode dn) {
426 VarDescriptor vd=dn.getVarDescriptor();
427 checkTypeDescriptor(((md instanceof MethodDescriptor)?((MethodDescriptor)md).getClassDesc():null), vd.getType());
428 Descriptor d=nametable.get(vd.getSymbol());
430 (d instanceof FieldDescriptor)) {
433 throw new Error(vd.getSymbol()+" in "+md+" defined a second time");
434 if (dn.getExpression()!=null)
435 checkExpressionNode(md, nametable, dn.getExpression(), vd.getType());
438 void checkTagDeclarationNode(Descriptor md, SymbolTable nametable, TagDeclarationNode dn) {
439 TagVarDescriptor vd=dn.getTagVarDescriptor();
440 Descriptor d=nametable.get(vd.getSymbol());
442 (d instanceof FieldDescriptor)) {
445 throw new Error(vd.getSymbol()+" defined a second time");
448 void checkSubBlockNode(Descriptor md, SymbolTable nametable, SubBlockNode sbn) {
449 checkBlockNode(md, nametable, sbn.getBlockNode());
452 void checkAtomicNode(Descriptor md, SymbolTable nametable, AtomicNode sbn) {
453 checkBlockNode(md, nametable, sbn.getBlockNode());
456 void checkSynchronizedNode(Descriptor md, SymbolTable nametable, SynchronizedNode sbn) {
457 checkBlockNode(md, nametable, sbn.getBlockNode());
458 //todo this could be Object
459 checkExpressionNode(md, nametable, sbn.getExpr(), null);
462 void checkContinueBreakNode(Descriptor md, SymbolTable nametable, ContinueBreakNode cbn) {
463 if (loopstack.empty())
464 throw new Error("continue/break outside of loop");
465 Object o = loopstack.peek();
466 if(o instanceof LoopNode) {
467 LoopNode ln=(LoopNode)o;
472 void checkReturnNode(Descriptor d, SymbolTable nametable, ReturnNode rn) {
473 if (d instanceof TaskDescriptor)
474 throw new Error("Illegal return appears in Task: "+d.getSymbol());
475 MethodDescriptor md=(MethodDescriptor)d;
476 if (rn.getReturnExpression()!=null)
477 if (md.getReturnType()==null)
478 throw new Error("Constructor can't return something.");
479 else if (md.getReturnType().isVoid())
480 throw new Error(md+" is void");
482 checkExpressionNode(md, nametable, rn.getReturnExpression(), md.getReturnType());
484 if (md.getReturnType()!=null&&!md.getReturnType().isVoid())
485 throw new Error("Need to return something for "+md);
488 void checkTaskExitNode(Descriptor md, SymbolTable nametable, TaskExitNode ten) {
489 if (md instanceof MethodDescriptor)
490 throw new Error("Illegal taskexit appears in Method: "+md.getSymbol());
491 checkFlagEffects((TaskDescriptor)md, ten.getFlagEffects(),nametable);
492 checkConstraintCheck((TaskDescriptor) md, nametable, ten.getChecks());
495 void checkIfStatementNode(Descriptor md, SymbolTable nametable, IfStatementNode isn) {
496 checkExpressionNode(md, nametable, isn.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
497 checkBlockNode(md, nametable, isn.getTrueBlock());
498 if (isn.getFalseBlock()!=null)
499 checkBlockNode(md, nametable, isn.getFalseBlock());
502 void checkSwitchStatementNode(Descriptor md, SymbolTable nametable, SwitchStatementNode ssn) {
503 checkExpressionNode(md, nametable, ssn.getCondition(), new TypeDescriptor(TypeDescriptor.INT));
505 BlockNode sbn = ssn.getSwitchBody();
506 boolean hasdefault = false;
507 for(int i = 0; i < sbn.size(); i++) {
508 boolean containdefault = checkSwitchBlockNode(md, nametable, (SwitchBlockNode)sbn.get(i));
509 if(hasdefault && containdefault) {
510 throw new Error("Error: duplicate default branch in switch-case statement in Method: " + md.getSymbol());
512 hasdefault = containdefault;
516 boolean checkSwitchBlockNode(Descriptor md, SymbolTable nametable, SwitchBlockNode sbn) {
517 Vector<SwitchLabelNode> slnv = sbn.getSwitchConditions();
519 for(int i = 0; i < slnv.size(); i++) {
520 if(slnv.elementAt(i).isdefault) {
523 checkConstantExpressionNode(md, nametable, slnv.elementAt(i).getCondition(), new TypeDescriptor(TypeDescriptor.INT));
527 throw new Error("Error: duplicate default branch in switch-case statement in Method: " + md.getSymbol());
530 checkBlockNode(md, nametable, sbn.getSwitchBlockStatement());
532 return (defaultb > 0);
536 void checkConstantExpressionNode(Descriptor md, SymbolTable nametable, ExpressionNode en, TypeDescriptor td) {
538 case Kind.FieldAccessNode:
539 checkFieldAccessNode(md,nametable,(FieldAccessNode)en,td);
542 case Kind.LiteralNode:
543 checkLiteralNode(md,nametable,(LiteralNode)en,td);
547 checkNameNode(md,nametable,(NameNode)en,td);
551 checkOpNode(md, nametable, (OpNode)en, td);
557 void checkExpressionNode(Descriptor md, SymbolTable nametable, ExpressionNode en, TypeDescriptor td) {
559 case Kind.AssignmentNode:
560 checkAssignmentNode(md,nametable,(AssignmentNode)en,td);
564 checkCastNode(md,nametable,(CastNode)en,td);
567 case Kind.CreateObjectNode:
568 checkCreateObjectNode(md,nametable,(CreateObjectNode)en,td);
571 case Kind.FieldAccessNode:
572 checkFieldAccessNode(md,nametable,(FieldAccessNode)en,td);
575 case Kind.ArrayAccessNode:
576 checkArrayAccessNode(md,nametable,(ArrayAccessNode)en,td);
579 case Kind.LiteralNode:
580 checkLiteralNode(md,nametable,(LiteralNode)en,td);
583 case Kind.MethodInvokeNode:
584 checkMethodInvokeNode(md,nametable,(MethodInvokeNode)en,td);
588 checkNameNode(md,nametable,(NameNode)en,td);
592 checkOpNode(md,nametable,(OpNode)en,td);
595 case Kind.OffsetNode:
596 checkOffsetNode(md, nametable, (OffsetNode)en, td);
599 case Kind.TertiaryNode:
600 checkTertiaryNode(md, nametable, (TertiaryNode)en, td);
603 case Kind.InstanceOfNode:
604 checkInstanceOfNode(md, nametable, (InstanceOfNode) en, td);
607 case Kind.ArrayInitializerNode:
608 checkArrayInitializerNode(md, nametable, (ArrayInitializerNode) en, td);
611 case Kind.ClassTypeNode:
612 checkClassTypeNode(md, nametable, (ClassTypeNode) en, td);
618 void checkClassTypeNode(Descriptor md, SymbolTable nametable, ClassTypeNode tn, TypeDescriptor td) {
619 checkTypeDescriptor(((md instanceof MethodDescriptor)?((MethodDescriptor)md).getClassDesc():null), tn.getType());
622 void checkCastNode(Descriptor md, SymbolTable nametable, CastNode cn, TypeDescriptor td) {
623 /* Get type descriptor */
624 if (cn.getType()==null) {
625 NameDescriptor typenamed=cn.getTypeName().getName();
626 TypeDescriptor ntd=new TypeDescriptor(getClass(((md instanceof MethodDescriptor)?((MethodDescriptor)md).getClassDesc():null), typenamed.toString()));
630 /* Check the type descriptor */
631 TypeDescriptor cast_type=cn.getType();
632 checkTypeDescriptor(((md instanceof MethodDescriptor)?((MethodDescriptor)md).getClassDesc():null),cast_type);
636 if (!typeutil.isSuperorType(td,cast_type))
637 throw new Error("Cast node returns "+cast_type+", but need "+td);
640 ExpressionNode en=cn.getExpression();
641 checkExpressionNode(md, nametable, en, null);
642 TypeDescriptor etd=en.getType();
643 if (typeutil.isSuperorType(cast_type,etd)) /* Cast trivially succeeds */
646 if (typeutil.isSuperorType(etd,cast_type)) /* Cast may succeed */
648 if (typeutil.isCastable(etd, cast_type))
651 //rough hack to handle interfaces...should clean up
652 if (etd.isClass()&&cast_type.isClass()) {
653 ClassDescriptor cdetd=etd.getClassDesc();
654 ClassDescriptor cdcast_type=cast_type.getClassDesc();
656 if (cdetd.isInterface()&&!cdcast_type.getModifier().isFinal())
659 if (cdcast_type.isInterface()&&!cdetd.getModifier().isFinal())
662 /* Different branches */
663 /* TODO: change if add interfaces */
664 throw new Error("Cast will always fail\n"+cn.printNode(0));
667 //FieldDescriptor checkFieldAccessNodeForParentNode( Descriptor md, SymbolTable na )
668 void checkFieldAccessNode(Descriptor md, SymbolTable nametable, FieldAccessNode fan, TypeDescriptor td) {
669 ExpressionNode left=fan.getExpression();
670 checkExpressionNode(md,nametable,left,null);
671 TypeDescriptor ltd=left.getType();
673 checkClass(ltd.getClassDesc(), INIT);
674 String fieldname=fan.getFieldName();
676 FieldDescriptor fd=null;
677 if (ltd.isArray()&&fieldname.equals("length"))
678 fd=FieldDescriptor.arrayLength;
679 else if(((left instanceof NameNode) && ((NameNode)left).isSuper())
680 ||((left instanceof FieldAccessNode) && ((FieldAccessNode)left).isSuper())){
681 fd = (FieldDescriptor) ltd.getClassDesc().getSuperDesc().getFieldTable().get(fieldname);
683 fd=(FieldDescriptor) ltd.getClassDesc().getFieldTable().get(fieldname);
685 if(ltd.isClassNameRef()) {
686 // the field access is using a class name directly
688 // check if it is to access a surrounding class in an inner class
689 if(fieldname.equals("this") || fieldname.equals("super")) {
690 ClassDescriptor icd = ((VarDescriptor)nametable.get("this")).getType().getClassDesc();
691 if(icd.isInnerClass()) {
692 NameNode nn = new NameNode(new NameDescriptor("this"));
693 nn.setVar((VarDescriptor)nametable.get("this"));
694 fan.setExpression(nn);
695 if(icd.getSurroundingDesc()==ltd.getClassDesc()) {
696 // this is a surrounding class access inside an inner class
697 fan.setExpression(nn);
698 fan.setFieldName("this$0");
699 fd = (FieldDescriptor)icd.getFieldTable().get("this$0");
700 } else if(icd==ltd.getClassDesc()) {
701 // this is an inner class this operation
702 fd = new FieldDescriptor(new Modifiers(),new TypeDescriptor(icd),"this",null,false);
704 if(fieldname.equals("super")) {
711 ClassDescriptor surroundingCls=ltd.getClassDesc().getSurroundingDesc();
713 while(surroundingCls!=null) {
714 fd=(FieldDescriptor) surroundingCls.getFieldTable().get(fieldname);
716 fan.left=new ClassTypeNode(new TypeDescriptor(surroundingCls));
719 surroundingCls=surroundingCls.getSurroundingDesc();
723 if(ltd.getClassDesc().isEnum()) {
724 int value = ltd.getClassDesc().getEnumConstant(fieldname);
726 // check if this field is an enum constant
727 throw new Error(fieldname + " is not an enum constant in "+fan.printNode(0)+" in "+md);
729 fd = new FieldDescriptor(new Modifiers(Modifiers.PUBLIC|Modifiers.FINAL), new TypeDescriptor(TypeDescriptor.INT), fieldname, null, false);
731 fd.setEnumValue(value);
732 } else if(fd == null) {
733 throw new Error("Could not find field "+ fieldname + " in "+fan.printNode(0)+" in "+md + " (Line: "+fan.getNumLine()+")");
734 } else if(fd.isStatic()) {
735 // check if this field is a static field
736 if(fd.getExpressionNode() != null) {
737 checkExpressionNode(md,nametable,fd.getExpressionNode(),null);
740 throw new Error("Dereference of the non-static field "+ fieldname + " in "+fan.printNode(0)+" in "+md);
745 if((md instanceof MethodDescriptor) && false == ((MethodDescriptor)md).isStaticBlock()) {
746 ClassDescriptor cd = ((MethodDescriptor)md).getClassDesc();
747 FieldAccessNode theFieldNode = fieldAccessExpression( cd, fieldname, fan.getNumLine() );
748 if( null != theFieldNode ) {
749 //fan = theFieldNode;
750 checkFieldAccessNode( md, nametable, theFieldNode, td );
751 fan.setField( theFieldNode.getField() );
752 fan.setExpression( theFieldNode.getExpression() );
753 //TypeDescriptor td1 = fan.getType();
758 throw new Error("Unknown field "+fieldname + " in "+fan.printNode(0)+" in "+md);
761 if (fd.getType().iswrapper()) {
762 FieldAccessNode fan2=new FieldAccessNode(left, fieldname);
763 fan2.setNumLine(left.getNumLine());
766 fan.fieldname="value";
768 ExpressionNode leftwr=fan.getExpression();
769 TypeDescriptor ltdwr=leftwr.getType();
770 String fieldnamewr=fan.getFieldName();
771 FieldDescriptor fdwr=(FieldDescriptor) ltdwr.getClassDesc().getFieldTable().get(fieldnamewr);
774 throw new Error("Unknown field "+fieldnamewr + " in "+fan.printNode(0)+" in "+md);
779 if (!typeutil.isSuperorType(td,fan.getType()))
780 throw new Error("Field node returns "+fan.getType()+", but need "+td);
784 void checkArrayAccessNode(Descriptor md, SymbolTable nametable, ArrayAccessNode aan, TypeDescriptor td) {
785 ExpressionNode left=aan.getExpression();
786 checkExpressionNode(md,nametable,left,null);
788 checkExpressionNode(md,nametable,aan.getIndex(),new TypeDescriptor(TypeDescriptor.INT));
789 TypeDescriptor ltd=left.getType();
790 if (ltd.dereference().iswrapper()) {
791 aan.wrappertype=((FieldDescriptor)ltd.dereference().getClassDesc().getFieldTable().get("value")).getType();
795 if (!typeutil.isSuperorType(td,aan.getType()))
796 throw new Error("Field node returns "+aan.getType()+", but need "+td);
799 void checkLiteralNode(Descriptor md, SymbolTable nametable, LiteralNode ln, TypeDescriptor td) {
800 /* Resolve the type */
801 Object o=ln.getValue();
802 if (ln.getTypeString().equals("null")) {
803 ln.setType(new TypeDescriptor(TypeDescriptor.NULL));
804 } else if (o instanceof Integer) {
805 ln.setType(new TypeDescriptor(TypeDescriptor.INT));
806 } else if (o instanceof Long) {
807 ln.setType(new TypeDescriptor(TypeDescriptor.LONG));
808 } else if (o instanceof Float) {
809 ln.setType(new TypeDescriptor(TypeDescriptor.FLOAT));
810 } else if (o instanceof Boolean) {
811 ln.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
812 } else if (o instanceof Double) {
813 ln.setType(new TypeDescriptor(TypeDescriptor.DOUBLE));
814 } else if (o instanceof Character) {
815 ln.setType(new TypeDescriptor(TypeDescriptor.CHAR));
816 } else if (o instanceof String) {
817 ln.setType(new TypeDescriptor(getClass(null, TypeUtil.StringClass)));
821 if (!typeutil.isSuperorType(td,ln.getType())) {
822 Long l = ln.evaluate();
823 if((ln.getType().isByte() || ln.getType().isShort()
824 || ln.getType().isChar() || ln.getType().isInt())
826 && (td.isByte() || td.isShort() || td.isChar()
827 || td.isInt() || td.isLong())) {
828 long lnvalue = l.longValue();
829 if((td.isByte() && ((lnvalue > 127) || (lnvalue < -128)))
830 || (td.isShort() && ((lnvalue > 32767) || (lnvalue < -32768)))
831 || (td.isChar() && ((lnvalue > 65535) || (lnvalue < 0)))
832 || (td.isInt() && ((lnvalue > 2147483647) || (lnvalue < -2147483648)))
833 || (td.isLong() && ((lnvalue > 9223372036854775807L) || (lnvalue < -9223372036854775808L)))) {
834 throw new Error("Field node returns "+ln.getType()+", but need "+td+" in "+md);
837 throw new Error("Field node returns "+ln.getType()+", but need "+td+" in "+md);
842 FieldDescriptor recurseSurroundingClasses( ClassDescriptor icd, String varname ) {
843 if( null == icd || false == icd.isInnerClass() )
846 ClassDescriptor surroundingDesc = icd.getSurroundingDesc();
847 if( null == surroundingDesc )
850 SymbolTable fieldTable = surroundingDesc.getFieldTable();
851 FieldDescriptor fd = ( FieldDescriptor ) fieldTable.get( varname );
854 return recurseSurroundingClasses( surroundingDesc, varname );
857 FieldAccessNode fieldAccessExpression( ClassDescriptor icd, String varname, int linenum ) {
858 FieldDescriptor fd = recurseSurroundingClasses( icd, varname );
862 ClassDescriptor cd = fd.getClassDescriptor();
864 int startingDepth = icd.getInnerDepth();
866 if( true == cd.isInnerClass() )
867 depth = cd.getInnerDepth();
869 String composed = "this";
870 NameDescriptor runningDesc = new NameDescriptor( "this" );;
872 for ( int index = startingDepth; index > depth; --index ) {
873 composed = "this$" + String.valueOf( index - 1 );
874 runningDesc = new NameDescriptor( runningDesc, composed );
876 if( false == cd.isInnerClass() )
877 runningDesc = new NameDescriptor( runningDesc, "this$" + String.valueOf(0) ); //all the way up.
878 NameDescriptor idDesc = new NameDescriptor( runningDesc, varname );
881 FieldAccessNode theFieldNode = ( FieldAccessNode )translateNameDescriptorintoExpression( idDesc, linenum );
885 void checkNameNode(Descriptor md, SymbolTable nametable, NameNode nn, TypeDescriptor td) {
886 NameDescriptor nd=nn.getName();
887 if (nd.getBase()!=null) {
889 /* Rewrite NameNode */
890 ExpressionNode en=translateNameDescriptorintoExpression(nd,nn.getNumLine());
891 nn.setExpression(en);
892 checkExpressionNode(md,nametable,en,td);
894 String varname=nd.toString();
895 if(varname.equals("this") || varname.equals("super")) {
897 nn.setVar((VarDescriptor)nametable.get("this"));
898 if(varname.equals("super")) {
903 Descriptor d=(Descriptor)nametable.get(varname);
905 ClassDescriptor cd = null;
906 //check the inner class case first.
907 if((md instanceof MethodDescriptor) && false == ((MethodDescriptor)md).isStaticBlock()) {
908 cd = ((MethodDescriptor)md).getClassDesc();
909 FieldAccessNode theFieldNode = fieldAccessExpression( cd, varname, nn.getNumLine() );
910 if( null != theFieldNode ) {
911 nn.setExpression(( ExpressionNode )theFieldNode);
912 checkExpressionNode(md,nametable,( ExpressionNode )theFieldNode,td);
914 } else if(cd.getInline() && this.trialcheck) {
915 // for the trial check of an inline class, cache the unknown var
916 d = cd.getSurroundingNameTable().get(varname);
917 if(!this.inlineClass2LiveVars.containsKey(cd)) {
918 this.inlineClass2LiveVars.put(cd, new Vector<VarDescriptor>());
920 Vector<VarDescriptor> vars = this.inlineClass2LiveVars.get(cd);
921 if(!vars.contains((VarDescriptor)d)) {
922 vars.add((VarDescriptor)d);
927 if((md instanceof MethodDescriptor) && ((MethodDescriptor)md).isStaticBlock()) {
928 // this is a static block, all the accessed fields should be static field
929 cd = ((MethodDescriptor)md).getClassDesc();
930 SymbolTable fieldtbl = cd.getFieldTable();
931 FieldDescriptor fd=(FieldDescriptor)fieldtbl.get(varname);
932 if((fd == null) || (!fd.isStatic())) {
933 // no such field in the class, check if this is a class
934 if(varname.equals("this")) {
935 throw new Error("Error: access this obj in a static block");
937 cd=getClass(((md instanceof MethodDescriptor)?((MethodDescriptor)md).getClassDesc():null), varname);
939 // this is a class name
943 throw new Error("Name "+varname+" should not be used in static block: "+md);
946 // this is a static field
952 // check if the var is a static field of the class
953 if(md instanceof MethodDescriptor) {
954 cd = ((MethodDescriptor)md).getClassDesc();
955 FieldDescriptor fd = (FieldDescriptor)cd.getFieldTable().get(varname);
956 if((fd != null) && (fd.isStatic())) {
960 if (!typeutil.isSuperorType(td,nn.getType()))
961 throw new Error("Field node returns "+nn.getType()+", but need "+td);
963 } else if(fd != null) {
964 throw new Error("Name "+varname+" should not be used in " + md);
967 cd=getClass(((md instanceof MethodDescriptor)?((MethodDescriptor)md).getClassDesc():null), varname);
969 // this is a class name
973 throw new Error("Name "+varname+" undefined in: "+md);
979 if (d instanceof VarDescriptor) {
981 } else if (d instanceof FieldDescriptor) {
982 FieldDescriptor fd=(FieldDescriptor)d;
983 if (fd.getType().iswrapper()) {
984 String id=nd.getIdentifier();
985 NameDescriptor base=nd.getBase();
986 NameNode n=new NameNode(nn.getName());
987 n.setNumLine(nn.getNumLine());
989 n.setVar((VarDescriptor)nametable.get("this")); /* Need a pointer to this */
990 FieldAccessNode fan=new FieldAccessNode(n,"value");
991 fan.setNumLine(n.getNumLine());
992 FieldDescriptor fdval=(FieldDescriptor) fd.getType().getClassDesc().getFieldTable().get("value");
994 nn.setExpression(fan);
997 nn.setVar((VarDescriptor)nametable.get("this")); /* Need a pointer to this */
999 } else if (d instanceof TagVarDescriptor) {
1001 } else throw new Error("Wrong type of descriptor");
1003 if (!typeutil.isSuperorType(td,nn.getType()))
1004 throw new Error("Field node returns "+nn.getType()+", but need "+td);
1008 void checkOffsetNode(Descriptor md, SymbolTable nameTable, OffsetNode ofn, TypeDescriptor td) {
1009 TypeDescriptor ltd=ofn.td;
1010 checkTypeDescriptor(((md instanceof MethodDescriptor)?((MethodDescriptor)md).getClassDesc():null),ltd);
1012 String fieldname = ofn.fieldname;
1013 FieldDescriptor fd=null;
1014 if (ltd.isArray()&&fieldname.equals("length")) {
1015 fd=FieldDescriptor.arrayLength;
1017 fd=(FieldDescriptor) ltd.getClassDesc().getFieldTable().get(fieldname);
1021 checkField(ltd.getClassDesc(), fd);
1024 throw new Error("Unknown field "+fieldname + " in "+ofn.printNode(1)+" in "+md);
1027 if (!typeutil.isSuperorType(td, ofn.getType())) {
1028 throw new Error("Type of rside not compatible with type of lside"+ofn.printNode(0));
1034 void checkTertiaryNode(Descriptor md, SymbolTable nametable, TertiaryNode tn, TypeDescriptor td) {
1035 checkExpressionNode(md, nametable, tn.getCond(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
1036 checkExpressionNode(md, nametable, tn.getTrueExpr(), td);
1037 checkExpressionNode(md, nametable, tn.getFalseExpr(), td);
1040 void checkInstanceOfNode(Descriptor md, SymbolTable nametable, InstanceOfNode tn, TypeDescriptor td) {
1041 if (td!=null&&!td.isBoolean())
1042 throw new Error("Expecting type "+td+"for instanceof expression");
1044 checkTypeDescriptor(((md instanceof MethodDescriptor)?((MethodDescriptor)md).getClassDesc():null), tn.getExprType());
1045 checkExpressionNode(md, nametable, tn.getExpr(), null);
1048 void checkArrayInitializerNode(Descriptor md, SymbolTable nametable, ArrayInitializerNode ain, TypeDescriptor td) {
1049 Vector<TypeDescriptor> vec_type = new Vector<TypeDescriptor>();
1050 for( int i = 0; i < ain.numVarInitializers(); ++i ) {
1051 checkExpressionNode(md, nametable, ain.getVarInitializer(i), td.dereference());
1052 vec_type.add(ain.getVarInitializer(i).getType());
1060 void checkAssignmentNode(Descriptor md, SymbolTable nametable, AssignmentNode an, TypeDescriptor td) {
1061 boolean postinc=true;
1062 if (an.getOperation().getBaseOp()==null||
1063 (an.getOperation().getBaseOp().getOp()!=Operation.POSTINC&&
1064 an.getOperation().getBaseOp().getOp()!=Operation.POSTDEC))
1067 checkExpressionNode(md, nametable, an.getSrc(),td);
1068 //TODO: Need check on validity of operation here
1069 if (!((an.getDest() instanceof FieldAccessNode)||
1070 (an.getDest() instanceof ArrayAccessNode)||
1071 (an.getDest() instanceof NameNode)))
1072 throw new Error("Bad lside in "+an.printNode(0));
1073 checkExpressionNode(md, nametable, an.getDest(), null);
1075 /* We want parameter variables to tasks to be immutable */
1076 if (md instanceof TaskDescriptor) {
1077 if (an.getDest() instanceof NameNode) {
1078 NameNode nn=(NameNode)an.getDest();
1079 if (nn.getVar()!=null) {
1080 if (((TaskDescriptor)md).getParameterTable().contains(nn.getVar().getSymbol()))
1081 throw new Error("Can't modify parameter "+nn.getVar()+ " to task "+td.getSymbol());
1086 if (an.getDest().getType().isString()&&an.getOperation().getOp()==AssignOperation.PLUSEQ) {
1088 ClassDescriptor stringcl=getClass(null, TypeUtil.StringClass);
1089 TypeDescriptor stringtd=new TypeDescriptor(stringcl);
1090 NameDescriptor nd=new NameDescriptor("String");
1091 NameDescriptor valuend=new NameDescriptor(nd, "valueOf");
1093 if (!(an.getSrc().getType().isString()&&(an.getSrc() instanceof OpNode))) {
1094 MethodInvokeNode rightmin=new MethodInvokeNode(valuend);
1095 rightmin.setNumLine(an.getSrc().getNumLine());
1096 rightmin.addArgument(an.getSrc());
1098 checkExpressionNode(md, nametable, an.getSrc(), null);
1102 if (!postinc&&!typeutil.isSuperorType(an.getDest().getType(),an.getSrc().getType())) {
1103 TypeDescriptor dt = an.getDest().getType();
1104 TypeDescriptor st = an.getSrc().getType();
1105 if(an.getSrc().kind() == Kind.ArrayInitializerNode) {
1106 if(dt.getArrayCount() != st.getArrayCount()) {
1107 throw new Error("Type of rside ("+an.getSrc().getType().toPrettyString()+") not compatible with type of lside ("+an.getDest().getType().toPrettyString()+")"+an.printNode(0));
1110 dt = dt.dereference();
1111 st = st.dereference();
1112 } while(dt.isArray());
1113 if((st.isByte() || st.isShort() || st.isChar() || st.isInt())
1114 && (dt.isByte() || dt.isShort() || dt.isChar() || dt.isInt() || dt.isLong())) {
1117 throw new Error("Type of rside ("+an.getSrc().getType().toPrettyString()+") not compatible with type of lside ("+an.getDest().getType().toPrettyString()+")"+an.printNode(0));
1121 Long l = an.getSrc().evaluate();
1122 if((st.isByte() || st.isShort() || st.isChar() || st.isInt())
1124 && (dt.isByte() || dt.isShort() || dt.isChar() || dt.isInt() || dt.isLong())) {
1125 long lnvalue = l.longValue();
1126 if((dt.isByte() && ((lnvalue > 127) || (lnvalue < -128)))
1127 || (dt.isShort() && ((lnvalue > 32767) || (lnvalue < -32768)))
1128 || (dt.isChar() && ((lnvalue > 65535) || (lnvalue < 0)))
1129 || (dt.isInt() && ((lnvalue > 2147483647) || (lnvalue < -2147483648)))
1130 || (dt.isLong() && ((lnvalue > 9223372036854775807L) || (lnvalue < -9223372036854775808L)))) {
1131 throw new Error("Type of rside ("+an.getSrc().getType().toPrettyString()+") not compatible with type of lside ("+an.getDest().getType().toPrettyString()+")"+an.printNode(0));
1134 throw new Error("Type of rside ("+an.getSrc().getType().toPrettyString()+") not compatible with type of lside ("+an.getDest().getType().toPrettyString()+")"+an.printNode(0));
1140 void checkLoopNode(Descriptor md, SymbolTable nametable, LoopNode ln) {
1142 if (ln.getType()==LoopNode.WHILELOOP||ln.getType()==LoopNode.DOWHILELOOP) {
1143 checkExpressionNode(md, nametable, ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
1144 checkBlockNode(md, nametable, ln.getBody());
1147 /* Link in the initializer naming environment */
1148 BlockNode bn=ln.getInitializer();
1149 bn.getVarTable().setParent(nametable);
1150 for(int i=0; i<bn.size(); i++) {
1151 BlockStatementNode bsn=bn.get(i);
1152 checkBlockStatementNode(md, bn.getVarTable(),bsn);
1154 //check the condition
1155 checkExpressionNode(md, bn.getVarTable(), ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
1156 checkBlockNode(md, bn.getVarTable(), ln.getBody());
1157 checkBlockNode(md, bn.getVarTable(), ln.getUpdate());
1162 void InnerClassAddParamToCtor( MethodDescriptor md, ClassDescriptor cd, SymbolTable nametable,
1163 CreateObjectNode con, TypeDescriptor td ) {
1165 TypeDescriptor cdsType = new TypeDescriptor( cd );
1166 ExpressionNode conExp = con.getSurroundingClassExpression();
1167 //System.out.println( "The surrounding class expression si " + con );
1168 if( null == conExp ) {
1169 if( md.isStatic() ) {
1170 throw new Error("trying to instantiate inner class: " + con.getType() + " in a static scope" );
1172 VarDescriptor thisVD = md.getThis();
1173 if( null == thisVD ) {
1174 throw new Error( "this pointer is not defined in a non static scope" );
1176 if( cdsType.equals( thisVD.getType() ) == false ) {
1177 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" );
1179 //make this into an expression node.
1180 NameNode nThis=new NameNode( new NameDescriptor( "this" ) );
1181 con.addArgument( nThis );
1184 //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.
1185 con.addArgument( conExp );
1187 //System.out.println( " the modified createObjectNode is " + con.printNode( 0 ) + "\n" );
1190 void trialSemanticCheck(ClassDescriptor cd) {
1191 if(!cd.getInline()) {
1192 throw new Error("Error! Try to do a trial check on a non-inline class " + cd.getSymbol());
1196 for (Iterator method_it = cd.getMethods(); method_it.hasNext(); ) {
1197 MethodDescriptor md = (MethodDescriptor) method_it.next();
1199 checkMethodBody(cd, md);
1201 System.out.println("Error in " + md);
1208 // add all the live vars that are referred by the inline class in the surrounding
1209 // context of the inline class into the inline class' field table and pass them to
1210 // the inline class' constructors
1211 // TODO: BUGFIX. These local vars should be final. But currently we've lost those
1212 // information, so we cannot have that checked.
1213 void InlineClassAddParamToCtor(MethodDescriptor md, ClassDescriptor cd , SymbolTable nametable, CreateObjectNode con, TypeDescriptor td, TypeDescriptor[] tdarray ) {
1214 // for an inline class, need to first add the original parameters of the CreatObjectNode
1215 // into its anonymous constructor and insert a super(...) into the anonymous constructor
1216 // if its super class is not null
1217 MethodDescriptor cd_construtor = null;
1218 for(Iterator it_methods = cd.getMethods(); it_methods.hasNext();) {
1219 MethodDescriptor imd = (MethodDescriptor)it_methods.next();
1220 if(imd.isConstructor()) {
1221 cd_construtor = imd; // an inline class should only have one anonymous constructor
1224 MethodInvokeNode min = null;
1225 if(cd.getSuper()!= null) {
1226 // add a super(...) into the anonymous constructor
1227 NameDescriptor nd=new NameDescriptor("super");
1228 min=new MethodInvokeNode(nd);
1229 BlockExpressionNode ben=new BlockExpressionNode(min);
1230 BlockNode bn = state.getMethodBody(cd_construtor);
1231 bn.addFirstBlockStatement(ben);
1233 for(int i = 0 ; i < tdarray.length; i++) {
1235 TypeDescriptor itd = tdarray[i];
1236 cd_construtor.addParameter(itd, itd.getSymbol()+"_from_con_node_"+i);
1237 min.addArgument(new NameNode(new NameDescriptor(itd.getSymbol()+"_from_con_node_"+i)));
1240 // Next add the live vars into the inline class' fields
1241 cd.setSurroundingNameTable(nametable);
1242 // do a round of semantic check trial to get all the live vars required by the inline class
1243 trialSemanticCheck(cd);
1244 Vector<VarDescriptor> vars = this.inlineClass2LiveVars.remove(cd);
1248 for(int i = 0; i < vars.size(); i++) {
1249 Descriptor d = vars.elementAt(i);
1250 if(d instanceof VarDescriptor && !d.getSymbol().equals("this")) {
1251 con.addArgument(new NameNode(new NameDescriptor(d.getSymbol())));
1252 cd.addField(new FieldDescriptor(new Modifiers(Modifiers.PUBLIC), ((VarDescriptor)d).getType(), d.getSymbol(), null, false));
1253 cd_construtor.addParameter(((VarDescriptor)d).getType(), d.getSymbol()+"_p");
1254 // add the initialize statement into this constructor
1255 BlockNode obn = state.getMethodBody(cd_construtor);
1256 NameNode nn=new NameNode(new NameDescriptor(d.getSymbol()));
1257 NameNode fn = new NameNode (new NameDescriptor(d.getSymbol()+"_p"));
1258 AssignmentNode an=new AssignmentNode(nn,fn,new AssignOperation(1));
1259 obn.addFirstBlockStatement(new BlockExpressionNode(an));
1260 state.addTreeCode(cd_construtor, obn);
1265 void checkCreateObjectNode(Descriptor md, SymbolTable nametable, CreateObjectNode con,
1266 TypeDescriptor td) {
1267 TypeDescriptor[] tdarray = new TypeDescriptor[con.numArgs()];
1268 for (int i = 0; i < con.numArgs(); i++) {
1269 ExpressionNode en = con.getArg(i);
1270 checkExpressionNode(md, nametable, en, null);
1271 tdarray[i] = en.getType();
1274 TypeDescriptor typetolookin = con.getType();
1275 checkTypeDescriptor(((md instanceof MethodDescriptor)?((MethodDescriptor)md).getClassDesc():null), typetolookin);
1277 if (td != null && !typeutil.isSuperorType(td, typetolookin))
1278 throw new Error(typetolookin + " isn't a " + td);
1280 /* Check Array Initializers */
1281 if ((con.getArrayInitializer() != null)) {
1282 checkArrayInitializerNode(md, nametable, con.getArrayInitializer(), typetolookin);
1285 if(this.trialcheck) {
1286 // for a trialcheck of an inline class, skip the rest process
1290 /* Check flag effects */
1291 if (con.getFlagEffects() != null) {
1292 FlagEffects fe = con.getFlagEffects();
1293 ClassDescriptor cd = typetolookin.getClassDesc();
1295 for (int j = 0; j < fe.numEffects(); j++) {
1296 FlagEffect flag = fe.getEffect(j);
1297 String name = flag.getName();
1298 FlagDescriptor flag_d = (FlagDescriptor) cd.getFlagTable().get(name);
1299 // Make sure the flag is declared
1301 throw new Error("Flag descriptor " + name + " undefined in class: " + cd.getSymbol());
1302 if (flag_d.getExternal())
1303 throw new Error("Attempting to modify external flag: " + name);
1304 flag.setFlag(flag_d);
1306 for (int j = 0; j < fe.numTagEffects(); j++) {
1307 TagEffect tag = fe.getTagEffect(j);
1308 String name = tag.getName();
1310 Descriptor d = (Descriptor) nametable.get(name);
1312 throw new Error("Tag descriptor " + name + " undeclared");
1313 else if (!(d instanceof TagVarDescriptor))
1314 throw new Error(name + " is not a tag descriptor");
1315 tag.setTag((TagVarDescriptor) d);
1319 if ((!typetolookin.isClass()) && (!typetolookin.isArray()))
1320 throw new Error("Can't allocate primitive type:" + con.printNode(0));
1322 if (!typetolookin.isArray()) {
1323 // Array's don't need constructor calls
1324 ClassDescriptor classtolookin = typetolookin.getClassDesc();
1325 checkClass(classtolookin, INIT);
1326 if( classtolookin.isInnerClass() ) {
1327 InnerClassAddParamToCtor( (MethodDescriptor)md, ((MethodDescriptor)md).getClassDesc() , nametable, con, td );
1328 if(classtolookin.getInline()) {
1329 // for an inline anonymous inner class, the live local variables that are
1330 // referred to by the inline class are passed as parameters of the constructors
1331 // of the inline class
1332 InlineClassAddParamToCtor( (MethodDescriptor)md, classtolookin, nametable, con, td, tdarray );
1334 tdarray = new TypeDescriptor[con.numArgs()];
1335 for (int i = 0; i < con.numArgs(); i++) {
1336 ExpressionNode en = con.getArg(i);
1337 checkExpressionNode(md, nametable, en, null);
1338 tdarray[i] = en.getType();
1341 Set methoddescriptorset = classtolookin.getMethodTable().getSet(classtolookin.getSymbol());
1342 MethodDescriptor bestmd = null;
1343 NextMethod: for (Iterator methodit = methoddescriptorset.iterator(); methodit.hasNext(); ) {
1344 MethodDescriptor currmd = (MethodDescriptor) methodit.next();
1345 /* Need correct number of parameters */
1346 if (con.numArgs() != currmd.numParameters())
1348 for (int i = 0; i < con.numArgs(); i++) {
1349 if (!typeutil.isSuperorType(currmd.getParamType(i), tdarray[i]))
1350 continue NextMethod;
1352 /* Local allocations can't call global allocator */
1353 if (!con.isGlobal() && currmd.isGlobal())
1356 /* Method okay so far */
1360 if (typeutil.isMoreSpecific(currmd, bestmd)) {
1362 } else if (con.isGlobal() && match(currmd, bestmd)) {
1363 if (currmd.isGlobal() && !bestmd.isGlobal())
1365 else if (currmd.isGlobal() && bestmd.isGlobal())
1367 } else if (!typeutil.isMoreSpecific(bestmd, currmd)) {
1368 throw new Error("No method is most specific:" + bestmd + " and " + currmd);
1371 /* Is this more specific than bestmd */
1374 if (bestmd == null) {
1375 throw new Error("No method found for " + con.printNode(0) + " in " + md);
1377 con.setConstructor(bestmd);
1382 /** Check to see if md1 is the same specificity as md2.*/
1384 boolean match(MethodDescriptor md1, MethodDescriptor md2) {
1385 /* Checks if md1 is more specific than md2 */
1386 if (md1.numParameters()!=md2.numParameters())
1388 for(int i=0; i<md1.numParameters(); i++) {
1389 if (!md2.getParamType(i).equals(md1.getParamType(i)))
1392 if (!md2.getReturnType().equals(md1.getReturnType()))
1395 if (!md2.getClassDesc().equals(md1.getClassDesc()))
1403 ExpressionNode translateNameDescriptorintoExpression(NameDescriptor nd, int numLine) {
1404 String id=nd.getIdentifier();
1405 NameDescriptor base=nd.getBase();
1407 NameNode nn=new NameNode(nd);
1408 nn.setNumLine(numLine);
1411 FieldAccessNode fan=new FieldAccessNode(translateNameDescriptorintoExpression(base,numLine),id);
1412 fan.setNumLine(numLine);
1417 MethodDescriptor recurseSurroundingClassesM( ClassDescriptor icd, String varname ) {
1418 if( null == icd || false == icd.isInnerClass() )
1421 ClassDescriptor surroundingDesc = icd.getSurroundingDesc();
1422 if( null == surroundingDesc )
1425 SymbolTable methodTable = surroundingDesc.getMethodTable();
1426 MethodDescriptor md = ( MethodDescriptor ) methodTable.get( varname );
1429 return recurseSurroundingClassesM( surroundingDesc, varname );
1432 ExpressionNode methodInvocationExpression( ClassDescriptor icd, MethodDescriptor md, int linenum ) {
1433 ClassDescriptor cd = md.getClassDesc();
1435 int startingDepth = icd.getInnerDepth();
1437 if( true == cd.isInnerClass() )
1438 depth = cd.getInnerDepth();
1440 String composed = "this";
1441 NameDescriptor runningDesc = new NameDescriptor( "this" );;
1443 for ( int index = startingDepth; index > depth; --index ) {
1444 composed = "this$" + String.valueOf( index - 1 );
1445 runningDesc = new NameDescriptor( runningDesc, composed );
1447 if( false == cd.isInnerClass() )
1448 runningDesc = new NameDescriptor( runningDesc, "this$" + String.valueOf(0) ); //all the way up.
1450 return new NameNode(runningDesc);
1453 void checkMethodInvokeNode(Descriptor md, SymbolTable nametable, MethodInvokeNode min, TypeDescriptor td) {
1454 /*Typecheck subexpressions
1455 and get types for expressions*/
1457 boolean isstatic = false;
1458 if((md instanceof MethodDescriptor) && ((MethodDescriptor)md).isStatic()) {
1461 TypeDescriptor[] tdarray=new TypeDescriptor[min.numArgs()];
1462 for(int i=0; i<min.numArgs(); i++) {
1463 ExpressionNode en=min.getArg(i);
1464 checkExpressionNode(md,nametable,en,null);
1465 tdarray[i]=en.getType();
1467 if(en.getType().isClass() && en.getType().getClassDesc().isEnum()) {
1468 tdarray[i] = new TypeDescriptor(TypeDescriptor.INT);
1471 TypeDescriptor typetolookin=null;
1472 if (min.getExpression()!=null) {
1473 checkExpressionNode(md,nametable,min.getExpression(),null);
1474 typetolookin=min.getExpression().getType();
1475 } else if (min.getBaseName()!=null) {
1476 String rootname=min.getBaseName().getRoot();
1477 if (rootname.equals("super")) {
1478 ClassDescriptor supercd=((MethodDescriptor)md).getClassDesc().getSuperDesc();
1479 typetolookin=new TypeDescriptor(supercd);
1481 } else if (rootname.equals("this")) {
1483 throw new Error("use this object in static method md = "+ md.toString());
1485 ClassDescriptor cd=((MethodDescriptor)md).getClassDesc();
1486 typetolookin=new TypeDescriptor(cd);
1487 } else if (nametable.get(rootname)!=null) {
1488 //we have an expression
1489 min.setExpression(translateNameDescriptorintoExpression(min.getBaseName(),min.getNumLine()));
1490 checkExpressionNode(md, nametable, min.getExpression(), null);
1491 typetolookin=min.getExpression().getType();
1493 if(!min.getBaseName().getSymbol().equals("System.out")||state.JNI) {
1494 ExpressionNode nn = translateNameDescriptorintoExpression(min.getBaseName(),min.getNumLine());
1495 checkExpressionNode(md, nametable, nn, null);
1496 typetolookin = nn.getType();
1497 if(!((nn.kind()== Kind.NameNode) && (((NameNode)nn).getField() == null)
1498 && (((NameNode)nn).getVar() == null) && (((NameNode)nn).getExpression() == null))) {
1499 // this is not a pure class name, need to add to
1500 min.setExpression(nn);
1504 ClassDescriptor cd = getClass(null, "System");
1507 throw new Error("md = "+ md.toString()+ " "+min.getBaseName()+" undefined");
1508 typetolookin=new TypeDescriptor(cd);
1511 } else if ((md instanceof MethodDescriptor)&&min.getMethodName().equals("super")) {
1512 ClassDescriptor supercd=((MethodDescriptor)md).getClassDesc().getSuperDesc();
1513 min.methodid=supercd.getSymbol();
1514 typetolookin=new TypeDescriptor(supercd);
1515 } else if (md instanceof MethodDescriptor) {
1516 typetolookin=new TypeDescriptor(((MethodDescriptor)md).getClassDesc());
1518 /* If this a task descriptor we throw an error at this point */
1519 throw new Error("Unknown method call to "+min.getMethodName()+"in task"+md.getSymbol());
1521 if (!typetolookin.isClass())
1522 throw new Error("Error with method call to "+min.getMethodName()+" in class "+typetolookin);
1523 ClassDescriptor classtolookin=typetolookin.getClassDesc();
1524 checkClass(classtolookin, REFERENCE);
1525 Set methoddescriptorset=classtolookin.getMethodTable().getSet(min.getMethodName());
1526 MethodDescriptor bestmd=null;
1528 for(Iterator methodit=methoddescriptorset.iterator(); methodit.hasNext(); ) {
1529 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
1530 /* Need correct number of parameters */
1531 if (min.numArgs()!=currmd.numParameters())
1533 for(int i=0; i<min.numArgs(); i++) {
1534 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
1535 if(((!tdarray[i].isArray() &&(tdarray[i].isInt() || tdarray[i].isLong()))
1536 && currmd.getParamType(i).isClass() && currmd.getParamType(i).getClassDesc().getSymbol().equals("Object"))) {
1537 // primitive parameters vs object
1539 continue NextMethod;
1542 /* Method okay so far */
1546 if (typeutil.isMoreSpecific(currmd,bestmd)) {
1548 } else if (!typeutil.isMoreSpecific(bestmd, currmd))
1549 throw new Error("No method is most specific:"+bestmd+" and "+currmd);
1551 /* Is this more specific than bestmd */
1555 // if this is an inner class, need to check the method table for the surrounding class
1556 bestmd = recurseSurroundingClassesM(classtolookin, min.getMethodName());
1558 throw new Error("No method found for :"+min.printNode(0)+" in class: " + classtolookin+" in "+md);
1560 // set the correct "this" expression here
1561 ExpressionNode en=methodInvocationExpression(classtolookin, bestmd, min.getNumLine());
1562 min.setExpression(en);
1563 checkExpressionNode(md, nametable, min.getExpression(), null);
1566 min.setMethod(bestmd);
1568 if ((td!=null)&&(min.getType()!=null)&&!typeutil.isSuperorType(td, min.getType()))
1569 throw new Error(min.getType()+ " is not equal to or a subclass of "+td);
1570 /* Check whether we need to set this parameter to implied this */
1571 if (!isstatic && !bestmd.isStatic()) {
1572 if (min.getExpression()==null) {
1573 ExpressionNode en=new NameNode(new NameDescriptor("this"));
1574 min.setExpression(en);
1575 checkExpressionNode(md, nametable, min.getExpression(), null);
1579 /* Check if we need to wrap primitive paratmeters to objects */
1580 for(int i=0; i<min.numArgs(); i++) {
1581 if(!tdarray[i].isArray() && (tdarray[i].isInt() || tdarray[i].isLong())
1582 && min.getMethod().getParamType(i).isClass() && min.getMethod().getParamType(i).getClassDesc().getSymbol().equals("Object")) {
1583 // Shall wrap this primitive parameter as a object
1584 ExpressionNode exp = min.getArg(i);
1585 TypeDescriptor ptd = null;
1586 NameDescriptor nd=null;
1587 if(exp.getType().isInt()) {
1588 nd = new NameDescriptor("Integer");
1589 ptd = state.getTypeDescriptor(nd);
1590 } else if(exp.getType().isLong()) {
1591 nd = new NameDescriptor("Long");
1592 ptd = state.getTypeDescriptor(nd);
1594 boolean isglobal = false;
1595 String disjointId = null;
1596 CreateObjectNode con=new CreateObjectNode(ptd, isglobal, disjointId);
1597 con.addArgument(exp);
1598 checkExpressionNode(md, nametable, con, null);
1599 min.setArgument(con, i);
1605 void checkOpNode(Descriptor md, SymbolTable nametable, OpNode on, TypeDescriptor td) {
1606 checkExpressionNode(md, nametable, on.getLeft(), null);
1607 if (on.getRight()!=null)
1608 checkExpressionNode(md, nametable, on.getRight(), null);
1609 TypeDescriptor ltd=on.getLeft().getType();
1610 TypeDescriptor rtd=on.getRight()!=null?on.getRight().getType():null;
1611 TypeDescriptor lefttype=null;
1612 TypeDescriptor righttype=null;
1613 Operation op=on.getOp();
1615 switch(op.getOp()) {
1616 case Operation.LOGIC_OR:
1617 case Operation.LOGIC_AND:
1618 if (!(rtd.isBoolean()))
1620 on.setRightType(rtd);
1622 case Operation.LOGIC_NOT:
1623 if (!(ltd.isBoolean()))
1626 on.setLeftType(ltd);
1628 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1631 case Operation.COMP:
1632 // 5.6.2 Binary Numeric Promotion
1633 //TODO unboxing of reference objects
1636 else if (ltd.isFloat())
1638 else if (ltd.isLong())
1639 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1641 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1642 on.setLeftType(lefttype);
1643 on.setType(lefttype);
1646 case Operation.BIT_OR:
1647 case Operation.BIT_XOR:
1648 case Operation.BIT_AND:
1649 // 5.6.2 Binary Numeric Promotion
1650 //TODO unboxing of reference objects
1651 if (ltd.isDouble()||rtd.isDouble())
1653 else if (ltd.isFloat()||rtd.isFloat())
1655 else if (ltd.isLong()||rtd.isLong())
1656 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1657 // 090205 hack for boolean
1658 else if (ltd.isBoolean()||rtd.isBoolean())
1659 lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
1661 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1664 on.setLeftType(lefttype);
1665 on.setRightType(righttype);
1666 on.setType(lefttype);
1669 case Operation.ISAVAILABLE:
1670 if (!(ltd.isPtr())) {
1671 throw new Error("Can't use isavailable on non-pointers/non-parameters.");
1674 on.setLeftType(lefttype);
1675 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1678 case Operation.EQUAL:
1679 case Operation.NOTEQUAL:
1680 // 5.6.2 Binary Numeric Promotion
1681 //TODO unboxing of reference objects
1682 if (ltd.isBoolean()||rtd.isBoolean()) {
1683 if (!(ltd.isBoolean()&&rtd.isBoolean()))
1685 righttype=lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
1686 } else if (ltd.isPtr()||rtd.isPtr()) {
1687 if (!(ltd.isPtr()&&rtd.isPtr())) {
1694 } else if (ltd.isDouble()||rtd.isDouble())
1695 righttype=lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
1696 else if (ltd.isFloat()||rtd.isFloat())
1697 righttype=lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
1698 else if (ltd.isLong()||rtd.isLong())
1699 righttype=lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1701 righttype=lefttype=new TypeDescriptor(TypeDescriptor.INT);
1703 on.setLeftType(lefttype);
1704 on.setRightType(righttype);
1705 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1714 // 5.6.2 Binary Numeric Promotion
1715 //TODO unboxing of reference objects
1716 if (!ltd.isNumber()||!rtd.isNumber()) {
1717 if (!ltd.isNumber())
1718 throw new Error("Leftside is not number"+on.printNode(0)+"type="+ltd.toPrettyString());
1719 if (!rtd.isNumber())
1720 throw new Error("Rightside is not number"+on.printNode(0));
1723 if (ltd.isDouble()||rtd.isDouble())
1724 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
1725 else if (ltd.isFloat()||rtd.isFloat())
1726 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
1727 else if (ltd.isLong()||rtd.isLong())
1728 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1730 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1732 on.setLeftType(lefttype);
1733 on.setRightType(righttype);
1734 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1738 if (ltd.isString()||rtd.isString()) {
1739 ClassDescriptor stringcl=getClass(null, TypeUtil.StringClass);
1740 TypeDescriptor stringtd=new TypeDescriptor(stringcl);
1741 NameDescriptor nd=new NameDescriptor("String");
1742 NameDescriptor valuend=new NameDescriptor(nd, "valueOf");
1743 if (!(ltd.isString()&&(on.getLeft() instanceof OpNode))) {
1744 MethodInvokeNode leftmin=new MethodInvokeNode(valuend);
1745 leftmin.setNumLine(on.getLeft().getNumLine());
1746 leftmin.addArgument(on.getLeft());
1748 checkExpressionNode(md, nametable, on.getLeft(), null);
1751 if (!(rtd.isString()&&(on.getRight() instanceof OpNode))) {
1752 MethodInvokeNode rightmin=new MethodInvokeNode(valuend);
1753 rightmin.setNumLine(on.getRight().getNumLine());
1754 rightmin.addArgument(on.getRight());
1756 checkExpressionNode(md, nametable, on.getRight(), null);
1759 on.setLeftType(stringtd);
1760 on.setRightType(stringtd);
1761 on.setType(stringtd);
1766 case Operation.MULT:
1769 // 5.6.2 Binary Numeric Promotion
1770 //TODO unboxing of reference objects
1771 if (ltd.isArray()||rtd.isArray()||!ltd.isNumber()||!rtd.isNumber())
1772 throw new Error("Error in "+on.printNode(0));
1774 if (ltd.isDouble()||rtd.isDouble())
1775 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
1776 else if (ltd.isFloat()||rtd.isFloat())
1777 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
1778 else if (ltd.isLong()||rtd.isLong())
1779 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1781 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1783 on.setLeftType(lefttype);
1784 on.setRightType(righttype);
1785 on.setType(lefttype);
1788 case Operation.LEFTSHIFT:
1789 case Operation.RIGHTSHIFT:
1790 case Operation.URIGHTSHIFT:
1791 if (!rtd.isIntegerType())
1793 //5.6.1 Unary Numeric Promotion
1794 if (rtd.isByte()||rtd.isShort()||rtd.isInt())
1795 righttype=new TypeDescriptor(TypeDescriptor.INT);
1799 on.setRightType(righttype);
1800 if (!ltd.isIntegerType())
1803 case Operation.UNARYPLUS:
1804 case Operation.UNARYMINUS:
1805 /* case Operation.POSTINC:
1806 case Operation.POSTDEC:
1807 case Operation.PREINC:
1808 case Operation.PREDEC:*/
1809 if (!ltd.isNumber())
1811 //5.6.1 Unary Numeric Promotion
1812 if (ltd.isByte()||ltd.isShort()||ltd.isInt())
1813 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1816 on.setLeftType(lefttype);
1817 on.setType(lefttype);
1821 throw new Error(op.toString());
1825 if (!typeutil.isSuperorType(td, on.getType())) {
1826 throw new Error("Type of rside not compatible with type of lside"+on.printNode(0));