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