changes.
[IRC.git] / Robust / src / Analysis / SSJava / FlowGraph.java
index a2d1791bb8a698609a668910a803c9a742774a76..39d706144144bcbe0d35333d6d4b30794498305b 100644 (file)
@@ -34,6 +34,9 @@ public class FlowGraph {
 
   // maps a paramter descriptor to its index
   Map<Descriptor, Integer> mapParamDescToIdx;
+
+  Map<Integer, FlowNode> mapIdxToFlowNode;
+
   boolean debug = true;
 
   public FlowGraph(MethodDescriptor md, Map<Descriptor, Integer> mapParamDescToIdx) {
@@ -46,6 +49,7 @@ 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
@@ -58,6 +62,25 @@ public class FlowGraph {
       thisVarNode = thisNode;
     }
 
+    setupMapIdxToDesc();
+
+  }
+
+  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);
+      mapIdxToFlowNode.put(idx, getFlowNode(descTuple));
+    }
+
+  }
+
+  public FlowNode getParamFlowNode(int idx) {
+    return mapIdxToFlowNode.get(idx);
   }
 
   public Set<FlowNode> getNodeSet() {
@@ -86,7 +109,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) {
@@ -111,10 +147,10 @@ public class FlowGraph {
 
   public void addValueFlowEdge(NTuple<Descriptor> fromDescTuple, NTuple<Descriptor> toDescTuple) {
 
-    FlowNode fromNode = mapDescTupleToInferNode.get(fromDescTuple);
-    FlowNode toNode = mapDescTupleToInferNode.get(toDescTuple);
+    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>();
@@ -140,7 +176,7 @@ 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);
 
   }
 
@@ -160,8 +196,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, isParameter(tuple));
       mapDescTupleToInferNode.put(tuple, node);
       nodeSet.add(node);
 
@@ -172,7 +207,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 +215,7 @@ public class FlowGraph {
 
   }
 
-  public void setReturnFlowNode(NTuple<Descriptor> tuple) {
+  public void addReturnFlowNode(NTuple<Descriptor> tuple) {
 
     if (!mapDescTupleToInferNode.containsKey(tuple)) {
       createNewFlowNode(tuple);
@@ -238,6 +273,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)) {
@@ -245,20 +284,33 @@ public class FlowGraph {
       NTuple<Location> locTuple = new NTuple<Location>();
       ClassDescriptor cd = null;
 
-      for (int i = 0; i < descTuple.size(); i++) {
-        Descriptor curDesc = descTuple.get(i);
-        Location loc;
-        if (i == 0) {
-          loc = new Location(md, curDesc.getSymbol());
-          loc.setLocDescriptor(md);
-          cd = ((VarDescriptor) curDesc).getType().getClassDesc();
-        } else {
-          loc = new Location(cd, curDesc.getSymbol());
-          loc.setLocDescriptor(curDesc);
-          cd = ((FieldDescriptor) curDesc).getType().getClassDesc();
+      Descriptor localDesc = fn.getDescTuple().get(0);
+      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 globalLoc = new Location(md, LocationInference.GLOBALLOC);
+        globalLoc.setLocDescriptor(LocationInference.GLOBALDESC);
+        locTuple.add(globalLoc);
+      } else {
+        // normal case
+        for (int i = 0; i < descTuple.size(); i++) {
+          Descriptor curDesc = descTuple.get(i);
+          Location loc;
+          if (i == 0) {
+            loc = new Location(md, curDesc.getSymbol());
+            loc.setLocDescriptor(curDesc);
+            cd = ((VarDescriptor) curDesc).getType().getClassDesc();
+          } else {
+            loc = new Location(cd, curDesc.getSymbol());
+            loc.setLocDescriptor(curDesc);
+            cd = ((FieldDescriptor) curDesc).getType().getClassDesc();
+          }
+          locTuple.add(loc);
         }
-        locTuple.add(loc);
       }
+
       mapFlowNodeToLocTuple.put(fn, locTuple);
     }
     return mapFlowNodeToLocTuple.get(fn);
@@ -327,7 +379,7 @@ 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);