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.needTobeAnnotated(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.needTobeAnnotated(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) {
153 if (subLattice == null) {
154 throw new Error("If a parent class '" + superCd
155 + "' has a ordering lattice, its subclass '" + cd + "' should have one.");
158 Set<Pair<String, String>> superPairSet = superLattice.getOrderingPairSet();
159 Set<Pair<String, String>> subPairSet = subLattice.getOrderingPairSet();
161 for (Iterator iterator = superPairSet.iterator(); iterator.hasNext();) {
162 Pair<String, String> pair = (Pair<String, String>) iterator.next();
164 if (!subPairSet.contains(pair)) {
165 throw new Error("Subclass '" + cd + "' does not have the relative ordering '"
166 + pair.getSecond() + " < " + pair.getFirst()
167 + "' that is defined by its superclass '" + superCd + "'.");
172 // if super class doesn't define lattice, then we don't need to check its
177 public Hashtable getMap() {
181 private void checkDeclarationInMethodBody(ClassDescriptor cd, MethodDescriptor md) {
182 BlockNode bn = state.getMethodBody(md);
184 // parsing returnloc annotation
185 if (ssjava.needTobeAnnotated(md)) {
187 Vector<AnnotationDescriptor> methodAnnotations = md.getModifiers().getAnnotations();
188 if (methodAnnotations != null) {
189 for (int i = 0; i < methodAnnotations.size(); i++) {
190 AnnotationDescriptor an = methodAnnotations.elementAt(i);
191 if (an.getMarker().equals(ssjava.RETURNLOC)) {
192 // developer explicitly defines method lattice
193 String returnLocDeclaration = an.getValue();
194 CompositeLocation returnLocComp =
195 parseLocationDeclaration(md, null, returnLocDeclaration);
196 md2ReturnLoc.put(md, returnLocComp);
200 if (!md.getReturnType().isVoid() && !md2ReturnLoc.containsKey(md)) {
201 throw new Error("Return location is not specified for the method " + md + " at "
202 + cd.getSourceFileName());
208 List<CompositeLocation> paramList = new ArrayList<CompositeLocation>();
210 boolean hasReturnValue = (!md.getReturnType().isVoid());
211 if (hasReturnValue) {
212 MethodLattice<String> methodLattice = ssjava.getMethodLattice(md);
213 String thisLocId = methodLattice.getThisLoc();
214 CompositeLocation thisLoc = new CompositeLocation(new Location(md, thisLocId));
215 paramList.add(thisLoc);
218 for (int i = 0; i < md.numParameters(); i++) {
219 // process annotations on method parameters
220 VarDescriptor vd = (VarDescriptor) md.getParameter(i);
221 assignLocationOfVarDescriptor(vd, md, md.getParameterTable(), bn);
222 if (hasReturnValue) {
223 paramList.add(d2loc.get(vd));
227 if (hasReturnValue) {
228 md2ReturnLocGen.put(md, new ReturnLocGenerator(md2ReturnLoc.get(md), paramList));
231 checkDeclarationInBlockNode(md, md.getParameterTable(), bn);
234 private void checkDeclarationInBlockNode(MethodDescriptor md, SymbolTable nametable, BlockNode bn) {
235 bn.getVarTable().setParent(nametable);
236 for (int i = 0; i < bn.size(); i++) {
237 BlockStatementNode bsn = bn.get(i);
238 checkDeclarationInBlockStatementNode(md, bn.getVarTable(), bsn);
242 private void checkDeclarationInBlockStatementNode(MethodDescriptor md, SymbolTable nametable,
243 BlockStatementNode bsn) {
245 switch (bsn.kind()) {
246 case Kind.SubBlockNode:
247 checkDeclarationInSubBlockNode(md, nametable, (SubBlockNode) bsn);
250 case Kind.DeclarationNode:
251 checkDeclarationNode(md, nametable, (DeclarationNode) bsn);
255 checkDeclarationInLoopNode(md, nametable, (LoopNode) bsn);
260 private void checkDeclarationInLoopNode(MethodDescriptor md, SymbolTable nametable, LoopNode ln) {
262 if (ln.getType() == LoopNode.FORLOOP) {
263 // check for loop case
264 ClassDescriptor cd = md.getClassDesc();
265 BlockNode bn = ln.getInitializer();
266 for (int i = 0; i < bn.size(); i++) {
267 BlockStatementNode bsn = bn.get(i);
268 checkDeclarationInBlockStatementNode(md, nametable, bsn);
273 checkDeclarationInBlockNode(md, nametable, ln.getBody());
276 private void checkMethodBody(ClassDescriptor cd, MethodDescriptor md) {
277 BlockNode bn = state.getMethodBody(md);
278 checkLocationFromBlockNode(md, md.getParameterTable(), bn);
281 private CompositeLocation checkLocationFromBlockNode(MethodDescriptor md, SymbolTable nametable,
284 bn.getVarTable().setParent(nametable);
285 // it will return the lowest location in the block node
286 CompositeLocation lowestLoc = null;
288 for (int i = 0; i < bn.size(); i++) {
289 BlockStatementNode bsn = bn.get(i);
290 CompositeLocation bLoc = checkLocationFromBlockStatementNode(md, bn.getVarTable(), bsn);
291 if (!bLoc.isEmpty()) {
292 if (lowestLoc == null) {
295 if (CompositeLattice.isGreaterThan(lowestLoc, bLoc)) {
303 if (lowestLoc == null) {
304 lowestLoc = new CompositeLocation(Location.createBottomLocation(md));
310 private CompositeLocation checkLocationFromBlockStatementNode(MethodDescriptor md,
311 SymbolTable nametable, BlockStatementNode bsn) {
313 CompositeLocation compLoc = null;
314 switch (bsn.kind()) {
315 case Kind.BlockExpressionNode:
316 compLoc = checkLocationFromBlockExpressionNode(md, nametable, (BlockExpressionNode) bsn);
319 case Kind.DeclarationNode:
320 compLoc = checkLocationFromDeclarationNode(md, nametable, (DeclarationNode) bsn);
323 case Kind.IfStatementNode:
324 compLoc = checkLocationFromIfStatementNode(md, nametable, (IfStatementNode) bsn);
328 compLoc = checkLocationFromLoopNode(md, nametable, (LoopNode) bsn);
331 case Kind.ReturnNode:
332 compLoc = checkLocationFromReturnNode(md, nametable, (ReturnNode) bsn);
335 case Kind.SubBlockNode:
336 compLoc = checkLocationFromSubBlockNode(md, nametable, (SubBlockNode) bsn);
339 case Kind.ContinueBreakNode:
340 compLoc = new CompositeLocation();
347 private CompositeLocation checkLocationFromReturnNode(MethodDescriptor md, SymbolTable nametable,
350 ExpressionNode returnExp = rn.getReturnExpression();
352 CompositeLocation expLoc;
353 if (returnExp != null) {
354 expLoc = checkLocationFromExpressionNode(md, nametable, returnExp, new CompositeLocation());
355 // check if return value is equal or higher than RETRUNLOC of method
356 // declaration annotation
357 CompositeLocation returnLocAt = md2ReturnLoc.get(md);
359 if (CompositeLattice.isGreaterThan(returnLocAt, expLoc)) {
361 "Return value location is not equal or higher than the declaraed return location at "
362 + md.getClassDesc().getSourceFileName() + "::" + rn.getNumLine());
366 return new CompositeLocation();
369 private boolean hasOnlyLiteralValue(ExpressionNode en) {
370 if (en.kind() == Kind.LiteralNode) {
377 private CompositeLocation checkLocationFromLoopNode(MethodDescriptor md, SymbolTable nametable,
380 ClassDescriptor cd = md.getClassDesc();
381 if (ln.getType() == LoopNode.WHILELOOP || ln.getType() == LoopNode.DOWHILELOOP) {
383 CompositeLocation condLoc =
384 checkLocationFromExpressionNode(md, nametable, ln.getCondition(), new CompositeLocation());
385 addLocationType(ln.getCondition().getType(), (condLoc));
387 CompositeLocation bodyLoc = checkLocationFromBlockNode(md, nametable, ln.getBody());
389 if (!CompositeLattice.isGreaterThan(condLoc, bodyLoc)) {
390 // loop condition should be higher than loop body
392 "The location of the while-condition statement is lower than the loop body at "
393 + cd.getSourceFileName() + ":" + ln.getCondition().getNumLine());
399 // check for loop case
400 BlockNode bn = ln.getInitializer();
401 bn.getVarTable().setParent(nametable);
403 // calculate glb location of condition and update statements
404 CompositeLocation condLoc =
405 checkLocationFromExpressionNode(md, bn.getVarTable(), ln.getCondition(),
406 new CompositeLocation());
407 addLocationType(ln.getCondition().getType(), condLoc);
409 CompositeLocation updateLoc =
410 checkLocationFromBlockNode(md, bn.getVarTable(), ln.getUpdate());
412 Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
413 glbInputSet.add(condLoc);
414 // glbInputSet.add(updateLoc);
416 CompositeLocation glbLocOfForLoopCond = CompositeLattice.calculateGLB(glbInputSet);
418 // check location of 'forloop' body
419 CompositeLocation blockLoc = checkLocationFromBlockNode(md, bn.getVarTable(), ln.getBody());
421 // compute glb of body including loop body and update statement
424 if (blockLoc == null) {
425 // when there is no statement in the loop body
427 if (updateLoc == null) {
428 // also there is no update statement in the loop body
429 return glbLocOfForLoopCond;
431 glbInputSet.add(updateLoc);
434 glbInputSet.add(blockLoc);
435 glbInputSet.add(updateLoc);
438 CompositeLocation loopBodyLoc = CompositeLattice.calculateGLB(glbInputSet);
440 if (!CompositeLattice.isGreaterThan(glbLocOfForLoopCond, loopBodyLoc)) {
442 "The location of the for-condition statement is lower than the for-loop body at "
443 + cd.getSourceFileName() + ":" + ln.getCondition().getNumLine());
450 private CompositeLocation checkLocationFromSubBlockNode(MethodDescriptor md,
451 SymbolTable nametable, SubBlockNode sbn) {
452 CompositeLocation compLoc = checkLocationFromBlockNode(md, nametable, sbn.getBlockNode());
456 private CompositeLocation checkLocationFromIfStatementNode(MethodDescriptor md,
457 SymbolTable nametable, IfStatementNode isn) {
459 ClassDescriptor localCD = md.getClassDesc();
460 Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
462 CompositeLocation condLoc =
463 checkLocationFromExpressionNode(md, nametable, isn.getCondition(), new CompositeLocation());
465 addLocationType(isn.getCondition().getType(), condLoc);
466 glbInputSet.add(condLoc);
468 CompositeLocation locTrueBlock = checkLocationFromBlockNode(md, nametable, isn.getTrueBlock());
469 if (locTrueBlock != null) {
470 glbInputSet.add(locTrueBlock);
471 // here, the location of conditional block should be higher than the
472 // location of true/false blocks
473 if (locTrueBlock != null && !CompositeLattice.isGreaterThan(condLoc, locTrueBlock)) {
476 "The location of the if-condition statement is lower than the conditional block at "
477 + localCD.getSourceFileName() + ":" + isn.getCondition().getNumLine());
481 if (isn.getFalseBlock() != null) {
482 CompositeLocation locFalseBlock =
483 checkLocationFromBlockNode(md, nametable, isn.getFalseBlock());
485 if (locFalseBlock != null) {
486 glbInputSet.add(locFalseBlock);
488 if (!CompositeLattice.isGreaterThan(condLoc, locFalseBlock)) {
491 "The location of the if-condition statement is lower than the conditional block at "
492 + localCD.getSourceFileName() + ":" + isn.getCondition().getNumLine());
498 // return GLB location of condition, true, and false block
499 CompositeLocation glbLoc = CompositeLattice.calculateGLB(glbInputSet);
504 private CompositeLocation checkLocationFromDeclarationNode(MethodDescriptor md,
505 SymbolTable nametable, DeclarationNode dn) {
507 VarDescriptor vd = dn.getVarDescriptor();
509 CompositeLocation destLoc = d2loc.get(vd);
511 if (dn.getExpression() != null) {
512 CompositeLocation expressionLoc =
513 checkLocationFromExpressionNode(md, nametable, dn.getExpression(),
514 new CompositeLocation());
515 // addTypeLocation(dn.getExpression().getType(), expressionLoc);
517 if (expressionLoc != null) {
518 // checking location order
519 if (!CompositeLattice.isGreaterThan(expressionLoc, destLoc)) {
520 throw new Error("The value flow from " + expressionLoc + " to " + destLoc
521 + " does not respect location hierarchy on the assignment " + dn.printNode(0)
522 + " at " + md.getClassDesc().getSourceFileName() + "::" + dn.getNumLine());
525 return expressionLoc;
529 return new CompositeLocation();
535 private void checkDeclarationInSubBlockNode(MethodDescriptor md, SymbolTable nametable,
537 checkDeclarationInBlockNode(md, nametable.getParent(), sbn.getBlockNode());
540 private CompositeLocation checkLocationFromBlockExpressionNode(MethodDescriptor md,
541 SymbolTable nametable, BlockExpressionNode ben) {
542 CompositeLocation compLoc =
543 checkLocationFromExpressionNode(md, nametable, ben.getExpression(), null);
544 // addTypeLocation(ben.getExpression().getType(), compLoc);
548 private CompositeLocation checkLocationFromExpressionNode(MethodDescriptor md,
549 SymbolTable nametable, ExpressionNode en, CompositeLocation loc) {
551 CompositeLocation compLoc = null;
554 case Kind.AssignmentNode:
555 compLoc = checkLocationFromAssignmentNode(md, nametable, (AssignmentNode) en, loc);
558 case Kind.FieldAccessNode:
559 compLoc = checkLocationFromFieldAccessNode(md, nametable, (FieldAccessNode) en, loc);
563 compLoc = checkLocationFromNameNode(md, nametable, (NameNode) en, loc);
567 compLoc = checkLocationFromOpNode(md, nametable, (OpNode) en);
570 case Kind.CreateObjectNode:
571 compLoc = checkLocationFromCreateObjectNode(md, nametable, (CreateObjectNode) en);
574 case Kind.ArrayAccessNode:
575 compLoc = checkLocationFromArrayAccessNode(md, nametable, (ArrayAccessNode) en);
578 case Kind.LiteralNode:
579 compLoc = checkLocationFromLiteralNode(md, nametable, (LiteralNode) en, loc);
582 case Kind.MethodInvokeNode:
583 compLoc = checkLocationFromMethodInvokeNode(md, nametable, (MethodInvokeNode) en, loc);
586 case Kind.TertiaryNode:
587 compLoc = checkLocationFromTertiaryNode(md, nametable, (TertiaryNode) en);
591 compLoc = checkLocationFromCastNode(md, nametable, (CastNode) en);
594 // case Kind.InstanceOfNode:
595 // checkInstanceOfNode(md, nametable, (InstanceOfNode) en, td);
598 // case Kind.ArrayInitializerNode:
599 // checkArrayInitializerNode(md, nametable, (ArrayInitializerNode) en,
603 // case Kind.ClassTypeNode:
604 // checkClassTypeNode(md, nametable, (ClassTypeNode) en, td);
607 // case Kind.OffsetNode:
608 // checkOffsetNode(md, nametable, (OffsetNode)en, td);
615 // addTypeLocation(en.getType(), compLoc);
620 private CompositeLocation checkLocationFromCastNode(MethodDescriptor md, SymbolTable nametable,
623 ExpressionNode en = cn.getExpression();
624 return checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation());
628 private CompositeLocation checkLocationFromTertiaryNode(MethodDescriptor md,
629 SymbolTable nametable, TertiaryNode tn) {
630 ClassDescriptor cd = md.getClassDesc();
632 CompositeLocation condLoc =
633 checkLocationFromExpressionNode(md, nametable, tn.getCond(), new CompositeLocation());
634 addLocationType(tn.getCond().getType(), condLoc);
635 CompositeLocation trueLoc =
636 checkLocationFromExpressionNode(md, nametable, tn.getTrueExpr(), new CompositeLocation());
637 addLocationType(tn.getTrueExpr().getType(), trueLoc);
638 CompositeLocation falseLoc =
639 checkLocationFromExpressionNode(md, nametable, tn.getFalseExpr(), new CompositeLocation());
640 addLocationType(tn.getFalseExpr().getType(), falseLoc);
642 // check if condLoc is higher than trueLoc & falseLoc
643 if (!CompositeLattice.isGreaterThan(condLoc, trueLoc)) {
645 "The location of the condition expression is lower than the true expression at "
646 + cd.getSourceFileName() + ":" + tn.getCond().getNumLine());
649 if (!CompositeLattice.isGreaterThan(condLoc, falseLoc)) {
651 "The location of the condition expression is lower than the true expression at "
652 + cd.getSourceFileName() + ":" + tn.getCond().getNumLine());
655 // then, return glb of trueLoc & falseLoc
656 Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
657 glbInputSet.add(trueLoc);
658 glbInputSet.add(falseLoc);
660 return CompositeLattice.calculateGLB(glbInputSet);
663 private CompositeLocation checkLocationFromMethodInvokeNode(MethodDescriptor md,
664 SymbolTable nametable, MethodInvokeNode min, CompositeLocation loc) {
666 checkCalleeConstraints(md, nametable, min);
668 CompositeLocation baseLocation = null;
669 if (min.getExpression() != null) {
671 checkLocationFromExpressionNode(md, nametable, min.getExpression(),
672 new CompositeLocation());
674 String thisLocId = ssjava.getMethodLattice(md).getThisLoc();
675 baseLocation = new CompositeLocation(new Location(md, thisLocId));
678 if (!min.getMethod().getReturnType().isVoid()) {
679 // If method has a return value, compute the highest possible return
680 // location in the caller's perspective
681 CompositeLocation ceilingLoc =
682 computeCeilingLocationForCaller(md, nametable, min, baseLocation);
686 return new CompositeLocation();
690 private CompositeLocation computeCeilingLocationForCaller(MethodDescriptor md,
691 SymbolTable nametable, MethodInvokeNode min, CompositeLocation baseLocation) {
692 List<CompositeLocation> argList = new ArrayList<CompositeLocation>();
694 // by default, method has a THIS parameter
695 argList.add(baseLocation);
697 for (int i = 0; i < min.numArgs(); i++) {
698 ExpressionNode en = min.getArg(i);
699 CompositeLocation callerArg =
700 checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation());
701 argList.add(callerArg);
704 return md2ReturnLocGen.get(min.getMethod()).computeReturnLocation(argList);
708 private void checkCalleeConstraints(MethodDescriptor md, SymbolTable nametable,
709 MethodInvokeNode min) {
711 if (min.numArgs() > 1) {
712 // caller needs to guarantee that it passes arguments in regarding to
713 // callee's hierarchy
714 for (int i = 0; i < min.numArgs(); i++) {
715 ExpressionNode en = min.getArg(i);
716 CompositeLocation callerArg1 =
717 checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation());
719 ClassDescriptor calleecd = min.getMethod().getClassDesc();
720 VarDescriptor calleevd = (VarDescriptor) min.getMethod().getParameter(i);
721 CompositeLocation calleeLoc1 = d2loc.get(calleevd);
723 if (!callerArg1.get(0).isTop()) {
724 // here, check if ordering relations among caller's args respect
725 // ordering relations in-between callee's args
726 for (int currentIdx = 0; currentIdx < min.numArgs(); currentIdx++) {
727 if (currentIdx != i) { // skip itself
728 ExpressionNode argExp = min.getArg(currentIdx);
730 CompositeLocation callerArg2 =
731 checkLocationFromExpressionNode(md, nametable, argExp, new CompositeLocation());
733 VarDescriptor calleevd2 = (VarDescriptor) min.getMethod().getParameter(currentIdx);
734 CompositeLocation calleeLoc2 = d2loc.get(calleevd2);
736 int callerResult = CompositeLattice.compare(callerArg1, callerArg2);
737 int calleeResult = CompositeLattice.compare(calleeLoc1, calleeLoc2);
738 if (calleeResult == ComparisonResult.GREATER
739 && callerResult != ComparisonResult.GREATER) {
740 // If calleeLoc1 is higher than calleeLoc2
741 // then, caller should have same ordering relation in-bet
742 // callerLoc1 & callerLoc2
744 throw new Error("Caller doesn't respect ordering relations among method arguments:"
745 + md.getClassDesc().getSourceFileName() + ":" + min.getNumLine());
758 private CompositeLocation checkLocationFromArrayAccessNode(MethodDescriptor md,
759 SymbolTable nametable, ArrayAccessNode aan) {
761 // return glb location of array itself and index
763 ClassDescriptor cd = md.getClassDesc();
765 Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
767 CompositeLocation arrayLoc =
768 checkLocationFromExpressionNode(md, nametable, aan.getExpression(), new CompositeLocation());
769 // addTypeLocation(aan.getExpression().getType(), arrayLoc);
770 glbInputSet.add(arrayLoc);
771 CompositeLocation indexLoc =
772 checkLocationFromExpressionNode(md, nametable, aan.getIndex(), new CompositeLocation());
773 glbInputSet.add(indexLoc);
774 // addTypeLocation(aan.getIndex().getType(), indexLoc);
776 CompositeLocation glbLoc = CompositeLattice.calculateGLB(glbInputSet);
780 private CompositeLocation checkLocationFromCreateObjectNode(MethodDescriptor md,
781 SymbolTable nametable, CreateObjectNode con) {
783 ClassDescriptor cd = md.getClassDesc();
786 Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
787 for (int i = 0; i < con.numArgs(); i++) {
788 ExpressionNode en = con.getArg(i);
789 CompositeLocation argLoc =
790 checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation());
791 glbInputSet.add(argLoc);
792 addLocationType(en.getType(), argLoc);
795 // check array initializers
796 // if ((con.getArrayInitializer() != null)) {
797 // checkLocationFromArrayInitializerNode(md, nametable,
798 // con.getArrayInitializer());
801 if (glbInputSet.size() > 0) {
802 return CompositeLattice.calculateGLB(glbInputSet);
805 CompositeLocation compLoc = new CompositeLocation();
806 compLoc.addLocation(Location.createTopLocation(md));
811 private CompositeLocation checkLocationFromOpNode(MethodDescriptor md, SymbolTable nametable,
814 ClassDescriptor cd = md.getClassDesc();
815 CompositeLocation leftLoc = new CompositeLocation();
816 leftLoc = checkLocationFromExpressionNode(md, nametable, on.getLeft(), leftLoc);
817 // addTypeLocation(on.getLeft().getType(), leftLoc);
819 CompositeLocation rightLoc = new CompositeLocation();
820 if (on.getRight() != null) {
821 rightLoc = checkLocationFromExpressionNode(md, nametable, on.getRight(), rightLoc);
822 // addTypeLocation(on.getRight().getType(), rightLoc);
825 // System.out.println("checking op node=" + on.printNode(0));
826 // System.out.println("left loc=" + leftLoc + " from " +
827 // on.getLeft().getClass());
828 // System.out.println("right loc=" + rightLoc + " from " +
829 // on.getRight().getClass());
831 Operation op = on.getOp();
833 switch (op.getOp()) {
835 case Operation.UNARYPLUS:
836 case Operation.UNARYMINUS:
837 case Operation.LOGIC_NOT:
841 case Operation.LOGIC_OR:
842 case Operation.LOGIC_AND:
844 case Operation.BIT_OR:
845 case Operation.BIT_XOR:
846 case Operation.BIT_AND:
847 case Operation.ISAVAILABLE:
848 case Operation.EQUAL:
849 case Operation.NOTEQUAL:
859 case Operation.LEFTSHIFT:
860 case Operation.RIGHTSHIFT:
861 case Operation.URIGHTSHIFT:
863 Set<CompositeLocation> inputSet = new HashSet<CompositeLocation>();
864 inputSet.add(leftLoc);
865 inputSet.add(rightLoc);
866 CompositeLocation glbCompLoc = CompositeLattice.calculateGLB(inputSet);
870 throw new Error(op.toString());
875 private CompositeLocation checkLocationFromLiteralNode(MethodDescriptor md,
876 SymbolTable nametable, LiteralNode en, CompositeLocation loc) {
878 // literal value has the top location so that value can be flowed into any
880 Location literalLoc = Location.createTopLocation(md);
881 loc.addLocation(literalLoc);
886 private CompositeLocation checkLocationFromNameNode(MethodDescriptor md, SymbolTable nametable,
887 NameNode nn, CompositeLocation loc) {
889 NameDescriptor nd = nn.getName();
890 if (nd.getBase() != null) {
892 loc = checkLocationFromExpressionNode(md, nametable, nn.getExpression(), loc);
893 // addTypeLocation(nn.getExpression().getType(), loc);
895 String varname = nd.toString();
897 if (varname.equals("this")) {
899 MethodLattice<String> methodLattice = ssjava.getMethodLattice(md);
900 String thisLocId = methodLattice.getThisLoc();
901 if (thisLocId == null) {
902 throw new Error("The location for 'this' is not defined at "
903 + md.getClassDesc().getSourceFileName() + "::" + nn.getNumLine());
905 Location locElement = new Location(md, thisLocId);
906 loc.addLocation(locElement);
909 Descriptor d = (Descriptor) nametable.get(varname);
911 // CompositeLocation localLoc = null;
912 if (d instanceof VarDescriptor) {
913 VarDescriptor vd = (VarDescriptor) d;
914 // localLoc = d2loc.get(vd);
915 // the type of var descriptor has a composite location!
916 loc = ((CompositeLocation) vd.getType().getExtension()).clone();
917 } else if (d instanceof FieldDescriptor) {
918 // the type of field descriptor has a location!
919 FieldDescriptor fd = (FieldDescriptor) d;
923 // if it is 'static final', the location has TOP since no one can
925 loc.addLocation(Location.createTopLocation(md));
927 // if 'static', the location has pre-assigned global loc
928 MethodLattice<String> localLattice = ssjava.getMethodLattice(md);
929 String globalLocId = localLattice.getGlobalLoc();
930 if (globalLocId == null) {
931 throw new Error("Global location element is not defined in the method " + md);
933 Location globalLoc = new Location(md, globalLocId);
935 loc.addLocation(globalLoc);
938 // the location of field access starts from this, followed by field
940 MethodLattice<String> localLattice = ssjava.getMethodLattice(md);
941 Location thisLoc = new Location(md, localLattice.getThisLoc());
942 loc.addLocation(thisLoc);
945 Location fieldLoc = (Location) fd.getType().getExtension();
946 loc.addLocation(fieldLoc);
952 private CompositeLocation checkLocationFromFieldAccessNode(MethodDescriptor md,
953 SymbolTable nametable, FieldAccessNode fan, CompositeLocation loc) {
955 ExpressionNode left = fan.getExpression();
956 loc = checkLocationFromExpressionNode(md, nametable, left, loc);
958 if (!left.getType().isPrimitive()) {
959 FieldDescriptor fd = fan.getField();
960 Location fieldLoc = (Location) fd.getType().getExtension();
961 loc.addLocation(fieldLoc);
967 private CompositeLocation checkLocationFromAssignmentNode(MethodDescriptor md,
968 SymbolTable nametable, AssignmentNode an, CompositeLocation loc) {
970 ClassDescriptor cd = md.getClassDesc();
972 boolean postinc = true;
973 if (an.getOperation().getBaseOp() == null
974 || (an.getOperation().getBaseOp().getOp() != Operation.POSTINC && an.getOperation()
975 .getBaseOp().getOp() != Operation.POSTDEC))
978 CompositeLocation destLocation =
979 checkLocationFromExpressionNode(md, nametable, an.getDest(), new CompositeLocation());
981 CompositeLocation srcLocation = new CompositeLocation();
984 if (hasOnlyLiteralValue(an.getSrc())) {
985 // if source is literal value, src location is TOP. so do not need to
989 srcLocation = new CompositeLocation();
990 srcLocation = checkLocationFromExpressionNode(md, nametable, an.getSrc(), srcLocation);
991 // System.out.println(" an= " + an.printNode(0) + " an.getSrc()=" +
992 // an.getSrc().getClass()
993 // + " at " + cd.getSourceFileName() + "::" + an.getNumLine());
994 // System.out.println("srcLocation=" + srcLocation);
995 // System.out.println("dstLocation=" + destLocation);
996 if (!CompositeLattice.isGreaterThan(srcLocation, destLocation)) {
997 throw new Error("The value flow from " + srcLocation + " to " + destLocation
998 + " does not respect location hierarchy on the assignment " + an.printNode(0) + " at "
999 + cd.getSourceFileName() + "::" + an.getNumLine());
1003 srcLocation = checkLocationFromExpressionNode(md, nametable, an.getDest(), srcLocation);
1005 if (!CompositeLattice.isGreaterThan(srcLocation, destLocation)) {
1006 throw new Error("Location " + destLocation
1007 + " is not allowed to have the value flow that moves within the same location at "
1008 + cd.getSourceFileName() + "::" + an.getNumLine());
1013 return destLocation;
1016 private void assignLocationOfVarDescriptor(VarDescriptor vd, MethodDescriptor md,
1017 SymbolTable nametable, TreeNode n) {
1019 ClassDescriptor cd = md.getClassDesc();
1020 Vector<AnnotationDescriptor> annotationVec = vd.getType().getAnnotationMarkers();
1022 // currently enforce every variable to have corresponding location
1023 if (annotationVec.size() == 0) {
1024 throw new Error("Location is not assigned to variable " + vd.getSymbol() + " in the method "
1025 + md.getSymbol() + " of the class " + cd.getSymbol());
1028 if (annotationVec.size() > 1) { // variable can have at most one location
1029 throw new Error(vd.getSymbol() + " has more than one location.");
1032 AnnotationDescriptor ad = annotationVec.elementAt(0);
1034 if (ad.getType() == AnnotationDescriptor.SINGLE_ANNOTATION) {
1036 if (ad.getMarker().equals(SSJavaAnalysis.LOC)) {
1037 String locDec = ad.getValue(); // check if location is defined
1039 if (locDec.startsWith(SSJavaAnalysis.DELTA)) {
1040 DeltaLocation deltaLoc = parseDeltaDeclaration(md, n, locDec);
1041 d2loc.put(vd, deltaLoc);
1042 addLocationType(vd.getType(), deltaLoc);
1044 CompositeLocation compLoc = parseLocationDeclaration(md, n, locDec);
1046 Location lastElement = compLoc.get(compLoc.getSize() - 1);
1047 if (ssjava.isSharedLocation(lastElement)) {
1048 ssjava.mapSharedLocation2Descriptor(lastElement, vd);
1051 d2loc.put(vd, compLoc);
1052 addLocationType(vd.getType(), compLoc);
1060 private DeltaLocation parseDeltaDeclaration(MethodDescriptor md, TreeNode n, String locDec) {
1063 int dIdx = locDec.indexOf(SSJavaAnalysis.DELTA);
1066 int beginIdx = dIdx + 6;
1067 locDec = locDec.substring(beginIdx, locDec.length() - 1);
1068 dIdx = locDec.indexOf(SSJavaAnalysis.DELTA);
1071 CompositeLocation compLoc = parseLocationDeclaration(md, n, locDec);
1072 DeltaLocation deltaLoc = new DeltaLocation(compLoc, deltaCount);
1077 private Location parseFieldLocDeclaraton(String decl) {
1079 int idx = decl.indexOf(".");
1080 String className = decl.substring(0, idx);
1081 String fieldName = decl.substring(idx + 1);
1083 Descriptor d = state.getClassSymbolTable().get(className);
1085 assert (d instanceof ClassDescriptor);
1086 SSJavaLattice<String> lattice = ssjava.getClassLattice((ClassDescriptor) d);
1087 if (!lattice.containsKey(fieldName)) {
1088 throw new Error("The location " + fieldName + " is not defined in the field lattice of '"
1089 + className + "'.");
1092 return new Location(d, fieldName);
1095 private CompositeLocation parseLocationDeclaration(MethodDescriptor md, TreeNode n, String locDec) {
1097 CompositeLocation compLoc = new CompositeLocation();
1099 StringTokenizer tokenizer = new StringTokenizer(locDec, ",");
1100 List<String> locIdList = new ArrayList<String>();
1101 while (tokenizer.hasMoreTokens()) {
1102 String locId = tokenizer.nextToken();
1103 locIdList.add(locId);
1106 // at least,one location element needs to be here!
1107 assert (locIdList.size() > 0);
1109 // assume that loc with idx 0 comes from the local lattice
1110 // loc with idx 1 comes from the field lattice
1112 String localLocId = locIdList.get(0);
1113 SSJavaLattice<String> localLattice = CompositeLattice.getLatticeByDescriptor(md);
1114 Location localLoc = new Location(md, localLocId);
1115 if (localLattice == null || (!localLattice.containsKey(localLocId))) {
1116 throw new Error("Location " + localLocId
1117 + " is not defined in the local variable lattice at "
1118 + md.getClassDesc().getSourceFileName() + "::" + (n != null ? n.getNumLine() : "") + ".");
1120 compLoc.addLocation(localLoc);
1122 for (int i = 1; i < locIdList.size(); i++) {
1123 String locName = locIdList.get(i);
1125 Location fieldLoc = parseFieldLocDeclaraton(locName);
1126 // ClassDescriptor cd = fieldLocName2cd.get(locName);
1127 // SSJavaLattice<String> fieldLattice =
1128 // CompositeLattice.getLatticeByDescriptor(cd);
1130 // if (fieldLattice == null || (!fieldLattice.containsKey(locName))) {
1131 // throw new Error("Location " + locName +
1132 // " is not defined in the field lattice at "
1133 // + cd.getSourceFileName() + ".");
1135 // Location fieldLoc = new Location(cd, locName);
1136 compLoc.addLocation(fieldLoc);
1143 private void checkDeclarationNode(MethodDescriptor md, SymbolTable nametable, DeclarationNode dn) {
1144 VarDescriptor vd = dn.getVarDescriptor();
1145 assignLocationOfVarDescriptor(vd, md, nametable, dn);
1148 private void checkClass(ClassDescriptor cd) {
1149 // Check to see that methods respects ss property
1150 for (Iterator method_it = cd.getMethods(); method_it.hasNext();) {
1151 MethodDescriptor md = (MethodDescriptor) method_it.next();
1152 checkMethodDeclaration(cd, md);
1156 private void checkDeclarationInClass(ClassDescriptor cd) {
1157 // Check to see that fields are okay
1158 for (Iterator field_it = cd.getFields(); field_it.hasNext();) {
1159 FieldDescriptor fd = (FieldDescriptor) field_it.next();
1160 checkFieldDeclaration(cd, fd);
1164 private void checkMethodDeclaration(ClassDescriptor cd, MethodDescriptor md) {
1168 private void checkFieldDeclaration(ClassDescriptor cd, FieldDescriptor fd) {
1170 Vector<AnnotationDescriptor> annotationVec = fd.getType().getAnnotationMarkers();
1172 // currently enforce every field to have corresponding location
1173 if (annotationVec.size() == 0) {
1174 throw new Error("Location is not assigned to the field '" + fd.getSymbol()
1175 + "' of the class " + cd.getSymbol() + " at " + cd.getSourceFileName());
1178 if (annotationVec.size() > 1) {
1179 // variable can have at most one location
1180 throw new Error("Field " + fd.getSymbol() + " of class " + cd
1181 + " has more than one location.");
1184 AnnotationDescriptor ad = annotationVec.elementAt(0);
1186 if (ad.getType() == AnnotationDescriptor.SINGLE_ANNOTATION) {
1188 if (ad.getMarker().equals(SSJavaAnalysis.LOC)) {
1189 String locationID = ad.getValue();
1190 // check if location is defined
1191 SSJavaLattice<String> lattice = ssjava.getClassLattice(cd);
1192 if (lattice == null || (!lattice.containsKey(locationID))) {
1193 throw new Error("Location " + locationID
1194 + " is not defined in the field lattice of class " + cd.getSymbol() + " at"
1195 + cd.getSourceFileName() + ".");
1197 Location loc = new Location(cd, locationID);
1199 if (ssjava.isSharedLocation(loc)) {
1200 ssjava.mapSharedLocation2Descriptor(loc, fd);
1203 addLocationType(fd.getType(), loc);
1210 private void addLocationType(TypeDescriptor type, CompositeLocation loc) {
1212 type.setExtension(loc);
1216 private void addLocationType(TypeDescriptor type, Location loc) {
1218 type.setExtension(loc);
1222 static class CompositeLattice {
1224 public static boolean isGreaterThan(CompositeLocation loc1, CompositeLocation loc2) {
1226 int baseCompareResult = compareBaseLocationSet(loc1, loc2, true);
1227 if (baseCompareResult == ComparisonResult.EQUAL) {
1228 if (compareDelta(loc1, loc2) == ComparisonResult.GREATER) {
1233 } else if (baseCompareResult == ComparisonResult.GREATER) {
1241 public static int compare(CompositeLocation loc1, CompositeLocation loc2) {
1243 // System.out.println("compare=" + loc1 + " " + loc2);
1244 int baseCompareResult = compareBaseLocationSet(loc1, loc2, false);
1246 if (baseCompareResult == ComparisonResult.EQUAL) {
1247 return compareDelta(loc1, loc2);
1249 return baseCompareResult;
1254 private static int compareDelta(CompositeLocation dLoc1, CompositeLocation dLoc2) {
1256 int deltaCount1 = 0;
1257 int deltaCount2 = 0;
1258 if (dLoc1 instanceof DeltaLocation) {
1259 deltaCount1 = ((DeltaLocation) dLoc1).getNumDelta();
1262 if (dLoc2 instanceof DeltaLocation) {
1263 deltaCount2 = ((DeltaLocation) dLoc2).getNumDelta();
1265 if (deltaCount1 < deltaCount2) {
1266 return ComparisonResult.GREATER;
1267 } else if (deltaCount1 == deltaCount2) {
1268 return ComparisonResult.EQUAL;
1270 return ComparisonResult.LESS;
1275 private static int compareBaseLocationSet(CompositeLocation compLoc1,
1276 CompositeLocation compLoc2, boolean awareSharedLoc) {
1278 // if compLoc1 is greater than compLoc2, return true
1279 // else return false;
1281 // compare one by one in according to the order of the tuple
1283 for (int i = 0; i < compLoc1.getSize(); i++) {
1284 Location loc1 = compLoc1.get(i);
1285 if (i >= compLoc2.getSize()) {
1286 throw new Error("Failed to compare two locations of " + compLoc1 + " and " + compLoc2
1287 + " because they are not comparable.");
1289 Location loc2 = compLoc2.get(i);
1291 if (!loc1.getDescriptor().equals(loc2.getDescriptor())) {
1292 throw new Error("Failed to compare two locations of " + compLoc1 + " and " + compLoc2
1293 + " because they are not comparable.");
1296 Descriptor d1 = loc1.getDescriptor();
1297 Descriptor d2 = loc2.getDescriptor();
1299 SSJavaLattice<String> lattice1 = getLatticeByDescriptor(d1);
1300 SSJavaLattice<String> lattice2 = getLatticeByDescriptor(d2);
1302 // check if the spin location is appeared only at the end of the
1303 // composite location
1304 if (lattice1.getSpinLocSet().contains(loc1.getLocIdentifier())) {
1305 if (i != (compLoc1.getSize() - 1)) {
1306 throw new Error("The spin location " + loc1.getLocIdentifier()
1307 + " cannot be appeared in the middle of composite location.");
1311 if (lattice2.getSpinLocSet().contains(loc2.getLocIdentifier())) {
1312 if (i != (compLoc2.getSize() - 1)) {
1313 throw new Error("The spin location " + loc2.getLocIdentifier()
1314 + " cannot be appeared in the middle of composite location.");
1318 if (!lattice1.equals(lattice2)) {
1319 throw new Error("Failed to compare two locations of " + compLoc1 + " and " + compLoc2
1320 + " because they are not comparable.");
1323 if (loc1.getLocIdentifier().equals(loc2.getLocIdentifier())) {
1325 // check if the current location is the spinning location
1326 // note that the spinning location only can be appeared in the last
1327 // part of the composite location
1328 if (awareSharedLoc && numOfTie == compLoc1.getSize()
1329 && lattice1.getSpinLocSet().contains(loc1.getLocIdentifier())) {
1330 return ComparisonResult.GREATER;
1333 } else if (lattice1.isGreaterThan(loc1.getLocIdentifier(), loc2.getLocIdentifier())) {
1334 return ComparisonResult.GREATER;
1336 return ComparisonResult.LESS;
1341 if (numOfTie == compLoc1.getSize()) {
1343 if (numOfTie != compLoc2.getSize()) {
1344 throw new Error("Failed to compare two locations of " + compLoc1 + " and " + compLoc2
1345 + " because they are not comparable.");
1348 return ComparisonResult.EQUAL;
1351 return ComparisonResult.LESS;
1355 public static CompositeLocation calculateGLB(Set<CompositeLocation> inputSet) {
1357 // System.out.println("Calculating GLB=" + inputSet);
1358 CompositeLocation glbCompLoc = new CompositeLocation();
1360 // calculate GLB of the first(priority) element
1361 Set<String> priorityLocIdentifierSet = new HashSet<String>();
1362 Descriptor priorityDescriptor = null;
1364 Hashtable<String, Set<CompositeLocation>> locId2CompLocSet =
1365 new Hashtable<String, Set<CompositeLocation>>();
1366 // mapping from the priority loc ID to its full representation by the
1367 // composite location
1369 int maxTupleSize = 0;
1370 CompositeLocation maxCompLoc = null;
1372 for (Iterator iterator = inputSet.iterator(); iterator.hasNext();) {
1373 CompositeLocation compLoc = (CompositeLocation) iterator.next();
1374 if (compLoc.getSize() > maxTupleSize) {
1375 maxTupleSize = compLoc.getSize();
1376 maxCompLoc = compLoc;
1378 Location priorityLoc = compLoc.get(0);
1379 String priorityLocId = priorityLoc.getLocIdentifier();
1380 priorityLocIdentifierSet.add(priorityLocId);
1382 if (locId2CompLocSet.containsKey(priorityLocId)) {
1383 locId2CompLocSet.get(priorityLocId).add(compLoc);
1385 Set<CompositeLocation> newSet = new HashSet<CompositeLocation>();
1386 newSet.add(compLoc);
1387 locId2CompLocSet.put(priorityLocId, newSet);
1390 // check if priority location are coming from the same lattice
1391 if (priorityDescriptor == null) {
1392 priorityDescriptor = priorityLoc.getDescriptor();
1393 } else if (!priorityDescriptor.equals(priorityLoc.getDescriptor())) {
1394 throw new Error("Failed to calculate GLB of " + inputSet
1395 + " because they are from different lattices.");
1399 SSJavaLattice<String> locOrder = getLatticeByDescriptor(priorityDescriptor);
1400 String glbOfPriorityLoc = locOrder.getGLB(priorityLocIdentifierSet);
1402 glbCompLoc.addLocation(new Location(priorityDescriptor, glbOfPriorityLoc));
1403 Set<CompositeLocation> compSet = locId2CompLocSet.get(glbOfPriorityLoc);
1405 // here find out composite location that has a maximum length tuple
1406 // if we have three input set: [A], [A,B], [A,B,C]
1407 // maximum length tuple will be [A,B,C]
1409 CompositeLocation maxFromCompSet = null;
1410 for (Iterator iterator = compSet.iterator(); iterator.hasNext();) {
1411 CompositeLocation c = (CompositeLocation) iterator.next();
1412 if (c.getSize() > max) {
1418 if (compSet == null) {
1419 // when GLB(x1,x2)!=x1 and !=x2 : GLB case 4
1420 // mean that the result is already lower than <x1,y1> and <x2,y2>
1421 // assign TOP to the rest of the location elements
1423 // in this case, do not take care about delta
1424 // CompositeLocation inputComp = inputSet.iterator().next();
1425 CompositeLocation inputComp = maxCompLoc;
1426 for (int i = 1; i < inputComp.getSize(); i++) {
1427 glbCompLoc.addLocation(Location.createTopLocation(inputComp.get(i).getDescriptor()));
1430 if (compSet.size() == 1) {
1432 // if GLB(x1,x2)==x1 or x2 : GLB case 2,3
1433 CompositeLocation comp = compSet.iterator().next();
1434 for (int i = 1; i < comp.getSize(); i++) {
1435 glbCompLoc.addLocation(comp.get(i));
1438 // if input location corresponding to glb is a delta, need to apply
1439 // delta to glb result
1440 if (comp instanceof DeltaLocation) {
1441 glbCompLoc = new DeltaLocation(glbCompLoc, 1);
1445 // when GLB(x1,x2)==x1 and x2 : GLB case 1
1446 // if more than one location shares the same priority GLB
1447 // need to calculate the rest of GLB loc
1449 // int compositeLocSize = compSet.iterator().next().getSize();
1450 int compositeLocSize = maxFromCompSet.getSize();
1452 Set<String> glbInputSet = new HashSet<String>();
1453 Descriptor currentD = null;
1454 for (int i = 1; i < compositeLocSize; i++) {
1455 for (Iterator iterator = compSet.iterator(); iterator.hasNext();) {
1456 CompositeLocation compositeLocation = (CompositeLocation) iterator.next();
1457 if (compositeLocation.getSize() > i) {
1458 Location currentLoc = compositeLocation.get(i);
1459 currentD = currentLoc.getDescriptor();
1460 // making set of the current location sharing the same idx
1461 glbInputSet.add(currentLoc.getLocIdentifier());
1464 // calculate glb for the current lattice
1466 SSJavaLattice<String> currentLattice = getLatticeByDescriptor(currentD);
1467 String currentGLBLocId = currentLattice.getGLB(glbInputSet);
1468 glbCompLoc.addLocation(new Location(currentD, currentGLBLocId));
1471 // if input location corresponding to glb is a delta, need to apply
1472 // delta to glb result
1474 for (Iterator iterator = compSet.iterator(); iterator.hasNext();) {
1475 CompositeLocation compLoc = (CompositeLocation) iterator.next();
1476 if (compLoc instanceof DeltaLocation) {
1477 if (glbCompLoc.equals(compLoc)) {
1478 glbCompLoc = new DeltaLocation(glbCompLoc, 1);
1491 static SSJavaLattice<String> getLatticeByDescriptor(Descriptor d) {
1493 SSJavaLattice<String> lattice = null;
1495 if (d instanceof ClassDescriptor) {
1496 lattice = ssjava.getCd2lattice().get(d);
1497 } else if (d instanceof MethodDescriptor) {
1498 if (ssjava.getMd2lattice().containsKey(d)) {
1499 lattice = ssjava.getMd2lattice().get(d);
1501 // use default lattice for the method
1502 lattice = ssjava.getCd2methodDefault().get(((MethodDescriptor) d).getClassDesc());
1511 class ComparisonResult {
1513 public static final int GREATER = 0;
1514 public static final int EQUAL = 1;
1515 public static final int LESS = 2;
1516 public static final int INCOMPARABLE = 3;
1523 class ReturnLocGenerator {
1525 public static final int PARAMISHIGHER = 0;
1526 public static final int PARAMISSAME = 1;
1527 public static final int IGNORE = 2;
1529 Hashtable<Integer, Integer> paramIdx2paramType;
1531 public ReturnLocGenerator(CompositeLocation returnLoc, List<CompositeLocation> params) {
1532 // creating mappings
1534 paramIdx2paramType = new Hashtable<Integer, Integer>();
1535 for (int i = 0; i < params.size(); i++) {
1536 CompositeLocation param = params.get(i);
1537 int compareResult = CompositeLattice.compare(param, returnLoc);
1540 if (compareResult == ComparisonResult.GREATER) {
1542 } else if (compareResult == ComparisonResult.EQUAL) {
1547 paramIdx2paramType.put(new Integer(i), new Integer(type));
1552 public CompositeLocation computeReturnLocation(List<CompositeLocation> args) {
1554 // compute the highest possible location in caller's side
1555 assert paramIdx2paramType.keySet().size() == args.size();
1557 Set<CompositeLocation> inputGLB = new HashSet<CompositeLocation>();
1558 for (int i = 0; i < args.size(); i++) {
1559 int type = (paramIdx2paramType.get(new Integer(i))).intValue();
1560 CompositeLocation argLoc = args.get(i);
1561 if (type == PARAMISHIGHER) {
1562 // return loc is lower than param
1563 DeltaLocation delta = new DeltaLocation(argLoc, 1);
1564 inputGLB.add(delta);
1565 } else if (type == PARAMISSAME) {
1566 // return loc is equal or lower than param
1567 inputGLB.add(argLoc);
1571 // compute GLB of arguments subset that are same or higher than return
1573 CompositeLocation glb = CompositeLattice.calculateGLB(inputGLB);