1 package Analysis.SSJava;
3 import java.util.ArrayList;
4 import java.util.HashSet;
5 import java.util.Hashtable;
6 import java.util.Iterator;
9 import java.util.StringTokenizer;
10 import java.util.Vector;
12 import Analysis.SSJava.FlowDownCheck.ComparisonResult;
13 import Analysis.SSJava.FlowDownCheck.CompositeLattice;
14 import IR.AnnotationDescriptor;
15 import IR.ClassDescriptor;
17 import IR.FieldDescriptor;
18 import IR.MethodDescriptor;
19 import IR.NameDescriptor;
22 import IR.SymbolTable;
23 import IR.TypeDescriptor;
24 import IR.VarDescriptor;
25 import IR.Tree.ArrayAccessNode;
26 import IR.Tree.AssignmentNode;
27 import IR.Tree.BlockExpressionNode;
28 import IR.Tree.BlockNode;
29 import IR.Tree.BlockStatementNode;
30 import IR.Tree.CastNode;
31 import IR.Tree.CreateObjectNode;
32 import IR.Tree.DeclarationNode;
33 import IR.Tree.ExpressionNode;
34 import IR.Tree.FieldAccessNode;
35 import IR.Tree.IfStatementNode;
37 import IR.Tree.LiteralNode;
38 import IR.Tree.LoopNode;
39 import IR.Tree.MethodInvokeNode;
40 import IR.Tree.NameNode;
41 import IR.Tree.OpNode;
42 import IR.Tree.ReturnNode;
43 import IR.Tree.SubBlockNode;
44 import IR.Tree.TertiaryNode;
45 import IR.Tree.TreeNode;
48 public class FlowDownCheck {
51 static SSJavaAnalysis ssjava;
55 // mapping from 'descriptor' to 'composite location'
56 Hashtable<Descriptor, CompositeLocation> d2loc;
58 Hashtable<MethodDescriptor, CompositeLocation> md2ReturnLoc;
59 Hashtable<MethodDescriptor, ReturnLocGenerator> md2ReturnLocGen;
61 // mapping from 'locID' to 'class descriptor'
62 Hashtable<String, ClassDescriptor> fieldLocName2cd;
64 public FlowDownCheck(SSJavaAnalysis ssjava, State state) {
67 this.toanalyze = new HashSet();
68 this.d2loc = new Hashtable<Descriptor, CompositeLocation>();
69 this.fieldLocName2cd = new Hashtable<String, ClassDescriptor>();
70 this.md2ReturnLoc = new Hashtable<MethodDescriptor, CompositeLocation>();
71 this.md2ReturnLocGen = new Hashtable<MethodDescriptor, ReturnLocGenerator>();
76 // construct mapping from the location name to the class descriptor
77 // assume that the location name is unique through the whole program
79 Set<ClassDescriptor> cdSet = ssjava.getCd2lattice().keySet();
80 for (Iterator iterator = cdSet.iterator(); iterator.hasNext();) {
81 ClassDescriptor cd = (ClassDescriptor) iterator.next();
82 SSJavaLattice<String> lattice = ssjava.getCd2lattice().get(cd);
83 Set<String> fieldLocNameSet = lattice.getKeySet();
85 for (Iterator iterator2 = fieldLocNameSet.iterator(); iterator2.hasNext();) {
86 String fieldLocName = (String) iterator2.next();
87 fieldLocName2cd.put(fieldLocName, cd);
94 public void flowDownCheck() {
95 SymbolTable classtable = state.getClassSymbolTable();
97 // phase 1 : checking declaration node and creating mapping of 'type
98 // desciptor' & 'location'
99 toanalyze.addAll(classtable.getValueSet());
100 toanalyze.addAll(state.getTaskSymbolTable().getValueSet());
101 while (!toanalyze.isEmpty()) {
102 Object obj = toanalyze.iterator().next();
103 ClassDescriptor cd = (ClassDescriptor) obj;
104 toanalyze.remove(cd);
106 if (!cd.isInterface()) {
108 ClassDescriptor superDesc = cd.getSuperDesc();
109 if (superDesc != null && (!superDesc.isInterface())
110 && (!superDesc.getSymbol().equals("Object"))) {
111 checkOrderingInheritance(superDesc, cd);
114 checkDeclarationInClass(cd);
115 for (Iterator method_it = cd.getMethods(); method_it.hasNext();) {
116 MethodDescriptor md = (MethodDescriptor) method_it.next();
117 if (ssjava.needAnnotation(md)) {
118 checkDeclarationInMethodBody(cd, md);
125 // phase2 : checking assignments
126 toanalyze.addAll(classtable.getValueSet());
127 toanalyze.addAll(state.getTaskSymbolTable().getValueSet());
128 while (!toanalyze.isEmpty()) {
129 Object obj = toanalyze.iterator().next();
130 ClassDescriptor cd = (ClassDescriptor) obj;
131 toanalyze.remove(cd);
134 for (Iterator method_it = cd.getMethods(); method_it.hasNext();) {
135 MethodDescriptor md = (MethodDescriptor) method_it.next();
136 if (ssjava.needAnnotation(md)) {
137 checkMethodBody(cd, md);
144 private void checkOrderingInheritance(ClassDescriptor superCd, ClassDescriptor cd) {
145 // here, we're going to check that sub class keeps same relative orderings
146 // in respect to super class
148 SSJavaLattice<String> superLattice = ssjava.getClassLattice(superCd);
149 SSJavaLattice<String> subLattice = ssjava.getClassLattice(cd);
151 if (superLattice != null && subLattice == null) {
152 throw new Error("If a parent class '" + superCd + "' has a ordering lattice, its subclass '"
153 + cd + "' should have one.");
156 Set<Pair<String, String>> superPairSet = superLattice.getOrderingPairSet();
157 Set<Pair<String, String>> subPairSet = subLattice.getOrderingPairSet();
159 for (Iterator iterator = superPairSet.iterator(); iterator.hasNext();) {
160 Pair<String, String> pair = (Pair<String, String>) iterator.next();
162 if (!subPairSet.contains(pair)) {
163 throw new Error("Subclass '" + cd + "' does not have the relative ordering '"
164 + pair.getSecond() + " < " + pair.getFirst() + "' that is defined by its superclass '"
171 public Hashtable getMap() {
175 private void checkDeclarationInMethodBody(ClassDescriptor cd, MethodDescriptor md) {
176 BlockNode bn = state.getMethodBody(md);
178 // parsing returnloc annotation
179 if (ssjava.needAnnotation(md)) {
181 Vector<AnnotationDescriptor> methodAnnotations = md.getModifiers().getAnnotations();
182 if (methodAnnotations != null) {
183 for (int i = 0; i < methodAnnotations.size(); i++) {
184 AnnotationDescriptor an = methodAnnotations.elementAt(i);
185 if (an.getMarker().equals(ssjava.RETURNLOC)) {
186 // developer explicitly defines method lattice
187 String returnLocDeclaration = an.getValue();
188 CompositeLocation returnLocComp =
189 parseLocationDeclaration(md, null, returnLocDeclaration);
190 md2ReturnLoc.put(md, returnLocComp);
194 if (!md.getReturnType().isVoid() && !md2ReturnLoc.containsKey(md)) {
195 throw new Error("Return location is not specified for the method " + md + " at "
196 + cd.getSourceFileName());
202 List<CompositeLocation> paramList = new ArrayList<CompositeLocation>();
204 boolean hasReturnValue = (!md.getReturnType().isVoid());
205 if (hasReturnValue) {
206 MethodLattice<String> methodLattice = ssjava.getMethodLattice(md);
207 String thisLocId = methodLattice.getThisLoc();
208 CompositeLocation thisLoc = new CompositeLocation(new Location(md, thisLocId));
209 paramList.add(thisLoc);
212 for (int i = 0; i < md.numParameters(); i++) {
213 // process annotations on method parameters
214 VarDescriptor vd = (VarDescriptor) md.getParameter(i);
215 assignLocationOfVarDescriptor(vd, md, md.getParameterTable(), bn);
216 if (hasReturnValue) {
217 paramList.add(d2loc.get(vd));
221 if (hasReturnValue) {
222 md2ReturnLocGen.put(md, new ReturnLocGenerator(md2ReturnLoc.get(md), paramList));
225 checkDeclarationInBlockNode(md, md.getParameterTable(), bn);
228 private void checkDeclarationInBlockNode(MethodDescriptor md, SymbolTable nametable, BlockNode bn) {
229 bn.getVarTable().setParent(nametable);
230 for (int i = 0; i < bn.size(); i++) {
231 BlockStatementNode bsn = bn.get(i);
232 checkDeclarationInBlockStatementNode(md, bn.getVarTable(), bsn);
236 private void checkDeclarationInBlockStatementNode(MethodDescriptor md, SymbolTable nametable,
237 BlockStatementNode bsn) {
239 switch (bsn.kind()) {
240 case Kind.SubBlockNode:
241 checkDeclarationInSubBlockNode(md, nametable, (SubBlockNode) bsn);
244 case Kind.DeclarationNode:
245 checkDeclarationNode(md, nametable, (DeclarationNode) bsn);
249 checkDeclarationInLoopNode(md, nametable, (LoopNode) bsn);
254 private void checkDeclarationInLoopNode(MethodDescriptor md, SymbolTable nametable, LoopNode ln) {
256 if (ln.getType() == LoopNode.FORLOOP) {
257 // check for loop case
258 ClassDescriptor cd = md.getClassDesc();
259 BlockNode bn = ln.getInitializer();
260 for (int i = 0; i < bn.size(); i++) {
261 BlockStatementNode bsn = bn.get(i);
262 checkDeclarationInBlockStatementNode(md, nametable, bsn);
267 checkDeclarationInBlockNode(md, nametable, ln.getBody());
270 private void checkMethodBody(ClassDescriptor cd, MethodDescriptor md) {
271 BlockNode bn = state.getMethodBody(md);
272 checkLocationFromBlockNode(md, md.getParameterTable(), bn);
275 private CompositeLocation checkLocationFromBlockNode(MethodDescriptor md, SymbolTable nametable,
278 bn.getVarTable().setParent(nametable);
279 // it will return the lowest location in the block node
280 CompositeLocation lowestLoc = null;
282 for (int i = 0; i < bn.size(); i++) {
283 BlockStatementNode bsn = bn.get(i);
284 CompositeLocation bLoc = checkLocationFromBlockStatementNode(md, bn.getVarTable(), bsn);
285 if (!bLoc.isEmpty()) {
286 if (lowestLoc == null) {
289 if (CompositeLattice.isGreaterThan(lowestLoc, bLoc)) {
297 if (lowestLoc == null) {
298 lowestLoc = new CompositeLocation(Location.createBottomLocation(md));
304 private CompositeLocation checkLocationFromBlockStatementNode(MethodDescriptor md,
305 SymbolTable nametable, BlockStatementNode bsn) {
307 CompositeLocation compLoc = null;
308 switch (bsn.kind()) {
309 case Kind.BlockExpressionNode:
310 compLoc = checkLocationFromBlockExpressionNode(md, nametable, (BlockExpressionNode) bsn);
313 case Kind.DeclarationNode:
314 compLoc = checkLocationFromDeclarationNode(md, nametable, (DeclarationNode) bsn);
317 case Kind.IfStatementNode:
318 compLoc = checkLocationFromIfStatementNode(md, nametable, (IfStatementNode) bsn);
322 compLoc = checkLocationFromLoopNode(md, nametable, (LoopNode) bsn);
325 case Kind.ReturnNode:
326 compLoc = checkLocationFromReturnNode(md, nametable, (ReturnNode) bsn);
329 case Kind.SubBlockNode:
330 compLoc = checkLocationFromSubBlockNode(md, nametable, (SubBlockNode) bsn);
333 case Kind.ContinueBreakNode:
334 compLoc = new CompositeLocation();
341 private CompositeLocation checkLocationFromReturnNode(MethodDescriptor md, SymbolTable nametable,
344 ExpressionNode returnExp = rn.getReturnExpression();
346 CompositeLocation expLoc =
347 checkLocationFromExpressionNode(md, nametable, returnExp, new CompositeLocation());
349 // check if return value is equal or higher than RETRUNLOC of method
350 // declaration annotation
351 CompositeLocation returnLocAt = md2ReturnLoc.get(md);
353 if (CompositeLattice.isGreaterThan(returnLocAt, expLoc)) {
355 "Return value location is not equal or higher than the declaraed return location at "
356 + md.getClassDesc().getSourceFileName() + "::" + rn.getNumLine());
359 return new CompositeLocation();
362 private boolean hasOnlyLiteralValue(ExpressionNode en) {
363 if (en.kind() == Kind.LiteralNode) {
370 private CompositeLocation checkLocationFromLoopNode(MethodDescriptor md, SymbolTable nametable,
373 ClassDescriptor cd = md.getClassDesc();
374 if (ln.getType() == LoopNode.WHILELOOP || ln.getType() == LoopNode.DOWHILELOOP) {
376 CompositeLocation condLoc =
377 checkLocationFromExpressionNode(md, nametable, ln.getCondition(), new CompositeLocation());
378 addTypeLocation(ln.getCondition().getType(), (condLoc));
380 CompositeLocation bodyLoc = checkLocationFromBlockNode(md, nametable, ln.getBody());
382 if (!CompositeLattice.isGreaterThan(condLoc, bodyLoc)) {
383 // loop condition should be higher than loop body
385 "The location of the while-condition statement is lower than the loop body at "
386 + cd.getSourceFileName() + ":" + ln.getCondition().getNumLine());
392 // check for loop case
393 BlockNode bn = ln.getInitializer();
394 bn.getVarTable().setParent(nametable);
396 // calculate glb location of condition and update statements
397 CompositeLocation condLoc =
398 checkLocationFromExpressionNode(md, bn.getVarTable(), ln.getCondition(),
399 new CompositeLocation());
400 addTypeLocation(ln.getCondition().getType(), condLoc);
402 CompositeLocation updateLoc =
403 checkLocationFromBlockNode(md, bn.getVarTable(), ln.getUpdate());
405 Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
406 glbInputSet.add(condLoc);
407 glbInputSet.add(updateLoc);
409 CompositeLocation glbLocOfForLoopCond = CompositeLattice.calculateGLB(glbInputSet);
411 // check location of 'forloop' body
412 CompositeLocation blockLoc = checkLocationFromBlockNode(md, bn.getVarTable(), ln.getBody());
414 if (blockLoc == null) {
415 // when there is no statement in the loop body
416 return glbLocOfForLoopCond;
419 if (!CompositeLattice.isGreaterThan(glbLocOfForLoopCond, blockLoc)) {
421 "The location of the for-condition statement is lower than the for-loop body at "
422 + cd.getSourceFileName() + ":" + ln.getCondition().getNumLine());
429 private CompositeLocation checkLocationFromSubBlockNode(MethodDescriptor md,
430 SymbolTable nametable, SubBlockNode sbn) {
431 CompositeLocation compLoc = checkLocationFromBlockNode(md, nametable, sbn.getBlockNode());
435 private CompositeLocation checkLocationFromIfStatementNode(MethodDescriptor md,
436 SymbolTable nametable, IfStatementNode isn) {
438 ClassDescriptor localCD = md.getClassDesc();
439 Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
441 CompositeLocation condLoc =
442 checkLocationFromExpressionNode(md, nametable, isn.getCondition(), new CompositeLocation());
444 addTypeLocation(isn.getCondition().getType(), condLoc);
445 glbInputSet.add(condLoc);
447 CompositeLocation locTrueBlock = checkLocationFromBlockNode(md, nametable, isn.getTrueBlock());
448 if (locTrueBlock != null) {
449 glbInputSet.add(locTrueBlock);
450 // here, the location of conditional block should be higher than the
451 // location of true/false blocks
452 if (locTrueBlock != null && !CompositeLattice.isGreaterThan(condLoc, locTrueBlock)) {
455 "The location of the if-condition statement is lower than the conditional block at "
456 + localCD.getSourceFileName() + ":" + isn.getCondition().getNumLine());
460 if (isn.getFalseBlock() != null) {
461 CompositeLocation locFalseBlock =
462 checkLocationFromBlockNode(md, nametable, isn.getFalseBlock());
464 if (locFalseBlock != null) {
465 glbInputSet.add(locFalseBlock);
467 if (!CompositeLattice.isGreaterThan(condLoc, locFalseBlock)) {
470 "The location of the if-condition statement is lower than the conditional block at "
471 + localCD.getSourceFileName() + ":" + isn.getCondition().getNumLine());
477 // return GLB location of condition, true, and false block
478 CompositeLocation glbLoc = CompositeLattice.calculateGLB(glbInputSet);
483 private CompositeLocation checkLocationFromDeclarationNode(MethodDescriptor md,
484 SymbolTable nametable, DeclarationNode dn) {
486 VarDescriptor vd = dn.getVarDescriptor();
488 CompositeLocation destLoc = d2loc.get(vd);
490 if (dn.getExpression() != null) {
491 CompositeLocation expressionLoc =
492 checkLocationFromExpressionNode(md, nametable, dn.getExpression(),
493 new CompositeLocation());
494 // addTypeLocation(dn.getExpression().getType(), expressionLoc);
496 if (expressionLoc != null) {
497 // checking location order
498 if (!CompositeLattice.isGreaterThan(expressionLoc, destLoc)) {
499 throw new Error("The value flow from " + expressionLoc + " to " + destLoc
500 + " does not respect location hierarchy on the assignment " + dn.printNode(0)
501 + " at " + md.getClassDesc().getSourceFileName() + "::" + dn.getNumLine());
504 return expressionLoc;
508 return new CompositeLocation();
514 private void checkDeclarationInSubBlockNode(MethodDescriptor md, SymbolTable nametable,
516 checkDeclarationInBlockNode(md, nametable.getParent(), sbn.getBlockNode());
519 private CompositeLocation checkLocationFromBlockExpressionNode(MethodDescriptor md,
520 SymbolTable nametable, BlockExpressionNode ben) {
521 CompositeLocation compLoc =
522 checkLocationFromExpressionNode(md, nametable, ben.getExpression(), null);
523 // addTypeLocation(ben.getExpression().getType(), compLoc);
527 private CompositeLocation checkLocationFromExpressionNode(MethodDescriptor md,
528 SymbolTable nametable, ExpressionNode en, CompositeLocation loc) {
530 CompositeLocation compLoc = null;
533 case Kind.AssignmentNode:
534 compLoc = checkLocationFromAssignmentNode(md, nametable, (AssignmentNode) en, loc);
537 case Kind.FieldAccessNode:
538 compLoc = checkLocationFromFieldAccessNode(md, nametable, (FieldAccessNode) en, loc);
542 compLoc = checkLocationFromNameNode(md, nametable, (NameNode) en, loc);
546 compLoc = checkLocationFromOpNode(md, nametable, (OpNode) en);
549 case Kind.CreateObjectNode:
550 compLoc = checkLocationFromCreateObjectNode(md, nametable, (CreateObjectNode) en);
553 case Kind.ArrayAccessNode:
554 compLoc = checkLocationFromArrayAccessNode(md, nametable, (ArrayAccessNode) en);
557 case Kind.LiteralNode:
558 compLoc = checkLocationFromLiteralNode(md, nametable, (LiteralNode) en, loc);
561 case Kind.MethodInvokeNode:
562 compLoc = checkLocationFromMethodInvokeNode(md, nametable, (MethodInvokeNode) en, loc);
565 case Kind.TertiaryNode:
566 compLoc = checkLocationFromTertiaryNode(md, nametable, (TertiaryNode) en);
570 compLoc = checkLocationFromCastNode(md, nametable, (CastNode) en);
573 // case Kind.InstanceOfNode:
574 // checkInstanceOfNode(md, nametable, (InstanceOfNode) en, td);
577 // case Kind.ArrayInitializerNode:
578 // checkArrayInitializerNode(md, nametable, (ArrayInitializerNode) en,
582 // case Kind.ClassTypeNode:
583 // checkClassTypeNode(md, nametable, (ClassTypeNode) en, td);
586 // case Kind.OffsetNode:
587 // checkOffsetNode(md, nametable, (OffsetNode)en, td);
594 // addTypeLocation(en.getType(), compLoc);
599 private CompositeLocation checkLocationFromCastNode(MethodDescriptor md, SymbolTable nametable,
602 ExpressionNode en = cn.getExpression();
603 return checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation());
607 private CompositeLocation checkLocationFromTertiaryNode(MethodDescriptor md,
608 SymbolTable nametable, TertiaryNode tn) {
609 ClassDescriptor cd = md.getClassDesc();
611 CompositeLocation condLoc =
612 checkLocationFromExpressionNode(md, nametable, tn.getCond(), new CompositeLocation());
613 addTypeLocation(tn.getCond().getType(), condLoc);
614 CompositeLocation trueLoc =
615 checkLocationFromExpressionNode(md, nametable, tn.getTrueExpr(), new CompositeLocation());
616 addTypeLocation(tn.getTrueExpr().getType(), trueLoc);
617 CompositeLocation falseLoc =
618 checkLocationFromExpressionNode(md, nametable, tn.getFalseExpr(), new CompositeLocation());
619 addTypeLocation(tn.getFalseExpr().getType(), falseLoc);
621 // check if condLoc is higher than trueLoc & falseLoc
622 if (!CompositeLattice.isGreaterThan(condLoc, trueLoc)) {
624 "The location of the condition expression is lower than the true expression at "
625 + cd.getSourceFileName() + ":" + tn.getCond().getNumLine());
628 if (!CompositeLattice.isGreaterThan(condLoc, falseLoc)) {
630 "The location of the condition expression is lower than the true expression at "
631 + cd.getSourceFileName() + ":" + tn.getCond().getNumLine());
634 // then, return glb of trueLoc & falseLoc
635 Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
636 glbInputSet.add(trueLoc);
637 glbInputSet.add(falseLoc);
639 return CompositeLattice.calculateGLB(glbInputSet);
642 private CompositeLocation checkLocationFromMethodInvokeNode(MethodDescriptor md,
643 SymbolTable nametable, MethodInvokeNode min, CompositeLocation loc) {
645 checkCalleeConstraints(md, nametable, min);
647 CompositeLocation baseLocation = null;
648 if (min.getExpression() != null) {
650 checkLocationFromExpressionNode(md, nametable, min.getExpression(),
651 new CompositeLocation());
653 String thisLocId = ssjava.getMethodLattice(md).getThisLoc();
654 baseLocation = new CompositeLocation(new Location(md, thisLocId));
657 if (!min.getMethod().getReturnType().isVoid()) {
658 // If method has a return value, compute the highest possible return
659 // location in the caller's perspective
660 CompositeLocation ceilingLoc =
661 computeCeilingLocationForCaller(md, nametable, min, baseLocation);
665 return new CompositeLocation();
669 private CompositeLocation computeCeilingLocationForCaller(MethodDescriptor md,
670 SymbolTable nametable, MethodInvokeNode min, CompositeLocation baseLocation) {
671 List<CompositeLocation> argList = new ArrayList<CompositeLocation>();
673 // by default, method has a THIS parameter
674 argList.add(baseLocation);
676 for (int i = 0; i < min.numArgs(); i++) {
677 ExpressionNode en = min.getArg(i);
678 CompositeLocation callerArg =
679 checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation());
680 argList.add(callerArg);
683 return md2ReturnLocGen.get(min.getMethod()).computeReturnLocation(argList);
687 private void checkCalleeConstraints(MethodDescriptor md, SymbolTable nametable,
688 MethodInvokeNode min) {
690 if (min.numArgs() > 1) {
691 // caller needs to guarantee that it passes arguments in regarding to
692 // callee's hierarchy
693 for (int i = 0; i < min.numArgs(); i++) {
694 ExpressionNode en = min.getArg(i);
695 CompositeLocation callerArg1 =
696 checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation());
698 ClassDescriptor calleecd = min.getMethod().getClassDesc();
699 VarDescriptor calleevd = (VarDescriptor) min.getMethod().getParameter(i);
700 CompositeLocation calleeLoc1 = d2loc.get(calleevd);
702 if (!callerArg1.get(0).isTop()) {
703 // here, check if ordering relations among caller's args respect
704 // ordering relations in-between callee's args
705 for (int currentIdx = 0; currentIdx < min.numArgs(); currentIdx++) {
706 if (currentIdx != i) { // skip itself
707 ExpressionNode argExp = min.getArg(currentIdx);
709 CompositeLocation callerArg2 =
710 checkLocationFromExpressionNode(md, nametable, argExp, new CompositeLocation());
712 VarDescriptor calleevd2 = (VarDescriptor) min.getMethod().getParameter(currentIdx);
713 CompositeLocation calleeLoc2 = d2loc.get(calleevd2);
715 boolean callerResult = CompositeLattice.isGreaterThan(callerArg1, callerArg2);
716 boolean calleeResult = CompositeLattice.isGreaterThan(calleeLoc1, calleeLoc2);
718 if (calleeResult && !callerResult) {
719 // If calleeLoc1 is higher than calleeLoc2
720 // then, caller should have same ordering relation in-bet
721 // callerLoc1 & callerLoc2
723 throw new Error("Caller doesn't respect ordering relations among method arguments:"
724 + md.getClassDesc().getSourceFileName() + ":" + min.getNumLine());
737 private CompositeLocation checkLocationFromArrayAccessNode(MethodDescriptor md,
738 SymbolTable nametable, ArrayAccessNode aan) {
740 // return glb location of array itself and index
742 ClassDescriptor cd = md.getClassDesc();
744 Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
746 CompositeLocation arrayLoc =
747 checkLocationFromExpressionNode(md, nametable, aan.getExpression(), new CompositeLocation());
748 // addTypeLocation(aan.getExpression().getType(), arrayLoc);
749 glbInputSet.add(arrayLoc);
750 CompositeLocation indexLoc =
751 checkLocationFromExpressionNode(md, nametable, aan.getIndex(), new CompositeLocation());
752 glbInputSet.add(indexLoc);
753 // addTypeLocation(aan.getIndex().getType(), indexLoc);
755 CompositeLocation glbLoc = CompositeLattice.calculateGLB(glbInputSet);
759 private CompositeLocation checkLocationFromCreateObjectNode(MethodDescriptor md,
760 SymbolTable nametable, CreateObjectNode con) {
762 ClassDescriptor cd = md.getClassDesc();
765 Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
766 for (int i = 0; i < con.numArgs(); i++) {
767 ExpressionNode en = con.getArg(i);
768 CompositeLocation argLoc =
769 checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation());
770 glbInputSet.add(argLoc);
771 addTypeLocation(en.getType(), argLoc);
774 // check array initializers
775 // if ((con.getArrayInitializer() != null)) {
776 // checkLocationFromArrayInitializerNode(md, nametable,
777 // con.getArrayInitializer());
780 if (glbInputSet.size() > 0) {
781 return CompositeLattice.calculateGLB(glbInputSet);
784 CompositeLocation compLoc = new CompositeLocation();
785 compLoc.addLocation(Location.createTopLocation(md));
790 private CompositeLocation checkLocationFromOpNode(MethodDescriptor md, SymbolTable nametable,
793 ClassDescriptor cd = md.getClassDesc();
794 CompositeLocation leftLoc = new CompositeLocation();
795 leftLoc = checkLocationFromExpressionNode(md, nametable, on.getLeft(), leftLoc);
796 // addTypeLocation(on.getLeft().getType(), leftLoc);
798 CompositeLocation rightLoc = new CompositeLocation();
799 if (on.getRight() != null) {
800 rightLoc = checkLocationFromExpressionNode(md, nametable, on.getRight(), rightLoc);
801 // addTypeLocation(on.getRight().getType(), rightLoc);
804 // System.out.println("checking op node=" + on.printNode(0));
805 // System.out.println("left loc=" + leftLoc + " from " +
806 // on.getLeft().getClass());
807 // System.out.println("right loc=" + rightLoc + " from " +
808 // on.getRight().getClass());
810 Operation op = on.getOp();
812 switch (op.getOp()) {
814 case Operation.UNARYPLUS:
815 case Operation.UNARYMINUS:
816 case Operation.LOGIC_NOT:
820 case Operation.LOGIC_OR:
821 case Operation.LOGIC_AND:
823 case Operation.BIT_OR:
824 case Operation.BIT_XOR:
825 case Operation.BIT_AND:
826 case Operation.ISAVAILABLE:
827 case Operation.EQUAL:
828 case Operation.NOTEQUAL:
838 case Operation.LEFTSHIFT:
839 case Operation.RIGHTSHIFT:
840 case Operation.URIGHTSHIFT:
842 Set<CompositeLocation> inputSet = new HashSet<CompositeLocation>();
843 inputSet.add(leftLoc);
844 inputSet.add(rightLoc);
845 CompositeLocation glbCompLoc = CompositeLattice.calculateGLB(inputSet);
849 throw new Error(op.toString());
854 private CompositeLocation checkLocationFromLiteralNode(MethodDescriptor md,
855 SymbolTable nametable, LiteralNode en, CompositeLocation loc) {
857 // literal value has the top location so that value can be flowed into any
859 Location literalLoc = Location.createTopLocation(md);
860 loc.addLocation(literalLoc);
865 private CompositeLocation checkLocationFromNameNode(MethodDescriptor md, SymbolTable nametable,
866 NameNode nn, CompositeLocation loc) {
868 NameDescriptor nd = nn.getName();
869 if (nd.getBase() != null) {
871 loc = checkLocationFromExpressionNode(md, nametable, nn.getExpression(), loc);
872 // addTypeLocation(nn.getExpression().getType(), loc);
874 String varname = nd.toString();
876 if (varname.equals("this")) {
878 MethodLattice<String> methodLattice = ssjava.getMethodLattice(md);
879 String thisLocId = methodLattice.getThisLoc();
880 if (thisLocId == null) {
881 throw new Error("The location for 'this' is not defined at "
882 + md.getClassDesc().getSourceFileName() + "::" + nn.getNumLine());
884 Location locElement = new Location(md, thisLocId);
885 loc.addLocation(locElement);
888 Descriptor d = (Descriptor) nametable.get(varname);
890 // CompositeLocation localLoc = null;
891 if (d instanceof VarDescriptor) {
892 VarDescriptor vd = (VarDescriptor) d;
893 // localLoc = d2loc.get(vd);
894 // the type of var descriptor has a composite location!
895 loc = ((CompositeLocation) vd.getType().getExtension()).clone();
896 } else if (d instanceof FieldDescriptor) {
897 // the type of field descriptor has a location!
898 FieldDescriptor fd = (FieldDescriptor) d;
902 // if it is 'static final', the location has TOP since no one can
904 loc.addLocation(Location.createTopLocation(md));
906 // if 'static', the location has pre-assigned global loc
907 MethodLattice<String> localLattice = ssjava.getMethodLattice(md);
908 String globalLocId = localLattice.getGlobalLoc();
909 if (globalLocId == null) {
910 throw new Error("Global location element is not defined in the method " + md);
912 Location globalLoc = new Location(md, globalLocId);
914 loc.addLocation(globalLoc);
917 // the location of field access starts from this, followed by field
919 MethodLattice<String> localLattice = ssjava.getMethodLattice(md);
920 Location thisLoc = new Location(md, localLattice.getThisLoc());
921 loc.addLocation(thisLoc);
924 Location fieldLoc = (Location) fd.getType().getExtension();
925 loc.addLocation(fieldLoc);
931 private CompositeLocation checkLocationFromFieldAccessNode(MethodDescriptor md,
932 SymbolTable nametable, FieldAccessNode fan, CompositeLocation loc) {
934 ExpressionNode left = fan.getExpression();
935 loc = checkLocationFromExpressionNode(md, nametable, left, loc);
936 // addTypeLocation(left.getType(), loc);
938 if (!left.getType().isPrimitive()) {
939 FieldDescriptor fd = fan.getField();
940 Location fieldLoc = (Location) fd.getType().getExtension();
941 loc.addLocation(fieldLoc);
947 private CompositeLocation checkLocationFromAssignmentNode(MethodDescriptor md,
948 SymbolTable nametable, AssignmentNode an, CompositeLocation loc) {
950 ClassDescriptor cd = md.getClassDesc();
952 boolean postinc = true;
953 if (an.getOperation().getBaseOp() == null
954 || (an.getOperation().getBaseOp().getOp() != Operation.POSTINC && an.getOperation()
955 .getBaseOp().getOp() != Operation.POSTDEC))
958 CompositeLocation destLocation =
959 checkLocationFromExpressionNode(md, nametable, an.getDest(), new CompositeLocation());
961 CompositeLocation srcLocation = new CompositeLocation();
964 if (hasOnlyLiteralValue(an.getSrc())) {
965 // if source is literal value, src location is TOP. so do not need to
969 srcLocation = new CompositeLocation();
970 srcLocation = checkLocationFromExpressionNode(md, nametable, an.getSrc(), srcLocation);
971 // System.out.println(" an= " + an.printNode(0) + " an.getSrc()=" +
972 // an.getSrc().getClass()
973 // + " at " + cd.getSourceFileName() + "::" + an.getNumLine());
974 // System.out.println("srcLocation=" + srcLocation);
975 // System.out.println("dstLocation=" + destLocation);
976 if (!CompositeLattice.isGreaterThan(srcLocation, destLocation)) {
977 throw new Error("The value flow from " + srcLocation + " to " + destLocation
978 + " does not respect location hierarchy on the assignment " + an.printNode(0) + " at "
979 + cd.getSourceFileName() + "::" + an.getNumLine());
983 srcLocation = checkLocationFromExpressionNode(md, nametable, an.getDest(), srcLocation);
985 if (!CompositeLattice.isGreaterThan(srcLocation, destLocation)) {
986 throw new Error("Location " + destLocation
987 + " is not allowed to have the value flow that moves within the same location at "
988 + cd.getSourceFileName() + "::" + an.getNumLine());
996 private void assignLocationOfVarDescriptor(VarDescriptor vd, MethodDescriptor md,
997 SymbolTable nametable, TreeNode n) {
999 ClassDescriptor cd = md.getClassDesc();
1000 Vector<AnnotationDescriptor> annotationVec = vd.getType().getAnnotationMarkers();
1002 // currently enforce every variable to have corresponding location
1003 if (annotationVec.size() == 0) {
1004 throw new Error("Location is not assigned to variable " + vd.getSymbol() + " in the method "
1005 + md.getSymbol() + " of the class " + cd.getSymbol());
1008 if (annotationVec.size() > 1) { // variable can have at most one location
1009 throw new Error(vd.getSymbol() + " has more than one location.");
1012 AnnotationDescriptor ad = annotationVec.elementAt(0);
1014 if (ad.getType() == AnnotationDescriptor.SINGLE_ANNOTATION) {
1016 if (ad.getMarker().equals(SSJavaAnalysis.LOC)) {
1017 String locDec = ad.getValue(); // check if location is defined
1019 if (locDec.startsWith(SSJavaAnalysis.DELTA)) {
1020 DeltaLocation deltaLoc = parseDeltaDeclaration(md, n, locDec);
1021 d2loc.put(vd, deltaLoc);
1022 addTypeLocation(vd.getType(), deltaLoc);
1024 CompositeLocation compLoc = parseLocationDeclaration(md, n, locDec);
1025 d2loc.put(vd, compLoc);
1026 addTypeLocation(vd.getType(), compLoc);
1034 private DeltaLocation parseDeltaDeclaration(MethodDescriptor md, TreeNode n, String locDec) {
1037 int dIdx = locDec.indexOf(SSJavaAnalysis.DELTA);
1040 int beginIdx = dIdx + 6;
1041 locDec = locDec.substring(beginIdx, locDec.length() - 1);
1042 dIdx = locDec.indexOf(SSJavaAnalysis.DELTA);
1045 CompositeLocation compLoc = parseLocationDeclaration(md, n, locDec);
1046 DeltaLocation deltaLoc = new DeltaLocation(compLoc, deltaCount);
1051 private Location parseFieldLocDeclaraton(String decl) {
1053 int idx = decl.indexOf(".");
1054 String className = decl.substring(0, idx);
1055 String fieldName = decl.substring(idx + 1);
1057 Descriptor d = state.getClassSymbolTable().get(className);
1059 assert (d instanceof ClassDescriptor);
1060 SSJavaLattice<String> lattice = ssjava.getClassLattice((ClassDescriptor) d);
1061 if (!lattice.containsKey(fieldName)) {
1062 throw new Error("The location " + fieldName + " is not defined in the field lattice of '"
1063 + className + "'.");
1066 return new Location(d, fieldName);
1069 private CompositeLocation parseLocationDeclaration(MethodDescriptor md, TreeNode n, String locDec) {
1071 CompositeLocation compLoc = new CompositeLocation();
1073 StringTokenizer tokenizer = new StringTokenizer(locDec, ",");
1074 List<String> locIdList = new ArrayList<String>();
1075 while (tokenizer.hasMoreTokens()) {
1076 String locId = tokenizer.nextToken();
1077 locIdList.add(locId);
1080 // at least,one location element needs to be here!
1081 assert (locIdList.size() > 0);
1083 // assume that loc with idx 0 comes from the local lattice
1084 // loc with idx 1 comes from the field lattice
1086 String localLocId = locIdList.get(0);
1087 SSJavaLattice<String> localLattice = CompositeLattice.getLatticeByDescriptor(md);
1088 Location localLoc = new Location(md, localLocId);
1089 if (localLattice == null || (!localLattice.containsKey(localLocId))) {
1090 throw new Error("Location " + localLocId
1091 + " is not defined in the local variable lattice at "
1092 + md.getClassDesc().getSourceFileName() + "::" + (n != null ? n.getNumLine() : "") + ".");
1094 compLoc.addLocation(localLoc);
1096 for (int i = 1; i < locIdList.size(); i++) {
1097 String locName = locIdList.get(i);
1099 Location fieldLoc = parseFieldLocDeclaraton(locName);
1100 // ClassDescriptor cd = fieldLocName2cd.get(locName);
1101 // SSJavaLattice<String> fieldLattice =
1102 // CompositeLattice.getLatticeByDescriptor(cd);
1104 // if (fieldLattice == null || (!fieldLattice.containsKey(locName))) {
1105 // throw new Error("Location " + locName +
1106 // " is not defined in the field lattice at "
1107 // + cd.getSourceFileName() + ".");
1109 // Location fieldLoc = new Location(cd, locName);
1110 compLoc.addLocation(fieldLoc);
1117 private void checkDeclarationNode(MethodDescriptor md, SymbolTable nametable, DeclarationNode dn) {
1118 VarDescriptor vd = dn.getVarDescriptor();
1119 assignLocationOfVarDescriptor(vd, md, nametable, dn);
1122 private void checkClass(ClassDescriptor cd) {
1123 // Check to see that methods respects ss property
1124 for (Iterator method_it = cd.getMethods(); method_it.hasNext();) {
1125 MethodDescriptor md = (MethodDescriptor) method_it.next();
1126 checkMethodDeclaration(cd, md);
1130 private void checkDeclarationInClass(ClassDescriptor cd) {
1131 // Check to see that fields are okay
1132 for (Iterator field_it = cd.getFields(); field_it.hasNext();) {
1133 FieldDescriptor fd = (FieldDescriptor) field_it.next();
1134 checkFieldDeclaration(cd, fd);
1138 private void checkMethodDeclaration(ClassDescriptor cd, MethodDescriptor md) {
1142 private void checkFieldDeclaration(ClassDescriptor cd, FieldDescriptor fd) {
1144 Vector<AnnotationDescriptor> annotationVec = fd.getType().getAnnotationMarkers();
1146 // currently enforce every field to have corresponding location
1147 if (annotationVec.size() == 0) {
1148 throw new Error("Location is not assigned to the field '" + fd.getSymbol()
1149 + "' of the class " + cd.getSymbol() + " at " + cd.getSourceFileName());
1152 if (annotationVec.size() > 1) {
1153 // variable can have at most one location
1154 throw new Error("Field " + fd.getSymbol() + " of class " + cd
1155 + " has more than one location.");
1158 AnnotationDescriptor ad = annotationVec.elementAt(0);
1160 if (ad.getType() == AnnotationDescriptor.SINGLE_ANNOTATION) {
1162 if (ad.getMarker().equals(SSJavaAnalysis.LOC)) {
1163 String locationID = ad.getValue();
1164 // check if location is defined
1165 SSJavaLattice<String> lattice = ssjava.getClassLattice(cd);
1166 if (lattice == null || (!lattice.containsKey(locationID))) {
1167 throw new Error("Location " + locationID
1168 + " is not defined in the field lattice of class " + cd.getSymbol() + " at"
1169 + cd.getSourceFileName() + ".");
1171 Location loc = new Location(cd, locationID);
1172 // d2loc.put(fd, loc);
1173 addTypeLocation(fd.getType(), loc);
1180 private void addTypeLocation(TypeDescriptor type, CompositeLocation loc) {
1182 type.setExtension(loc);
1186 private void addTypeLocation(TypeDescriptor type, Location loc) {
1188 type.setExtension(loc);
1192 static class CompositeLattice {
1194 public static boolean isGreaterThan(CompositeLocation loc1, CompositeLocation loc2) {
1196 // System.out.println("isGreaterThan= " + loc1 + " " + loc2);
1198 int baseCompareResult = compareBaseLocationSet(loc1, loc2);
1199 if (baseCompareResult == ComparisonResult.EQUAL) {
1200 if (compareDelta(loc1, loc2) == ComparisonResult.GREATER) {
1205 } else if (baseCompareResult == ComparisonResult.GREATER) {
1213 public static int compare(CompositeLocation loc1, CompositeLocation loc2) {
1215 // System.out.println("compare=" + loc1 + " " + loc2);
1216 int baseCompareResult = compareBaseLocationSet(loc1, loc2);
1218 if (baseCompareResult == ComparisonResult.EQUAL) {
1219 return compareDelta(loc1, loc2);
1221 return baseCompareResult;
1226 private static int compareDelta(CompositeLocation dLoc1, CompositeLocation dLoc2) {
1228 int deltaCount1 = 0;
1229 int deltaCount2 = 0;
1230 if (dLoc1 instanceof DeltaLocation) {
1231 deltaCount1 = ((DeltaLocation) dLoc1).getNumDelta();
1234 if (dLoc2 instanceof DeltaLocation) {
1235 deltaCount2 = ((DeltaLocation) dLoc2).getNumDelta();
1237 if (deltaCount1 < deltaCount2) {
1238 return ComparisonResult.GREATER;
1239 } else if (deltaCount1 == deltaCount2) {
1240 return ComparisonResult.EQUAL;
1242 return ComparisonResult.LESS;
1247 private static int compareBaseLocationSet(CompositeLocation compLoc1, CompositeLocation compLoc2) {
1249 // if compLoc1 is greater than compLoc2, return true
1250 // else return false;
1252 // compare one by one in according to the order of the tuple
1254 for (int i = 0; i < compLoc1.getSize(); i++) {
1255 Location loc1 = compLoc1.get(i);
1256 if (i >= compLoc2.getSize()) {
1257 throw new Error("Failed to compare two locations of " + compLoc1 + " and " + compLoc2
1258 + " because they are not comparable.");
1260 Location loc2 = compLoc2.get(i);
1262 if (!loc1.getDescriptor().equals(loc2.getDescriptor())) {
1263 throw new Error("Failed to compare two locations of " + compLoc1 + " and " + compLoc2
1264 + " because they are not comparable.");
1267 Descriptor d1 = loc1.getDescriptor();
1268 Descriptor d2 = loc2.getDescriptor();
1270 SSJavaLattice<String> lattice1 = getLatticeByDescriptor(d1);
1271 SSJavaLattice<String> lattice2 = getLatticeByDescriptor(d2);
1273 // check if the spin location is appeared only at the end of the
1274 // composite location
1275 if (lattice1.getSpinLocSet().contains(loc1.getLocIdentifier())) {
1276 if (i != (compLoc1.getSize() - 1)) {
1277 throw new Error("The spin location " + loc1.getLocIdentifier()
1278 + " cannot be appeared in the middle of composite location.");
1282 if (lattice2.getSpinLocSet().contains(loc2.getLocIdentifier())) {
1283 if (i != (compLoc2.getSize() - 1)) {
1284 throw new Error("The spin location " + loc2.getLocIdentifier()
1285 + " cannot be appeared in the middle of composite location.");
1289 if (!lattice1.equals(lattice2)) {
1290 throw new Error("Failed to compare two locations of " + compLoc1 + " and " + compLoc2
1291 + " because they are not comparable.");
1294 if (loc1.getLocIdentifier().equals(loc2.getLocIdentifier())) {
1296 // check if the current location is the spinning location
1297 // note that the spinning location only can be appeared in the last
1298 // part of the composite location
1299 if (numOfTie == compLoc1.getSize()
1300 && lattice1.getSpinLocSet().contains(loc1.getLocIdentifier())) {
1301 return ComparisonResult.GREATER;
1304 } else if (lattice1.isGreaterThan(loc1.getLocIdentifier(), loc2.getLocIdentifier())) {
1305 return ComparisonResult.GREATER;
1307 return ComparisonResult.LESS;
1312 if (numOfTie == compLoc1.getSize()) {
1314 if (numOfTie != compLoc2.getSize()) {
1315 throw new Error("Failed to compare two locations of " + compLoc1 + " and " + compLoc2
1316 + " because they are not comparable.");
1319 return ComparisonResult.EQUAL;
1322 return ComparisonResult.LESS;
1326 public static CompositeLocation calculateGLB(Set<CompositeLocation> inputSet) {
1328 // System.out.println("Calculating GLB=" + inputSet);
1329 CompositeLocation glbCompLoc = new CompositeLocation();
1331 // calculate GLB of the first(priority) element
1332 Set<String> priorityLocIdentifierSet = new HashSet<String>();
1333 Descriptor priorityDescriptor = null;
1335 Hashtable<String, Set<CompositeLocation>> locId2CompLocSet =
1336 new Hashtable<String, Set<CompositeLocation>>();
1337 // mapping from the priority loc ID to its full representation by the
1338 // composite location
1340 int maxTupleSize = 0;
1341 CompositeLocation maxCompLoc = null;
1343 for (Iterator iterator = inputSet.iterator(); iterator.hasNext();) {
1344 CompositeLocation compLoc = (CompositeLocation) iterator.next();
1345 if (compLoc.getSize() > maxTupleSize) {
1346 maxTupleSize = compLoc.getSize();
1347 maxCompLoc = compLoc;
1349 Location priorityLoc = compLoc.get(0);
1350 String priorityLocId = priorityLoc.getLocIdentifier();
1351 priorityLocIdentifierSet.add(priorityLocId);
1353 if (locId2CompLocSet.containsKey(priorityLocId)) {
1354 locId2CompLocSet.get(priorityLocId).add(compLoc);
1356 Set<CompositeLocation> newSet = new HashSet<CompositeLocation>();
1357 newSet.add(compLoc);
1358 locId2CompLocSet.put(priorityLocId, newSet);
1361 // check if priority location are coming from the same lattice
1362 if (priorityDescriptor == null) {
1363 priorityDescriptor = priorityLoc.getDescriptor();
1364 } else if (!priorityDescriptor.equals(priorityLoc.getDescriptor())) {
1365 throw new Error("Failed to calculate GLB of " + inputSet
1366 + " because they are from different lattices.");
1370 SSJavaLattice<String> locOrder = getLatticeByDescriptor(priorityDescriptor);
1371 String glbOfPriorityLoc = locOrder.getGLB(priorityLocIdentifierSet);
1373 glbCompLoc.addLocation(new Location(priorityDescriptor, glbOfPriorityLoc));
1374 Set<CompositeLocation> compSet = locId2CompLocSet.get(glbOfPriorityLoc);
1376 // here find out composite location that has a maximum length tuple
1377 // if we have three input set: [A], [A,B], [A,B,C]
1378 // maximum length tuple will be [A,B,C]
1380 CompositeLocation maxFromCompSet = null;
1381 for (Iterator iterator = compSet.iterator(); iterator.hasNext();) {
1382 CompositeLocation c = (CompositeLocation) iterator.next();
1383 if (c.getSize() > max) {
1389 if (compSet == null) {
1390 // when GLB(x1,x2)!=x1 and !=x2 : GLB case 4
1391 // mean that the result is already lower than <x1,y1> and <x2,y2>
1392 // assign TOP to the rest of the location elements
1394 // in this case, do not take care about delta
1395 // CompositeLocation inputComp = inputSet.iterator().next();
1396 CompositeLocation inputComp = maxCompLoc;
1397 for (int i = 1; i < inputComp.getSize(); i++) {
1398 glbCompLoc.addLocation(Location.createTopLocation(inputComp.get(i).getDescriptor()));
1401 if (compSet.size() == 1) {
1403 // if GLB(x1,x2)==x1 or x2 : GLB case 2,3
1404 CompositeLocation comp = compSet.iterator().next();
1405 for (int i = 1; i < comp.getSize(); i++) {
1406 glbCompLoc.addLocation(comp.get(i));
1409 // if input location corresponding to glb is a delta, need to apply
1410 // delta to glb result
1411 if (comp instanceof DeltaLocation) {
1412 glbCompLoc = new DeltaLocation(glbCompLoc, 1);
1416 // when GLB(x1,x2)==x1 and x2 : GLB case 1
1417 // if more than one location shares the same priority GLB
1418 // need to calculate the rest of GLB loc
1420 // int compositeLocSize = compSet.iterator().next().getSize();
1421 int compositeLocSize = maxFromCompSet.getSize();
1423 Set<String> glbInputSet = new HashSet<String>();
1424 Descriptor currentD = null;
1425 for (int i = 1; i < compositeLocSize; i++) {
1426 for (Iterator iterator = compSet.iterator(); iterator.hasNext();) {
1427 CompositeLocation compositeLocation = (CompositeLocation) iterator.next();
1428 if (compositeLocation.getSize() > i) {
1429 Location currentLoc = compositeLocation.get(i);
1430 currentD = currentLoc.getDescriptor();
1431 // making set of the current location sharing the same idx
1432 glbInputSet.add(currentLoc.getLocIdentifier());
1435 // calculate glb for the current lattice
1437 SSJavaLattice<String> currentLattice = getLatticeByDescriptor(currentD);
1438 String currentGLBLocId = currentLattice.getGLB(glbInputSet);
1439 glbCompLoc.addLocation(new Location(currentD, currentGLBLocId));
1442 // if input location corresponding to glb is a delta, need to apply
1443 // delta to glb result
1445 for (Iterator iterator = compSet.iterator(); iterator.hasNext();) {
1446 CompositeLocation compLoc = (CompositeLocation) iterator.next();
1447 if (compLoc instanceof DeltaLocation) {
1448 if (glbCompLoc.equals(compLoc)) {
1449 glbCompLoc = new DeltaLocation(glbCompLoc, 1);
1462 static SSJavaLattice<String> getLatticeByDescriptor(Descriptor d) {
1464 SSJavaLattice<String> lattice = null;
1466 if (d instanceof ClassDescriptor) {
1467 lattice = ssjava.getCd2lattice().get(d);
1468 } else if (d instanceof MethodDescriptor) {
1469 if (ssjava.getMd2lattice().containsKey(d)) {
1470 lattice = ssjava.getMd2lattice().get(d);
1472 // use default lattice for the method
1473 lattice = ssjava.getCd2methodDefault().get(((MethodDescriptor) d).getClassDesc());
1482 class ComparisonResult {
1484 public static final int GREATER = 0;
1485 public static final int EQUAL = 1;
1486 public static final int LESS = 2;
1487 public static final int INCOMPARABLE = 3;
1494 class ReturnLocGenerator {
1496 public static final int PARAMISHIGHER = 0;
1497 public static final int PARAMISSAME = 1;
1498 public static final int IGNORE = 2;
1500 Hashtable<Integer, Integer> paramIdx2paramType;
1502 public ReturnLocGenerator(CompositeLocation returnLoc, List<CompositeLocation> params) {
1503 // creating mappings
1505 paramIdx2paramType = new Hashtable<Integer, Integer>();
1506 for (int i = 0; i < params.size(); i++) {
1507 CompositeLocation param = params.get(i);
1508 int compareResult = CompositeLattice.compare(param, returnLoc);
1511 if (compareResult == ComparisonResult.GREATER) {
1513 } else if (compareResult == ComparisonResult.EQUAL) {
1518 paramIdx2paramType.put(new Integer(i), new Integer(type));
1523 public CompositeLocation computeReturnLocation(List<CompositeLocation> args) {
1525 // compute the highest possible location in caller's side
1526 assert paramIdx2paramType.keySet().size() == args.size();
1528 Set<CompositeLocation> inputGLB = new HashSet<CompositeLocation>();
1529 for (int i = 0; i < args.size(); i++) {
1530 int type = (paramIdx2paramType.get(new Integer(i))).intValue();
1531 CompositeLocation argLoc = args.get(i);
1532 if (type == PARAMISHIGHER) {
1533 // return loc is lower than param
1534 DeltaLocation delta = new DeltaLocation(argLoc, 1);
1535 inputGLB.add(delta);
1536 } else if (type == PARAMISSAME) {
1537 // return loc is equal or lower than param
1538 inputGLB.add(argLoc);
1542 // compute GLB of arguments subset that are same or higher than return
1544 CompositeLocation glb = CompositeLattice.calculateGLB(inputGLB);