d0a854b8e29c4897e17a2003b1fceb3eef7ea6f6
[IRC.git] / Robust / src / Analysis / SSJava / FlowGraph.java
1 package Analysis.SSJava;
2
3 import java.io.BufferedWriter;
4 import java.io.FileWriter;
5 import java.io.IOException;
6 import java.util.HashMap;
7 import java.util.HashSet;
8 import java.util.Iterator;
9 import java.util.Map;
10 import java.util.Set;
11
12 import IR.ClassDescriptor;
13 import IR.Descriptor;
14 import IR.FieldDescriptor;
15 import IR.MethodDescriptor;
16 import IR.VarDescriptor;
17
18 public class FlowGraph {
19
20   MethodDescriptor md;
21
22   Set<FlowNode> returnNodeSet;
23   FlowNode thisVarNode;
24
25   Map<FlowNode, Set<FlowEdge>> mapFlowNodeToInEdgeSet;
26   Map<FlowNode, Set<FlowEdge>> mapFlowNodeToOutEdgeSet;
27
28   Map<NTuple<Location>, FlowNode> mapLocTupleToFlowNode;
29   Map<FlowNode, NTuple<Location>> mapFlowNodeToLocTuple;
30
31   // maps the composite representation of field/var descriptors to infer nodes
32   Map<NTuple<Descriptor>, FlowNode> mapDescTupleToInferNode;
33
34   // maps a paramter descriptor to its index
35   Map<Descriptor, Integer> mapParamDescToIdx;
36
37   // DS for the lattice generation
38   Map<Integer, FlowNode> mapIdxToFlowNode;
39
40   public static int interseed = 0;
41
42   boolean debug = true;
43
44   public FlowGraph(MethodDescriptor md, Map<Descriptor, Integer> mapParamDescToIdx) {
45     this.md = md;
46     this.mapFlowNodeToLocTuple = new HashMap<FlowNode, NTuple<Location>>();
47     this.mapLocTupleToFlowNode = new HashMap<NTuple<Location>, FlowNode>();
48     this.mapDescTupleToInferNode = new HashMap<NTuple<Descriptor>, FlowNode>();
49     this.mapParamDescToIdx = new HashMap<Descriptor, Integer>();
50     this.mapParamDescToIdx.putAll(mapParamDescToIdx);
51     this.returnNodeSet = new HashSet<FlowNode>();
52     this.mapIdxToFlowNode = new HashMap<Integer, FlowNode>();
53     this.mapFlowNodeToOutEdgeSet = new HashMap<FlowNode, Set<FlowEdge>>();
54     this.mapFlowNodeToInEdgeSet = new HashMap<FlowNode, Set<FlowEdge>>();
55
56     if (!md.isStatic()) {
57       // create a node for 'this' varialbe
58       // NTuple<Descriptor> thisDescTuple = new NTuple<Descriptor>();
59       // thisDescTuple.add(md.getThis());
60
61       NTuple<Descriptor> thisVarTuple = new NTuple<Descriptor>();
62       thisVarTuple.add(md.getThis());
63       FlowNode thisNode = createNewFlowNode(thisVarTuple);
64       thisNode.setSkeleton(true);
65       thisVarNode = thisNode;
66     }
67
68     setupMapIdxToDesc();
69
70   }
71
72   public Map<NTuple<Descriptor>, FlowNode> getMapDescTupleToInferNode() {
73     return mapDescTupleToInferNode;
74   }
75
76   public void setMapDescTupleToInferNode(Map<NTuple<Descriptor>, FlowNode> in) {
77     this.mapDescTupleToInferNode.putAll(in);
78   }
79
80   public Map<NTuple<Location>, FlowNode> getMapLocTupleToFlowNode() {
81     return mapLocTupleToFlowNode;
82   }
83
84   public void setMapLocTupleToFlowNode(Map<NTuple<Location>, FlowNode> in) {
85     this.mapLocTupleToFlowNode.putAll(in);
86   }
87
88   public void setReturnNodeSet(Set<FlowNode> in) {
89     this.returnNodeSet.addAll(in);
90   }
91
92   public void setThisVarNode(FlowNode thisVarNode) {
93     this.thisVarNode = thisVarNode;
94   }
95
96   public Map<Descriptor, Integer> getMapParamDescToIdx() {
97     return mapParamDescToIdx;
98   }
99
100   public FlowNode createIntermediateNode() {
101     NTuple<Descriptor> tuple = new NTuple<Descriptor>();
102     Descriptor interDesc = new InterDescriptor(LocationInference.INTERLOC + interseed);
103     tuple.add(interDesc);
104     interseed++;
105
106     FlowNode newNode = new FlowNode(tuple);
107     newNode.setIntermediate(true);
108
109     mapDescTupleToInferNode.put(tuple, newNode);
110     // nodeSet.add(newNode);
111
112     System.out.println("create new intermediate node= " + newNode);
113
114     return newNode;
115   }
116
117   private void setupMapIdxToDesc() {
118
119     Set<Descriptor> descSet = mapParamDescToIdx.keySet();
120     for (Iterator iterator = descSet.iterator(); iterator.hasNext();) {
121       Descriptor paramDesc = (Descriptor) iterator.next();
122       int idx = mapParamDescToIdx.get(paramDesc);
123       NTuple<Descriptor> descTuple = new NTuple<Descriptor>();
124       descTuple.add(paramDesc);
125       FlowNode paramNode = getFlowNode(descTuple);
126       mapIdxToFlowNode.put(idx, paramNode);
127       paramNode.setSkeleton(true);
128     }
129
130   }
131
132   public int getNumParameters() {
133     return mapIdxToFlowNode.keySet().size();
134   }
135
136   public FlowNode getParamFlowNode(int idx) {
137     return mapIdxToFlowNode.get(idx);
138   }
139
140   public Set<FlowEdge> getEdgeSet() {
141     Set<FlowEdge> edgeSet = new HashSet<FlowEdge>();
142
143     Set<FlowNode> nodeSet = getNodeSet();
144     for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
145       FlowNode flowNode = (FlowNode) iterator.next();
146       edgeSet.addAll(getOutEdgeSet(flowNode));
147     }
148
149     return edgeSet;
150   }
151
152   public Set<FlowNode> getNodeSet() {
153     Set<FlowNode> set = new HashSet<FlowNode>();
154     set.addAll(mapDescTupleToInferNode.values());
155     return set;
156   }
157
158   public MethodDescriptor getMethodDescriptor() {
159     return md;
160   }
161
162   public boolean isParamDesc(Descriptor desc) {
163
164     if (mapParamDescToIdx.containsKey(desc)) {
165       int idx = mapParamDescToIdx.get(desc);
166       if (!md.isStatic() && idx == 0) {
167         return false;
168       }
169       return true;
170     }
171
172     return false;
173   }
174
175   public boolean hasEdge(NTuple<Descriptor> fromDescTuple, NTuple<Descriptor> toDescTuple) {
176
177     FlowNode fromNode = mapDescTupleToInferNode.get(fromDescTuple);
178     FlowNode toNode = mapDescTupleToInferNode.get(toDescTuple);
179
180     Set<FlowEdge> fromNodeOutEdgeSet = getOutEdgeSet(fromNode);
181     for (Iterator iterator = fromNodeOutEdgeSet.iterator(); iterator.hasNext();) {
182       FlowEdge flowEdge = (FlowEdge) iterator.next();
183       if (flowEdge.getDst().equals(toNode)) {
184         return true;
185       } else {
186         if (hasEdge(flowEdge.getDst().getDescTuple(), toDescTuple)) {
187           return true;
188         }
189       }
190     }
191
192     return false;
193   }
194
195   public Set<FlowEdge> getOutEdgeSetStartingFrom(FlowNode startNode) {
196
197     Descriptor prefixDesc = startNode.getCurrentDescTuple().get(0);
198
199     // returns the set of edges that share the same prefix of startNode
200     Set<FlowEdge> edgeSet = new HashSet<FlowEdge>();
201
202     for (Iterator<Set<FlowEdge>> iter = mapFlowNodeToOutEdgeSet.values().iterator(); iter.hasNext();) {
203       Set<FlowEdge> nodeEdgeSet = iter.next();
204       for (Iterator<FlowEdge> iter2 = nodeEdgeSet.iterator(); iter2.hasNext();) {
205         FlowEdge edge = iter2.next();
206         if (edge.getInitTuple().get(0).equals(prefixDesc)) {
207           edgeSet.add(edge);
208         }
209       }
210     }
211
212     return edgeSet;
213   }
214
215   public Set<FlowEdge> getOutEdgeSet(FlowNode node) {
216     if (!mapFlowNodeToOutEdgeSet.containsKey(node)) {
217       mapFlowNodeToOutEdgeSet.put(node, new HashSet<FlowEdge>());
218     }
219     return mapFlowNodeToOutEdgeSet.get(node);
220   }
221
222   public Set<FlowEdge> getInEdgeSet(FlowNode node) {
223     if (!mapFlowNodeToInEdgeSet.containsKey(node)) {
224       mapFlowNodeToInEdgeSet.put(node, new HashSet<FlowEdge>());
225     }
226     return mapFlowNodeToInEdgeSet.get(node);
227   }
228
229   public void addValueFlowEdge(NTuple<Descriptor> fromDescTuple, NTuple<Descriptor> toDescTuple) {
230
231     FlowNode fromNode = getFlowNode(fromDescTuple);
232     FlowNode toNode = getFlowNode(toDescTuple);
233
234     // System.out.println("create an edge from " + fromNode + " to " + toNode);
235
236     int fromTupleSize = fromDescTuple.size();
237     NTuple<Descriptor> curFromTuple = new NTuple<Descriptor>();
238     for (int i = 0; i < fromTupleSize; i++) {
239       Descriptor desc = fromDescTuple.get(i);
240       curFromTuple.add(desc);
241       int toTupleSize = toDescTuple.size();
242       NTuple<Descriptor> curToTuple = new NTuple<Descriptor>();
243       for (int k = 0; k < toTupleSize; k++) {
244         Descriptor toDesc = toDescTuple.get(k);
245         curToTuple.add(toDesc);
246         addFlowEdge(getFlowNode(curFromTuple), getFlowNode(curToTuple), fromDescTuple, toDescTuple);
247       }
248     }
249
250     // int fromTupleSize = fromDescTuple.size();
251     // NTuple<Descriptor> curTuple = new NTuple<Descriptor>();
252     // for (int i = 0; i < fromTupleSize; i++) {
253     // Descriptor desc = fromDescTuple.get(i);
254     // curTuple.add(desc);
255     // addFlowEdge(getFlowNode(curTuple), toNode, fromDescTuple, toDescTuple);
256     // }
257     //
258     // int toTupleSize = toDescTuple.size();
259     // curTuple = new NTuple<Descriptor>();
260     // for (int i = 0; i < toTupleSize; i++) {
261     // Descriptor desc = toDescTuple.get(i);
262     // curTuple.add(desc);
263     // addFlowEdge(fromNode, getFlowNode(curTuple), fromDescTuple, toDescTuple);
264     // }
265
266   }
267
268   private void addFlowEdge(FlowNode fromNode, FlowNode toNode, NTuple<Descriptor> initTuple,
269       NTuple<Descriptor> endTuple) {
270
271     FlowEdge edge = new FlowEdge(fromNode, toNode, initTuple, endTuple);
272     addOutEdge(fromNode, edge);
273     addInEdge(toNode, edge);
274
275     // System.out.println("add a new edge=" + edge);
276   }
277
278   private void addInEdge(FlowNode toNode, FlowEdge edge) {
279     getInEdgeSet(toNode).add(edge);
280   }
281
282   private void addOutEdge(FlowNode fromNode, FlowEdge edge) {
283     if (!mapFlowNodeToOutEdgeSet.containsKey(fromNode)) {
284       mapFlowNodeToOutEdgeSet.put(fromNode, new HashSet<FlowEdge>());
285     }
286     mapFlowNodeToOutEdgeSet.get(fromNode).add(edge);
287   }
288
289   public FlowNode getFlowNode(NTuple<Descriptor> descTuple) {
290     if (!mapDescTupleToInferNode.containsKey(descTuple)) {
291       FlowNode node = createNewFlowNode(descTuple);
292       mapDescTupleToInferNode.put(descTuple, node);
293     }
294     return mapDescTupleToInferNode.get(descTuple);
295   }
296
297   public FlowNode getThisVarNode() {
298     return thisVarNode;
299   }
300
301   public FlowNode createNewFlowNode(NTuple<Descriptor> tuple) {
302
303     if (!mapDescTupleToInferNode.containsKey(tuple)) {
304       FlowNode node = new FlowNode(tuple);
305       mapDescTupleToInferNode.put(tuple, node);
306       // nodeSet.add(node);
307
308       mapLocTupleToFlowNode.put(getLocationTuple(node), node);
309
310       if (tuple.size() > 1) {
311         NTuple<Descriptor> baseTuple = tuple.subList(0, tuple.size() - 1);
312         getFlowNode(baseTuple).addFieldNode(node);
313       }
314
315       // System.out.println("Creating new node=" + node);
316       return node;
317     } else {
318       return mapDescTupleToInferNode.get(tuple);
319     }
320
321   }
322
323   public void addReturnFlowNode(NTuple<Descriptor> tuple) {
324
325     if (!mapDescTupleToInferNode.containsKey(tuple)) {
326       createNewFlowNode(tuple);
327     }
328
329     FlowNode node = mapDescTupleToInferNode.get(tuple);
330     node.setReturn(true);
331
332     returnNodeSet.add(node);
333   }
334
335   public Set<FlowNode> getReturnNodeSet() {
336     return returnNodeSet;
337   }
338
339   public Set<FlowNode> getLocalReachFlowNodeSetFrom(FlowNode fn) {
340     Set<FlowNode> set = new HashSet<FlowNode>();
341     recurLocalReachFlowNodeSet(fn, set);
342     return set;
343   }
344
345   private void recurLocalReachFlowNodeSet(FlowNode fn, Set<FlowNode> visited) {
346
347     Set<FlowEdge> outEdgeSet = getOutEdgeSet(fn);
348     for (Iterator iterator = outEdgeSet.iterator(); iterator.hasNext();) {
349       FlowEdge edge = (FlowEdge) iterator.next();
350       FlowNode dstNode = edge.getDst();
351
352       if (!visited.contains(dstNode)) {
353         visited.add(dstNode);
354         recurLocalReachFlowNodeSet(dstNode, visited);
355       }
356     }
357
358   }
359
360   private void getReachFlowNodeSetFrom(FlowNode fn, Set<FlowNode> visited) {
361
362     Set<FlowEdge> outEdgeSet = getOutEdgeSet(fn);
363     for (Iterator iterator = outEdgeSet.iterator(); iterator.hasNext();) {
364       FlowEdge edge = (FlowEdge) iterator.next();
365
366       if (fn.equals(getFlowNode(edge.getInitTuple()))) {
367         FlowNode dstNode = getFlowNode(edge.getEndTuple());
368         if (!visited.contains(dstNode)) {
369           visited.add(dstNode);
370           getReachFlowNodeSetFrom(dstNode, visited);
371         }
372       }
373     }
374
375   }
376
377   public Set<FlowNode> getReachFlowNodeSetFrom(FlowNode fn) {
378     Set<FlowNode> set = new HashSet<FlowNode>();
379     getReachFlowNodeSetFrom(fn, set);
380     return set;
381   }
382
383   public Set<FlowNode> getReachableSetFrom(NTuple<Descriptor> prefix) {
384     Set<FlowNode> reachableSet = new HashSet<FlowNode>();
385
386     Set<FlowNode> nodeSet = getNodeSet();
387     for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
388       FlowNode flowNode = (FlowNode) iterator.next();
389       if (flowNode.getCurrentDescTuple().startsWith(prefix)) {
390         recurReachableSetFrom(flowNode, reachableSet);
391       }
392     }
393
394     return reachableSet;
395   }
396
397   // private void getReachFlowNodeSetFrom(FlowNode fn, Set<FlowNode> visited) {
398   //
399   // for (Iterator iterator = fn.getOutEdgeSet().iterator();
400   // iterator.hasNext();) {
401   // FlowEdge edge = (FlowEdge) iterator.next();
402   //
403   // if (fn.equals(getFlowNode(edge.getInitTuple()))) {
404   //
405   // FlowNode dstNode = getFlowNode(edge.getEndTuple());
406   //
407   // if (!visited.contains(dstNode)) {
408   // visited.add(dstNode);
409   // getReachFlowNodeSetFrom(dstNode, visited);
410   // }
411   // }
412   // }
413   //
414   // }
415
416   private void recurReachableSetFrom(FlowNode curNode, Set<FlowNode> reachableSet) {
417
418     Set<FlowEdge> edgeSet = getOutEdgeSet(curNode);
419     for (Iterator iterator = edgeSet.iterator(); iterator.hasNext();) {
420       FlowEdge edge = (FlowEdge) iterator.next();
421       FlowNode dstNode = getFlowNode(edge.getEndTuple());
422       if (!reachableSet.contains(dstNode)) {
423         reachableSet.add(dstNode);
424         recurReachableSetFrom(dstNode, reachableSet);
425       }
426     }
427
428   }
429
430   public Set<NTuple<Location>> getReachableFlowTupleSet(Set<NTuple<Location>> visited, FlowNode fn) {
431
432     Set<FlowEdge> outEdgeSet = getOutEdgeSet(fn);
433
434     for (Iterator iterator = outEdgeSet.iterator(); iterator.hasNext();) {
435       FlowEdge edge = (FlowEdge) iterator.next();
436
437       if (fn.getDescTuple().equals(edge.getInitTuple())) {
438         FlowNode dstNode = getFlowNode(edge.getEndTuple());
439         NTuple<Location> dstTuple = getLocationTuple(dstNode);
440
441         if (!visited.contains(dstTuple)) {
442           visited.add(dstTuple);
443           visited.addAll(getReachableFlowTupleSet(visited, dstNode));
444         }
445
446       }
447     }
448     return visited;
449   }
450
451   public NTuple<Location> getLocationTuple(NTuple<Descriptor> descTuple) {
452     return getLocationTuple(getFlowNode(descTuple));
453   }
454
455   public NTuple<Location> getLocationTuple(FlowNode fn) {
456
457     if (!mapFlowNodeToLocTuple.containsKey(fn)) {
458       NTuple<Descriptor> descTuple = fn.getDescTuple();
459       NTuple<Location> locTuple = new NTuple<Location>();
460       ClassDescriptor cd = null;
461
462       Descriptor localDesc = fn.getDescTuple().get(0);
463
464       if (fn.isIntermediate()) {
465         Location interLoc = new Location(md, localDesc.getSymbol());
466         interLoc.setLocDescriptor(localDesc);
467         locTuple.add(interLoc);
468       } else if (localDesc.getSymbol().equals(LocationInference.TOPLOC)) {
469         Location topLoc = new Location(md, Location.TOP);
470         topLoc.setLocDescriptor(LocationInference.TOPDESC);
471         locTuple.add(topLoc);
472       } else if (localDesc.getSymbol().equals(LocationInference.GLOBALLOC)) {
473         Location globalLoc = new Location(md, LocationInference.GLOBALLOC);
474         globalLoc.setLocDescriptor(LocationInference.GLOBALDESC);
475         locTuple.add(globalLoc);
476       } else {
477         // normal case
478         for (int i = 0; i < descTuple.size(); i++) {
479           Descriptor curDesc = descTuple.get(i);
480           Location loc;
481           if (i == 0) {
482             loc = new Location(md, curDesc.getSymbol());
483             loc.setLocDescriptor(curDesc);
484             cd = ((VarDescriptor) curDesc).getType().getClassDesc();
485           } else {
486             loc = new Location(cd, curDesc.getSymbol());
487             loc.setLocDescriptor(curDesc);
488
489             if (curDesc instanceof FieldDescriptor) {
490               cd = ((FieldDescriptor) curDesc).getType().getClassDesc();
491             } else {
492               cd = ((LocationDescriptor) curDesc).getEnclosingClassDesc();
493             }
494
495           }
496           locTuple.add(loc);
497         }
498       }
499
500       mapFlowNodeToLocTuple.put(fn, locTuple);
501     }
502     return mapFlowNodeToLocTuple.get(fn);
503   }
504
505   public Set<FlowNode> getIncomingFlowNodeSet(FlowNode node) {
506     Set<FlowNode> set = new HashSet<FlowNode>();
507     getIncomingFlowNodeSet(node, set);
508     return set;
509   }
510
511   public void getIncomingFlowNodeSet(FlowNode node, Set<FlowNode> visited) {
512
513     for (Iterator iterator = getNodeSet().iterator(); iterator.hasNext();) {
514       FlowNode curNode = (FlowNode) iterator.next();
515       Set<FlowEdge> edgeSet = getOutEdgeSet(curNode);
516
517       for (Iterator iterator2 = edgeSet.iterator(); iterator2.hasNext();) {
518         FlowEdge flowEdge = (FlowEdge) iterator2.next();
519
520         if (node.equals(getFlowNode(flowEdge.getEndTuple()))) {
521           FlowNode incomingNode = getFlowNode(flowEdge.getInitTuple());
522
523           if (!visited.contains(incomingNode)) {
524             visited.add(incomingNode);
525             getIncomingFlowNodeSet(incomingNode, visited);
526           }
527         }
528       }
529     }
530
531   }
532
533   public Set<NTuple<Location>> getIncomingFlowTupleSet(FlowNode fn) {
534
535     NTuple<Descriptor> dstTuple = fn.getDescTuple();
536
537     Set<NTuple<Location>> set = new HashSet<NTuple<Location>>();
538
539     ClassDescriptor cd = null;
540
541     for (Iterator iterator = getNodeSet().iterator(); iterator.hasNext();) {
542       FlowNode node = (FlowNode) iterator.next();
543
544       Set<FlowEdge> edgeSet = getOutEdgeSet(node);
545       for (Iterator iterator2 = edgeSet.iterator(); iterator2.hasNext();) {
546         FlowEdge flowEdge = (FlowEdge) iterator2.next();
547         if (dstTuple.equals(flowEdge.getEndTuple())) {
548           NTuple<Descriptor> initTuple = flowEdge.getInitTuple();
549           NTuple<Location> locTuple = new NTuple<Location>();
550           for (int i = 0; i < initTuple.size(); i++) {
551             Descriptor d = initTuple.get(i);
552             Location loc;
553             if (i == 0) {
554               loc = new Location(md, d.getSymbol());
555               cd = ((VarDescriptor) d).getType().getClassDesc();
556             } else {
557               loc = new Location(cd, d.getSymbol());
558               cd = ((FieldDescriptor) d).getType().getClassDesc();
559             }
560             locTuple.add(loc);
561           }
562           set.add(locTuple);
563         }
564       }
565     }
566     return set;
567   }
568
569   public boolean isParameter(NTuple<Descriptor> tuple) {
570     // return true if a descriptor tuple is started with a parameter descriptor
571     Descriptor firstIdxDesc = tuple.get(0);
572     return mapParamDescToIdx.containsKey(firstIdxDesc);
573   }
574
575   public int getParamIdx(NTuple<Descriptor> tuple) {
576     Descriptor firstIdxDesc = tuple.get(0);
577     return mapParamDescToIdx.get(firstIdxDesc);
578   }
579
580   public FlowGraph clone() {
581     FlowGraph clone = new FlowGraph(md, mapParamDescToIdx);
582
583     // clone.setNodeSet(getNodeSet());
584     clone.setMapLocTupleToFlowNode(getMapLocTupleToFlowNode());
585     clone.setMapFlowNodeToLocTuple(getMapFlowNodeToLocTuple());
586     clone.setMapDescTupleToInferNode(getMapDescTupleToInferNode());
587
588     clone.setMapFlowNodeToOutEdgeSet(getMapFlowNodeToOutEdgeSet());
589     clone.setReturnNodeSet(getReturnNodeSet());
590     clone.setThisVarNode(getThisVarNode());
591
592     return clone;
593   }
594
595   public Map<FlowNode, NTuple<Location>> getMapFlowNodeToLocTuple() {
596     return mapFlowNodeToLocTuple;
597   }
598
599   public void setMapFlowNodeToLocTuple(Map<FlowNode, NTuple<Location>> in) {
600     this.mapFlowNodeToLocTuple.putAll(in);
601   }
602
603   public Map<FlowNode, Set<FlowEdge>> getMapFlowNodeToOutEdgeSet() {
604     return mapFlowNodeToOutEdgeSet;
605   }
606
607   public Set<FlowNode> getIncomingNodeSetByPrefix(Descriptor prefix) {
608
609     Set<FlowNode> incomingNodeSet = new HashSet<FlowNode>();
610
611     for (Iterator iterator = getNodeSet().iterator(); iterator.hasNext();) {
612       FlowNode curNode = (FlowNode) iterator.next();
613       Set<FlowEdge> outEdgeSet = getOutEdgeSet(curNode);
614
615       for (Iterator iterator2 = outEdgeSet.iterator(); iterator2.hasNext();) {
616         FlowEdge outEdge = (FlowEdge) iterator2.next();
617
618         if (outEdge.getEndTuple().startsWith(prefix)) {
619           incomingNodeSet.add(curNode);
620           recurIncomingNodeSetByPrefix(prefix, curNode, incomingNodeSet);
621
622         }
623
624       }
625     }
626
627     return incomingNodeSet;
628
629   }
630
631   private void recurIncomingNodeSetByPrefix(Descriptor prefix, FlowNode node, Set<FlowNode> visited) {
632
633     Set<FlowEdge> inEdgeSet = getInEdgeSet(node);
634
635     for (Iterator iterator = inEdgeSet.iterator(); iterator.hasNext();) {
636       FlowEdge inEdge = (FlowEdge) iterator.next();
637
638       FlowNode inNode = getFlowNode(inEdge.getInitTuple());
639       if (!inEdge.getInitTuple().startsWith(prefix) && !visited.contains(inNode)) {
640         visited.add(inNode);
641         recurIncomingNodeSetByPrefix(prefix, inNode, visited);
642       }
643     }
644
645   }
646
647   public void setMapFlowNodeToOutEdgeSet(Map<FlowNode, Set<FlowEdge>> inMap) {
648     Set<FlowNode> keySet = inMap.keySet();
649     for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
650       FlowNode key = (FlowNode) iterator.next();
651       Set<FlowEdge> newEdgeSet = new HashSet<FlowEdge>();
652       newEdgeSet.addAll(inMap.get(key));
653       mapFlowNodeToOutEdgeSet.put(key, newEdgeSet);
654     }
655   }
656
657   private void drawEdges(FlowNode node, BufferedWriter bw, Set<FlowNode> addedNodeSet,
658       Set<FlowEdge> addedEdgeSet) throws IOException {
659
660     Set<FlowEdge> edgeSet = getOutEdgeSet(node);
661
662     for (Iterator<FlowEdge> iterator = edgeSet.iterator(); iterator.hasNext();) {
663       FlowEdge flowEdge = iterator.next();
664
665       FlowNode u = flowEdge.getSrc();
666       FlowNode v = flowEdge.getDst();
667
668       if (u.getDescTuple().equals(flowEdge.getInitTuple())
669           && v.getDescTuple().equals(flowEdge.getEndTuple())) {
670         // only draw an edge of the actual value flow
671
672         if (!addedEdgeSet.contains(flowEdge)) {
673
674           if (!addedNodeSet.contains(u)) {
675             drawNode(u, bw);
676             addedNodeSet.add(u);
677           }
678           if (!addedNodeSet.contains(v)) {
679             drawNode(v, bw);
680             addedNodeSet.add(v);
681           }
682
683           bw.write("" + u.getID() + " -> " + v.getID() + ";\n");
684           addedEdgeSet.add(flowEdge);
685         }
686       }
687
688     }
689
690   }
691
692   private void drawNode(FlowNode node, BufferedWriter bw) throws IOException {
693     bw.write(node.getID() + " [label=\"" + node.getPrettyID() + "\"]" + ";\n");
694   }
695
696   public void writeGraph() throws java.io.IOException {
697     writeGraph("");
698   }
699
700   public void writeGraph(String suffix) throws java.io.IOException {
701
702     String graphName = "flowgraph_" + md.toString() + suffix;
703     graphName = graphName.replaceAll("[\\W]", "");
704
705     BufferedWriter bw = new BufferedWriter(new FileWriter(graphName + ".dot"));
706     bw.write("digraph " + graphName + " {\n");
707     bw.write("compound=true;\n");
708
709     // then visit every flow node
710
711     // Iterator<FlowNode> iter = nodeSet.iterator();
712     Iterator<FlowNode> iter = getNodeSet().iterator();
713
714     Set<FlowEdge> addedEdgeSet = new HashSet<FlowEdge>();
715     Set<FlowNode> addedNodeSet = new HashSet<FlowNode>();
716
717     while (iter.hasNext()) {
718       FlowNode node = iter.next();
719
720       if (node.getDescTuple().size() == 1) {
721         // here, we just care about the local variable
722         if (node.getFieldNodeSet().size() > 0) {
723           drawSubgraph(node, bw, addedEdgeSet);
724         }
725       }
726       drawEdges(node, bw, addedNodeSet, addedEdgeSet);
727
728     }
729
730     bw.write("}\n");
731     bw.close();
732
733   }
734
735   public boolean constainsNode(FlowNode node) {
736     return getNodeSet().contains(node);
737   }
738
739   private void drawSubgraph(FlowNode node, BufferedWriter bw, Set<FlowEdge> addedSet)
740       throws IOException {
741
742     bw.write("subgraph cluster_" + node.getID() + "{\n");
743     bw.write("label=\"" + node.getPrettyID() + "\";\n");
744
745     Set<FlowNode> fieldNodeSet = node.getFieldNodeSet();
746     for (Iterator iterator = fieldNodeSet.iterator(); iterator.hasNext();) {
747       FlowNode fieldNode = (FlowNode) iterator.next();
748       if (fieldNode.getFieldNodeSet().size() > 0) {
749         drawSubgraph(fieldNode, bw, addedSet);
750       } else {
751         Descriptor desc = fieldNode.getDescTuple().getLastElement();
752         if (desc instanceof VarDescriptor) {
753           VarDescriptor varDesc = (VarDescriptor) desc;
754           if (varDesc.getType().isPrimitive()) {
755             bw.write(fieldNode.getID() + " [label=\"" + fieldNode.getPrettyID() + "\"];\n");
756           }
757         } else if (desc instanceof FieldDescriptor) {
758           FieldDescriptor fieldDesc = (FieldDescriptor) desc;
759           if (fieldDesc.getType().isPrimitive()) {
760             bw.write(fieldNode.getID() + " [label=\"" + fieldNode.getPrettyID() + "\"];\n");
761           }
762         }
763       }
764     }
765
766     bw.write("}\n");
767   }
768
769 }