a bunch of fixes.
[IRC.git] / Robust / src / Analysis / SSJava / FlowGraph.java
index 049317d8ee210ca72c975d20e774be05c10d9ef5..ce0021cadcfa6c7ea3f79e5b9583cc604b8cedff 100644 (file)
@@ -23,6 +23,7 @@ public class FlowGraph {
   MethodDescriptor md;
 
   Set<FlowNode> nodeSet;
+  Set<FlowNode> returnNodeSet;
   FlowNode thisVarNode;
 
   // maps the composite representation of field/var descriptors to infer nodes
@@ -31,25 +32,45 @@ public class FlowGraph {
   // maps an infer node to the set of neighbors which is pointed by the node
   Map<NTuple<Descriptor>, Set<FlowNode>> mapNodeToNeighborSet;
 
+  // maps a paramter descriptor to its index
+  Map<Descriptor, Integer> mapParamDescToIdx;
   boolean debug = true;
 
-  public FlowGraph(MethodDescriptor md) {
+  public FlowGraph(MethodDescriptor md, Map<Descriptor, Integer> mapParamDescToIdx) {
     this.md = md;
-    nodeSet = new HashSet<FlowNode>();
-    mapDescTupleToInferNode = new HashMap<NTuple<Descriptor>, FlowNode>();
-    mapNodeToNeighborSet = new HashMap<NTuple<Descriptor>, Set<FlowNode>>();
+    this.nodeSet = new HashSet<FlowNode>();
+    this.mapDescTupleToInferNode = new HashMap<NTuple<Descriptor>, FlowNode>();
+    this.mapNodeToNeighborSet = new HashMap<NTuple<Descriptor>, Set<FlowNode>>();
+    this.mapParamDescToIdx = new HashMap<Descriptor, Integer>();
+    this.mapParamDescToIdx.putAll(mapParamDescToIdx);
+    this.returnNodeSet = new HashSet<FlowNode>();
 
     // create a node for 'this' varialbe
     NTuple<Descriptor> thisDescTuple = new NTuple<Descriptor>();
     thisDescTuple.add(md.getThis());
-    FlowNode thisNode = new FlowNode(thisDescTuple);
+    FlowNode thisNode = new FlowNode(thisDescTuple, true);
     NTuple<Descriptor> thisVarTuple = new NTuple<Descriptor>();
     thisVarTuple.add(md.getThis());
-    mapDescTupleToInferNode.put(thisVarTuple, thisNode);
+    createNewFlowNode(thisVarTuple);
     thisVarNode = thisNode;
 
   }
 
+  public Set<FlowNode> getNodeSet() {
+    return nodeSet;
+  }
+
+  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) {
@@ -60,6 +81,26 @@ public class FlowGraph {
     System.out.println("add a new neighbor " + neighbor + " to " + node);
   }
 
+  public boolean hasEdge(NTuple<Descriptor> fromDescTuple, NTuple<Descriptor> toDescTuple) {
+
+    FlowNode fromNode = mapDescTupleToInferNode.get(fromDescTuple);
+    FlowNode toNode = mapDescTupleToInferNode.get(toDescTuple);
+
+    Set<FlowEdge> fromNodeOutEdgeSet = fromNode.getOutEdgeSet();
+    for (Iterator iterator = fromNodeOutEdgeSet.iterator(); iterator.hasNext();) {
+      FlowEdge flowEdge = (FlowEdge) iterator.next();
+      if (flowEdge.getDst().equals(toNode)) {
+        return true;
+      } else {
+        if (hasEdge(flowEdge.getDst().getDescTuple(), toDescTuple)) {
+          return true;
+        }
+      }
+    }
+
+    return false;
+  }
+
   public void addValueFlowEdge(NTuple<Descriptor> fromDescTuple, NTuple<Descriptor> toDescTuple) {
 
     FlowNode fromNode = mapDescTupleToInferNode.get(fromDescTuple);
@@ -111,7 +152,8 @@ public class FlowGraph {
   public FlowNode createNewFlowNode(NTuple<Descriptor> tuple) {
 
     if (!mapDescTupleToInferNode.containsKey(tuple)) {
-      FlowNode node = new FlowNode(tuple);
+
+      FlowNode node = new FlowNode(tuple, isParamter(tuple));
       mapDescTupleToInferNode.put(tuple, node);
       nodeSet.add(node);
 
@@ -128,6 +170,33 @@ public class FlowGraph {
 
   }
 
+  public void setReturnFlowNode(NTuple<Descriptor> tuple) {
+
+    if (!mapDescTupleToInferNode.containsKey(tuple)) {
+      createNewFlowNode(tuple);
+    }
+
+    FlowNode node = mapDescTupleToInferNode.get(tuple);
+    node.setReturn(true);
+
+    returnNodeSet.add(node);
+  }
+
+  public Set<FlowNode> getReturnNodeSet() {
+    return returnNodeSet;
+  }
+
+  public boolean isParamter(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 {
 
@@ -169,7 +238,7 @@ public class FlowGraph {
 
   public void writeGraph() throws java.io.IOException {
 
-    String graphName = md.toString();
+    String graphName = "flowgraph_" + md.toString();
     graphName = graphName.replaceAll("[\\W]", "");
 
     BufferedWriter bw = new BufferedWriter(new FileWriter(graphName + ".dot"));