7 public class SemanticCheck {
12 HashMap<ClassDescriptor, Integer> completed;
13 HashMap<ClassDescriptor, Vector<VarDescriptor>> inlineClass2LiveVars;
14 boolean trialcheck = false;
15 Vector<ClassDescriptor> inlineClassWithTrialCheck;
17 public static final int NOCHECK=0;
18 public static final int REFERENCE=1;
19 public static final int INIT=2;
23 public boolean hasLayout(ClassDescriptor cd) {
24 return completed.get(cd)!=null&&completed.get(cd)==INIT;
27 public SemanticCheck(State state, TypeUtil tu) {
28 this(state, tu, true);
31 public SemanticCheck(State state, TypeUtil tu, boolean checkAll) {
34 this.loopstack=new Stack();
35 this.toanalyze=new HashSet();
36 this.completed=new HashMap<ClassDescriptor, Integer>();
37 this.checkAll=checkAll;
38 this.inlineClass2LiveVars = new HashMap<ClassDescriptor, Vector<VarDescriptor>>();
39 this.inlineClassWithTrialCheck = new Vector<ClassDescriptor>();
42 public ClassDescriptor getClass(ClassDescriptor context, String classname) {
43 return getClass(context, classname, INIT);
45 public ClassDescriptor getClass(ClassDescriptor context, String classnameIn, int fullcheck) {
46 ClassDescriptor cd=typeutil.getClass(context, classnameIn, toanalyze);
47 checkClass(cd, fullcheck);
51 public void checkClass(ClassDescriptor cd) {
55 public void checkClass(ClassDescriptor cd, int fullcheck) {
56 if (!completed.containsKey(cd)||completed.get(cd)<fullcheck) {
57 int oldstatus=completed.containsKey(cd)?completed.get(cd):0;
58 completed.put(cd, fullcheck);
60 if (fullcheck>=REFERENCE&&oldstatus<INIT) {
61 //Set superclass link up
62 if (cd.getSuper()!=null) {
63 ClassDescriptor superdesc=getClass(cd, cd.getSuper(), fullcheck);
64 if (superdesc.isInnerClass()) {
67 if (superdesc.isInterface()) {
70 cd.getSuperInterface().add(superdesc.getSymbol());
72 throw new Error("Error! Class " + cd.getSymbol() + " extends interface " + cd.getSuper());
75 cd.setSuperDesc(superdesc);
77 // Link together Field, Method, and Flag tables so classes
78 // inherit these from their superclasses
79 if (oldstatus<REFERENCE) {
80 cd.getFieldTable().setParent(cd.getSuperDesc().getFieldTable());
81 cd.getMethodTable().setParent(cd.getSuperDesc().getMethodTable());
82 cd.getFlagTable().setParent(cd.getSuperDesc().getFlagTable());
86 // Link together Field, Method tables do classes inherit these from
87 // their ancestor interfaces
88 Vector<String> sifv = cd.getSuperInterface();
89 for(int i = 0; i < sifv.size(); i++) {
90 ClassDescriptor superif = getClass(cd, sifv.elementAt(i), fullcheck);
91 if(!superif.isInterface()) {
92 throw new Error("Error! Class " + cd.getSymbol() + " implements non-interface " + superif.getSymbol());
94 if (oldstatus<REFERENCE) {
95 cd.addSuperInterfaces(superif);
96 cd.getMethodTable().addParentIF(superif.getMethodTable());
97 cd.getFieldTable().addParentIF(superif.getFieldTable());
101 if (oldstatus<INIT&&fullcheck>=INIT) {
102 /* Check to see that fields are well typed */
103 for(Iterator field_it=cd.getFields(); field_it.hasNext(); ) {
104 FieldDescriptor fd=(FieldDescriptor)field_it.next();
108 System.out.println("Class/Field in "+cd+":"+fd);
112 for(Iterator method_it=cd.getMethods(); method_it.hasNext(); ) {
113 MethodDescriptor md=(MethodDescriptor)method_it.next();
117 System.out.println("Class/Method in "+cd+":"+md);
125 public void semanticCheckClass(ClassDescriptor cd) {
126 // need to initialize typeutil object here...only place we can
127 // get class descriptors without first calling getclass
128 getClass(cd, cd.getSymbol());
131 // for inline defined anonymous classes, we need to check its
132 // surrounding class first to get its surrounding context
133 ClassDescriptor surroundingcd = cd.getSurroundingDesc();
134 if(toanalyze.contains(surroundingcd)) {
135 toanalyze.remove(surroundingcd);
136 semanticCheckClass(surroundingcd);
140 for (Iterator method_it = cd.getMethods(); method_it.hasNext(); ) {
141 MethodDescriptor md = (MethodDescriptor) method_it.next();
143 checkMethodBody(cd, md);
145 System.out.println("Error in " + md);
151 public void semanticCheck() {
152 SymbolTable classtable = state.getClassSymbolTable();
153 toanalyze.addAll(classtable.getValueSet());
154 toanalyze.addAll(state.getTaskSymbolTable().getValueSet());
157 while (!toanalyze.isEmpty()) {
158 Object obj = toanalyze.iterator().next();
159 if (obj instanceof TaskDescriptor) {
160 toanalyze.remove(obj);
161 TaskDescriptor td = (TaskDescriptor) obj;
165 System.out.println("Error in " + td);
169 ClassDescriptor cd = (ClassDescriptor) obj;
170 toanalyze.remove(cd);
171 semanticCheckClass(cd);
176 private void checkTypeDescriptor(ClassDescriptor cd, TypeDescriptor td) {
177 if (td.isPrimitive())
179 else if (td.isClass()) {
180 String name=td.toString();
181 ClassDescriptor field_cd=checkAll?getClass(cd, name):getClass(cd, name, REFERENCE);
184 throw new Error("Undefined class "+name);
185 td.setClassDescriptor(field_cd);
187 } else if (td.isTag())
193 public void checkField(ClassDescriptor cd, FieldDescriptor fd) {
194 checkTypeDescriptor(cd, fd.getType());
197 public void checkConstraintCheck(TaskDescriptor td, SymbolTable nametable, Vector ccs) {
199 return; /* No constraint checks to check */
200 for(int i=0; i<ccs.size(); i++) {
201 ConstraintCheck cc=(ConstraintCheck) ccs.get(i);
203 for(int j=0; j<cc.numArgs(); j++) {
204 ExpressionNode en=cc.getArg(j);
205 checkExpressionNode(td,nametable,en,null);
210 public void checkFlagEffects(TaskDescriptor td, Vector vfe, SymbolTable nametable) {
212 return; /* No flag effects to check */
213 for(int i=0; i<vfe.size(); i++) {
214 FlagEffects fe=(FlagEffects) vfe.get(i);
215 String varname=fe.getName();
216 //Make sure the variable is declared as a parameter to the task
217 VarDescriptor vd=(VarDescriptor)td.getParameterTable().get(varname);
219 throw new Error("Parameter "+varname+" in Flag Effects not declared in "+td);
222 //Make sure it correspods to a class
223 TypeDescriptor type_d=vd.getType();
224 if (!type_d.isClass())
225 throw new Error("Cannot have non-object argument for flag_effect");
227 ClassDescriptor cd=type_d.getClassDesc();
228 for(int j=0; j<fe.numEffects(); j++) {
229 FlagEffect flag=fe.getEffect(j);
230 String name=flag.getName();
231 FlagDescriptor flag_d=(FlagDescriptor)cd.getFlagTable().get(name);
232 //Make sure the flag is declared
234 throw new Error("Flag descriptor "+name+" undefined in class: "+cd.getSymbol());
235 if (flag_d.getExternal())
236 throw new Error("Attempting to modify external flag: "+name);
237 flag.setFlag(flag_d);
239 for(int j=0; j<fe.numTagEffects(); j++) {
240 TagEffect tag=fe.getTagEffect(j);
241 String name=tag.getName();
243 Descriptor d=(Descriptor)nametable.get(name);
245 throw new Error("Tag descriptor "+name+" undeclared");
246 else if (!(d instanceof TagVarDescriptor))
247 throw new Error(name+" is not a tag descriptor");
248 tag.setTag((TagVarDescriptor)d);
253 public void checkTask(TaskDescriptor td) {
254 for(int i=0; i<td.numParameters(); i++) {
255 /* Check that parameter is well typed */
256 TypeDescriptor param_type=td.getParamType(i);
257 checkTypeDescriptor(null, param_type);
259 /* Check the parameter's flag expression is well formed */
260 FlagExpressionNode fen=td.getFlag(td.getParameter(i));
261 if (!param_type.isClass())
262 throw new Error("Cannot have non-object argument to a task");
263 ClassDescriptor cd=param_type.getClassDesc();
265 checkFlagExpressionNode(cd, fen);
268 checkFlagEffects(td, td.getFlagEffects(),td.getParameterTable());
269 /* Check that the task code is valid */
270 BlockNode bn=state.getMethodBody(td);
271 checkBlockNode(td, td.getParameterTable(),bn);
274 public void checkFlagExpressionNode(ClassDescriptor cd, FlagExpressionNode fen) {
276 case Kind.FlagOpNode:
278 FlagOpNode fon=(FlagOpNode)fen;
279 checkFlagExpressionNode(cd, fon.getLeft());
280 if (fon.getRight()!=null)
281 checkFlagExpressionNode(cd, fon.getRight());
287 FlagNode fn=(FlagNode)fen;
288 String name=fn.getFlagName();
289 FlagDescriptor fd=(FlagDescriptor)cd.getFlagTable().get(name);
291 throw new Error("Undeclared flag: "+name);
297 throw new Error("Unrecognized FlagExpressionNode");
301 public void checkMethod(ClassDescriptor cd, MethodDescriptor md) {
302 /* Check for abstract methods */
303 if(md.isAbstract()) {
304 if(!cd.isAbstract() && !cd.isInterface()) {
305 throw new Error("Error! The non-abstract Class " + cd.getSymbol() + " contains an abstract method " + md.getSymbol());
309 /* Check return type */
310 if (!md.isConstructor() && !md.isStaticBlock())
311 if (!md.getReturnType().isVoid()) {
312 checkTypeDescriptor(cd, md.getReturnType());
314 for(int i=0; i<md.numParameters(); i++) {
315 TypeDescriptor param_type=md.getParamType(i);
316 checkTypeDescriptor(cd, param_type);
318 /* Link the naming environments */
319 if (!md.isStatic()) /* Fields aren't accessible directly in a static method, so don't link in this table */
320 md.getParameterTable().setParent(cd.getFieldTable());
322 if (!md.isStatic()) {
323 VarDescriptor thisvd=new VarDescriptor(new TypeDescriptor(cd),"this");
326 if(md.isDefaultConstructor() && (cd.getSuperDesc() != null) && !cd.getInline()) {
327 // add the construction of it super class, can only be super()
328 // NOTE: inline class should be treated differently
329 NameDescriptor nd=new NameDescriptor("super");
330 MethodInvokeNode min=new MethodInvokeNode(nd);
331 BlockExpressionNode ben=new BlockExpressionNode(min);
332 BlockNode bn = state.getMethodBody(md);
333 bn.addFirstBlockStatement(ben);
337 public void checkMethodBody(ClassDescriptor cd, MethodDescriptor md) {
338 ClassDescriptor superdesc=cd.getSuperDesc();
339 // for inline classes, it has done this during trial check
340 if ((!cd.getInline() || !this.inlineClassWithTrialCheck.contains(cd)) && (superdesc!=null)) {
341 Set possiblematches=superdesc.getMethodTable().getSet(md.getSymbol());
342 for(Iterator methodit=possiblematches.iterator(); methodit.hasNext(); ) {
343 MethodDescriptor matchmd=(MethodDescriptor)methodit.next();
344 if (md.matches(matchmd)) {
345 if (matchmd.getModifiers().isFinal()) {
346 throw new Error("Try to override final method in method:"+md+" declared in "+cd);
351 BlockNode bn=state.getMethodBody(md);
352 checkBlockNode(md, md.getParameterTable(),bn);
355 public void checkBlockNode(Descriptor md, SymbolTable nametable, BlockNode bn) {
356 /* Link in the naming environment */
357 bn.getVarTable().setParent(nametable);
358 for(int i=0; i<bn.size(); i++) {
359 BlockStatementNode bsn=bn.get(i);
360 checkBlockStatementNode(md, bn.getVarTable(),bsn);
364 public void checkBlockStatementNode(Descriptor md, SymbolTable nametable, BlockStatementNode bsn) {
366 case Kind.BlockExpressionNode:
367 checkBlockExpressionNode(md, nametable,(BlockExpressionNode)bsn);
370 case Kind.DeclarationNode:
371 checkDeclarationNode(md, nametable, (DeclarationNode)bsn);
374 case Kind.TagDeclarationNode:
375 checkTagDeclarationNode(md, nametable, (TagDeclarationNode)bsn);
378 case Kind.IfStatementNode:
379 checkIfStatementNode(md, nametable, (IfStatementNode)bsn);
382 case Kind.SwitchStatementNode:
383 checkSwitchStatementNode(md, nametable, (SwitchStatementNode)bsn);
387 checkLoopNode(md, nametable, (LoopNode)bsn);
390 case Kind.ReturnNode:
391 checkReturnNode(md, nametable, (ReturnNode)bsn);
394 case Kind.TaskExitNode:
395 checkTaskExitNode(md, nametable, (TaskExitNode)bsn);
398 case Kind.SubBlockNode:
399 checkSubBlockNode(md, nametable, (SubBlockNode)bsn);
402 case Kind.AtomicNode:
403 checkAtomicNode(md, nametable, (AtomicNode)bsn);
406 case Kind.SynchronizedNode:
407 checkSynchronizedNode(md, nametable, (SynchronizedNode)bsn);
410 case Kind.ContinueBreakNode:
411 checkContinueBreakNode(md, nametable, (ContinueBreakNode) bsn);
415 case Kind.GenReachNode:
416 case Kind.GenDefReachNode:
417 // do nothing, no semantic check for SESEs
424 void checkBlockExpressionNode(Descriptor md, SymbolTable nametable, BlockExpressionNode ben) {
425 checkExpressionNode(md, nametable, ben.getExpression(), null);
428 void checkDeclarationNode(Descriptor md, SymbolTable nametable, DeclarationNode dn) {
429 VarDescriptor vd=dn.getVarDescriptor();
430 checkTypeDescriptor(((md instanceof MethodDescriptor)?((MethodDescriptor)md).getClassDesc():null), vd.getType());
431 Descriptor d=nametable.get(vd.getSymbol());
433 (d instanceof FieldDescriptor)) {
435 } else if((md instanceof MethodDescriptor) && (((MethodDescriptor)md).getClassDesc().getInline()) && this.inlineClassWithTrialCheck.contains(((MethodDescriptor)md).getClassDesc())) {
436 // for inline classes, the var has been checked during trial check and added into the nametable
438 throw new Error(vd.getSymbol()+" in "+md+" defined a second time");
439 if (dn.getExpression()!=null)
440 checkExpressionNode(md, nametable, dn.getExpression(), vd.getType());
443 void checkTagDeclarationNode(Descriptor md, SymbolTable nametable, TagDeclarationNode dn) {
444 TagVarDescriptor vd=dn.getTagVarDescriptor();
445 Descriptor d=nametable.get(vd.getSymbol());
447 (d instanceof FieldDescriptor)) {
449 } else if((md instanceof MethodDescriptor) && (((MethodDescriptor)md).getClassDesc().getInline()) && this.inlineClassWithTrialCheck.contains(((MethodDescriptor)md).getClassDesc())) {
450 // for inline classes, the var has been checked during trial check and added into the nametable
452 throw new Error(vd.getSymbol()+" defined a second time");
455 void checkSubBlockNode(Descriptor md, SymbolTable nametable, SubBlockNode sbn) {
456 checkBlockNode(md, nametable, sbn.getBlockNode());
459 void checkAtomicNode(Descriptor md, SymbolTable nametable, AtomicNode sbn) {
460 checkBlockNode(md, nametable, sbn.getBlockNode());
463 void checkSynchronizedNode(Descriptor md, SymbolTable nametable, SynchronizedNode sbn) {
464 checkBlockNode(md, nametable, sbn.getBlockNode());
465 //todo this could be Object
466 checkExpressionNode(md, nametable, sbn.getExpr(), null);
469 void checkContinueBreakNode(Descriptor md, SymbolTable nametable, ContinueBreakNode cbn) {
470 if (loopstack.empty())
471 throw new Error("continue/break outside of loop");
472 Object o = loopstack.peek();
473 if(o instanceof LoopNode) {
474 LoopNode ln=(LoopNode)o;
479 void checkReturnNode(Descriptor d, SymbolTable nametable, ReturnNode rn) {
480 if (d instanceof TaskDescriptor)
481 throw new Error("Illegal return appears in Task: "+d.getSymbol());
482 MethodDescriptor md=(MethodDescriptor)d;
483 if (rn.getReturnExpression()!=null)
484 if (md.getReturnType()==null)
485 throw new Error("Constructor can't return something.");
486 else if (md.getReturnType().isVoid())
487 throw new Error(md+" is void");
489 checkExpressionNode(md, nametable, rn.getReturnExpression(), md.getReturnType());
491 if (md.getReturnType()!=null&&!md.getReturnType().isVoid())
492 throw new Error("Need to return something for "+md);
495 void checkTaskExitNode(Descriptor md, SymbolTable nametable, TaskExitNode ten) {
496 if (md instanceof MethodDescriptor)
497 throw new Error("Illegal taskexit appears in Method: "+md.getSymbol());
498 checkFlagEffects((TaskDescriptor)md, ten.getFlagEffects(),nametable);
499 checkConstraintCheck((TaskDescriptor) md, nametable, ten.getChecks());
502 void checkIfStatementNode(Descriptor md, SymbolTable nametable, IfStatementNode isn) {
503 checkExpressionNode(md, nametable, isn.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
504 checkBlockNode(md, nametable, isn.getTrueBlock());
505 if (isn.getFalseBlock()!=null)
506 checkBlockNode(md, nametable, isn.getFalseBlock());
509 void checkSwitchStatementNode(Descriptor md, SymbolTable nametable, SwitchStatementNode ssn) {
510 checkExpressionNode(md, nametable, ssn.getCondition(), new TypeDescriptor(TypeDescriptor.INT));
512 BlockNode sbn = ssn.getSwitchBody();
513 boolean hasdefault = false;
514 for(int i = 0; i < sbn.size(); i++) {
515 boolean containdefault = checkSwitchBlockNode(md, nametable, (SwitchBlockNode)sbn.get(i));
516 if(hasdefault && containdefault) {
517 throw new Error("Error: duplicate default branch in switch-case statement in Method: " + md.getSymbol());
519 hasdefault = containdefault;
523 boolean checkSwitchBlockNode(Descriptor md, SymbolTable nametable, SwitchBlockNode sbn) {
524 Vector<SwitchLabelNode> slnv = sbn.getSwitchConditions();
526 for(int i = 0; i < slnv.size(); i++) {
527 if(slnv.elementAt(i).isdefault) {
530 checkConstantExpressionNode(md, nametable, slnv.elementAt(i).getCondition(), new TypeDescriptor(TypeDescriptor.INT));
534 throw new Error("Error: duplicate default branch in switch-case statement in Method: " + md.getSymbol());
537 checkBlockNode(md, nametable, sbn.getSwitchBlockStatement());
539 return (defaultb > 0);
543 void checkConstantExpressionNode(Descriptor md, SymbolTable nametable, ExpressionNode en, TypeDescriptor td) {
545 case Kind.FieldAccessNode:
546 checkFieldAccessNode(md,nametable,(FieldAccessNode)en,td);
549 case Kind.LiteralNode:
550 checkLiteralNode(md,nametable,(LiteralNode)en,td);
554 checkNameNode(md,nametable,(NameNode)en,td);
558 checkOpNode(md, nametable, (OpNode)en, td);
564 void checkExpressionNode(Descriptor md, SymbolTable nametable, ExpressionNode en, TypeDescriptor td) {
566 case Kind.AssignmentNode:
567 checkAssignmentNode(md,nametable,(AssignmentNode)en,td);
571 checkCastNode(md,nametable,(CastNode)en,td);
574 case Kind.CreateObjectNode:
575 checkCreateObjectNode(md,nametable,(CreateObjectNode)en,td);
578 case Kind.FieldAccessNode:
579 checkFieldAccessNode(md,nametable,(FieldAccessNode)en,td);
582 case Kind.ArrayAccessNode:
583 checkArrayAccessNode(md,nametable,(ArrayAccessNode)en,td);
586 case Kind.LiteralNode:
587 checkLiteralNode(md,nametable,(LiteralNode)en,td);
590 case Kind.MethodInvokeNode:
591 checkMethodInvokeNode(md,nametable,(MethodInvokeNode)en,td);
595 checkNameNode(md,nametable,(NameNode)en,td);
599 checkOpNode(md,nametable,(OpNode)en,td);
602 case Kind.OffsetNode:
603 checkOffsetNode(md, nametable, (OffsetNode)en, td);
606 case Kind.TertiaryNode:
607 checkTertiaryNode(md, nametable, (TertiaryNode)en, td);
610 case Kind.InstanceOfNode:
611 checkInstanceOfNode(md, nametable, (InstanceOfNode) en, td);
614 case Kind.ArrayInitializerNode:
615 checkArrayInitializerNode(md, nametable, (ArrayInitializerNode) en, td);
618 case Kind.ClassTypeNode:
619 checkClassTypeNode(md, nametable, (ClassTypeNode) en, td);
625 void checkClassTypeNode(Descriptor md, SymbolTable nametable, ClassTypeNode tn, TypeDescriptor td) {
626 checkTypeDescriptor(((md instanceof MethodDescriptor)?((MethodDescriptor)md).getClassDesc():null), tn.getType());
629 void checkCastNode(Descriptor md, SymbolTable nametable, CastNode cn, TypeDescriptor td) {
630 /* Get type descriptor */
631 if (cn.getType()==null) {
632 NameDescriptor typenamed=cn.getTypeName().getName();
633 TypeDescriptor ntd=new TypeDescriptor(getClass(((md instanceof MethodDescriptor)?((MethodDescriptor)md).getClassDesc():null), typenamed.toString()));
637 /* Check the type descriptor */
638 TypeDescriptor cast_type=cn.getType();
639 checkTypeDescriptor(((md instanceof MethodDescriptor)?((MethodDescriptor)md).getClassDesc():null),cast_type);
643 if (!typeutil.isSuperorType(td,cast_type))
644 throw new Error("Cast node returns "+cast_type+", but need "+td);
647 ExpressionNode en=cn.getExpression();
648 checkExpressionNode(md, nametable, en, null);
649 TypeDescriptor etd=en.getType();
650 if (typeutil.isSuperorType(cast_type,etd)) /* Cast trivially succeeds */
653 if (typeutil.isSuperorType(etd,cast_type)) /* Cast may succeed */
655 if (typeutil.isCastable(etd, cast_type))
658 //rough hack to handle interfaces...should clean up
659 if (etd.isClass()&&cast_type.isClass()) {
660 ClassDescriptor cdetd=etd.getClassDesc();
661 ClassDescriptor cdcast_type=cast_type.getClassDesc();
663 if (cdetd.isInterface()&&!cdcast_type.getModifier().isFinal())
666 if (cdcast_type.isInterface()&&!cdetd.getModifier().isFinal())
669 /* Different branches */
670 /* TODO: change if add interfaces */
671 throw new Error("Cast will always fail\n"+cn.printNode(0));
674 //FieldDescriptor checkFieldAccessNodeForParentNode( Descriptor md, SymbolTable na )
675 void checkFieldAccessNode(Descriptor md, SymbolTable nametable, FieldAccessNode fan, TypeDescriptor td) {
676 ExpressionNode left=fan.getExpression();
677 checkExpressionNode(md,nametable,left,null);
678 TypeDescriptor ltd=left.getType();
680 checkClass(ltd.getClassDesc(), INIT);
681 String fieldname=fan.getFieldName();
683 FieldDescriptor fd=null;
684 if (ltd.isArray()&&fieldname.equals("length"))
685 fd=FieldDescriptor.arrayLength;
686 else if(((left instanceof NameNode) && ((NameNode)left).isSuper())
687 ||((left instanceof FieldAccessNode) && ((FieldAccessNode)left).isSuper())){
688 fd = (FieldDescriptor) ltd.getClassDesc().getSuperDesc().getFieldTable().get(fieldname);
690 fd=(FieldDescriptor) ltd.getClassDesc().getFieldTable().get(fieldname);
692 if(ltd.isClassNameRef()) {
693 // the field access is using a class name directly
695 // check if it is to access a surrounding class in an inner class
696 if(fieldname.equals("this") || fieldname.equals("super")) {
697 ClassDescriptor icd = ((VarDescriptor)nametable.get("this")).getType().getClassDesc();
698 if(icd.isInnerClass()) {
699 NameNode nn = new NameNode(new NameDescriptor("this"));
700 nn.setVar((VarDescriptor)nametable.get("this"));
701 fan.setExpression(nn);
702 if(icd.getSurroundingDesc()==ltd.getClassDesc()) {
703 // this is a surrounding class access inside an inner class
704 fan.setExpression(nn);
705 fan.setFieldName("this$0");
706 fd = (FieldDescriptor)icd.getFieldTable().get("this$0");
707 } else if(icd==ltd.getClassDesc()) {
708 // this is an inner class this operation
709 fd = new FieldDescriptor(new Modifiers(),new TypeDescriptor(icd),"this",null,false);
711 if(fieldname.equals("super")) {
718 ClassDescriptor surroundingCls=ltd.getClassDesc().getSurroundingDesc();
720 while(surroundingCls!=null) {
721 fd=(FieldDescriptor) surroundingCls.getFieldTable().get(fieldname);
723 fan.left=new ClassTypeNode(new TypeDescriptor(surroundingCls));
726 surroundingCls=surroundingCls.getSurroundingDesc();
729 // check if it is to access an enum field
730 Iterator it_enum = ltd.getClassDesc().getEnum();
731 while(it_enum.hasNext()) {
732 ClassDescriptor ecd = (ClassDescriptor)it_enum.next();
733 if(ecd.getSymbol().equals(ltd.getClassDesc().getSymbol()+"$"+fieldname)) {
734 // this is an enum field access
735 if(!ecd.isStatic() && !ecd.getModifier().isStatic() && !ecd.getModifier().isPublic()) {
736 throw new Error(fieldname + " is not a public/static enum field in "+fan.printNode(0)+" in "+md);
738 TypeDescriptor tp = new TypeDescriptor(ecd);
739 tp.setClassNameRef();
740 fd=new FieldDescriptor(ecd.getModifier(), tp, ltd.getClassDesc().getSymbol()+"$"+fieldname, null, false);;
747 if(ltd.getClassDesc().isEnum()) {
748 int value = ltd.getClassDesc().getEnumConstant(fieldname);
750 // check if this field is an enum constant
751 throw new Error(fieldname + " is not an enum constant in "+fan.printNode(0)+" in "+md);
753 fd = new FieldDescriptor(new Modifiers(Modifiers.PUBLIC|Modifiers.FINAL), new TypeDescriptor(TypeDescriptor.INT), fieldname, null, false);
755 fd.setEnumValue(value);
756 } else if(fd == null) {
757 throw new Error("Could not find field "+ fieldname + " in "+fan.printNode(0)+" in "+md + " (Line: "+fan.getNumLine()+")");
758 } else if(fd.isStatic()) {
759 // check if this field is a static field
760 if(fd.getExpressionNode() != null) {
761 checkExpressionNode(md,nametable,fd.getExpressionNode(),null);
764 throw new Error("Dereference of the non-static field "+ fieldname + " in "+fan.printNode(0)+" in "+md);
769 if((md instanceof MethodDescriptor) && false == ((MethodDescriptor)md).isStaticBlock()) {
770 ClassDescriptor cd = ((MethodDescriptor)md).getClassDesc();
771 FieldAccessNode theFieldNode = fieldAccessExpression( cd, fieldname, fan.getNumLine() );
772 if( null != theFieldNode ) {
773 //fan = theFieldNode;
774 checkFieldAccessNode( md, nametable, theFieldNode, td );
775 fan.setField( theFieldNode.getField() );
776 fan.setExpression( theFieldNode.getExpression() );
777 //TypeDescriptor td1 = fan.getType();
782 throw new Error("Unknown field "+fieldname + " in "+fan.printNode(0)+" in "+md);
785 if (fd.getType().iswrapper()) {
786 FieldAccessNode fan2=new FieldAccessNode(left, fieldname);
787 fan2.setNumLine(left.getNumLine());
790 fan.fieldname="value";
792 ExpressionNode leftwr=fan.getExpression();
793 TypeDescriptor ltdwr=leftwr.getType();
794 String fieldnamewr=fan.getFieldName();
795 FieldDescriptor fdwr=(FieldDescriptor) ltdwr.getClassDesc().getFieldTable().get(fieldnamewr);
798 throw new Error("Unknown field "+fieldnamewr + " in "+fan.printNode(0)+" in "+md);
803 if (!typeutil.isSuperorType(td,fan.getType()))
804 throw new Error("Field node returns "+fan.getType()+", but need "+td);
808 void checkArrayAccessNode(Descriptor md, SymbolTable nametable, ArrayAccessNode aan, TypeDescriptor td) {
809 ExpressionNode left=aan.getExpression();
810 checkExpressionNode(md,nametable,left,null);
812 checkExpressionNode(md,nametable,aan.getIndex(),new TypeDescriptor(TypeDescriptor.INT));
813 TypeDescriptor ltd=left.getType();
814 if (ltd.dereference().iswrapper()) {
815 aan.wrappertype=((FieldDescriptor)ltd.dereference().getClassDesc().getFieldTable().get("value")).getType();
819 if (!typeutil.isSuperorType(td,aan.getType()))
820 throw new Error("Field node returns "+aan.getType()+", but need "+td);
823 void checkLiteralNode(Descriptor md, SymbolTable nametable, LiteralNode ln, TypeDescriptor td) {
824 /* Resolve the type */
825 Object o=ln.getValue();
826 if (ln.getTypeString().equals("null")) {
827 ln.setType(new TypeDescriptor(TypeDescriptor.NULL));
828 } else if (o instanceof Integer) {
829 ln.setType(new TypeDescriptor(TypeDescriptor.INT));
830 } else if (o instanceof Long) {
831 ln.setType(new TypeDescriptor(TypeDescriptor.LONG));
832 } else if (o instanceof Float) {
833 ln.setType(new TypeDescriptor(TypeDescriptor.FLOAT));
834 } else if (o instanceof Boolean) {
835 ln.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
836 } else if (o instanceof Double) {
837 ln.setType(new TypeDescriptor(TypeDescriptor.DOUBLE));
838 } else if (o instanceof Character) {
839 ln.setType(new TypeDescriptor(TypeDescriptor.CHAR));
840 } else if (o instanceof String) {
841 ln.setType(new TypeDescriptor(getClass(null, TypeUtil.StringClass)));
845 if (!typeutil.isSuperorType(td,ln.getType())) {
846 Long l = ln.evaluate();
847 if((ln.getType().isByte() || ln.getType().isShort()
848 || ln.getType().isChar() || ln.getType().isInt())
850 && (td.isByte() || td.isShort() || td.isChar()
851 || td.isInt() || td.isLong())) {
852 long lnvalue = l.longValue();
853 if((td.isByte() && ((lnvalue > 127) || (lnvalue < -128)))
854 || (td.isShort() && ((lnvalue > 32767) || (lnvalue < -32768)))
855 || (td.isChar() && ((lnvalue > 65535) || (lnvalue < 0)))
856 || (td.isInt() && ((lnvalue > 2147483647) || (lnvalue < -2147483648)))
857 || (td.isLong() && ((lnvalue > 9223372036854775807L) || (lnvalue < -9223372036854775808L)))) {
858 throw new Error("Field node returns "+ln.getType()+", but need "+td+" in "+md);
861 throw new Error("Field node returns "+ln.getType()+", but need "+td+" in "+md);
866 FieldDescriptor recurseSurroundingClasses( ClassDescriptor icd, String varname ) {
867 if( null == icd || false == icd.isInnerClass() )
870 ClassDescriptor surroundingDesc = icd.getSurroundingDesc();
871 if( null == surroundingDesc )
874 SymbolTable fieldTable = surroundingDesc.getFieldTable();
875 FieldDescriptor fd = ( FieldDescriptor ) fieldTable.get( varname );
878 return recurseSurroundingClasses( surroundingDesc, varname );
881 FieldAccessNode fieldAccessExpression( ClassDescriptor icd, String varname, int linenum ) {
882 FieldDescriptor fd = recurseSurroundingClasses( icd, varname );
886 ClassDescriptor cd = fd.getClassDescriptor();
887 if(icd.getInStaticContext()) {
888 // if the inner class is in a static context, it does not have the this$0
889 // pointer to its surrounding class. Instead, it might have reference to
890 // its static surrounding method/block is there is any and it can refer
891 // to static fields in its surrounding class too.
893 NameNode nn = new NameNode(new NameDescriptor(cd.getSymbol()));
894 nn.setNumLine(linenum);
895 FieldAccessNode theFieldNode = new FieldAccessNode(nn,varname);
896 theFieldNode.setNumLine(linenum);
899 throw new Error("Error: access non-static field " + cd.getSymbol() + "." + fd.getSymbol() + " in an inner class " + icd.getSymbol() + " that is declared in a static context");
903 int startingDepth = icd.getInnerDepth();
905 if( true == cd.isInnerClass() )
906 depth = cd.getInnerDepth();
908 String composed = "this";
909 NameDescriptor runningDesc = new NameDescriptor( "this" );;
911 for ( int index = startingDepth; index > depth; --index ) {
912 composed = "this$" + String.valueOf( index - 1 );
913 runningDesc = new NameDescriptor( runningDesc, composed );
915 if( false == cd.isInnerClass() )
916 runningDesc = new NameDescriptor( runningDesc, "this$" + String.valueOf(0) ); //all the way up.
917 NameDescriptor idDesc = new NameDescriptor( runningDesc, varname );
920 FieldAccessNode theFieldNode = ( FieldAccessNode )translateNameDescriptorintoExpression( idDesc, linenum );
924 Descriptor searchSurroundingNameTable(ClassDescriptor cd, String varname) {
926 if(cd.getInline()&&this.trialcheck) {
927 d = cd.getSurroundingNameTable().get(varname);
932 d = searchSurroundingNameTable(cd.getSurroundingDesc(), varname);
935 if(!this.inlineClass2LiveVars.containsKey(cd)) {
936 this.inlineClass2LiveVars.put(cd, new Vector<VarDescriptor>());
938 Vector<VarDescriptor> vars = this.inlineClass2LiveVars.get(cd);
939 if(!vars.contains((VarDescriptor)d)) {
940 vars.add((VarDescriptor)d);
946 void checkNameNode(Descriptor md, SymbolTable nametable, NameNode nn, TypeDescriptor td) {
947 NameDescriptor nd=nn.getName();
948 if (nd.getBase()!=null) {
950 /* Rewrite NameNode */
951 ExpressionNode en=translateNameDescriptorintoExpression(nd,nn.getNumLine());
952 nn.setExpression(en);
953 checkExpressionNode(md,nametable,en,td);
955 String varname=nd.toString();
956 if(varname.equals("this") || varname.equals("super")) {
958 nn.setVar((VarDescriptor)nametable.get("this"));
959 if(varname.equals("super")) {
964 Descriptor d=(Descriptor)nametable.get(varname);
966 ClassDescriptor cd = null;
967 //check the inner class case first.
968 if((md instanceof MethodDescriptor) && false == ((MethodDescriptor)md).isStaticBlock()) {
969 cd = ((MethodDescriptor)md).getClassDesc();
970 FieldAccessNode theFieldNode = fieldAccessExpression( cd, varname, nn.getNumLine() );
971 if( null != theFieldNode ) {
972 nn.setExpression(( ExpressionNode )theFieldNode);
973 checkExpressionNode(md,nametable,( ExpressionNode )theFieldNode,td);
976 // for the trial check of an inline class, cache the unknown var
977 d = searchSurroundingNameTable(cd, varname);
981 if((md instanceof MethodDescriptor) && ((MethodDescriptor)md).isStaticBlock()) {
982 // this is a static block, all the accessed fields should be static field
983 cd = ((MethodDescriptor)md).getClassDesc();
984 SymbolTable fieldtbl = cd.getFieldTable();
985 FieldDescriptor fd=(FieldDescriptor)fieldtbl.get(varname);
986 if((fd == null) || (!fd.isStatic())) {
987 // no such field in the class, check if this is a class
988 if(varname.equals("this")) {
989 throw new Error("Error: access this obj in a static block");
991 cd=getClass(((md instanceof MethodDescriptor)?((MethodDescriptor)md).getClassDesc():null), varname);
993 // this is a class name
997 throw new Error("Name "+varname+" should not be used in static block: "+md);
1000 // this is a static field
1002 nn.setClassDesc(cd);
1006 // check if the var is a static field of the class
1007 if(md instanceof MethodDescriptor) {
1008 cd = ((MethodDescriptor)md).getClassDesc();
1009 FieldDescriptor fd = (FieldDescriptor)cd.getFieldTable().get(varname);
1010 if((fd != null) && (fd.isStatic())) {
1012 nn.setClassDesc(cd);
1014 if (!typeutil.isSuperorType(td,nn.getType()))
1015 throw new Error("Field node returns "+nn.getType()+", but need "+td);
1017 } else if(fd != null) {
1018 throw new Error("Name "+varname+" should not be used in " + md);
1021 cd=getClass(((md instanceof MethodDescriptor)?((MethodDescriptor)md).getClassDesc():null), varname);
1023 // this is a class name
1024 nn.setClassDesc(cd);
1027 throw new Error("Name "+varname+" undefined in: "+md);
1033 if (d instanceof VarDescriptor) {
1035 } else if (d instanceof FieldDescriptor) {
1036 FieldDescriptor fd=(FieldDescriptor)d;
1037 if (fd.getType().iswrapper()) {
1038 String id=nd.getIdentifier();
1039 NameDescriptor base=nd.getBase();
1040 NameNode n=new NameNode(nn.getName());
1041 n.setNumLine(nn.getNumLine());
1043 n.setVar((VarDescriptor)nametable.get("this")); /* Need a pointer to this */
1044 FieldAccessNode fan=new FieldAccessNode(n,"value");
1045 fan.setNumLine(n.getNumLine());
1046 FieldDescriptor fdval=(FieldDescriptor) fd.getType().getClassDesc().getFieldTable().get("value");
1047 fan.setField(fdval);
1048 nn.setExpression(fan);
1051 nn.setVar((VarDescriptor)nametable.get("this")); /* Need a pointer to this */
1053 } else if (d instanceof TagVarDescriptor) {
1055 } else throw new Error("Wrong type of descriptor");
1057 if (!typeutil.isSuperorType(td,nn.getType()))
1058 throw new Error("Field node returns "+nn.getType()+", but need "+td);
1062 void checkOffsetNode(Descriptor md, SymbolTable nameTable, OffsetNode ofn, TypeDescriptor td) {
1063 TypeDescriptor ltd=ofn.td;
1064 checkTypeDescriptor(((md instanceof MethodDescriptor)?((MethodDescriptor)md).getClassDesc():null),ltd);
1066 String fieldname = ofn.fieldname;
1067 FieldDescriptor fd=null;
1068 if (ltd.isArray()&&fieldname.equals("length")) {
1069 fd=FieldDescriptor.arrayLength;
1071 fd=(FieldDescriptor) ltd.getClassDesc().getFieldTable().get(fieldname);
1075 checkField(ltd.getClassDesc(), fd);
1078 throw new Error("Unknown field "+fieldname + " in "+ofn.printNode(1)+" in "+md);
1081 if (!typeutil.isSuperorType(td, ofn.getType())) {
1082 throw new Error("Type of rside not compatible with type of lside"+ofn.printNode(0));
1088 void checkTertiaryNode(Descriptor md, SymbolTable nametable, TertiaryNode tn, TypeDescriptor td) {
1089 checkExpressionNode(md, nametable, tn.getCond(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
1090 checkExpressionNode(md, nametable, tn.getTrueExpr(), td);
1091 checkExpressionNode(md, nametable, tn.getFalseExpr(), td);
1094 void checkInstanceOfNode(Descriptor md, SymbolTable nametable, InstanceOfNode tn, TypeDescriptor td) {
1095 if (td!=null&&!td.isBoolean())
1096 throw new Error("Expecting type "+td+"for instanceof expression");
1098 checkTypeDescriptor(((md instanceof MethodDescriptor)?((MethodDescriptor)md).getClassDesc():null), tn.getExprType());
1099 checkExpressionNode(md, nametable, tn.getExpr(), null);
1102 void checkArrayInitializerNode(Descriptor md, SymbolTable nametable, ArrayInitializerNode ain, TypeDescriptor td) {
1103 for( int i = 0; i < ain.numVarInitializers(); ++i ) {
1104 checkExpressionNode(md, nametable, ain.getVarInitializer(i), td.dereference());
1112 void checkAssignmentNode(Descriptor md, SymbolTable nametable, AssignmentNode an, TypeDescriptor td) {
1113 // Need to first check the lside to decide the correct type of the rside
1114 // TODO: Need check on validity of operation here
1115 if (!((an.getDest() instanceof FieldAccessNode)||
1116 (an.getDest() instanceof ArrayAccessNode)||
1117 (an.getDest() instanceof NameNode)))
1118 throw new Error("Bad lside in "+an.printNode(0));
1119 checkExpressionNode(md, nametable, an.getDest(), null);
1120 boolean postinc=true;
1121 if (an.getOperation().getBaseOp()==null||
1122 (an.getOperation().getBaseOp().getOp()!=Operation.POSTINC&&
1123 an.getOperation().getBaseOp().getOp()!=Operation.POSTDEC))
1126 if(an.getSrc() instanceof ArrayInitializerNode) {
1127 checkExpressionNode(md, nametable, an.getSrc(), an.getDest().getType());
1129 checkExpressionNode(md, nametable, an.getSrc(),td);
1133 /* We want parameter variables to tasks to be immutable */
1134 if (md instanceof TaskDescriptor) {
1135 if (an.getDest() instanceof NameNode) {
1136 NameNode nn=(NameNode)an.getDest();
1137 if (nn.getVar()!=null) {
1138 if (((TaskDescriptor)md).getParameterTable().contains(nn.getVar().getSymbol()))
1139 throw new Error("Can't modify parameter "+nn.getVar()+ " to task "+td.getSymbol());
1144 if (an.getDest().getType().isString()&&an.getOperation().getOp()==AssignOperation.PLUSEQ) {
1146 ClassDescriptor stringcl=getClass(null, TypeUtil.StringClass);
1147 TypeDescriptor stringtd=new TypeDescriptor(stringcl);
1148 NameDescriptor nd=new NameDescriptor("String");
1149 NameDescriptor valuend=new NameDescriptor(nd, "valueOf");
1151 if (!(an.getSrc().getType().isString()&&(an.getSrc() instanceof OpNode))) {
1152 MethodInvokeNode rightmin=new MethodInvokeNode(valuend);
1153 rightmin.setNumLine(an.getSrc().getNumLine());
1154 rightmin.addArgument(an.getSrc());
1156 checkExpressionNode(md, nametable, an.getSrc(), null);
1160 if (!postinc&&!typeutil.isSuperorType(an.getDest().getType(),an.getSrc().getType())) {
1161 TypeDescriptor dt = an.getDest().getType();
1162 TypeDescriptor st = an.getSrc().getType();
1163 if(an.getSrc().kind() == Kind.ArrayInitializerNode) {
1164 if(dt.getArrayCount() != st.getArrayCount()) {
1165 throw new Error("Type of rside ("+an.getSrc().getType().toPrettyString()+") not compatible with type of lside ("+an.getDest().getType().toPrettyString()+")"+an.printNode(0));
1168 dt = dt.dereference();
1169 st = st.dereference();
1170 } while(dt.isArray());
1171 if((st.isByte() || st.isShort() || st.isChar() || st.isInt())
1172 && (dt.isByte() || dt.isShort() || dt.isChar() || dt.isInt() || dt.isLong())) {
1175 throw new Error("Type of rside ("+an.getSrc().getType().toPrettyString()+") not compatible with type of lside ("+an.getDest().getType().toPrettyString()+")"+an.printNode(0));
1179 Long l = an.getSrc().evaluate();
1180 if((st.isByte() || st.isShort() || st.isChar() || st.isInt())
1182 && (dt.isByte() || dt.isShort() || dt.isChar() || dt.isInt() || dt.isLong())) {
1183 long lnvalue = l.longValue();
1184 if((dt.isByte() && ((lnvalue > 127) || (lnvalue < -128)))
1185 || (dt.isShort() && ((lnvalue > 32767) || (lnvalue < -32768)))
1186 || (dt.isChar() && ((lnvalue > 65535) || (lnvalue < 0)))
1187 || (dt.isInt() && ((lnvalue > 2147483647) || (lnvalue < -2147483648)))
1188 || (dt.isLong() && ((lnvalue > 9223372036854775807L) || (lnvalue < -9223372036854775808L)))) {
1189 throw new Error("Type of rside ("+an.getSrc().getType().toPrettyString()+") not compatible with type of lside ("+an.getDest().getType().toPrettyString()+")"+an.printNode(0));
1192 throw new Error("Type of rside ("+an.getSrc().getType().toPrettyString()+") not compatible with type of lside ("+an.getDest().getType().toPrettyString()+")"+an.printNode(0));
1198 void checkLoopNode(Descriptor md, SymbolTable nametable, LoopNode ln) {
1200 if (ln.getType()==LoopNode.WHILELOOP||ln.getType()==LoopNode.DOWHILELOOP) {
1201 checkExpressionNode(md, nametable, ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
1202 checkBlockNode(md, nametable, ln.getBody());
1205 /* Link in the initializer naming environment */
1206 BlockNode bn=ln.getInitializer();
1207 bn.getVarTable().setParent(nametable);
1208 for(int i=0; i<bn.size(); i++) {
1209 BlockStatementNode bsn=bn.get(i);
1210 checkBlockStatementNode(md, bn.getVarTable(),bsn);
1212 //check the condition
1213 checkExpressionNode(md, bn.getVarTable(), ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
1214 checkBlockNode(md, bn.getVarTable(), ln.getBody());
1215 checkBlockNode(md, bn.getVarTable(), ln.getUpdate());
1220 void InnerClassAddParamToCtor( MethodDescriptor md, ClassDescriptor cd, SymbolTable nametable,
1221 CreateObjectNode con, TypeDescriptor td ) {
1222 if(this.inlineClassWithTrialCheck.contains(cd)) {
1223 // this class has done this with its first trial check
1226 TypeDescriptor cdsType = new TypeDescriptor( cd );
1227 ExpressionNode conExp = con.getSurroundingClassExpression();
1228 //System.out.println( "The surrounding class expression si " + con );
1229 if( null == conExp ) {
1230 if( md.isStatic()) {
1231 throw new Error("trying to instantiate inner class: " + con.getType() + " in a static scope" );
1233 VarDescriptor thisVD = md.getThis();
1234 if( null == thisVD ) {
1235 throw new Error( "this pointer is not defined in a non static scope" );
1237 if( cdsType.equals( thisVD.getType() ) == false ) {
1238 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" );
1240 //make this into an expression node.
1241 NameNode nThis=new NameNode( new NameDescriptor( "this" ) );
1242 con.addArgument( nThis );
1243 if(cd.isInnerClass()&&!cd.isStatic()) {
1244 // add all the ith lexically enclosing instance if applicable
1245 Iterator it_fields = cd.getFields();
1247 while(it_fields.hasNext()) {
1248 FieldDescriptor fd = (FieldDescriptor)(it_fields.next());
1249 if(fd.getSymbol().startsWith("this$")) {
1250 con.addArgument(new NameNode(new NameDescriptor("this$"+index)));
1257 //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.
1258 con.addArgument( conExp );
1260 //System.out.println( " the modified createObjectNode is " + con.printNode( 0 ) + "\n" );
1263 void trialSemanticCheck(ClassDescriptor cd) {
1264 if(this.inlineClassWithTrialCheck.contains(cd)) {
1265 // this inline class has done trial check
1268 if(!cd.getInline()) {
1269 throw new Error("Error! Try to do a trial check on a non-inline class " + cd.getSymbol());
1271 boolean settrial = false;
1277 for (Iterator method_it = cd.getMethods(); method_it.hasNext(); ) {
1278 MethodDescriptor md = (MethodDescriptor) method_it.next();
1280 checkMethodBody(cd, md);
1282 System.out.println("Error in " + md);
1286 this.inlineClassWithTrialCheck.add(cd);
1292 // add all the live vars that are referred by the inline class in the surrounding
1293 // context of the inline class into the inline class' field table and pass them to
1294 // the inline class' constructors
1295 // TODO: BUGFIX. These local vars should be final. But currently we've lost those
1296 // information, so we cannot have that checked.
1297 void InlineClassAddParamToCtor(MethodDescriptor md, ClassDescriptor cd , SymbolTable nametable, CreateObjectNode con, TypeDescriptor td, TypeDescriptor[] tdarray ) {
1298 if(this.inlineClassWithTrialCheck.contains(cd)) {
1299 // this class has done this with its first trial check
1302 // for an inline class, need to first add the original parameters of the CreatObjectNode
1303 // into its anonymous constructor and insert a super(...) into the anonymous constructor
1304 // if its super class is not null
1305 MethodDescriptor cd_constructor = null;
1306 for(Iterator it_methods = cd.getMethods(); it_methods.hasNext();) {
1307 MethodDescriptor imd = (MethodDescriptor)it_methods.next();
1308 if(imd.isConstructor()) {
1309 cd_constructor = imd; // an inline class should only have one anonymous constructor
1312 MethodInvokeNode min = null;
1313 if(cd.getSuper()!= null) {
1314 // add a super(...) into the anonymous constructor
1315 NameDescriptor nd=new NameDescriptor("super");
1316 min=new MethodInvokeNode(nd);
1317 BlockExpressionNode ben=new BlockExpressionNode(min);
1318 BlockNode bn = state.getMethodBody(cd_constructor);
1319 bn.addFirstBlockStatement(ben);
1320 if(cd.getSuperDesc().isInnerClass()&&!cd.getSuperDesc().isStatic()&&!cd.getSuperDesc().getInStaticContext()) {
1321 // for a super class that is also an inner class with surrounding reference, add the surrounding
1322 // instance of the child instance as the parent instance's surrounding instance
1323 min.addArgument(new NameNode(new NameDescriptor("surrounding$0")));
1326 for(int i = 0 ; i < tdarray.length; i++) {
1328 TypeDescriptor itd = tdarray[i];
1329 cd_constructor.addParameter(itd, itd.getSymbol()+"_from_con_node_"+i);
1331 min.addArgument(new NameNode(new NameDescriptor(itd.getSymbol()+"_from_con_node_"+i)));
1335 // Next add the live vars into the inline class' fields
1336 cd.setSurroundingNameTable(nametable);
1337 // do a round of semantic check trial to get all the live vars required by the inline class
1338 trialSemanticCheck(cd);
1339 Vector<VarDescriptor> vars = this.inlineClass2LiveVars.remove(cd);
1343 for(int i = 0; i < vars.size(); i++) {
1344 Descriptor d = vars.elementAt(i);
1345 if(d instanceof VarDescriptor && !d.getSymbol().equals("this")) {
1346 con.addArgument(new NameNode(new NameDescriptor(d.getSymbol())));
1347 cd.addField(new FieldDescriptor(new Modifiers(Modifiers.PUBLIC), ((VarDescriptor)d).getType(), d.getSymbol(), null, false));
1348 cd_constructor.addParameter(((VarDescriptor)d).getType(), d.getSymbol()+"_p");
1349 // add the initialize statement into this constructor
1350 BlockNode obn = state.getMethodBody(cd_constructor);
1351 NameNode nn=new NameNode(new NameDescriptor(d.getSymbol()));
1352 NameNode fn = new NameNode (new NameDescriptor(d.getSymbol()+"_p"));
1353 AssignmentNode an=new AssignmentNode(nn,fn,new AssignOperation(1));
1354 obn.addFirstBlockStatement(new BlockExpressionNode(an));
1355 state.addTreeCode(cd_constructor, obn);
1360 void checkCreateObjectNode(Descriptor md, SymbolTable nametable, CreateObjectNode con,
1361 TypeDescriptor td) {
1362 TypeDescriptor[] tdarray = new TypeDescriptor[con.numArgs()];
1363 for (int i = 0; i < con.numArgs(); i++) {
1364 ExpressionNode en = con.getArg(i);
1365 checkExpressionNode(md, nametable, en, null);
1366 tdarray[i] = en.getType();
1369 TypeDescriptor typetolookin = con.getType();
1370 checkTypeDescriptor(((md instanceof MethodDescriptor)?((MethodDescriptor)md).getClassDesc():null), typetolookin);
1372 if (td != null && !typeutil.isSuperorType(td, typetolookin))
1373 throw new Error(typetolookin + " isn't a " + td);
1375 /* Check Array Initializers */
1376 if ((con.getArrayInitializer() != null)) {
1377 checkArrayInitializerNode(md, nametable, con.getArrayInitializer(), typetolookin);
1380 /* Check flag effects */
1381 if (con.getFlagEffects() != null) {
1382 FlagEffects fe = con.getFlagEffects();
1383 ClassDescriptor cd = typetolookin.getClassDesc();
1385 for (int j = 0; j < fe.numEffects(); j++) {
1386 FlagEffect flag = fe.getEffect(j);
1387 String name = flag.getName();
1388 FlagDescriptor flag_d = (FlagDescriptor) cd.getFlagTable().get(name);
1389 // Make sure the flag is declared
1391 throw new Error("Flag descriptor " + name + " undefined in class: " + cd.getSymbol());
1392 if (flag_d.getExternal())
1393 throw new Error("Attempting to modify external flag: " + name);
1394 flag.setFlag(flag_d);
1396 for (int j = 0; j < fe.numTagEffects(); j++) {
1397 TagEffect tag = fe.getTagEffect(j);
1398 String name = tag.getName();
1400 Descriptor d = (Descriptor) nametable.get(name);
1402 throw new Error("Tag descriptor " + name + " undeclared");
1403 else if (!(d instanceof TagVarDescriptor))
1404 throw new Error(name + " is not a tag descriptor");
1405 tag.setTag((TagVarDescriptor) d);
1409 if ((!typetolookin.isClass()) && (!typetolookin.isArray()))
1410 throw new Error("Can't allocate primitive type:" + con.printNode(0));
1412 if (!typetolookin.isArray()) {
1413 // Array's don't need constructor calls
1414 ClassDescriptor classtolookin = typetolookin.getClassDesc();
1415 checkClass(classtolookin, INIT);
1416 if( classtolookin.isInnerClass() ) {
1417 // for inner class that is declared in a static context, it does not have
1418 // lexically enclosing instances
1419 if(!classtolookin.getInStaticContext()) {
1420 InnerClassAddParamToCtor( (MethodDescriptor)md, ((MethodDescriptor)md).getClassDesc() , nametable, con, td );
1422 if(classtolookin.getInline()) {
1423 // for an inline anonymous inner class, the live local variables that are
1424 // referred to by the inline class are passed as parameters of the constructors
1425 // of the inline class
1426 InlineClassAddParamToCtor( (MethodDescriptor)md, classtolookin, nametable, con, td, tdarray );
1428 tdarray = new TypeDescriptor[con.numArgs()];
1429 for (int i = 0; i < con.numArgs(); i++) {
1430 ExpressionNode en = con.getArg(i);
1431 checkExpressionNode(md, nametable, en, null);
1432 tdarray[i] = en.getType();
1435 Set methoddescriptorset = classtolookin.getMethodTable().getSet(classtolookin.getSymbol());
1436 MethodDescriptor bestmd = null;
1437 NextMethod: for (Iterator methodit = methoddescriptorset.iterator(); methodit.hasNext(); ) {
1438 MethodDescriptor currmd = (MethodDescriptor) methodit.next();
1439 /* Need correct number of parameters */
1440 if (con.numArgs() != currmd.numParameters())
1442 for (int i = 0; i < con.numArgs(); i++) {
1443 if (!typeutil.isSuperorType(currmd.getParamType(i), tdarray[i]))
1444 continue NextMethod;
1446 /* Local allocations can't call global allocator */
1447 if (!con.isGlobal() && currmd.isGlobal())
1450 /* Method okay so far */
1454 if (typeutil.isMoreSpecific(currmd, bestmd, true)) {
1456 } else if (con.isGlobal() && match(currmd, bestmd)) {
1457 if (currmd.isGlobal() && !bestmd.isGlobal())
1459 else if (currmd.isGlobal() && bestmd.isGlobal())
1461 } else if (!typeutil.isMoreSpecific(bestmd, currmd, true)) {
1462 throw new Error("No method is most specific:" + bestmd + " and " + currmd);
1465 /* Is this more specific than bestmd */
1468 if (bestmd == null) {
1469 throw new Error("No method found for " + con.printNode(0) + " in " + md);
1471 con.setConstructor(bestmd);
1476 /** Check to see if md1 is the same specificity as md2.*/
1478 boolean match(MethodDescriptor md1, MethodDescriptor md2) {
1479 /* Checks if md1 is more specific than md2 */
1480 if (md1.numParameters()!=md2.numParameters())
1482 for(int i=0; i<md1.numParameters(); i++) {
1483 if (!md2.getParamType(i).equals(md1.getParamType(i)))
1486 if (!md2.getReturnType().equals(md1.getReturnType()))
1489 if (!md2.getClassDesc().equals(md1.getClassDesc()))
1497 ExpressionNode translateNameDescriptorintoExpression(NameDescriptor nd, int numLine) {
1498 String id=nd.getIdentifier();
1499 NameDescriptor base=nd.getBase();
1501 NameNode nn=new NameNode(nd);
1502 nn.setNumLine(numLine);
1505 FieldAccessNode fan=new FieldAccessNode(translateNameDescriptorintoExpression(base,numLine),id);
1506 fan.setNumLine(numLine);
1511 MethodDescriptor recurseSurroundingClassesM( ClassDescriptor icd, String varname ) {
1512 if( null == icd || false == icd.isInnerClass() )
1515 ClassDescriptor surroundingDesc = icd.getSurroundingDesc();
1516 if( null == surroundingDesc )
1519 SymbolTable methodTable = surroundingDesc.getMethodTable();
1520 MethodDescriptor md = ( MethodDescriptor ) methodTable.get( varname );
1523 return recurseSurroundingClassesM( surroundingDesc, varname );
1526 ExpressionNode methodInvocationExpression( ClassDescriptor icd, MethodDescriptor md, int linenum ) {
1527 ClassDescriptor cd = md.getClassDesc();
1529 int startingDepth = icd.getInnerDepth();
1531 if( true == cd.isInnerClass() )
1532 depth = cd.getInnerDepth();
1534 String composed = "this";
1535 NameDescriptor runningDesc = new NameDescriptor( "this" );;
1537 for ( int index = startingDepth; index > depth; --index ) {
1538 composed = "this$" + String.valueOf( index - 1 );
1539 runningDesc = new NameDescriptor( runningDesc, composed );
1541 if( false == cd.isInnerClass() )
1542 runningDesc = new NameDescriptor( runningDesc, "this$" + String.valueOf(0) ); //all the way up.
1544 return new NameNode(runningDesc);
1547 void checkMethodInvokeNode(Descriptor md, SymbolTable nametable, MethodInvokeNode min, TypeDescriptor td) {
1548 /*Typecheck subexpressions
1549 and get types for expressions*/
1551 boolean isstatic = false;
1552 if((md instanceof MethodDescriptor) && ((MethodDescriptor)md).isStatic()) {
1555 TypeDescriptor[] tdarray=new TypeDescriptor[min.numArgs()];
1556 for(int i=0; i<min.numArgs(); i++) {
1557 ExpressionNode en=min.getArg(i);
1558 checkExpressionNode(md,nametable,en,null);
1559 tdarray[i]=en.getType();
1561 if(en.getType().isClass() && en.getType().getClassDesc().isEnum()) {
1562 tdarray[i] = new TypeDescriptor(TypeDescriptor.INT);
1565 TypeDescriptor typetolookin=null;
1566 if (min.getExpression()!=null) {
1567 checkExpressionNode(md,nametable,min.getExpression(),null);
1568 typetolookin=min.getExpression().getType();
1569 } else if (min.getBaseName()!=null) {
1570 String rootname=min.getBaseName().getRoot();
1571 if (rootname.equals("super")) {
1572 ClassDescriptor supercd=((MethodDescriptor)md).getClassDesc().getSuperDesc();
1573 typetolookin=new TypeDescriptor(supercd);
1575 } else if (rootname.equals("this")) {
1577 throw new Error("use this object in static method md = "+ md.toString());
1579 ClassDescriptor cd=((MethodDescriptor)md).getClassDesc();
1580 typetolookin=new TypeDescriptor(cd);
1581 } else if (nametable.get(rootname)!=null) {
1582 //we have an expression
1583 min.setExpression(translateNameDescriptorintoExpression(min.getBaseName(),min.getNumLine()));
1584 checkExpressionNode(md, nametable, min.getExpression(), null);
1585 typetolookin=min.getExpression().getType();
1587 if(!min.getBaseName().getSymbol().equals("System.out")||state.JNI) {
1588 ExpressionNode nn = translateNameDescriptorintoExpression(min.getBaseName(),min.getNumLine());
1589 checkExpressionNode(md, nametable, nn, null);
1590 typetolookin = nn.getType();
1591 if(!((nn.kind()== Kind.NameNode) && (((NameNode)nn).getField() == null)
1592 && (((NameNode)nn).getVar() == null) && (((NameNode)nn).getExpression() == null))) {
1593 // this is not a pure class name, need to add to
1594 min.setExpression(nn);
1598 ClassDescriptor cd = getClass(null, "System");
1601 throw new Error("md = "+ md.toString()+ " "+min.getBaseName()+" undefined");
1602 typetolookin=new TypeDescriptor(cd);
1605 } else if ((md instanceof MethodDescriptor)&&min.getMethodName().equals("super")) {
1606 ClassDescriptor supercd=((MethodDescriptor)md).getClassDesc().getSuperDesc();
1607 min.methodid=supercd.getSymbol();
1608 typetolookin=new TypeDescriptor(supercd);
1609 if(supercd.isInnerClass()&&!supercd.isStatic()&&!supercd.getInStaticContext()) {
1610 // for a super class that is also an inner class with surrounding reference, add the surrounding
1611 // instance of the child instance as the parent instance's surrounding instance
1612 if(((MethodDescriptor)md).isConstructor()) {
1613 min.addArgument(new NameNode(new NameDescriptor("surrounding$0")));
1614 } else if(((MethodDescriptor)md).getClassDesc().isInnerClass()&&!((MethodDescriptor)md).getClassDesc().isStatic()&&!((MethodDescriptor)md).getClassDesc().getInStaticContext()) {
1615 min.addArgument(new NameNode(new NameDescriptor("this$0")));
1618 tdarray=new TypeDescriptor[min.numArgs()];
1619 for(int i=0; i<min.numArgs(); i++) {
1620 ExpressionNode en=min.getArg(i);
1621 checkExpressionNode(md,nametable,en,null);
1622 tdarray[i]=en.getType();
1624 if(en.getType().isClass() && en.getType().getClassDesc().isEnum()) {
1625 tdarray[i] = new TypeDescriptor(TypeDescriptor.INT);
1628 } else if (md instanceof MethodDescriptor) {
1629 typetolookin=new TypeDescriptor(((MethodDescriptor)md).getClassDesc());
1631 /* If this a task descriptor we throw an error at this point */
1632 throw new Error("Unknown method call to "+min.getMethodName()+"in task"+md.getSymbol());
1634 if (!typetolookin.isClass())
1635 throw new Error("Error with method call to "+min.getMethodName()+" in class "+typetolookin);
1636 ClassDescriptor classtolookin=typetolookin.getClassDesc();
1637 checkClass(classtolookin, REFERENCE);
1638 Set methoddescriptorset=classtolookin.getMethodTable().getSet(min.getMethodName());
1639 MethodDescriptor bestmd=null;
1641 for(Iterator methodit=methoddescriptorset.iterator(); methodit.hasNext(); ) {
1642 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
1643 /* Need correct number of parameters */
1644 if (min.numArgs()!=currmd.numParameters())
1646 for(int i=0; i<min.numArgs(); i++) {
1647 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
1648 if(((!tdarray[i].isArray() &&(tdarray[i].isInt() || tdarray[i].isLong()))
1649 && currmd.getParamType(i).isClass() && currmd.getParamType(i).getClassDesc().getSymbol().equals("Object"))) {
1650 // primitive parameters vs object
1652 continue NextMethod;
1655 /* Method okay so far */
1659 if (typeutil.isMoreSpecific(currmd,bestmd, false)) {
1661 } else if (!typeutil.isMoreSpecific(bestmd, currmd, false)) {
1662 // if the two methods are inherited from super class/interface, use the super class' as first priority
1663 if(bestmd.getClassDesc().isInterface()&&!currmd.getClassDesc().isInterface()) {
1665 } else if(!bestmd.getClassDesc().isInterface()&&currmd.getClassDesc().isInterface()) {
1666 // maintain the non-interface one
1668 throw new Error("No method is most specific:"+bestmd+" and "+currmd);
1672 /* Is this more specific than bestmd */
1676 // if this is an inner class, need to check the method table for the surrounding class
1677 bestmd = recurseSurroundingClassesM(classtolookin, min.getMethodName());
1679 throw new Error("No method found for :"+min.printNode(0)+" in class: " + classtolookin+" in "+md);
1681 // set the correct "this" expression here
1682 ExpressionNode en=methodInvocationExpression(classtolookin, bestmd, min.getNumLine());
1683 min.setExpression(en);
1684 checkExpressionNode(md, nametable, min.getExpression(), null);
1687 min.setMethod(bestmd);
1689 if ((td!=null)&&(min.getType()!=null)&&!typeutil.isSuperorType(td, min.getType()))
1690 throw new Error(min.getType()+ " is not equal to or a subclass of "+td);
1691 /* Check whether we need to set this parameter to implied this */
1692 if (!isstatic && !bestmd.isStatic()) {
1693 if (min.getExpression()==null) {
1694 ExpressionNode en=new NameNode(new NameDescriptor("this"));
1695 min.setExpression(en);
1696 checkExpressionNode(md, nametable, min.getExpression(), null);
1700 /* Check if we need to wrap primitive paratmeters to objects */
1701 for(int i=0; i<min.numArgs(); i++) {
1702 if(!tdarray[i].isArray() && (tdarray[i].isInt() || tdarray[i].isLong())
1703 && min.getMethod().getParamType(i).isClass() && min.getMethod().getParamType(i).getClassDesc().getSymbol().equals("Object")) {
1704 // Shall wrap this primitive parameter as a object
1705 ExpressionNode exp = min.getArg(i);
1706 TypeDescriptor ptd = null;
1707 NameDescriptor nd=null;
1708 if(exp.getType().isInt()) {
1709 nd = new NameDescriptor("Integer");
1710 ptd = state.getTypeDescriptor(nd);
1711 } else if(exp.getType().isLong()) {
1712 nd = new NameDescriptor("Long");
1713 ptd = state.getTypeDescriptor(nd);
1715 boolean isglobal = false;
1716 String disjointId = null;
1717 CreateObjectNode con=new CreateObjectNode(ptd, isglobal, disjointId);
1718 con.addArgument(exp);
1719 checkExpressionNode(md, nametable, con, null);
1720 min.setArgument(con, i);
1726 void checkOpNode(Descriptor md, SymbolTable nametable, OpNode on, TypeDescriptor td) {
1727 checkExpressionNode(md, nametable, on.getLeft(), null);
1728 if (on.getRight()!=null)
1729 checkExpressionNode(md, nametable, on.getRight(), null);
1730 TypeDescriptor ltd=on.getLeft().getType();
1731 TypeDescriptor rtd=on.getRight()!=null?on.getRight().getType():null;
1732 TypeDescriptor lefttype=null;
1733 TypeDescriptor righttype=null;
1734 Operation op=on.getOp();
1736 switch(op.getOp()) {
1737 case Operation.LOGIC_OR:
1738 case Operation.LOGIC_AND:
1739 if (!(rtd.isBoolean()))
1741 on.setRightType(rtd);
1743 case Operation.LOGIC_NOT:
1744 if (!(ltd.isBoolean()))
1747 on.setLeftType(ltd);
1749 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1752 case Operation.COMP:
1753 // 5.6.2 Binary Numeric Promotion
1754 //TODO unboxing of reference objects
1757 else if (ltd.isFloat())
1759 else if (ltd.isLong())
1760 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1762 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1763 on.setLeftType(lefttype);
1764 on.setType(lefttype);
1767 case Operation.BIT_OR:
1768 case Operation.BIT_XOR:
1769 case Operation.BIT_AND:
1770 // 5.6.2 Binary Numeric Promotion
1771 //TODO unboxing of reference objects
1772 if (ltd.isDouble()||rtd.isDouble())
1774 else if (ltd.isFloat()||rtd.isFloat())
1776 else if (ltd.isLong()||rtd.isLong())
1777 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1778 // 090205 hack for boolean
1779 else if (ltd.isBoolean()||rtd.isBoolean())
1780 lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
1782 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1785 on.setLeftType(lefttype);
1786 on.setRightType(righttype);
1787 on.setType(lefttype);
1790 case Operation.ISAVAILABLE:
1791 if (!(ltd.isPtr())) {
1792 throw new Error("Can't use isavailable on non-pointers/non-parameters.");
1795 on.setLeftType(lefttype);
1796 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1799 case Operation.EQUAL:
1800 case Operation.NOTEQUAL:
1801 // 5.6.2 Binary Numeric Promotion
1802 //TODO unboxing of reference objects
1803 if (ltd.isBoolean()||rtd.isBoolean()) {
1804 if (!(ltd.isBoolean()&&rtd.isBoolean()))
1806 righttype=lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
1807 } else if (ltd.isPtr()||rtd.isPtr()) {
1808 if (!(ltd.isPtr()&&rtd.isPtr())) {
1815 } else if (ltd.isDouble()||rtd.isDouble())
1816 righttype=lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
1817 else if (ltd.isFloat()||rtd.isFloat())
1818 righttype=lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
1819 else if (ltd.isLong()||rtd.isLong())
1820 righttype=lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1822 righttype=lefttype=new TypeDescriptor(TypeDescriptor.INT);
1824 on.setLeftType(lefttype);
1825 on.setRightType(righttype);
1826 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1835 // 5.6.2 Binary Numeric Promotion
1836 //TODO unboxing of reference objects
1837 if (!ltd.isNumber()||!rtd.isNumber()) {
1838 if (!ltd.isNumber())
1839 throw new Error("Leftside is not number"+on.printNode(0)+"type="+ltd.toPrettyString());
1840 if (!rtd.isNumber())
1841 throw new Error("Rightside is not number"+on.printNode(0));
1844 if (ltd.isDouble()||rtd.isDouble())
1845 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
1846 else if (ltd.isFloat()||rtd.isFloat())
1847 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
1848 else if (ltd.isLong()||rtd.isLong())
1849 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1851 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1853 on.setLeftType(lefttype);
1854 on.setRightType(righttype);
1855 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1859 if (ltd.isString()||rtd.isString()) {
1860 ClassDescriptor stringcl=getClass(null, TypeUtil.StringClass);
1861 TypeDescriptor stringtd=new TypeDescriptor(stringcl);
1862 NameDescriptor nd=new NameDescriptor("String");
1863 NameDescriptor valuend=new NameDescriptor(nd, "valueOf");
1864 if (!(ltd.isString()&&(on.getLeft() instanceof OpNode))) {
1865 MethodInvokeNode leftmin=new MethodInvokeNode(valuend);
1866 leftmin.setNumLine(on.getLeft().getNumLine());
1867 leftmin.addArgument(on.getLeft());
1869 checkExpressionNode(md, nametable, on.getLeft(), null);
1872 if (!(rtd.isString()&&(on.getRight() instanceof OpNode))) {
1873 MethodInvokeNode rightmin=new MethodInvokeNode(valuend);
1874 rightmin.setNumLine(on.getRight().getNumLine());
1875 rightmin.addArgument(on.getRight());
1877 checkExpressionNode(md, nametable, on.getRight(), null);
1880 on.setLeftType(stringtd);
1881 on.setRightType(stringtd);
1882 on.setType(stringtd);
1887 case Operation.MULT:
1890 // 5.6.2 Binary Numeric Promotion
1891 //TODO unboxing of reference objects
1892 if (ltd.isArray()||rtd.isArray()||!ltd.isNumber()||!rtd.isNumber())
1893 throw new Error("Error in "+on.printNode(0));
1895 if (ltd.isDouble()||rtd.isDouble())
1896 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
1897 else if (ltd.isFloat()||rtd.isFloat())
1898 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
1899 else if (ltd.isLong()||rtd.isLong())
1900 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1902 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1904 on.setLeftType(lefttype);
1905 on.setRightType(righttype);
1906 on.setType(lefttype);
1909 case Operation.LEFTSHIFT:
1910 case Operation.RIGHTSHIFT:
1911 case Operation.URIGHTSHIFT:
1912 if (!rtd.isIntegerType())
1914 //5.6.1 Unary Numeric Promotion
1915 if (rtd.isByte()||rtd.isShort()||rtd.isInt())
1916 righttype=new TypeDescriptor(TypeDescriptor.INT);
1920 on.setRightType(righttype);
1921 if (!ltd.isIntegerType())
1924 case Operation.UNARYPLUS:
1925 case Operation.UNARYMINUS:
1926 /* case Operation.POSTINC:
1927 case Operation.POSTDEC:
1928 case Operation.PREINC:
1929 case Operation.PREDEC:*/
1930 if (!ltd.isNumber())
1932 //5.6.1 Unary Numeric Promotion
1933 if (ltd.isByte()||ltd.isShort()||ltd.isInt())
1934 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1937 on.setLeftType(lefttype);
1938 on.setType(lefttype);
1942 throw new Error(op.toString());
1946 if (!typeutil.isSuperorType(td, on.getType())) {
1947 throw new Error("Type of rside not compatible with type of lside"+on.printNode(0));