changes: building field/method hierarchy graph + inserting combination nodes at the...
[IRC.git] / Robust / src / Analysis / SSJava / FlowGraph.java
index 4e0a7a66a5bddafeb279274ac01569e3a6fe3dbd..44a859ba84e9480e743bc08d95c33d2b93a296f9 100644 (file)
@@ -13,6 +13,7 @@ import IR.ClassDescriptor;
 import IR.Descriptor;
 import IR.FieldDescriptor;
 import IR.MethodDescriptor;
+import IR.NameDescriptor;
 import IR.VarDescriptor;
 
 public class FlowGraph {
@@ -34,6 +35,11 @@ public class FlowGraph {
 
   // maps a paramter descriptor to its index
   Map<Descriptor, Integer> mapParamDescToIdx;
+
+  Map<Integer, FlowNode> mapIdxToFlowNode;
+
+  public static int interseed = 0;
+
   boolean debug = true;
 
   public FlowGraph(MethodDescriptor md, Map<Descriptor, Integer> mapParamDescToIdx) {
@@ -46,18 +52,66 @@ public class FlowGraph {
     this.mapParamDescToIdx = new HashMap<Descriptor, Integer>();
     this.mapParamDescToIdx.putAll(mapParamDescToIdx);
     this.returnNodeSet = new HashSet<FlowNode>();
+    this.mapIdxToFlowNode = new HashMap<Integer, FlowNode>();
 
     if (!md.isStatic()) {
       // create a node for 'this' varialbe
-      NTuple<Descriptor> thisDescTuple = new NTuple<Descriptor>();
-      thisDescTuple.add(md.getThis());
-      FlowNode thisNode = new FlowNode(thisDescTuple, true);
+      // NTuple<Descriptor> thisDescTuple = new NTuple<Descriptor>();
+      // thisDescTuple.add(md.getThis());
+
       NTuple<Descriptor> thisVarTuple = new NTuple<Descriptor>();
       thisVarTuple.add(md.getThis());
-      createNewFlowNode(thisVarTuple);
+      FlowNode thisNode = createNewFlowNode(thisVarTuple);
+      thisNode.setSkeleton(true);
       thisVarNode = thisNode;
     }
 
+    setupMapIdxToDesc();
+
+  }
+
+  public Map<Descriptor, Integer> getMapParamDescToIdx() {
+    return mapParamDescToIdx;
+  }
+
+  public FlowNode createIntermediateNode() {
+    NTuple<Descriptor> tuple = new NTuple<Descriptor>();
+    Descriptor interDesc = new InterDescriptor(LocationInference.INTERLOC + interseed);
+    tuple.add(interDesc);
+    interseed++;
+
+    FlowNode newNode = new FlowNode(tuple);
+    newNode.setIntermediate(true);
+
+    mapDescTupleToInferNode.put(tuple, newNode);
+    nodeSet.add(newNode);
+
+    System.out.println("create new intermediate node= " + newNode);
+
+    return newNode;
+  }
+
+  private void setupMapIdxToDesc() {
+
+    Set<Descriptor> descSet = mapParamDescToIdx.keySet();
+    for (Iterator iterator = descSet.iterator(); iterator.hasNext();) {
+      Descriptor paramDesc = (Descriptor) iterator.next();
+      int idx = mapParamDescToIdx.get(paramDesc);
+      NTuple<Descriptor> descTuple = new NTuple<Descriptor>();
+      descTuple.add(paramDesc);
+      FlowNode paramNode = getFlowNode(descTuple);
+      mapIdxToFlowNode.put(idx, paramNode);
+      paramNode.setSkeleton(true);
+    }
+
+  }
+
+  public int getNumParameters() {
+    return mapIdxToFlowNode.keySet().size();
+  }
+
+  public FlowNode getParamFlowNode(int idx) {
+    return mapIdxToFlowNode.get(idx);
   }
 
   public Set<FlowNode> getNodeSet() {
@@ -68,17 +122,6 @@ public class FlowGraph {
     return md;
   }
 
-  public Set<FlowNode> getParameterNodeSet() {
-    Set<FlowNode> paramNodeSet = new HashSet<FlowNode>();
-    for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
-      FlowNode fn = (FlowNode) iterator.next();
-      if (fn.isParameter()) {
-        paramNodeSet.add(fn);
-      }
-    }
-    return paramNodeSet;
-  }
-
   public void addNeighbor(FlowNode node, FlowNode neighbor) {
     Set<FlowNode> set = mapNodeToNeighborSet.get(node);
     if (set == null) {
@@ -86,7 +129,20 @@ public class FlowGraph {
     }
     set.add(neighbor);
 
-//    System.out.println("add a new neighbor " + neighbor + " to " + node);
+    // System.out.println("add a new neighbor " + neighbor + " to " + node);
+  }
+
+  public boolean isParamDesc(Descriptor desc) {
+
+    if (mapParamDescToIdx.containsKey(desc)) {
+      int idx = mapParamDescToIdx.get(desc);
+      if (!md.isStatic() && idx == 0) {
+        return false;
+      }
+      return true;
+    }
+
+    return false;
   }
 
   public boolean hasEdge(NTuple<Descriptor> fromDescTuple, NTuple<Descriptor> toDescTuple) {
@@ -114,23 +170,37 @@ public class FlowGraph {
     FlowNode fromNode = getFlowNode(fromDescTuple);
     FlowNode toNode = getFlowNode(toDescTuple);
 
-    // System.out.println("create an edge from " + fromNode + " to " + toNode);
+    System.out.println("create an edge from " + fromNode + " to " + toNode);
 
     int fromTupleSize = fromDescTuple.size();
-    NTuple<Descriptor> curTuple = new NTuple<Descriptor>();
+    NTuple<Descriptor> curFromTuple = new NTuple<Descriptor>();
     for (int i = 0; i < fromTupleSize; i++) {
       Descriptor desc = fromDescTuple.get(i);
-      curTuple.add(desc);
-      addFlowEdge(getFlowNode(curTuple), toNode, fromDescTuple, toDescTuple);
+      curFromTuple.add(desc);
+      int toTupleSize = toDescTuple.size();
+      NTuple<Descriptor> curToTuple = new NTuple<Descriptor>();
+      for (int k = 0; k < toTupleSize; k++) {
+        Descriptor toDesc = toDescTuple.get(k);
+        curToTuple.add(toDesc);
+        addFlowEdge(getFlowNode(curFromTuple), getFlowNode(curToTuple), fromDescTuple, toDescTuple);
+      }
     }
 
-    int toTupleSize = toDescTuple.size();
-    curTuple = new NTuple<Descriptor>();
-    for (int i = 0; i < toTupleSize; i++) {
-      Descriptor desc = toDescTuple.get(i);
-      curTuple.add(desc);
-      addFlowEdge(fromNode, getFlowNode(curTuple), fromDescTuple, toDescTuple);
-    }
+    // int fromTupleSize = fromDescTuple.size();
+    // NTuple<Descriptor> curTuple = new NTuple<Descriptor>();
+    // for (int i = 0; i < fromTupleSize; i++) {
+    // Descriptor desc = fromDescTuple.get(i);
+    // curTuple.add(desc);
+    // addFlowEdge(getFlowNode(curTuple), toNode, fromDescTuple, toDescTuple);
+    // }
+    //
+    // int toTupleSize = toDescTuple.size();
+    // curTuple = new NTuple<Descriptor>();
+    // for (int i = 0; i < toTupleSize; i++) {
+    // Descriptor desc = toDescTuple.get(i);
+    // curTuple.add(desc);
+    // addFlowEdge(fromNode, getFlowNode(curTuple), fromDescTuple, toDescTuple);
+    // }
 
   }
 
@@ -140,17 +210,15 @@ public class FlowGraph {
     FlowEdge edge = new FlowEdge(fromNode, toNode, initTuple, endTuple);
     fromNode.addOutEdge(edge);
 
-//    System.out.println("add a new edge=" + edge);
-
+    System.out.println("add a new edge=" + edge);
   }
 
   public FlowNode getFlowNode(NTuple<Descriptor> descTuple) {
-    if (mapDescTupleToInferNode.containsKey(descTuple)) {
-      return mapDescTupleToInferNode.get(descTuple);
-    } else {
+    if (!mapDescTupleToInferNode.containsKey(descTuple)) {
       FlowNode node = createNewFlowNode(descTuple);
-      return node;
+      mapDescTupleToInferNode.put(descTuple, node);
     }
+    return mapDescTupleToInferNode.get(descTuple);
   }
 
   public FlowNode getThisVarNode() {
@@ -160,8 +228,7 @@ public class FlowGraph {
   public FlowNode createNewFlowNode(NTuple<Descriptor> tuple) {
 
     if (!mapDescTupleToInferNode.containsKey(tuple)) {
-
-      FlowNode node = new FlowNode(tuple, isParamter(tuple));
+      FlowNode node = new FlowNode(tuple);
       mapDescTupleToInferNode.put(tuple, node);
       nodeSet.add(node);
 
@@ -172,7 +239,7 @@ public class FlowGraph {
         getFlowNode(baseTuple).addFieldNode(node);
       }
 
-//      System.out.println("Creating new node=" + node);
+      // System.out.println("Creating new node=" + node);
       return node;
     } else {
       return mapDescTupleToInferNode.get(tuple);
@@ -180,7 +247,7 @@ public class FlowGraph {
 
   }
 
-  public void setReturnFlowNode(NTuple<Descriptor> tuple) {
+  public void addReturnFlowNode(NTuple<Descriptor> tuple) {
 
     if (!mapDescTupleToInferNode.containsKey(tuple)) {
       createNewFlowNode(tuple);
@@ -196,30 +263,67 @@ public class FlowGraph {
     return returnNodeSet;
   }
 
-  public Set<FlowNode> getReachableFlowNodeSet(FlowNode fn) {
+  public Set<FlowNode> getLocalReachFlowNodeSetFrom(FlowNode fn) {
     Set<FlowNode> set = new HashSet<FlowNode>();
-    getReachableFlowNodeSet(fn, set);
+    recurLocalReachFlowNodeSet(fn, set);
     return set;
   }
 
-  private void getReachableFlowNodeSet(FlowNode fn, Set<FlowNode> visited) {
+  private void recurLocalReachFlowNodeSet(FlowNode fn, Set<FlowNode> visited) {
 
     for (Iterator iterator = fn.getOutEdgeSet().iterator(); iterator.hasNext();) {
       FlowEdge edge = (FlowEdge) iterator.next();
+      FlowNode dstNode = edge.getDst();
 
-      if (fn.equals(getFlowNode(edge.getInitTuple()))) {
+      if (!visited.contains(dstNode)) {
+        visited.add(dstNode);
+        recurLocalReachFlowNodeSet(dstNode, visited);
+      }
+    }
 
-        FlowNode dstNode = getFlowNode(edge.getEndTuple());
+  }
+
+  private void getReachFlowNodeSetFrom(FlowNode fn, Set<FlowNode> visited) {
 
+    for (Iterator iterator = fn.getOutEdgeSet().iterator(); iterator.hasNext();) {
+      FlowEdge edge = (FlowEdge) iterator.next();
+
+      if (fn.equals(getFlowNode(edge.getInitTuple()))) {
+        FlowNode dstNode = getFlowNode(edge.getEndTuple());
         if (!visited.contains(dstNode)) {
           visited.add(dstNode);
-          getReachableFlowNodeSet(dstNode, visited);
+          getReachFlowNodeSetFrom(dstNode, visited);
         }
       }
     }
 
   }
 
+  public Set<FlowNode> getReachFlowNodeSetFrom(FlowNode fn) {
+    Set<FlowNode> set = new HashSet<FlowNode>();
+    getReachFlowNodeSetFrom(fn, set);
+    return set;
+  }
+
+  // private void getReachFlowNodeSetFrom(FlowNode fn, Set<FlowNode> visited) {
+  //
+  // for (Iterator iterator = fn.getOutEdgeSet().iterator();
+  // iterator.hasNext();) {
+  // FlowEdge edge = (FlowEdge) iterator.next();
+  //
+  // if (fn.equals(getFlowNode(edge.getInitTuple()))) {
+  //
+  // FlowNode dstNode = getFlowNode(edge.getEndTuple());
+  //
+  // if (!visited.contains(dstNode)) {
+  // visited.add(dstNode);
+  // getReachFlowNodeSetFrom(dstNode, visited);
+  // }
+  // }
+  // }
+  //
+  // }
+
   public Set<NTuple<Location>> getReachableFlowTupleSet(Set<NTuple<Location>> visited, FlowNode fn) {
     for (Iterator iterator = fn.getOutEdgeSet().iterator(); iterator.hasNext();) {
       FlowEdge edge = (FlowEdge) iterator.next();
@@ -238,6 +342,10 @@ public class FlowGraph {
     return visited;
   }
 
+  public NTuple<Location> getLocationTuple(NTuple<Descriptor> descTuple) {
+    return getLocationTuple(getFlowNode(descTuple));
+  }
+
   public NTuple<Location> getLocationTuple(FlowNode fn) {
 
     if (!mapFlowNodeToLocTuple.containsKey(fn)) {
@@ -246,12 +354,19 @@ public class FlowGraph {
       ClassDescriptor cd = null;
 
       Descriptor localDesc = fn.getDescTuple().get(0);
-      if (localDesc.getSymbol().equals(LocationInference.TOPLOC)) {
+
+      if (fn.isIntermediate()) {
+        Location interLoc = new Location(md, localDesc.getSymbol());
+        interLoc.setLocDescriptor(localDesc);
+        locTuple.add(interLoc);
+      } else if (localDesc.getSymbol().equals(LocationInference.TOPLOC)) {
         Location topLoc = new Location(md, Location.TOP);
+        topLoc.setLocDescriptor(LocationInference.TOPDESC);
         locTuple.add(topLoc);
       } else if (localDesc.getSymbol().equals(LocationInference.GLOBALLOC)) {
-        Location topLoc = new Location(md, LocationInference.GLOBALLOC);
-        locTuple.add(topLoc);
+        Location globalLoc = new Location(md, LocationInference.GLOBALLOC);
+        globalLoc.setLocDescriptor(LocationInference.GLOBALDESC);
+        locTuple.add(globalLoc);
       } else {
         // normal case
         for (int i = 0; i < descTuple.size(); i++) {
@@ -259,7 +374,7 @@ public class FlowGraph {
           Location loc;
           if (i == 0) {
             loc = new Location(md, curDesc.getSymbol());
-            loc.setLocDescriptor(md);
+            loc.setLocDescriptor(curDesc);
             cd = ((VarDescriptor) curDesc).getType().getClassDesc();
           } else {
             loc = new Location(cd, curDesc.getSymbol());
@@ -338,17 +453,12 @@ public class FlowGraph {
     return set;
   }
 
-  public boolean isParamter(NTuple<Descriptor> tuple) {
+  public boolean isParameter(NTuple<Descriptor> tuple) {
     // return true if a descriptor tuple is started with a parameter descriptor
     Descriptor firstIdxDesc = tuple.get(0);
     return mapParamDescToIdx.containsKey(firstIdxDesc);
   }
 
-  public int getParamIdx(NTuple<Descriptor> tuple) {
-    Descriptor firstDesc = tuple.get(0);
-    return mapParamDescToIdx.get(firstDesc).intValue();
-  }
-
   private void drawEdges(FlowNode node, BufferedWriter bw, Set<FlowNode> addedNodeSet,
       Set<FlowEdge> addedEdgeSet) throws IOException {