changes: now Inference engine works fine with the EyeTracking benchmark.
[IRC.git] / Robust / src / Analysis / SSJava / FlowGraph.java
index 8ea9fe5454f40f338896d01fec31dc718f06e8d6..4054f701a78eef73b4eed01519d7151e7f2d6275 100644 (file)
@@ -13,7 +13,9 @@ import IR.ClassDescriptor;
 import IR.Descriptor;
 import IR.FieldDescriptor;
 import IR.MethodDescriptor;
+import IR.NameDescriptor;
 import IR.VarDescriptor;
+import IR.Tree.MethodInvokeNode;
 
 public class FlowGraph {
 
@@ -37,6 +39,10 @@ public class FlowGraph {
   // DS for the lattice generation
   Map<Integer, FlowNode> mapIdxToFlowNode;
 
+  Map<MethodInvokeNode, FlowReturnNode> mapMethodInvokeNodeToFlowReturnNode;
+
+  Map<Descriptor, ClassDescriptor> mapIntersectionDescToEnclosingDescriptor;
+
   public static int interseed = 0;
 
   boolean debug = true;
@@ -52,6 +58,8 @@ public class FlowGraph {
     this.mapIdxToFlowNode = new HashMap<Integer, FlowNode>();
     this.mapFlowNodeToOutEdgeSet = new HashMap<FlowNode, Set<FlowEdge>>();
     this.mapFlowNodeToInEdgeSet = new HashMap<FlowNode, Set<FlowEdge>>();
+    this.mapMethodInvokeNodeToFlowReturnNode = new HashMap<MethodInvokeNode, FlowReturnNode>();
+    this.mapIntersectionDescToEnclosingDescriptor = new HashMap<Descriptor, ClassDescriptor>();
 
     if (!md.isStatic()) {
       // create a node for 'this' varialbe
@@ -69,6 +77,15 @@ public class FlowGraph {
 
   }
 
+  public void addMapInterLocNodeToEnclosingDescriptor(Descriptor interDesc, ClassDescriptor desc) {
+    System.out.println("##### INTERLOC=" + interDesc + "    enclosing desc=" + desc);
+    mapIntersectionDescToEnclosingDescriptor.put(interDesc, desc);
+  }
+
+  public ClassDescriptor getEnclosingDescriptor(Descriptor interDesc) {
+    return mapIntersectionDescToEnclosingDescriptor.get(interDesc);
+  }
+
   public Map<NTuple<Descriptor>, FlowNode> getMapDescTupleToInferNode() {
     return mapDescTupleToInferNode;
   }
@@ -114,6 +131,25 @@ public class FlowGraph {
     return newNode;
   }
 
+  public FlowReturnNode getFlowReturnNode(MethodInvokeNode min) {
+    return mapMethodInvokeNodeToFlowReturnNode.get(min);
+  }
+
+  public FlowReturnNode createReturnNode(MethodInvokeNode min) {
+    NTuple<Descriptor> tuple = new NTuple<Descriptor>();
+    NameDescriptor n = new NameDescriptor("RETURNLOC" + (LocationInference.locSeed++));
+    tuple.add(n);
+
+    FlowReturnNode newNode = new FlowReturnNode(tuple, min);
+    mapDescTupleToInferNode.put(tuple, newNode);
+    mapMethodInvokeNodeToFlowReturnNode.put(min, newNode);
+    // nodeSet.add(newNode);
+
+    System.out.println("create new set node= " + newNode);
+
+    return newNode;
+  }
+
   private void setupMapIdxToDesc() {
 
     Set<Descriptor> descSet = mapParamDescToIdx.keySet();
@@ -149,6 +185,12 @@ public class FlowGraph {
     return edgeSet;
   }
 
+  public Set<FlowNode> getParamFlowNodeSet() {
+    Set<FlowNode> setParamFlowNode = new HashSet<FlowNode>();
+    setParamFlowNode.addAll(mapIdxToFlowNode.values());
+    return setParamFlowNode;
+  }
+
   public Set<FlowNode> getNodeSet() {
     Set<FlowNode> set = new HashSet<FlowNode>();
     set.addAll(mapDescTupleToInferNode.values());
@@ -231,6 +273,10 @@ public class FlowGraph {
     FlowNode fromNode = getFlowNode(fromDescTuple);
     FlowNode toNode = getFlowNode(toDescTuple);
 
+    if (toNode.getDescTuple().get(0).equals(LocationInference.LITERALDESC)) {
+      return;
+    }
+
     // System.out.println("create an edge from " + fromNode + " to " + toNode);
 
     int fromTupleSize = fromDescTuple.size();
@@ -304,6 +350,7 @@ public class FlowGraph {
 
   public FlowNode createNewFlowNode(NTuple<Descriptor> tuple) {
 
+    // System.out.println("createNewFlowNode=" + tuple);
     if (!mapDescTupleToInferNode.containsKey(tuple)) {
       FlowNode node = new FlowNode(tuple);
       mapDescTupleToInferNode.put(tuple, node);
@@ -349,12 +396,29 @@ public class FlowGraph {
     Set<FlowEdge> outEdgeSet = getOutEdgeSet(fn);
     for (Iterator iterator = outEdgeSet.iterator(); iterator.hasNext();) {
       FlowEdge edge = (FlowEdge) iterator.next();
-      FlowNode dstNode = edge.getDst();
+      FlowNode originalDstNode = edge.getDst();
+
+      Set<FlowNode> dstNodeSet = new HashSet<FlowNode>();
+      if (originalDstNode instanceof FlowReturnNode) {
+        FlowReturnNode rnode = (FlowReturnNode) originalDstNode;
+        Set<NTuple<Descriptor>> rtupleSetFromRNode = rnode.getReturnTupleSet();
+        Set<NTuple<Descriptor>> rtupleSet = getReturnTupleSet(rtupleSetFromRNode);
+        for (Iterator iterator2 = rtupleSet.iterator(); iterator2.hasNext();) {
+          NTuple<Descriptor> rtuple = (NTuple<Descriptor>) iterator2.next();
+          dstNodeSet.add(getFlowNode(rtuple));
+        }
+      } else {
+        dstNodeSet.add(originalDstNode);
+      }
 
-      if (!visited.contains(dstNode)) {
-        visited.add(dstNode);
-        recurLocalReachFlowNodeSet(dstNode, visited);
+      for (Iterator iterator2 = dstNodeSet.iterator(); iterator2.hasNext();) {
+        FlowNode dstNode = (FlowNode) iterator2.next();
+        if (!visited.contains(dstNode)) {
+          visited.add(dstNode);
+          recurLocalReachFlowNodeSet(dstNode, visited);
+        }
       }
+
     }
 
   }
@@ -387,15 +451,47 @@ public class FlowGraph {
 
     Set<FlowNode> nodeSet = getNodeSet();
     for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
-      FlowNode flowNode = (FlowNode) iterator.next();
-      if (flowNode.getCurrentDescTuple().startsWith(prefix)) {
-        recurReachableSetFrom(flowNode, reachableSet);
+      FlowNode originalSrcNode = (FlowNode) iterator.next();
+
+      Set<FlowNode> srcNodeSet = new HashSet<FlowNode>();
+      if (originalSrcNode instanceof FlowReturnNode) {
+        FlowReturnNode rnode = (FlowReturnNode) originalSrcNode;
+        Set<NTuple<Descriptor>> rtupleSetFromRNode = rnode.getReturnTupleSet();
+        Set<NTuple<Descriptor>> rtupleSet = getReturnTupleSet(rtupleSetFromRNode);
+        // System.out.println("#rnode=" + rnode + "  rtupleSet=" + rtupleSet);
+        for (Iterator iterator2 = rtupleSet.iterator(); iterator2.hasNext();) {
+          NTuple<Descriptor> rtuple = (NTuple<Descriptor>) iterator2.next();
+          if (rtuple.startsWith(prefix)) {
+            // System.out.println("rtuple=" + rtuple + "   give it to recur=" + originalSrcNode);
+            recurReachableSetFrom(originalSrcNode, reachableSet);
+          }
+        }
+      } else {
+        if (originalSrcNode.getCurrentDescTuple().startsWith(prefix)) {
+          recurReachableSetFrom(originalSrcNode, reachableSet);
+        }
       }
+
     }
 
     return reachableSet;
   }
 
+  public Set<NTuple<Descriptor>> getReturnTupleSet(Set<NTuple<Descriptor>> in) {
+
+    Set<NTuple<Descriptor>> normalTupleSet = new HashSet<NTuple<Descriptor>>();
+    for (Iterator iterator2 = in.iterator(); iterator2.hasNext();) {
+      NTuple<Descriptor> tuple = (NTuple<Descriptor>) iterator2.next();
+      FlowNode tupleNode = getFlowNode(tuple);
+      if (tupleNode instanceof FlowReturnNode) {
+        normalTupleSet.addAll(getReturnTupleSet(((FlowReturnNode) tupleNode).getReturnTupleSet()));
+      } else {
+        normalTupleSet.add(tuple);
+      }
+    }
+    return normalTupleSet;
+  }
+
   // private void getReachFlowNodeSetFrom(FlowNode fn, Set<FlowNode> visited) {
   //
   // for (Iterator iterator = fn.getOutEdgeSet().iterator();
@@ -463,13 +559,11 @@ public class FlowGraph {
 
       Descriptor localDesc = fn.getDescTuple().get(0);
 
-      System.out.println("descTuple=" + descTuple);
-
-      if (fn.isIntermediate()) {
+      if (fn.isIntermediate() && fn.getDescTuple().size() == 1) {
         Location interLoc = new Location(md, localDesc.getSymbol());
         interLoc.setLocDescriptor(localDesc);
         locTuple.add(interLoc);
-      } else if (localDesc.getSymbol().equals(LocationInference.TOPLOC)) {
+      } else if (localDesc.getSymbol().equals(SSJavaAnalysis.TOP)) {
         Location topLoc = new Location(md, Location.TOP);
         topLoc.setLocDescriptor(LocationInference.TOPDESC);
         locTuple.add(topLoc);
@@ -487,6 +581,8 @@ public class FlowGraph {
             loc.setLocDescriptor(curDesc);
             if (curDesc instanceof VarDescriptor) {
               cd = ((VarDescriptor) curDesc).getType().getClassDesc();
+            } else if (curDesc instanceof InterDescriptor) {
+              cd = mapIntersectionDescToEnclosingDescriptor.get(curDesc);
             } else {
               // otherwise it should be the last element
               cd = null;
@@ -532,50 +628,29 @@ public class FlowGraph {
         if (node.equals(getFlowNode(flowEdge.getEndTuple()))) {
           FlowNode incomingNode = getFlowNode(flowEdge.getInitTuple());
 
-          if (!visited.contains(incomingNode)) {
-            visited.add(incomingNode);
-            getIncomingFlowNodeSet(incomingNode, visited);
-          }
-        }
-      }
-    }
-
-  }
-
-  public Set<NTuple<Location>> getIncomingFlowTupleSet(FlowNode fn) {
-
-    NTuple<Descriptor> dstTuple = fn.getDescTuple();
-
-    Set<NTuple<Location>> set = new HashSet<NTuple<Location>>();
-
-    ClassDescriptor cd = null;
-
-    for (Iterator iterator = getNodeSet().iterator(); iterator.hasNext();) {
-      FlowNode node = (FlowNode) iterator.next();
-
-      Set<FlowEdge> edgeSet = getOutEdgeSet(node);
-      for (Iterator iterator2 = edgeSet.iterator(); iterator2.hasNext();) {
-        FlowEdge flowEdge = (FlowEdge) iterator2.next();
-        if (dstTuple.equals(flowEdge.getEndTuple())) {
-          NTuple<Descriptor> initTuple = flowEdge.getInitTuple();
-          NTuple<Location> locTuple = new NTuple<Location>();
-          for (int i = 0; i < initTuple.size(); i++) {
-            Descriptor d = initTuple.get(i);
-            Location loc;
-            if (i == 0) {
-              loc = new Location(md, d.getSymbol());
-              cd = ((VarDescriptor) d).getType().getClassDesc();
-            } else {
-              loc = new Location(cd, d.getSymbol());
-              cd = ((FieldDescriptor) d).getType().getClassDesc();
+          if (incomingNode instanceof FlowReturnNode) {
+            FlowReturnNode rnode = (FlowReturnNode) incomingNode;
+            Set<NTuple<Descriptor>> nodeTupleSet = rnode.getReturnTupleSet();
+            Set<NTuple<Descriptor>> rtupleSet = getReturnTupleSet(nodeTupleSet);
+            for (Iterator iterator3 = rtupleSet.iterator(); iterator3.hasNext();) {
+              NTuple<Descriptor> nodeTuple = (NTuple<Descriptor>) iterator3.next();
+              FlowNode fn = getFlowNode(nodeTuple);
+              if (!visited.contains(fn)) {
+                visited.add(fn);
+                getIncomingFlowNodeSet(fn, visited);
+              }
+            }
+          } else {
+            if (!visited.contains(incomingNode)) {
+              visited.add(incomingNode);
+              getIncomingFlowNodeSet(incomingNode, visited);
             }
-            locTuple.add(loc);
           }
-          set.add(locTuple);
+
         }
       }
     }
-    return set;
+
   }
 
   public boolean isParameter(NTuple<Descriptor> tuple) {
@@ -702,7 +777,13 @@ public class FlowGraph {
   }
 
   private void drawNode(FlowNode node, BufferedWriter bw) throws IOException {
-    bw.write(node.getID() + " [label=\"" + node.getPrettyID() + "\"]" + ";\n");
+    if (node instanceof FlowReturnNode) {
+      FlowReturnNode rnode = (FlowReturnNode) node;
+      bw.write(node.getID() + " [label=\"" + node.getPrettyID() + "\"]" + ";\n");
+    } else {
+      bw.write(node.getID() + " [label=\"" + node.getPrettyID() + "\"]" + ";\n");
+    }
+
   }
 
   public void writeGraph() throws java.io.IOException {
@@ -778,4 +859,20 @@ public class FlowGraph {
     bw.write("}\n");
   }
 
+  public void removeEdge(NTuple<Descriptor> from, NTuple<Descriptor> to) {
+
+    Set<FlowEdge> toberemoved = new HashSet<FlowEdge>();
+    Set<FlowEdge> edgeSet = getOutEdgeSet(getFlowNode(from));
+
+    for (Iterator iterator = edgeSet.iterator(); iterator.hasNext();) {
+      FlowEdge flowEdge = (FlowEdge) iterator.next();
+      if (flowEdge.getInitTuple().equals(from) && flowEdge.getEndTuple().equals(to)) {
+        toberemoved.add(flowEdge);
+      }
+    }
+
+    edgeSet.removeAll(toberemoved);
+
+  }
+
 }
\ No newline at end of file