changes.
[IRC.git] / Robust / src / Analysis / SSJava / BuildLattice.java
index a08e01663764020bc2d4e77340bb61186a636f97..b306a4fc00cf9abbd8d1cd1ccf8970fd5d41a302 100644 (file)
@@ -13,14 +13,11 @@ import Util.Pair;
 public class BuildLattice {
 
   private LocationInference infer;
-
-  private final HNode topNode;
-  private final HNode bottomNode;
+  private Map<HNode, TripleItem> mapSharedNodeToTripleItem;
 
   public BuildLattice(LocationInference infer) {
     this.infer = infer;
-    topNode = new HNode(infer.ssjava.TOP);
-    bottomNode = new HNode(infer.ssjava.BOTTOM);
+    this.mapSharedNodeToTripleItem = new HashMap<HNode, TripleItem>();
   }
 
   public SSJavaLattice<String> buildLattice(Descriptor desc) {
@@ -149,6 +146,8 @@ public class BuildLattice {
     // perform DFS that starts from each skeleton/combination node and ends by another
     // skeleton/combination node
 
+    mapSharedNodeToTripleItem.clear();
+
     HierarchyGraph simpleGraph = infer.getSimpleHierarchyGraph(desc);
     HierarchyGraph scGraph = infer.getSkeletonCombinationHierarchyGraph(desc);
     LocationSummary locSummary = infer.getLocationSummary(desc);
@@ -161,11 +160,12 @@ public class BuildLattice {
 
     Map<TripleItem, String> mapIntermediateLoc = new HashMap<TripleItem, String>();
 
-    System.out.println("*insert=" + desc);
-    System.out.println("***nodeSet=" + nodeSet);
+    // System.out.println("*insert=" + desc);
+    // System.out.println("***nodeSet=" + nodeSet);
     for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
       HNode node = (HNode) iterator.next();
-      System.out.println("node=" + node);
+      // System.out.println("node=" + node);
+
       if (node.isSkeleton() && (!visited.contains(node))) {
         visited.add(node);
 
@@ -208,10 +208,9 @@ public class BuildLattice {
               } else if (endCombNodeSet.size() == 0) {
                 // the outNode is (directly/transitively) connected to the bottom node
                 // therefore, we just add a dummy bottom HNode to the endCombNodeSet.
-                endCombNodeSet.add(bottomNode);
+                endCombNodeSet.add(LocationInference.BOTTOMHNODE);
               }
 
-
               recurDFSNormalNode(desc, lattice, startNode, endCombNodeSet, visited,
                   mapIntermediateLoc, 1, locSummary, outNode);
             }
@@ -225,9 +224,8 @@ public class BuildLattice {
         System.out.println("n=" + node);
 
         // an intermediate node 'node' may be located between "TOP" location and a skeleton node
-        int sizeIncomingNode = simpleGraph.getIncomingNodeSet(node).size();
+        if (simpleGraph.getIncomingNodeSet(node).size() == 0) {
 
-        if (sizeIncomingNode == 0) {
           // this node will be directly connected to the TOP location
           // start adding the following nodes from this node
 
@@ -240,7 +238,8 @@ public class BuildLattice {
             endCombNodeSet.add(getCombinationNodeInSCGraph(desc, endNode));
           }
 
-          HNode startNode = topNode;
+          System.out.println("endCombNodeSet=" + endCombNodeSet);
+          HNode startNode = LocationInference.TOPHNODE;
           visited.add(startNode);
           if (endCombNodeSet.size() > 0) {
             // follows the straight line up to another skeleton/combination node
@@ -254,14 +253,69 @@ public class BuildLattice {
       }
     }
 
+    // add shared locations
+    Set<HNode> sharedNodeSet = mapSharedNodeToTripleItem.keySet();
+    for (Iterator iterator = sharedNodeSet.iterator(); iterator.hasNext();) {
+      HNode sharedNode = (HNode) iterator.next();
+      TripleItem item = mapSharedNodeToTripleItem.get(sharedNode);
+      String nonSharedLocName = mapIntermediateLoc.get(item);
+      // System.out.println("sharedNode=" + sharedNode + "    locName=" + nonSharedLocName);
+
+      String newLocName;
+      if (locSummary.getHNodeNameSetByLatticeLoationName(nonSharedLocName) != null
+          && !lattice.isSharedLoc(nonSharedLocName)) {
+        // need to generate a new shared location in the lattice, which is one level lower than the
+        // 'locName' location
+        newLocName = "ILOC" + (LocationInference.locSeed++);
+
+        // Set<String> aboveElementSet = getAboveElementSet(lattice, locName);
+        Set<String> belowElementSet = new HashSet<String>();
+        belowElementSet.addAll(lattice.get(nonSharedLocName));
+
+        // System.out.println("nonSharedLocName=" + nonSharedLocName + "   belowElementSet="
+        // + belowElementSet + "  newLocName=" + newLocName);
+
+        lattice.insertNewLocationBetween(nonSharedLocName, belowElementSet, newLocName);
+      } else {
+        newLocName = nonSharedLocName;
+      }
+
+      lattice.addSharedLoc(newLocName);
+      HierarchyGraph graph = infer.getSimpleHierarchyGraph(desc);
+      Set<Descriptor> descSet = graph.getDescSetOfNode(sharedNode);
+      for (Iterator iterator2 = descSet.iterator(); iterator2.hasNext();) {
+        Descriptor d = (Descriptor) iterator2.next();
+        locSummary.addMapHNodeNameToLocationName(d.getSymbol(), newLocName);
+      }
+      locSummary.addMapHNodeNameToLocationName(sharedNode.getName(), newLocName);
+
+    }
+
     return lattice;
 
   }
 
+  private Set<String> getAboveElementSet(SSJavaLattice<String> lattice, String loc) {
+
+    Set<String> aboveSet = new HashSet<String>();
+
+    Map<String, Set<String>> latticeMap = lattice.getTable();
+    Set<String> keySet = latticeMap.keySet();
+    for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
+      String key = (String) iterator.next();
+      if (latticeMap.get(key).contains(loc)) {
+        aboveSet.add(key);
+      }
+    }
+
+    return aboveSet;
+  }
+
   private void expandCombinationNode(Descriptor desc, SSJavaLattice<String> lattice,
       Set<HNode> visited, Map<TripleItem, String> mapIntermediateLoc, LocationSummary locSummary,
       HNode cnode) {
 
+    // System.out.println("expandCombinationNode=" + cnode);
     // expand the combination node 'outNode'
     // here we need to expand the corresponding combination location in the lattice
     HNode combinationNodeInSCGraph = getCombinationNodeInSCGraph(desc, cnode);
@@ -289,18 +343,17 @@ public class BuildLattice {
 
     // follows the straight line up to another skeleton/combination node
     if (endCombNodeSet.size() > 0) {
-      System.out.println("---endCombNodeSet=" + endCombNodeSet);
+      // System.out.println("---endCombNodeSet=" + endCombNodeSet);
       endCombNodeSet =
           removeTransitivelyReachToNode(desc, combinationNodeInSCGraph, endCombNodeSet);
 
-
       recurDFS(desc, lattice, combinationNodeInSCGraph, endCombNodeSet, visited,
           mapIntermediateLoc, 1, locSummary, cnode);
     } else {
-      endCombNodeSet.add(bottomNode);
-      System.out.println("---endCombNodeSet is zero");
-      System.out.println("---endNodeSetFromSimpleGraph=" + endNodeSetFromSimpleGraph);
-      System.out.println("---incoming=" + simpleGraph.getIncomingNodeSet(cnode));
+      endCombNodeSet.add(LocationInference.BOTTOMHNODE);
+      // System.out.println("---endCombNodeSet is zero");
+      // System.out.println("---endNodeSetFromSimpleGraph=" + endNodeSetFromSimpleGraph);
+      // System.out.println("---incoming=" + simpleGraph.getIncomingNodeSet(cnode));
       recurDFS(desc, lattice, combinationNodeInSCGraph, endCombNodeSet, visited,
           mapIntermediateLoc, 1, locSummary, cnode);
 
@@ -339,8 +392,9 @@ public class BuildLattice {
   private HNode getDirectlyReachableSCNodeFromEndNode(HierarchyGraph scGraph, HNode startNode,
       Set<HNode> endNodeSet) {
 
-    System.out.println("getDirectlyReachableSCNodeFromEndNode start=" + startNode + " endNodeSet="
-        + endNodeSet);
+    // System.out.println("getDirectlyReachableSCNodeFromEndNode start=" + startNode +
+    // " endNodeSet="
+    // + endNodeSet);
     Set<HNode> newStartNodeSet = new HashSet<HNode>();
 
     for (Iterator iterator = endNodeSet.iterator(); iterator.hasNext();) {
@@ -355,7 +409,7 @@ public class BuildLattice {
       }
     }
 
-    System.out.println("newStartNodeSet=" + newStartNodeSet);
+    // System.out.println("newStartNodeSet=" + newStartNodeSet);
 
     if (newStartNodeSet.size() == 0) {
       newStartNodeSet.add(startNode);
@@ -385,14 +439,14 @@ public class BuildLattice {
 
   private HNode getDirectlyReachableNodeFromStartNodeReachToEndNode(HierarchyGraph scGraph,
       HNode startNode, HNode endNode) {
-    System.out.println("getDirectlyReachableNodeFromStartNodeReachToEndNode start=" + startNode
-        + " end=" + endNode);
+    // System.out.println("getDirectlyReachableNodeFromStartNodeReachToEndNode start=" + startNode
+    // + " end=" + endNode);
     Set<HNode> connected = new HashSet<HNode>();
     recurDirectlyReachableNodeFromStartNodeReachToEndNode(scGraph, startNode, endNode, connected);
     if (connected.size() == 0) {
       connected.add(endNode);
     }
-    System.out.println("connected=" + connected);
+    // System.out.println("connected=" + connected);
 
     return connected.iterator().next();
   }
@@ -433,42 +487,55 @@ public class BuildLattice {
       Set<String> belowSet = new HashSet<String>();
       for (Iterator iterator = endNodeSet.iterator(); iterator.hasNext();) {
         HNode endNode = (HNode) iterator.next();
-        belowSet.add(endNode.getName());
+        String locName;
+        if (locSummary.getMapHNodeNameToLocationName().containsKey(endNode.getName())) {
+          locName = locSummary.getLocationName(endNode.getName());
+        } else {
+          locName = endNode.getName();
+        }
+        belowSet.add(locName);
       }
-
       lattice.insertNewLocationBetween(above, belowSet, newLocName);
 
       mapIntermediateLoc.put(item, newLocName);
     }
 
     String locName = mapIntermediateLoc.get(item);
-
     HierarchyGraph graph = infer.getSimpleHierarchyGraph(desc);
 
-    Set<Descriptor> descSet = graph.getDescSetOfNode(curNode);
-    for (Iterator iterator = descSet.iterator(); iterator.hasNext();) {
-      Descriptor d = (Descriptor) iterator.next();
-      locSummary.addMapHNodeNameToLocationName(d.getSymbol(), locName);
-    }
-    locSummary.addMapHNodeNameToLocationName(curNode.getName(), locName);
-
     if (curNode.isSharedNode()) {
-      lattice.addSharedLoc(locName);
+      // if the current node is shared location, add a shared location to the lattice later
+      mapSharedNodeToTripleItem.put(curNode, item);
+    } else {
+      Set<Descriptor> descSet = graph.getDescSetOfNode(curNode);
+      for (Iterator iterator = descSet.iterator(); iterator.hasNext();) {
+        Descriptor d = (Descriptor) iterator.next();
+        locSummary.addMapHNodeNameToLocationName(d.getSymbol(), locName);
+      }
+      locSummary.addMapHNodeNameToLocationName(curNode.getName(), locName);
     }
 
-    System.out.println("-TripleItem=" + item);
-    System.out.println("-curNode=" + curNode.getName() + " locName=" + locName);
+    // System.out.println("-TripleItem=" + item);
+    // System.out.println("-curNode=" + curNode.getName() + " S=" + curNode.isSharedNode()
+    // + " locName=" + locName);
 
     Set<HNode> outSet = graph.getOutgoingNodeSet(curNode);
     for (Iterator iterator2 = outSet.iterator(); iterator2.hasNext();) {
       HNode outNode = (HNode) iterator2.next();
-      if (!outNode.isSkeleton() && !outNode.isCombinationNode() && !visited.contains(outNode)) {
-        visited.add(outNode);
-        recurDFSNormalNode(desc, lattice, startNode, endNodeSet, visited, mapIntermediateLoc,
-            idx + 1, locSummary, outNode);
-      } else if (!outNode.isSkeleton() && outNode.isCombinationNode() && !visited.contains(outNode)) {
-        expandCombinationNode(desc, lattice, visited, mapIntermediateLoc, locSummary, outNode);
+
+      Set<HNode> inNodeSetToOutNode = graph.getIncomingNodeSet(outNode);
+
+      if (visited.containsAll(inNodeSetToOutNode)) {
+        if (!outNode.isSkeleton() && !outNode.isCombinationNode() && !visited.contains(outNode)) {
+          visited.add(outNode);
+          recurDFSNormalNode(desc, lattice, startNode, endNodeSet, visited, mapIntermediateLoc,
+              idx + 1, locSummary, outNode);
+        } else if (!outNode.isSkeleton() && outNode.isCombinationNode()
+            && !visited.contains(outNode)) {
+          expandCombinationNode(desc, lattice, visited, mapIntermediateLoc, locSummary, outNode);
+        }
       }
+
     }
 
   }
@@ -498,33 +565,63 @@ public class BuildLattice {
         }
         lattice.insertNewLocationBetween(above, belowSet, newLocName);
         mapIntermediateLoc.put(item, newLocName);
-
       }
 
     }
 
+    // TODO
+    // Do we need to skip the combination node and assign a shared location to the next node?
+    // if (idx == 1 && curNode.isSharedNode()) {
+    // System.out.println("THE FIRST COMBINATION NODE EXPANSION IS SHARED!");
+    // recurDFS(desc, lattice, combinationNodeInSCGraph, endNodeSet, visited, mapIntermediateLoc,
+    // idx + 1, locSummary, curNode);
+    // return;
+    // }
+
     HierarchyGraph graph = infer.getSimpleHierarchyGraph(desc);
     String locName = mapIntermediateLoc.get(item);
-    Set<Descriptor> descSet = graph.getDescSetOfNode(curNode);
-    for (Iterator iterator = descSet.iterator(); iterator.hasNext();) {
-      Descriptor d = (Descriptor) iterator.next();
-      locSummary.addMapHNodeNameToLocationName(d.getSymbol(), locName);
+    if (curNode.isSharedNode()) {
+      // if the current node is shared location, add a shared location to the lattice later
+      mapSharedNodeToTripleItem.put(curNode, item);
+    } else {
+      Set<Descriptor> descSet = graph.getDescSetOfNode(curNode);
+      for (Iterator iterator = descSet.iterator(); iterator.hasNext();) {
+        Descriptor d = (Descriptor) iterator.next();
+        locSummary.addMapHNodeNameToLocationName(d.getSymbol(), locName);
+      }
+      locSummary.addMapHNodeNameToLocationName(curNode.getName(), locName);
     }
-    locSummary.addMapHNodeNameToLocationName(curNode.getName(), locName);
 
     // System.out.println("-TripleItem=" + item);
-    // System.out.println("-curNode=" + curNode.getName() + " locName=" + locName);
+    // System.out.println("-curNode=" + curNode.getName() + " S=" + curNode.isSharedNode()
+    // + " locName=" + locName);
 
     Set<HNode> outSet = graph.getOutgoingNodeSet(curNode);
     for (Iterator iterator2 = outSet.iterator(); iterator2.hasNext();) {
       HNode outNode = (HNode) iterator2.next();
-      if (!outNode.isSkeleton() && !visited.contains(outNode)) {
-        if (combinationNodeInSCGraph.equals(getCombinationNodeInSCGraph(desc, outNode))) {
-          visited.add(outNode);
-          recurDFS(desc, lattice, combinationNodeInSCGraph, endNodeSet, visited,
-              mapIntermediateLoc, idx + 1, locSummary, outNode);
+      // System.out.println("recurDFS outNode=" + outNode);
+      // System.out.println("---cur combinationNodeInSCGraph=" + combinationNodeInSCGraph);
+      // System.out.println("---outNode combinationNodeInSCGraph="
+      // + getCombinationNodeInSCGraph(desc, outNode));
+
+      Set<HNode> inNodeSetToOutNode = graph.getIncomingNodeSet(outNode);
+
+      if (visited.containsAll(inNodeSetToOutNode)) {
+        if (!outNode.isSkeleton() && !visited.contains(outNode)) {
+          if (outNode.isCombinationNode()) {
+            // check whether the next combination node is different from the current node
+            if (combinationNodeInSCGraph.equals(getCombinationNodeInSCGraph(desc, outNode))) {
+              visited.add(outNode);
+              recurDFS(desc, lattice, combinationNodeInSCGraph, endNodeSet, visited,
+                  mapIntermediateLoc, idx + 1, locSummary, outNode);
+            } else {
+              expandCombinationNode(desc, lattice, visited, mapIntermediateLoc, locSummary, outNode);
+            }
+          }
+
         }
       }
+
     }
 
   }
@@ -699,11 +796,21 @@ class TripleItem {
   public HNode higherNode;
   public Set<HNode> lowerNodeSet;
   public int idx;
+  public boolean isShared;
 
   public TripleItem(HNode h, Set<HNode> l, int i) {
     higherNode = h;
     lowerNodeSet = l;
     idx = i;
+    isShared = false;
+  }
+
+  public void setShared(boolean in) {
+    this.isShared = in;
+  }
+
+  public boolean isShared() {
+    return isShared;
   }
 
   public int hashCode() {
@@ -713,6 +820,10 @@ class TripleItem {
       h = higherNode.hashCode();
     }
 
+    if (isShared) {
+      h++;
+    }
+
     return h + lowerNodeSet.hashCode() + idx;
   }
 
@@ -721,7 +832,7 @@ class TripleItem {
     if (obj instanceof TripleItem) {
       TripleItem in = (TripleItem) obj;
       if ((higherNode == null || (higherNode != null && higherNode.equals(in.higherNode)))
-          && lowerNodeSet.equals(in.lowerNodeSet) && idx == in.idx) {
+          && lowerNodeSet.equals(in.lowerNodeSet) && idx == in.idx && isShared == in.isShared()) {
         return true;
       }
     }
@@ -730,6 +841,10 @@ class TripleItem {
   }
 
   public String toString() {
-    return higherNode + "-" + idx + "->" + lowerNodeSet;
+    String rtr = higherNode + "-" + idx + "->" + lowerNodeSet;
+    if (isShared) {
+      rtr += " S";
+    }
+    return rtr;
   }
 }