1 package Analysis.SSJava;
3 import java.util.HashSet;
4 import java.util.Hashtable;
5 import java.util.Iterator;
8 import java.util.StringTokenizer;
9 import java.util.Vector;
11 import IR.AnnotationDescriptor;
12 import IR.ClassDescriptor;
14 import IR.FieldDescriptor;
15 import IR.MethodDescriptor;
16 import IR.NameDescriptor;
19 import IR.SymbolTable;
20 import IR.TypeDescriptor;
21 import IR.VarDescriptor;
22 import IR.Flat.FlatFieldNode;
23 import IR.Tree.ArrayAccessNode;
24 import IR.Tree.ArrayInitializerNode;
25 import IR.Tree.AssignmentNode;
26 import IR.Tree.BlockExpressionNode;
27 import IR.Tree.BlockNode;
28 import IR.Tree.BlockStatementNode;
29 import IR.Tree.CastNode;
30 import IR.Tree.ClassTypeNode;
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;
36 import IR.Tree.InstanceOfNode;
38 import IR.Tree.LiteralNode;
39 import IR.Tree.MethodInvokeNode;
40 import IR.Tree.NameNode;
41 import IR.Tree.OffsetNode;
42 import IR.Tree.OpNode;
43 import IR.Tree.SubBlockNode;
44 import IR.Tree.TertiaryNode;
47 public class FlowDownCheck {
51 Hashtable<TypeDescriptor, Location> td2loc; // mapping from 'type descriptor'
53 Hashtable<String, ClassDescriptor> id2cd; // mapping from 'locID' to 'class
57 public FlowDownCheck(State state) {
59 this.toanalyze = new HashSet();
60 this.td2loc = new Hashtable<TypeDescriptor, Location>();
65 id2cd = new Hashtable<String, ClassDescriptor>();
66 Hashtable cd2lattice = state.getCd2LocationOrder();
68 Set cdSet = cd2lattice.keySet();
69 for (Iterator iterator = cdSet.iterator(); iterator.hasNext();) {
70 ClassDescriptor cd = (ClassDescriptor) iterator.next();
71 Lattice<String> lattice = (Lattice<String>) cd2lattice.get(cd);
73 Set<String> locIdSet = lattice.getKeySet();
74 for (Iterator iterator2 = locIdSet.iterator(); iterator2.hasNext();) {
75 String locID = (String) iterator2.next();
82 public void flowDownCheck() {
83 SymbolTable classtable = state.getClassSymbolTable();
85 // phase 1 : checking declaration node and creating mapping of 'type
86 // desciptor' & 'location'
87 toanalyze.addAll(classtable.getValueSet());
88 toanalyze.addAll(state.getTaskSymbolTable().getValueSet());
89 while (!toanalyze.isEmpty()) {
90 Object obj = toanalyze.iterator().next();
91 ClassDescriptor cd = (ClassDescriptor) obj;
93 if (cd.isClassLibrary()) {
94 // doesn't care about class libraries now
97 checkDeclarationInClass(cd);
98 for (Iterator method_it = cd.getMethods(); method_it.hasNext();) {
99 MethodDescriptor md = (MethodDescriptor) method_it.next();
101 checkDeclarationInMethodBody(cd, md);
103 System.out.println("Error in " + md);
109 // post-processing for delta location
110 // for a nested delta location, assigning a concrete reference to delta
112 Set<TypeDescriptor> tdSet = td2loc.keySet();
113 for (Iterator iterator = tdSet.iterator(); iterator.hasNext();) {
114 TypeDescriptor td = (TypeDescriptor) iterator.next();
115 Location loc = td2loc.get(td);
117 if (loc.getType() == Location.DELTA) {
118 // if it contains delta reference pointing to another location element
119 CompositeLocation compLoc = (CompositeLocation) loc;
121 Location locElement = compLoc.getTuple().at(0);
122 assert (locElement instanceof DeltaLocation);
124 DeltaLocation delta = (DeltaLocation) locElement;
125 TypeDescriptor refType = delta.getRefLocationId();
126 if (refType != null) {
127 Location refLoc = td2loc.get(refType);
129 assert (refLoc instanceof CompositeLocation);
130 CompositeLocation refCompLoc = (CompositeLocation) refLoc;
132 assert (refCompLoc.getTuple().at(0) instanceof DeltaLocation);
133 DeltaLocation refDelta = (DeltaLocation) refCompLoc.getTuple().at(0);
135 delta.addDeltaOperand(refDelta);
136 // compLoc.addLocation(refDelta);
142 // phase2 : checking assignments
143 toanalyze.addAll(classtable.getValueSet());
144 toanalyze.addAll(state.getTaskSymbolTable().getValueSet());
145 while (!toanalyze.isEmpty()) {
146 Object obj = toanalyze.iterator().next();
147 ClassDescriptor cd = (ClassDescriptor) obj;
148 toanalyze.remove(cd);
149 if (cd.isClassLibrary()) {
150 // doesn't care about class libraries now
154 for (Iterator method_it = cd.getMethods(); method_it.hasNext();) {
155 MethodDescriptor md = (MethodDescriptor) method_it.next();
157 checkMethodBody(cd, md);
159 System.out.println("Error in " + md);
167 private void checkDeclarationInMethodBody(ClassDescriptor cd, MethodDescriptor md) {
168 BlockNode bn = state.getMethodBody(md);
169 checkDeclarationInBlockNode(md, md.getParameterTable(), bn);
172 private void checkDeclarationInBlockNode(MethodDescriptor md, SymbolTable nametable, BlockNode bn) {
173 bn.getVarTable().setParent(nametable);
174 for (int i = 0; i < bn.size(); i++) {
175 BlockStatementNode bsn = bn.get(i);
176 checkDeclarationInBlockStatementNode(md, bn.getVarTable(), bsn);
180 private void checkDeclarationInBlockStatementNode(MethodDescriptor md, SymbolTable nametable,
181 BlockStatementNode bsn) {
183 switch (bsn.kind()) {
184 case Kind.SubBlockNode:
185 checkDeclarationInSubBlockNode(md, nametable, (SubBlockNode) bsn);
187 case Kind.DeclarationNode:
188 checkDeclarationNode(md, nametable, (DeclarationNode) bsn);
193 private void checkMethodBody(ClassDescriptor cd, MethodDescriptor md) {
194 BlockNode bn = state.getMethodBody(md);
195 checkBlockNode(md, md.getParameterTable(), bn);
198 private void checkBlockNode(MethodDescriptor md, SymbolTable nametable, BlockNode bn) {
199 bn.getVarTable().setParent(nametable);
200 for (int i = 0; i < bn.size(); i++) {
201 BlockStatementNode bsn = bn.get(i);
202 checkBlockStatementNode(md, bn.getVarTable(), bsn);
206 private void checkBlockStatementNode(MethodDescriptor md, SymbolTable nametable,
207 BlockStatementNode bsn) {
209 switch (bsn.kind()) {
210 case Kind.BlockExpressionNode:
211 checkLocationFromBlockExpressionNode(md, nametable, (BlockExpressionNode) bsn);
214 case Kind.DeclarationNode:
215 checkLocationFromDeclarationNode(md, nametable, (DeclarationNode) bsn);
218 case Kind.IfStatementNode:
219 // checkLocationFromIfStatementNode(md, nametable, (IfStatementNode) bsn);
223 // checkLocationFromLoopNode(md, nametable, (LoopNode) bsn);
226 case Kind.ReturnNode:
227 // checkLocationFromReturnNode(md, nametable, (ReturnNode) bsn);
230 case Kind.SubBlockNode:
231 // checkLocationFromSubBlockNode(md, nametable, (SubBlockNode) bsn);
234 case Kind.ContinueBreakNode:
235 // checkLocationFromContinueBreakNode(md, nametable,(ContinueBreakNode)
241 private void checkLocationFromDeclarationNode(MethodDescriptor md, SymbolTable nametable,
242 DeclarationNode dn) {
243 VarDescriptor vd = dn.getVarDescriptor();
245 Location destLoc = td2loc.get(vd.getType());
247 ClassDescriptor localCD = md.getClassDesc();
248 if (dn.getExpression() != null) {
249 CompositeLocation expressionLoc = new CompositeLocation(localCD);
251 checkLocationFromExpressionNode(md, nametable, dn.getExpression(), expressionLoc);
253 if (expressionLoc != null) {
254 System.out.println("expressionLoc=" + expressionLoc + " and destLoc=" + destLoc);
256 // checking location order
258 if (!CompositeLattice.isGreaterThan(expressionLoc, destLoc, localCD)) {
259 throw new Error("The value flow from " + expressionLoc + " to " + destLoc
260 + " does not respect location hierarchy on the assignment " + dn.printNode(0));
269 private void checkDeclarationInSubBlockNode(MethodDescriptor md, SymbolTable nametable,
271 checkDeclarationInBlockNode(md, nametable, sbn.getBlockNode());
274 private void checkLocationFromBlockExpressionNode(MethodDescriptor md, SymbolTable nametable,
275 BlockExpressionNode ben) {
276 checkLocationFromExpressionNode(md, nametable, ben.getExpression(), null);
279 private CompositeLocation checkLocationFromExpressionNode(MethodDescriptor md,
280 SymbolTable nametable, ExpressionNode en, CompositeLocation loc) {
284 case Kind.AssignmentNode:
285 return checkLocationFromAssignmentNode(md, nametable, (AssignmentNode) en, loc);
287 case Kind.FieldAccessNode:
288 return checkLocationFromFieldAccessNode(md, nametable, (FieldAccessNode) en, loc);
291 return checkLocationFromNameNode(md, nametable, (NameNode) en, loc);
294 return checkLocationFromOpNode(md, nametable, (OpNode) en);
296 case Kind.CreateObjectNode:
297 // checkCreateObjectNode(md, nametable, (CreateObjectNode) en, td);
300 case Kind.ArrayAccessNode:
301 // checkArrayAccessNode(md, nametable, (ArrayAccessNode) en, td);
304 case Kind.LiteralNode:
305 return checkLocationFromLiteralNode(md, nametable, (LiteralNode) en, loc);
307 case Kind.MethodInvokeNode:
308 // checkMethodInvokeNode(md,nametable,(MethodInvokeNode)en,td);
311 case Kind.OffsetNode:
312 // checkOffsetNode(md, nametable, (OffsetNode)en, td);
315 case Kind.TertiaryNode:
316 // checkTertiaryNode(md, nametable, (TertiaryNode)en, td);
319 case Kind.InstanceOfNode:
320 // checkInstanceOfNode(md, nametable, (InstanceOfNode) en, td);
323 case Kind.ArrayInitializerNode:
324 // checkArrayInitializerNode(md, nametable, (ArrayInitializerNode) en,
328 case Kind.ClassTypeNode:
329 // checkClassTypeNode(md, nametable, (ClassTypeNode) en, td);
339 private CompositeLocation checkLocationFromOpNode(MethodDescriptor md, SymbolTable nametable,
342 Lattice<String> locOrder = (Lattice<String>) state.getCd2LocationOrder().get(md.getClassDesc());
344 ClassDescriptor cd = md.getClassDesc();
345 CompositeLocation leftLoc = new CompositeLocation(cd);
346 leftLoc = checkLocationFromExpressionNode(md, nametable, on.getLeft(), leftLoc);
348 CompositeLocation rightLoc = new CompositeLocation(cd);
349 if (on.getRight() != null) {
350 rightLoc = checkLocationFromExpressionNode(md, nametable, on.getRight(), rightLoc);
353 System.out.println("checking op node");
354 System.out.println("left loc=" + leftLoc);
355 System.out.println("right loc=" + rightLoc);
357 Operation op = on.getOp();
359 switch (op.getOp()) {
361 case Operation.UNARYPLUS:
362 case Operation.UNARYMINUS:
363 case Operation.LOGIC_NOT:
365 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
368 case Operation.LOGIC_OR:
369 case Operation.LOGIC_AND:
371 case Operation.BIT_OR:
372 case Operation.BIT_XOR:
373 case Operation.BIT_AND:
374 case Operation.ISAVAILABLE:
375 case Operation.EQUAL:
376 case Operation.NOTEQUAL:
386 case Operation.LEFTSHIFT:
387 case Operation.RIGHTSHIFT:
388 case Operation.URIGHTSHIFT:
390 Set<CompositeLocation> inputSet = new HashSet<CompositeLocation>();
391 inputSet.add(leftLoc);
392 inputSet.add(rightLoc);
393 CompositeLocation glbCompLoc = CompositeLattice.calculateGLB(cd, inputSet, cd);
397 throw new Error(op.toString());
404 private CompositeLocation checkLocationFromLiteralNode(MethodDescriptor md,
405 SymbolTable nametable, LiteralNode en, CompositeLocation loc) {
407 // literal value has the top location so that value can be flowed into any
409 Location literalLoc = Location.createTopLocation(md.getClassDesc());
410 loc.addLocation(literalLoc);
415 private CompositeLocation checkLocationFromNameNode(MethodDescriptor md, SymbolTable nametable,
416 NameNode nn, CompositeLocation loc) {
418 NameDescriptor nd = nn.getName();
419 if (nd.getBase() != null) {
420 loc = checkLocationFromExpressionNode(md, nametable, nn.getExpression(), loc);
423 String varname = nd.toString();
424 Descriptor d = (Descriptor) nametable.get(varname);
426 Location localLoc = null;
427 if (d instanceof VarDescriptor) {
428 VarDescriptor vd = (VarDescriptor) d;
429 localLoc = td2loc.get(vd.getType());
430 } else if (d instanceof FieldDescriptor) {
431 FieldDescriptor fd = (FieldDescriptor) d;
432 localLoc = td2loc.get(fd.getType());
434 assert (localLoc != null);
436 if (localLoc instanceof CompositeLocation) {
437 loc = (CompositeLocation) localLoc;
439 loc.addLocation(localLoc);
446 private CompositeLocation checkLocationFromFieldAccessNode(MethodDescriptor md,
447 SymbolTable nametable, FieldAccessNode fan, CompositeLocation loc) {
448 FieldDescriptor fd = fan.getField();
449 Location fieldLoc = td2loc.get(fd.getType());
450 loc.addLocation(fieldLoc);
452 ExpressionNode left = fan.getExpression();
453 return checkLocationFromExpressionNode(md, nametable, left, loc);
456 private CompositeLocation checkLocationFromAssignmentNode(MethodDescriptor md,
457 SymbolTable nametable, AssignmentNode an, CompositeLocation loc) {
458 System.out.println("checkAssignmentNode=" + an.printNode(0));
460 ClassDescriptor localCD = md.getClassDesc();
461 CompositeLocation srcLocation = new CompositeLocation(md.getClassDesc());
462 CompositeLocation destLocation = new CompositeLocation(md.getClassDesc());
464 boolean postinc = true;
465 if (an.getOperation().getBaseOp() == null
466 || (an.getOperation().getBaseOp().getOp() != Operation.POSTINC && an.getOperation()
467 .getBaseOp().getOp() != Operation.POSTDEC))
470 srcLocation = checkLocationFromExpressionNode(md, nametable, an.getSrc(), srcLocation);
473 destLocation = checkLocationFromExpressionNode(md, nametable, an.getDest(), destLocation);
475 if (!CompositeLattice.isGreaterThan(srcLocation, destLocation, localCD)) {
476 throw new Error("The value flow from " + srcLocation + " to " + destLocation
477 + " does not respect location hierarchy on the assignment " + an.printNode(0));
483 private Location createCompositeLocation(FieldAccessNode fan, Location loc) {
485 FieldDescriptor field = fan.getField();
486 ClassDescriptor fieldCD = field.getClassDescriptor();
488 assert (field.getType().getAnnotationMarkers().size() == 1);
490 AnnotationDescriptor locAnnotation = field.getType().getAnnotationMarkers().elementAt(0);
491 if (locAnnotation.getType() == AnnotationDescriptor.SINGLE_ANNOTATION) {
498 // Location localLoc=new Location(field.getClassDescriptor(),field.get)
500 // System.out.println("creatingComposite's field="+field+" localLoc="+localLoc);
501 ExpressionNode leftNode = fan.getExpression();
502 System.out.println("creatingComposite's leftnode=" + leftNode.printNode(0));
507 private void checkDeclarationNode(MethodDescriptor md, SymbolTable nametable, DeclarationNode dn) {
509 System.out.println("*** Check declarationNode=" + dn.printNode(0));
511 ClassDescriptor cd = md.getClassDesc();
512 VarDescriptor vd = dn.getVarDescriptor();
513 Vector<AnnotationDescriptor> annotationVec = vd.getType().getAnnotationMarkers();
515 // currently enforce every variable to have corresponding location
516 if (annotationVec.size() == 0) {
517 throw new Error("Location is not assigned to variable " + vd.getSymbol() + " in the method "
518 + md.getSymbol() + " of the class " + cd.getSymbol());
521 if (annotationVec.size() > 1) {
522 // variable can have at most one location
523 throw new Error(vd.getSymbol() + " has more than one location.");
526 AnnotationDescriptor ad = annotationVec.elementAt(0);
527 System.out.println("its ad=" + ad);
529 if (ad.getType() == AnnotationDescriptor.MARKER_ANNOTATION) {
531 // check if location is defined
532 String locationID = ad.getMarker();
533 Lattice<String> lattice = (Lattice<String>) state.getCd2LocationOrder().get(cd);
535 if (lattice == null || (!lattice.containsKey(locationID))) {
536 throw new Error("Location " + locationID
537 + " is not defined in the location hierarchy of class " + cd.getSymbol() + ".");
540 Location loc = new Location(cd, locationID);
541 td2loc.put(vd.getType(), loc);
543 } else if (ad.getType() == AnnotationDescriptor.SINGLE_ANNOTATION) {
544 if (ad.getMarker().equals(SSJavaAnalysis.DELTA)) {
546 CompositeLocation compLoc = new CompositeLocation(cd);
548 if (ad.getData().length() == 0) {
549 throw new Error("Delta function of " + vd.getSymbol() + " does not have any locations: "
550 + cd.getSymbol() + ".");
553 String deltaStr = ad.getData();
554 if (deltaStr.startsWith("LOC(")) {
556 if (!deltaStr.endsWith(")")) {
557 throw new Error("The declaration of the delta location is wrong at "
558 + cd.getSourceFileName() + ":" + dn.getNumLine());
560 String locationOperand = deltaStr.substring(4, deltaStr.length() - 1);
562 nametable.get(locationOperand);
563 Descriptor d = (Descriptor) nametable.get(locationOperand);
565 if (d instanceof VarDescriptor) {
566 VarDescriptor varDescriptor = (VarDescriptor) d;
567 DeltaLocation deltaLoc = new DeltaLocation(cd, varDescriptor.getType());
568 // td2loc.put(vd.getType(), compLoc);
569 compLoc.addLocation(deltaLoc);
570 } else if (d instanceof FieldDescriptor) {
571 throw new Error("Applying delta operation to the field " + locationOperand
572 + " is not allowed at " + cd.getSourceFileName() + ":" + dn.getNumLine());
575 StringTokenizer token = new StringTokenizer(deltaStr, ",");
576 DeltaLocation deltaLoc = new DeltaLocation(cd);
578 while (token.hasMoreTokens()) {
579 String deltaOperand = token.nextToken();
580 ClassDescriptor deltaCD = id2cd.get(deltaOperand);
581 if (deltaCD == null) {
582 // delta operand is not defined in the location hierarchy
583 throw new Error("Delta operand '" + deltaOperand + "' of declaration node '" + vd
584 + "' is not defined by location hierarchies.");
587 Location loc = new Location(deltaCD, deltaOperand);
588 deltaLoc.addDeltaOperand(loc);
590 compLoc.addLocation(deltaLoc);
594 td2loc.put(vd.getType(), compLoc);
595 System.out.println("vd=" + vd + " is assigned by " + compLoc);
602 private void checkClass(ClassDescriptor cd) {
603 // Check to see that methods respects ss property
604 for (Iterator method_it = cd.getMethods(); method_it.hasNext();) {
605 MethodDescriptor md = (MethodDescriptor) method_it.next();
606 checkMethodDeclaration(cd, md);
610 private void checkDeclarationInClass(ClassDescriptor cd) {
611 // Check to see that fields are okay
612 for (Iterator field_it = cd.getFields(); field_it.hasNext();) {
613 FieldDescriptor fd = (FieldDescriptor) field_it.next();
614 checkFieldDeclaration(cd, fd);
618 private void checkMethodDeclaration(ClassDescriptor cd, MethodDescriptor md) {
622 private void checkFieldDeclaration(ClassDescriptor cd, FieldDescriptor fd) {
624 Vector<AnnotationDescriptor> annotationVec = fd.getType().getAnnotationMarkers();
626 // currently enforce every variable to have corresponding location
627 if (annotationVec.size() == 0) {
628 throw new Error("Location is not assigned to the field " + fd.getSymbol() + " of the class "
632 if (annotationVec.size() > 1) {
633 // variable can have at most one location
634 throw new Error("Field " + fd.getSymbol() + " of class " + cd
635 + " has more than one location.");
638 // check if location is defined
639 AnnotationDescriptor ad = annotationVec.elementAt(0);
640 if (ad.getType() == AnnotationDescriptor.MARKER_ANNOTATION) {
641 String locationID = annotationVec.elementAt(0).getMarker();
642 Lattice<String> lattice = (Lattice<String>) state.getCd2LocationOrder().get(cd);
644 if (lattice == null || (!lattice.containsKey(locationID))) {
645 throw new Error("Location " + locationID
646 + " is not defined in the location hierarchy of class " + cd.getSymbol() + ".");
649 Location localLoc = new Location(cd, locationID);
650 td2loc.put(fd.getType(), localLoc);
652 } else if (ad.getType() == AnnotationDescriptor.SINGLE_ANNOTATION) {
653 if (ad.getMarker().equals(SSJavaAnalysis.DELTA)) {
655 if (ad.getData().length() == 0) {
656 throw new Error("Delta function of " + fd.getSymbol() + " does not have any locations: "
657 + cd.getSymbol() + ".");
660 CompositeLocation compLoc = new CompositeLocation(cd);
661 DeltaLocation deltaLoc = new DeltaLocation(cd);
663 StringTokenizer token = new StringTokenizer(ad.getData(), ",");
664 while (token.hasMoreTokens()) {
665 String deltaOperand = token.nextToken();
666 ClassDescriptor deltaCD = id2cd.get(deltaOperand);
667 if (deltaCD == null) {
668 // delta operand is not defined in the location hierarchy
669 throw new Error("Delta operand '" + deltaOperand + "' of field node '" + fd
670 + "' is not defined by location hierarchies.");
673 Location loc = new Location(deltaCD, deltaOperand);
674 deltaLoc.addDeltaOperand(loc);
676 compLoc.addLocation(deltaLoc);
677 td2loc.put(fd.getType(), compLoc);
684 static class CompositeLattice {
686 public static boolean isGreaterThan(Location loc1, Location loc2, ClassDescriptor priorityCD) {
688 System.out.println("isGreaterThan=" + loc1 + " ? " + loc2);
689 CompositeLocation compLoc1;
690 CompositeLocation compLoc2;
692 if (loc1 instanceof CompositeLocation) {
693 compLoc1 = (CompositeLocation) loc1;
695 // create a bogus composite location for a single location
696 compLoc1 = new CompositeLocation(loc1.getClassDescriptor());
697 compLoc1.addLocation(loc1);
700 if (loc2 instanceof CompositeLocation) {
701 compLoc2 = (CompositeLocation) loc2;
703 // create a bogus composite location for a single location
704 compLoc2 = new CompositeLocation(loc2.getClassDescriptor());
705 compLoc2.addLocation(loc2);
708 // comparing two composite locations
709 System.out.println("compare base location=" + compLoc1 + " ? " + compLoc2);
711 int baseCompareResult = compareBaseLocationSet(compLoc1, compLoc2);
712 if (baseCompareResult == ComparisonResult.EQUAL) {
713 if (compareDelta(compLoc1, compLoc2) == ComparisonResult.GREATER) {
718 } else if (baseCompareResult == ComparisonResult.GREATER) {
726 private static int compareDelta(CompositeLocation compLoc1, CompositeLocation compLoc2) {
728 if (compLoc1.getNumofDelta() < compLoc2.getNumofDelta()) {
729 return ComparisonResult.GREATER;
731 return ComparisonResult.LESS;
735 private static int compareBaseLocationSet(CompositeLocation compLoc1, CompositeLocation compLoc2) {
737 // if compLoc1 is greater than compLoc2, return true
738 // else return false;
740 Map<ClassDescriptor, Location> cd2loc1 = compLoc1.getCd2Loc();
741 Map<ClassDescriptor, Location> cd2loc2 = compLoc2.getCd2Loc();
743 // start to compare the first item of tuples:
744 // assumes that the first item has priority than other items.
746 NTuple<Location> locTuple1 = compLoc1.getBaseLocationTuple();
747 NTuple<Location> locTuple2 = compLoc2.getBaseLocationTuple();
749 Location priorityLoc1 = locTuple1.at(0);
750 Location priorityLoc2 = locTuple2.at(0);
752 assert (priorityLoc1.getClassDescriptor().equals(priorityLoc2.getClassDescriptor()));
754 ClassDescriptor cd = priorityLoc1.getClassDescriptor();
755 Lattice<String> locationOrder = (Lattice<String>) state.getCd2LocationOrder().get(cd);
757 if (priorityLoc1.getLocIdentifier().equals(priorityLoc2.getLocIdentifier())) {
758 // have the same level of local hierarchy
759 } else if (locationOrder.isGreaterThan(priorityLoc1.getLocIdentifier(), priorityLoc2
760 .getLocIdentifier())) {
761 // if priority loc of compLoc1 is higher than compLoc2
762 // then, compLoc 1 is higher than compLoc2
763 return ComparisonResult.GREATER;
765 // if priority loc of compLoc1 is NOT higher than compLoc2
766 // then, compLoc 1 is NOT higher than compLoc2
767 return ComparisonResult.LESS;
770 // compare base locations except priority by class descriptor
771 Set<ClassDescriptor> keySet1 = cd2loc1.keySet();
774 for (Iterator iterator = keySet1.iterator(); iterator.hasNext();) {
775 ClassDescriptor cd1 = (ClassDescriptor) iterator.next();
777 Location loc1 = cd2loc1.get(cd1);
778 Location loc2 = cd2loc2.get(cd1);
780 if (priorityLoc1.equals(loc1)) {
785 // if comploc2 doesn't have corresponding location,
786 // then we determines that comploc1 is lower than comploc 2
787 return ComparisonResult.LESS;
790 System.out.println("lattice comparison:" + loc1.getLocIdentifier() + " ? "
791 + loc2.getLocIdentifier());
792 locationOrder = (Lattice<String>) state.getCd2LocationOrder().get(cd1);
793 if (loc1.getLocIdentifier().equals(loc2.getLocIdentifier())) {
794 // have the same level of local hierarchy
796 } else if (!locationOrder.isGreaterThan(loc1.getLocIdentifier(), loc2.getLocIdentifier())) {
797 // if one element of composite location 1 is not higher than composite
799 // then, composite loc 1 is not higher than composite loc 2
801 System.out.println(compLoc1 + " < " + compLoc2);
802 return ComparisonResult.LESS;
807 if (numEqualLoc == compLoc1.getBaseLocationSize()) {
808 System.out.println(compLoc1 + " == " + compLoc2);
809 return ComparisonResult.EQUAL;
812 System.out.println(compLoc1 + " > " + compLoc2);
813 return ComparisonResult.GREATER;
816 public static CompositeLocation calculateGLB(ClassDescriptor cd,
817 Set<CompositeLocation> inputSet, ClassDescriptor priorityCD) {
819 CompositeLocation glbCompLoc = new CompositeLocation(cd);
820 int maxDeltaFunction = 0;
822 // calculate GLB of priority element first
824 Hashtable<ClassDescriptor, Set<Location>> cd2locSet =
825 new Hashtable<ClassDescriptor, Set<Location>>();
827 // creating mapping from class to set of locations
828 for (Iterator iterator = inputSet.iterator(); iterator.hasNext();) {
829 CompositeLocation compLoc = (CompositeLocation) iterator.next();
831 int numOfDelta = compLoc.getNumofDelta();
832 if (numOfDelta > maxDeltaFunction) {
833 maxDeltaFunction = numOfDelta;
836 Set<Location> baseLocationSet = compLoc.getBaseLocationSet();
837 for (Iterator iterator2 = baseLocationSet.iterator(); iterator2.hasNext();) {
838 Location locElement = (Location) iterator2.next();
839 ClassDescriptor locCD = locElement.getClassDescriptor();
841 Set<Location> locSet = cd2locSet.get(locCD);
842 if (locSet == null) {
843 locSet = new HashSet<Location>();
845 locSet.add(locElement);
847 cd2locSet.put(locCD, locSet);
852 Set<Location> locSetofClass = cd2locSet.get(priorityCD);
853 Set<String> locIdentifierSet = new HashSet<String>();
855 for (Iterator<Location> locIterator = locSetofClass.iterator(); locIterator.hasNext();) {
856 Location locElement = locIterator.next();
857 locIdentifierSet.add(locElement.getLocIdentifier());
860 Lattice<String> locOrder = (Lattice<String>) state.getCd2LocationOrder().get(priorityCD);
861 String glbLocIdentifer = locOrder.getGLB(locIdentifierSet);
863 Location priorityGLB = new Location(priorityCD, glbLocIdentifer);
865 Set<CompositeLocation> sameGLBLoc = new HashSet<CompositeLocation>();
867 for (Iterator<CompositeLocation> iterator = inputSet.iterator(); iterator.hasNext();) {
868 CompositeLocation inputComploc = iterator.next();
869 Location locElement = inputComploc.getLocation(priorityCD);
871 if (locElement.equals(priorityGLB)) {
872 sameGLBLoc.add(inputComploc);
875 glbCompLoc.addLocation(priorityGLB);
876 if (sameGLBLoc.size() > 0) {
877 // if more than one location shares the same priority GLB
878 // need to calculate the rest of GLB loc
880 Set<Location> glbElementSet = new HashSet<Location>();
882 for (Iterator<ClassDescriptor> iterator = cd2locSet.keySet().iterator(); iterator.hasNext();) {
883 ClassDescriptor localCD = iterator.next();
884 if (!localCD.equals(priorityCD)) {
885 Set<Location> localLocSet = cd2locSet.get(localCD);
886 Set<String> LocalLocIdSet = new HashSet<String>();
888 for (Iterator<Location> locIterator = localLocSet.iterator(); locIterator.hasNext();) {
889 Location locElement = locIterator.next();
890 LocalLocIdSet.add(locElement.getLocIdentifier());
893 Lattice<String> localOrder = (Lattice<String>) state.getCd2LocationOrder().get(localCD);
894 Location localGLBLoc = new Location(localCD, localOrder.getGLB(LocalLocIdSet));
895 glbCompLoc.addLocation(localGLBLoc);
899 // if priority glb loc is lower than all of input loc
900 // assign top location to the rest of loc element
902 for (Iterator<ClassDescriptor> iterator = cd2locSet.keySet().iterator(); iterator.hasNext();) {
903 ClassDescriptor localCD = iterator.next();
904 if (!localCD.equals(priorityCD)) {
905 Location localGLBLoc = Location.createTopLocation(localCD);
906 glbCompLoc.addLocation(localGLBLoc);
919 class ComparisonResult {
921 public static final int GREATER = 0;
922 public static final int EQUAL = 1;
923 public static final int LESS = 2;