89c206d654a4dd6665fd581f907db64e751a9342
[IRC.git] / Robust / src / Analysis / SSJava / LocationInference.java
1 package Analysis.SSJava;
2
3 import java.io.IOException;
4 import java.util.ArrayList;
5 import java.util.Collections;
6 import java.util.Comparator;
7 import java.util.HashMap;
8 import java.util.HashSet;
9 import java.util.Iterator;
10 import java.util.LinkedList;
11 import java.util.List;
12 import java.util.Map;
13 import java.util.Set;
14 import java.util.Stack;
15
16 import IR.ClassDescriptor;
17 import IR.Descriptor;
18 import IR.FieldDescriptor;
19 import IR.MethodDescriptor;
20 import IR.NameDescriptor;
21 import IR.Operation;
22 import IR.State;
23 import IR.SymbolTable;
24 import IR.TypeDescriptor;
25 import IR.VarDescriptor;
26 import IR.Tree.ArrayAccessNode;
27 import IR.Tree.AssignmentNode;
28 import IR.Tree.BlockExpressionNode;
29 import IR.Tree.BlockNode;
30 import IR.Tree.BlockStatementNode;
31 import IR.Tree.CastNode;
32 import IR.Tree.CreateObjectNode;
33 import IR.Tree.DeclarationNode;
34 import IR.Tree.ExpressionNode;
35 import IR.Tree.FieldAccessNode;
36 import IR.Tree.IfStatementNode;
37 import IR.Tree.Kind;
38 import IR.Tree.LiteralNode;
39 import IR.Tree.LoopNode;
40 import IR.Tree.MethodInvokeNode;
41 import IR.Tree.NameNode;
42 import IR.Tree.OpNode;
43 import IR.Tree.ReturnNode;
44 import IR.Tree.SubBlockNode;
45 import IR.Tree.SwitchStatementNode;
46 import IR.Tree.TertiaryNode;
47
48 public class LocationInference {
49
50   State state;
51   SSJavaAnalysis ssjava;
52
53   List<ClassDescriptor> toanalyzeList;
54   List<MethodDescriptor> toanalyzeMethodList;
55   Map<MethodDescriptor, FlowGraph> mapMethodDescriptorToFlowGraph;
56
57   // map a method descriptor to its set of parameter descriptors
58   Map<MethodDescriptor, Set<Descriptor>> mapMethodDescriptorToParamDescSet;
59
60   // keep current descriptors to visit in fixed-point interprocedural analysis,
61   private Stack<MethodDescriptor> methodDescriptorsToVisitStack;
62
63   // map a class descriptor to a field lattice
64   private Map<ClassDescriptor, SSJavaLattice<String>> cd2lattice;
65
66   // map a method descriptor to a method lattice
67   private Map<MethodDescriptor, SSJavaLattice<String>> md2lattice;
68
69   // map a method descriptor to the set of method invocation nodes which are
70   // invoked by the method descriptor
71   private Map<MethodDescriptor, Set<MethodInvokeNode>> mapMethodDescriptorToMethodInvokeNodeSet;
72
73   private Map<MethodInvokeNode, Map<Integer, NTuple<Descriptor>>> mapMethodInvokeNodeToArgIdxMap;
74
75   private Map<MethodDescriptor, MethodLocationInfo> mapMethodDescToMethodLocationInfo;
76
77   private Map<ClassDescriptor, LocationInfo> mapClassToLocationInfo;
78
79   private Map<MethodDescriptor, Set<MethodDescriptor>> mapMethodDescToPossibleMethodDescSet;
80
81   boolean debug = true;
82
83   public LocationInference(SSJavaAnalysis ssjava, State state) {
84     this.ssjava = ssjava;
85     this.state = state;
86     this.toanalyzeList = new ArrayList<ClassDescriptor>();
87     this.toanalyzeMethodList = new ArrayList<MethodDescriptor>();
88     this.mapMethodDescriptorToFlowGraph = new HashMap<MethodDescriptor, FlowGraph>();
89     this.cd2lattice = new HashMap<ClassDescriptor, SSJavaLattice<String>>();
90     this.md2lattice = new HashMap<MethodDescriptor, SSJavaLattice<String>>();
91     this.methodDescriptorsToVisitStack = new Stack<MethodDescriptor>();
92     this.mapMethodDescriptorToMethodInvokeNodeSet =
93         new HashMap<MethodDescriptor, Set<MethodInvokeNode>>();
94     this.mapMethodInvokeNodeToArgIdxMap =
95         new HashMap<MethodInvokeNode, Map<Integer, NTuple<Descriptor>>>();
96     this.mapMethodDescToMethodLocationInfo = new HashMap<MethodDescriptor, MethodLocationInfo>();
97     this.mapMethodDescToPossibleMethodDescSet =
98         new HashMap<MethodDescriptor, Set<MethodDescriptor>>();
99     this.mapClassToLocationInfo = new HashMap<ClassDescriptor, LocationInfo>();
100   }
101
102   public void setupToAnalyze() {
103     SymbolTable classtable = state.getClassSymbolTable();
104     toanalyzeList.clear();
105     toanalyzeList.addAll(classtable.getValueSet());
106     Collections.sort(toanalyzeList, new Comparator<ClassDescriptor>() {
107       public int compare(ClassDescriptor o1, ClassDescriptor o2) {
108         return o1.getClassName().compareToIgnoreCase(o2.getClassName());
109       }
110     });
111   }
112
113   public void setupToAnalazeMethod(ClassDescriptor cd) {
114
115     SymbolTable methodtable = cd.getMethodTable();
116     toanalyzeMethodList.clear();
117     toanalyzeMethodList.addAll(methodtable.getValueSet());
118     Collections.sort(toanalyzeMethodList, new Comparator<MethodDescriptor>() {
119       public int compare(MethodDescriptor o1, MethodDescriptor o2) {
120         return o1.getSymbol().compareToIgnoreCase(o2.getSymbol());
121       }
122     });
123   }
124
125   public boolean toAnalyzeMethodIsEmpty() {
126     return toanalyzeMethodList.isEmpty();
127   }
128
129   public boolean toAnalyzeIsEmpty() {
130     return toanalyzeList.isEmpty();
131   }
132
133   public ClassDescriptor toAnalyzeNext() {
134     return toanalyzeList.remove(0);
135   }
136
137   public MethodDescriptor toAnalyzeMethodNext() {
138     return toanalyzeMethodList.remove(0);
139   }
140
141   public void inference() {
142
143     // 1) construct value flow graph
144     constructFlowGraph();
145
146     // 2) construct lattices
147     inferLattices();
148
149     simplifyLattices();
150
151     debug_writeLatticeDotFile();
152
153     // 3) check properties
154     checkLattices();
155
156   }
157
158   private void simplifyLattices() {
159
160     // generate lattice dot file
161     setupToAnalyze();
162
163     while (!toAnalyzeIsEmpty()) {
164       ClassDescriptor cd = toAnalyzeNext();
165
166       setupToAnalazeMethod(cd);
167
168       SSJavaLattice<String> classLattice = cd2lattice.get(cd);
169       if (classLattice != null) {
170         classLattice.removeRedundantEdges();
171       }
172
173       while (!toAnalyzeMethodIsEmpty()) {
174         MethodDescriptor md = toAnalyzeMethodNext();
175         if (ssjava.needTobeAnnotated(md)) {
176           SSJavaLattice<String> methodLattice = md2lattice.get(md);
177           if (methodLattice != null) {
178             methodLattice.removeRedundantEdges();
179           }
180         }
181       }
182     }
183
184   }
185
186   private void checkLattices() {
187
188     LinkedList<MethodDescriptor> descriptorListToAnalyze = ssjava.getSortedDescriptors();
189
190     // current descriptors to visit in fixed-point interprocedural analysis,
191     // prioritized by
192     // dependency in the call graph
193     methodDescriptorsToVisitStack.clear();
194
195     descriptorListToAnalyze.removeFirst();
196
197     Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
198     methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
199
200     while (!descriptorListToAnalyze.isEmpty()) {
201       MethodDescriptor md = descriptorListToAnalyze.removeFirst();
202       checkLatticesOfVirtualMethods(md);
203     }
204
205   }
206
207   private void debug_writeLatticeDotFile() {
208     // generate lattice dot file
209
210     setupToAnalyze();
211
212     while (!toAnalyzeIsEmpty()) {
213       ClassDescriptor cd = toAnalyzeNext();
214
215       setupToAnalazeMethod(cd);
216
217       SSJavaLattice<String> classLattice = cd2lattice.get(cd);
218       if (classLattice != null) {
219         ssjava.writeLatticeDotFile(cd, null, classLattice);
220         debug_printDescriptorToLocNameMapping(cd);
221       }
222
223       while (!toAnalyzeMethodIsEmpty()) {
224         MethodDescriptor md = toAnalyzeMethodNext();
225         if (ssjava.needTobeAnnotated(md)) {
226           SSJavaLattice<String> methodLattice = md2lattice.get(md);
227           if (methodLattice != null) {
228             ssjava.writeLatticeDotFile(cd, md, methodLattice);
229             debug_printDescriptorToLocNameMapping(md);
230           }
231         }
232       }
233     }
234
235   }
236
237   private void debug_printDescriptorToLocNameMapping(Descriptor desc) {
238
239     LocationInfo info = getLocationInfo(desc);
240     System.out.println("## " + desc + " ##");
241     System.out.println(info.getMapDescToInferLocation());
242     LocationInfo locInfo = getLocationInfo(desc);
243     System.out.println("mapping=" + locInfo.getMapLocSymbolToDescSet());
244     System.out.println("###################");
245
246   }
247
248   private void inferLattices() {
249
250     // do fixed-point analysis
251
252     LinkedList<MethodDescriptor> descriptorListToAnalyze = ssjava.getSortedDescriptors();
253
254     // current descriptors to visit in fixed-point interprocedural analysis,
255     // prioritized by
256     // dependency in the call graph
257     methodDescriptorsToVisitStack.clear();
258
259     descriptorListToAnalyze.removeFirst();
260
261     Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
262     methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
263
264     while (!descriptorListToAnalyze.isEmpty()) {
265       MethodDescriptor md = descriptorListToAnalyze.removeFirst();
266       methodDescriptorsToVisitStack.add(md);
267     }
268
269     // analyze scheduled methods until there are no more to visit
270     while (!methodDescriptorsToVisitStack.isEmpty()) {
271       // start to analyze leaf node
272       MethodDescriptor md = methodDescriptorsToVisitStack.pop();
273
274       SSJavaLattice<String> methodLattice =
275           new SSJavaLattice<String>(SSJavaAnalysis.TOP, SSJavaAnalysis.BOTTOM);
276
277       MethodLocationInfo methodInfo = new MethodLocationInfo(md);
278
279       System.out.println();
280       System.out.println("SSJAVA: Inferencing the lattice from " + md);
281
282       analyzeMethodLattice(md, methodLattice, methodInfo);
283
284       SSJavaLattice<String> prevMethodLattice = getMethodLattice(md);
285       MethodLocationInfo prevMethodInfo = getMethodLocationInfo(md);
286
287       if ((!methodLattice.equals(prevMethodLattice)) || (!methodInfo.equals(prevMethodInfo))) {
288
289         setMethodLattice(md, methodLattice);
290         setMethodLocInfo(md, methodInfo);
291
292         // results for callee changed, so enqueue dependents caller for
293         // further analysis
294         Iterator<MethodDescriptor> depsItr = ssjava.getDependents(md).iterator();
295         while (depsItr.hasNext()) {
296           MethodDescriptor methodNext = depsItr.next();
297           if (!methodDescriptorsToVisitStack.contains(methodNext)
298               && methodDescriptorToVistSet.contains(methodNext)) {
299             methodDescriptorsToVisitStack.add(methodNext);
300           }
301         }
302
303       }
304
305     }
306
307   }
308
309   private void setMethodLocInfo(MethodDescriptor md, MethodLocationInfo methodInfo) {
310     mapMethodDescToMethodLocationInfo.put(md, methodInfo);
311   }
312
313   private void checkLatticesOfVirtualMethods(MethodDescriptor md) {
314
315     if (!md.isStatic()) {
316       Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
317       setPossibleCallees.addAll(ssjava.getCallGraph().getMethods(md));
318
319       for (Iterator iterator = setPossibleCallees.iterator(); iterator.hasNext();) {
320         MethodDescriptor mdCallee = (MethodDescriptor) iterator.next();
321         if (!md.equals(mdCallee)) {
322           checkConsistency(md, mdCallee);
323         }
324       }
325
326     }
327
328   }
329
330   private void checkConsistency(MethodDescriptor md1, MethodDescriptor md2) {
331
332     // check that two lattice have the same relations between parameters(+PC
333     // LOC, RETURN LOC)
334
335     MethodLocationInfo methodInfo1 = getMethodLocationInfo(md1);
336
337     SSJavaLattice<String> lattice1 = getMethodLattice(md1);
338     SSJavaLattice<String> lattice2 = getMethodLattice(md2);
339
340     Set<String> paramLocNameSet1 = methodInfo1.getParameterLocNameSet();
341
342     for (Iterator iterator = paramLocNameSet1.iterator(); iterator.hasNext();) {
343       String locName1 = (String) iterator.next();
344       for (Iterator iterator2 = paramLocNameSet1.iterator(); iterator2.hasNext();) {
345         String locName2 = (String) iterator2.next();
346
347
348         if (!locName1.equals(locName2)) {
349
350           boolean r1 = lattice1.isGreaterThan(locName1, locName2);
351           boolean r2 = lattice2.isGreaterThan(locName1, locName2);
352
353           if (r1 != r2) {
354             throw new Error("The method " + md1 + " is not consistent with the method " + md2
355                 + ".:: They have a different ordering relation between parameters " + locName1
356                 + " and " + locName2 + ".");
357           }
358         }
359
360       }
361     }
362
363   }
364
365   private String getSymbol(int idx, FlowNode node) {
366     Descriptor desc = node.getDescTuple().get(idx);
367     return desc.getSymbol();
368   }
369
370   private Descriptor getDescriptor(int idx, FlowNode node) {
371     Descriptor desc = node.getDescTuple().get(idx);
372     return desc;
373   }
374
375   private void analyzeMethodLattice(MethodDescriptor md, SSJavaLattice<String> methodLattice,
376       MethodLocationInfo methodInfo) {
377
378     // first take a look at method invocation nodes to newly added relations
379     // from the callee
380     analyzeLatticeMethodInvocationNode(md);
381
382     // grab the this location if the method use the 'this' reference
383     String thisLocSymbol = md.getThis().getSymbol();
384     // if (methodLattice.getKeySet().contains(thisLocSymbol)) {
385     methodInfo.setThisLocName(thisLocSymbol);
386     // }
387
388     // visit each node of method flow graph
389     FlowGraph fg = getFlowGraph(md);
390     Set<FlowNode> nodeSet = fg.getNodeSet();
391
392     // for the method lattice, we need to look at the first element of
393     // NTuple<Descriptor>
394     for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
395       FlowNode srcNode = (FlowNode) iterator.next();
396
397       Set<FlowEdge> outEdgeSet = srcNode.getOutEdgeSet();
398       for (Iterator iterator2 = outEdgeSet.iterator(); iterator2.hasNext();) {
399         FlowEdge outEdge = (FlowEdge) iterator2.next();
400         FlowNode dstNode = outEdge.getDst();
401
402         NTuple<Descriptor> srcNodeTuple = srcNode.getDescTuple();
403         NTuple<Descriptor> dstNodeTuple = dstNode.getDescTuple();
404
405         if (outEdge.getInitTuple().equals(srcNodeTuple)
406             && outEdge.getEndTuple().equals(dstNodeTuple)) {
407
408           if ((srcNodeTuple.size() > 1 && dstNodeTuple.size() > 1)
409               && srcNodeTuple.get(0).equals(dstNodeTuple.get(0))) {
410
411             // value flows between fields
412             VarDescriptor varDesc = (VarDescriptor) srcNodeTuple.get(0);
413             ClassDescriptor varClassDesc = varDesc.getType().getClassDesc();
414             extractRelationFromFieldFlows(varClassDesc, srcNode, dstNode, 1);
415
416           } else if (srcNodeTuple.size() == 1 || dstNodeTuple.size() == 1) {
417             // for the method lattice, we need to look at the first element of
418             // NTuple<Descriptor>
419             // in this case, take a look at connected nodes at the local level
420             addRelationToLattice(md, methodLattice, methodInfo, srcNode, dstNode);
421           } else {
422
423             if (!srcNode.getDescTuple().get(0).equals(dstNode.getDescTuple().get(0))) {
424               // in this case, take a look at connected nodes at the local level
425               addRelationToLattice(md, methodLattice, methodInfo, srcNode, dstNode);
426             } else {
427               Descriptor srcDesc = srcNode.getDescTuple().get(0);
428               Descriptor dstDesc = dstNode.getDescTuple().get(0);
429               recursivelyAddCompositeRelation(md, fg, methodInfo, srcNode, dstNode, srcDesc,
430                   dstDesc);
431               // recursiveAddRelationToLattice(1, md, srcNode, dstNode);
432             }
433           }
434
435         }
436       }
437     }
438
439     // calculate a return location
440     if (!md.getReturnType().isVoid()) {
441       Set<FlowNode> returnNodeSet = fg.getReturnNodeSet();
442       Set<String> returnVarSymbolSet = new HashSet<String>();
443
444       for (Iterator iterator = returnNodeSet.iterator(); iterator.hasNext();) {
445         FlowNode rtrNode = (FlowNode) iterator.next();
446         String localSymbol = rtrNode.getDescTuple().get(0).getSymbol();
447         returnVarSymbolSet.add(localSymbol);
448       }
449
450       String returnGLB = methodLattice.getGLB(returnVarSymbolSet);
451       if (returnGLB.equals(SSJavaAnalysis.BOTTOM)) {
452         // need to insert a new location in-between the bottom and all locations
453         // that is directly connected to the bottom
454         String returnNewLocationSymbol = "Loc" + (SSJavaLattice.seed++);
455         methodLattice.insertNewLocationAtOneLevelHigher(returnGLB, returnNewLocationSymbol);
456         methodInfo.setReturnLocName(returnNewLocationSymbol);
457       } else {
458         methodInfo.setReturnLocName(returnGLB);
459       }
460     }
461
462   }
463
464   private void recursiveAddRelationToLattice(int idx, MethodDescriptor md,
465       CompositeLocation srcInferLoc, CompositeLocation dstInferLoc) {
466
467     String srcLocSymbol = srcInferLoc.get(idx).getLocIdentifier();
468     String dstLocSymbol = dstInferLoc.get(idx).getLocIdentifier();
469
470     if (srcLocSymbol.equals(dstLocSymbol)) {
471       recursiveAddRelationToLattice(idx + 1, md, srcInferLoc, dstInferLoc);
472     } else {
473
474       Descriptor parentDesc = srcInferLoc.get(idx).getDescriptor();
475       LocationInfo locInfo = getLocationInfo(parentDesc);
476
477       addRelationHigherToLower(getLattice(parentDesc), getLocationInfo(parentDesc), srcLocSymbol,
478           dstLocSymbol);
479     }
480
481   }
482
483   private void analyzeLatticeMethodInvocationNode(MethodDescriptor mdCaller) {
484
485     // the transformation for a call site propagates all relations between
486     // parameters from the callee
487     // if the method is virtual, it also grab all relations from any possible
488     // callees
489
490     Set<MethodInvokeNode> setMethodInvokeNode =
491         mapMethodDescriptorToMethodInvokeNodeSet.get(mdCaller);
492     if (setMethodInvokeNode != null) {
493
494       for (Iterator iterator = setMethodInvokeNode.iterator(); iterator.hasNext();) {
495         MethodInvokeNode min = (MethodInvokeNode) iterator.next();
496         MethodDescriptor mdCallee = min.getMethod();
497         Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
498         if (mdCallee.isStatic()) {
499           setPossibleCallees.add(mdCallee);
500         } else {
501           setPossibleCallees.addAll(ssjava.getCallGraph().getMethods(mdCallee));
502         }
503
504         for (Iterator iterator2 = setPossibleCallees.iterator(); iterator2.hasNext();) {
505           MethodDescriptor possibleMdCallee = (MethodDescriptor) iterator2.next();
506           propagateRelationToCaller(min, mdCaller, possibleMdCallee);
507         }
508
509       }
510     }
511
512   }
513
514   private void propagateRelationToCaller(MethodInvokeNode min, MethodDescriptor mdCaller,
515       MethodDescriptor possibleMdCallee) {
516
517     SSJavaLattice<String> calleeLattice = getMethodLattice(possibleMdCallee);
518
519     FlowGraph calleeFlowGraph = getFlowGraph(possibleMdCallee);
520
521     // find parameter node
522     Set<FlowNode> paramNodeSet = calleeFlowGraph.getParameterNodeSet();
523
524     for (Iterator iterator = paramNodeSet.iterator(); iterator.hasNext();) {
525       FlowNode paramFlowNode1 = (FlowNode) iterator.next();
526
527       for (Iterator iterator2 = paramNodeSet.iterator(); iterator2.hasNext();) {
528         FlowNode paramFlowNode2 = (FlowNode) iterator2.next();
529
530         String paramSymbol1 = getSymbol(0, paramFlowNode1);
531         String paramSymbol2 = getSymbol(0, paramFlowNode2);
532         // if two parameters have a relation, we need to propagate this relation
533         // to the caller
534         if (!(paramSymbol1.equals(paramSymbol2))
535             && calleeLattice.isComparable(paramSymbol1, paramSymbol2)) {
536           int higherLocIdxCallee;
537           int lowerLocIdxCallee;
538           if (calleeLattice.isGreaterThan(paramSymbol1, paramSymbol2)) {
539             higherLocIdxCallee = calleeFlowGraph.getParamIdx(paramFlowNode1.getDescTuple());
540             lowerLocIdxCallee = calleeFlowGraph.getParamIdx(paramFlowNode2.getDescTuple());
541           } else {
542             higherLocIdxCallee = calleeFlowGraph.getParamIdx(paramFlowNode2.getDescTuple());
543             lowerLocIdxCallee = calleeFlowGraph.getParamIdx(paramFlowNode1.getDescTuple());
544           }
545
546           NTuple<Descriptor> higherArg = getArgTupleByArgIdx(min, higherLocIdxCallee);
547           NTuple<Descriptor> lowerArg = getArgTupleByArgIdx(min, lowerLocIdxCallee);
548
549           addFlowGraphEdge(mdCaller, higherArg, lowerArg);
550
551         }
552
553       }
554
555     }
556
557   }
558
559   private LocationInfo getLocationInfo(Descriptor d) {
560     if (d instanceof MethodDescriptor) {
561       return getMethodLocationInfo((MethodDescriptor) d);
562     } else {
563       return getFieldLocationInfo((ClassDescriptor) d);
564     }
565   }
566
567   private MethodLocationInfo getMethodLocationInfo(MethodDescriptor md) {
568
569     if (!mapMethodDescToMethodLocationInfo.containsKey(md)) {
570       mapMethodDescToMethodLocationInfo.put(md, new MethodLocationInfo(md));
571     }
572
573     return mapMethodDescToMethodLocationInfo.get(md);
574
575   }
576
577   private LocationInfo getFieldLocationInfo(ClassDescriptor cd) {
578
579     if (!mapClassToLocationInfo.containsKey(cd)) {
580       mapClassToLocationInfo.put(cd, new LocationInfo(cd));
581     }
582
583     return mapClassToLocationInfo.get(cd);
584
585   }
586
587   private void addRelationToLattice(MethodDescriptor md, SSJavaLattice<String> methodLattice,
588       MethodLocationInfo methodInfo, FlowNode srcNode, FlowNode dstNode) {
589
590     System.out.println();
591     System.out.println("### addRelationToLattice src=" + srcNode + " dst=" + dstNode);
592
593     // add a new binary relation of dstNode < srcNode
594     FlowGraph flowGraph = getFlowGraph(md);
595     // MethodLocationInfo methodInfo = getMethodLocationInfo(md);
596
597     // String srcOriginSymbol = getSymbol(0, srcNode);
598     // String dstOriginSymbol = getSymbol(0, dstNode);
599
600     Descriptor srcDesc = getDescriptor(0, srcNode);
601     Descriptor dstDesc = getDescriptor(0, dstNode);
602
603     // consider a composite location case
604     boolean isSrcLocalVar = false;
605     boolean isDstLocalVar = false;
606     if (srcNode.getDescTuple().size() == 1) {
607       isSrcLocalVar = true;
608     }
609
610     if (dstNode.getDescTuple().size() == 1) {
611       isDstLocalVar = true;
612     }
613
614     boolean isAssignedCompositeLocation = false;
615     if (!methodInfo.getInferLocation(srcDesc).get(0).getLocIdentifier()
616         .equals(methodInfo.getThisLocName())) {
617       isAssignedCompositeLocation =
618           calculateCompositeLocation(flowGraph, methodLattice, methodInfo, srcNode);
619     }
620
621     String srcSymbol = methodInfo.getInferLocation(srcDesc).get(0).getLocIdentifier();
622     String dstSymbol = methodInfo.getInferLocation(dstDesc).get(0).getLocIdentifier();
623
624
625     if (srcNode.isParameter()) {
626       int paramIdx = flowGraph.getParamIdx(srcNode.getDescTuple());
627       methodInfo.addParameter(srcSymbol, srcDesc, paramIdx);
628     } else {
629       // methodInfo.addMappingOfLocNameToDescriptor(srcSymbol, srcDesc);
630     }
631
632     if (dstNode.isParameter()) {
633       int paramIdx = flowGraph.getParamIdx(dstNode.getDescTuple());
634       methodInfo.addParameter(dstSymbol, dstDesc, paramIdx);
635     } else {
636       // methodInfo.addMappingOfLocNameToDescriptor(dstSymbol, dstDesc);
637     }
638
639     if (!isAssignedCompositeLocation) {
640       // source does not have a composite location
641       if (!srcSymbol.equals(dstSymbol)) {
642         // add a local relation
643         if (!methodLattice.isGreaterThan(srcSymbol, dstSymbol)) {
644           // if the lattice does not have this relation, add it
645           addRelationHigherToLower(methodLattice, methodInfo, srcSymbol, dstSymbol);
646           // methodLattice.addRelationHigherToLower(srcSymbol, dstSymbol);
647         }
648       } else {
649         // if src and dst have the same local location...
650
651         recursivelyAddCompositeRelation(md, flowGraph, methodInfo, srcNode, dstNode, srcDesc,
652             dstDesc);
653
654       }
655
656     } else {
657       // source variable has a composite location
658       if (methodInfo.getInferLocation(dstDesc).getSize() == 1) {
659         if (!srcSymbol.equals(dstSymbol)) {
660           addRelationHigherToLower(methodLattice, methodInfo, srcSymbol, dstSymbol);
661         }
662       }
663
664     }
665
666
667   }
668
669   private void recursivelyAddCompositeRelation(MethodDescriptor md, FlowGraph flowGraph,
670       MethodLocationInfo methodInfo, FlowNode srcNode, FlowNode dstNode, Descriptor srcDesc,
671       Descriptor dstDesc) {
672
673     CompositeLocation inferSrcLoc;
674     CompositeLocation inferDstLoc = methodInfo.getInferLocation(dstDesc);
675
676     if (srcNode.getDescTuple().size() > 1) {
677       // field access
678       inferSrcLoc = new CompositeLocation();
679
680       NTuple<Location> locTuple = flowGraph.getLocationTuple(srcNode);
681       for (int i = 0; i < locTuple.size(); i++) {
682         inferSrcLoc.addLocation(locTuple.get(i));
683       }
684
685     } else {
686       inferSrcLoc = methodInfo.getInferLocation(srcDesc);
687     }
688
689     if (dstNode.getDescTuple().size() > 1) {
690       // field access
691       inferDstLoc = new CompositeLocation();
692
693       NTuple<Location> locTuple = flowGraph.getLocationTuple(dstNode);
694       for (int i = 0; i < locTuple.size(); i++) {
695         inferDstLoc.addLocation(locTuple.get(i));
696       }
697
698     } else {
699       inferDstLoc = methodInfo.getInferLocation(dstDesc);
700     }
701
702     recursiveAddRelationToLattice(1, md, inferSrcLoc, inferDstLoc);
703   }
704
705   private void addPrefixMapping(Map<NTuple<Location>, Set<NTuple<Location>>> map,
706       NTuple<Location> prefix, NTuple<Location> element) {
707
708     if (!map.containsKey(prefix)) {
709       map.put(prefix, new HashSet<NTuple<Location>>());
710     }
711     map.get(prefix).add(element);
712   }
713
714   private boolean calculateCompositeLocation(FlowGraph flowGraph,
715       SSJavaLattice<String> methodLattice, MethodLocationInfo methodInfo, FlowNode flowNode) {
716
717     Descriptor localVarDesc = flowNode.getDescTuple().get(0);
718
719     Set<FlowNode> inNodeSet = flowGraph.getIncomingFlowNodeSet(flowNode);
720     Set<FlowNode> reachableNodeSet = flowGraph.getReachableFlowNodeSet(flowNode);
721
722     Map<NTuple<Location>, Set<NTuple<Location>>> mapPrefixToIncomingLocTupleSet =
723         new HashMap<NTuple<Location>, Set<NTuple<Location>>>();
724
725     Set<FlowNode> localInNodeSet = new HashSet<FlowNode>();
726     Set<FlowNode> localOutNodeSet = new HashSet<FlowNode>();
727
728     List<NTuple<Location>> prefixList = new ArrayList<NTuple<Location>>();
729
730     for (Iterator iterator = inNodeSet.iterator(); iterator.hasNext();) {
731       FlowNode inNode = (FlowNode) iterator.next();
732       NTuple<Location> inTuple = flowGraph.getLocationTuple(inNode);
733
734       if (inTuple.size() > 1) {
735         for (int i = 1; i < inTuple.size(); i++) {
736           NTuple<Location> prefix = inTuple.subList(0, i);
737           if (!prefixList.contains(prefix)) {
738             prefixList.add(prefix);
739           }
740           addPrefixMapping(mapPrefixToIncomingLocTupleSet, prefix, inTuple);
741         }
742       } else {
743         localInNodeSet.add(inNode);
744       }
745     }
746
747     Collections.sort(prefixList, new Comparator<NTuple<Location>>() {
748       public int compare(NTuple<Location> arg0, NTuple<Location> arg1) {
749         int s0 = arg0.size();
750         int s1 = arg1.size();
751         if (s0 > s1) {
752           return -1;
753         } else if (s0 == s1) {
754           return 0;
755         } else {
756           return 1;
757         }
758       }
759     });
760
761
762     for (Iterator iterator2 = reachableNodeSet.iterator(); iterator2.hasNext();) {
763       FlowNode reachableNode = (FlowNode) iterator2.next();
764       if (reachableNode.getDescTuple().size() == 1) {
765         localOutNodeSet.add(reachableNode);
766       }
767     }
768
769     // find out reachable nodes that have the longest common prefix
770     for (int i = 0; i < prefixList.size(); i++) {
771       NTuple<Location> curPrefix = prefixList.get(i);
772       Set<NTuple<Location>> reachableCommonPrefixSet = new HashSet<NTuple<Location>>();
773
774       for (Iterator iterator2 = reachableNodeSet.iterator(); iterator2.hasNext();) {
775         FlowNode reachableNode = (FlowNode) iterator2.next();
776         NTuple<Location> reachLocTuple = flowGraph.getLocationTuple(reachableNode);
777         if (reachLocTuple.startsWith(curPrefix)) {
778           reachableCommonPrefixSet.add(reachLocTuple);
779         }
780
781       }
782
783
784       if (!reachableCommonPrefixSet.isEmpty()) {
785         // found reachable nodes that start with the prefix curPrefix
786         // need to assign a composite location
787
788         // first, check if there are more than one the set of locations that has
789         // the same length of the longest reachable prefix, no way to assign
790         // a composite location to the input local var
791         prefixSanityCheck(prefixList, i, flowGraph, reachableNodeSet);
792
793         Set<NTuple<Location>> incomingCommonPrefixSet =
794             mapPrefixToIncomingLocTupleSet.get(curPrefix);
795
796         int idx = curPrefix.size();
797         NTuple<Location> element = incomingCommonPrefixSet.iterator().next();
798         Descriptor desc = element.get(idx).getDescriptor();
799
800         SSJavaLattice<String> lattice = getLattice(desc);
801         LocationInfo locInfo = getLocationInfo(desc);
802
803         // CompositeLocation inferLocation =
804         // methodInfo.getInferLocation(flowNode);
805         CompositeLocation inferLocation = methodInfo.getInferLocation(localVarDesc);
806
807         String newlyInsertedLocName;
808         if (inferLocation.getSize() == 1) {
809           // need to replace the old local location with a new composite
810           // location
811
812           String oldMethodLocationSymbol = inferLocation.get(0).getLocIdentifier();
813
814           String newLocSymbol = "Loc" + (SSJavaLattice.seed++);
815           inferLocation = new CompositeLocation();
816           for (int locIdx = 0; locIdx < curPrefix.size(); locIdx++) {
817             inferLocation.addLocation(curPrefix.get(locIdx));
818           }
819           Location fieldLoc = new Location(desc, newLocSymbol);
820           inferLocation.addLocation(fieldLoc);
821
822
823           methodInfo.mapDescriptorToLocation(localVarDesc, inferLocation);
824           methodInfo.removeMaplocalVarToLocSet(localVarDesc);
825
826           String newMethodLocationSymbol = curPrefix.get(0).getLocIdentifier();
827
828           replaceOldLocWithNewLoc(methodLattice, oldMethodLocationSymbol, newMethodLocationSymbol);
829
830         } else {
831
832           String localLocName = methodInfo.getInferLocation(localVarDesc).get(0).getLocIdentifier();
833           return true;
834
835
836         }
837
838         newlyInsertedLocName = inferLocation.get(inferLocation.getSize() - 1).getLocIdentifier();
839
840         for (Iterator iterator = incomingCommonPrefixSet.iterator(); iterator.hasNext();) {
841           NTuple<Location> tuple = (NTuple<Location>) iterator.next();
842
843           Location loc = tuple.get(idx);
844           String higher = locInfo.getFieldInferLocation(loc.getLocDescriptor()).getLocIdentifier();
845           System.out.println("here3");
846
847           addRelationHigherToLower(lattice, locInfo, higher, newlyInsertedLocName);
848         }
849
850
851         for (Iterator iterator = localInNodeSet.iterator(); iterator.hasNext();) {
852           FlowNode localNode = (FlowNode) iterator.next();
853           Descriptor localInVarDesc = localNode.getDescTuple().get(0);
854           CompositeLocation inNodeInferLoc = methodInfo.getInferLocation(localInVarDesc);
855
856           if (isCompositeLocation(inNodeInferLoc)) {
857             // need to make sure that newLocSymbol is lower than the infernode
858             // location in the field lattice
859
860             if (inNodeInferLoc.getTuple().startsWith(curPrefix)
861                 && inNodeInferLoc.getSize() == (curPrefix.size() + 1)) {
862               String higher = inNodeInferLoc.get(inNodeInferLoc.getSize() - 1).getLocIdentifier();
863               if (!higher.equals(newlyInsertedLocName)) {
864
865                 addRelationHigherToLower(lattice, locInfo, higher, newlyInsertedLocName);
866               }
867             } else {
868               throw new Error("Failed to generate a composite location.");
869             }
870
871           }
872         }
873
874         for (Iterator iterator = reachableCommonPrefixSet.iterator(); iterator.hasNext();) {
875           NTuple<Location> tuple = (NTuple<Location>) iterator.next();
876           Location loc = tuple.get(idx);
877           String lower = locInfo.getFieldInferLocation(loc.getLocDescriptor()).getLocIdentifier();
878           // lattice.addRelationHigherToLower(newlyInsertedLocName, lower);
879
880           addRelationHigherToLower(lattice, locInfo, newlyInsertedLocName, lower);
881         }
882
883         for (Iterator iterator = localOutNodeSet.iterator(); iterator.hasNext();) {
884           FlowNode localOutNode = (FlowNode) iterator.next();
885
886           Descriptor localOutDesc = localOutNode.getDescTuple().get(0);
887           // String localOutNodeSymbol =
888           // localOutNode.getDescTuple().get(0).getSymbol();
889           CompositeLocation outNodeInferLoc = methodInfo.getInferLocation(localOutDesc);
890
891           // System.out
892           // .println("localOutNode=" + localOutNode + " outNodeInferLoc=" +
893           // outNodeInferLoc);
894           if (isCompositeLocation(outNodeInferLoc)) {
895             // need to make sure that newLocSymbol is higher than the infernode
896             // location
897
898             if (outNodeInferLoc.getTuple().startsWith(curPrefix)
899                 && outNodeInferLoc.getSize() == (curPrefix.size() + 1)) {
900
901               String lower = outNodeInferLoc.get(outNodeInferLoc.getSize() - 1).getLocIdentifier();
902
903               addRelationHigherToLower(lattice, locInfo, newlyInsertedLocName, lower);
904
905             } else {
906               throw new Error("Failed to generate a composite location.");
907             }
908           }
909         }
910
911         return true;
912       }
913
914     }
915
916     return false;
917
918   }
919
920   private boolean isCompositeLocation(CompositeLocation cl) {
921     return cl.getSize() > 1;
922   }
923
924   private boolean containsNonPrimitiveElement(Set<Descriptor> descSet) {
925     for (Iterator iterator = descSet.iterator(); iterator.hasNext();) {
926       Descriptor desc = (Descriptor) iterator.next();
927
928       if (desc instanceof VarDescriptor) {
929         if (!((VarDescriptor) desc).getType().isPrimitive()) {
930           return true;
931         }
932       } else if (desc instanceof FieldDescriptor) {
933         if (!((FieldDescriptor) desc).getType().isPrimitive()) {
934           return true;
935         }
936       }
937
938     }
939     return false;
940   }
941
942   private void addRelationHigherToLower(SSJavaLattice<String> lattice, LocationInfo locInfo,
943       String higher, String lower) {
944
945     Set<String> cycleElementSet = lattice.getPossibleCycleElements(higher, lower);
946
947     boolean hasNonPrimitiveElement = false;
948     for (Iterator iterator = cycleElementSet.iterator(); iterator.hasNext();) {
949       String cycleElementLocSymbol = (String) iterator.next();
950
951       Set<Descriptor> descSet = locInfo.getDescSet(cycleElementLocSymbol);
952       if (containsNonPrimitiveElement(descSet)) {
953         hasNonPrimitiveElement = true;
954         break;
955       }
956     }
957
958     if (hasNonPrimitiveElement) {
959       // if there is non-primitive element in the cycle, no way to merge cyclic
960       // elements into the shared location
961       throw new Error("Failed to merge cyclic value flows into a shared location.");
962     }
963
964     if (cycleElementSet.size() > 0) {
965       String newSharedLoc = "SharedLoc" + (SSJavaLattice.seed++);
966
967       lattice.mergeIntoSharedLocation(cycleElementSet, newSharedLoc);
968
969       for (Iterator iterator = cycleElementSet.iterator(); iterator.hasNext();) {
970         String oldLocSymbol = (String) iterator.next();
971         locInfo.mergeMapping(oldLocSymbol, newSharedLoc);
972       }
973
974
975     } else if (!lattice.isGreaterThan(higher, lower)) {
976       lattice.addRelationHigherToLower(higher, lower);
977     }
978   }
979
980   private void replaceOldLocWithNewLoc(SSJavaLattice<String> methodLattice, String oldLocSymbol,
981       String newLocSymbol) {
982
983     if (methodLattice.containsKey(oldLocSymbol)) {
984       methodLattice.substituteLocation(oldLocSymbol, newLocSymbol);
985     }
986
987   }
988
989   private void prefixSanityCheck(List<NTuple<Location>> prefixList, int curIdx,
990       FlowGraph flowGraph, Set<FlowNode> reachableNodeSet) {
991
992
993     NTuple<Location> curPrefix = prefixList.get(curIdx);
994
995     for (int i = curIdx + 1; i < prefixList.size(); i++) {
996       NTuple<Location> prefixTuple = prefixList.get(i);
997
998       if (curPrefix.startsWith(prefixTuple)) {
999         continue;
1000       }
1001
1002       for (Iterator iterator2 = reachableNodeSet.iterator(); iterator2.hasNext();) {
1003         FlowNode reachableNode = (FlowNode) iterator2.next();
1004         NTuple<Location> reachLocTuple = flowGraph.getLocationTuple(reachableNode);
1005         if (reachLocTuple.startsWith(prefixTuple)) {
1006           // TODO
1007           throw new Error("Failed to generate a composite location");
1008         }
1009       }
1010     }
1011   }
1012
1013   public boolean isPrimitiveLocalVariable(FlowNode node) {
1014     VarDescriptor varDesc = (VarDescriptor) node.getDescTuple().get(0);
1015     return varDesc.getType().isPrimitive();
1016   }
1017
1018   private SSJavaLattice<String> getLattice(Descriptor d) {
1019     if (d instanceof MethodDescriptor) {
1020       return getMethodLattice((MethodDescriptor) d);
1021     } else {
1022       return getFieldLattice((ClassDescriptor) d);
1023     }
1024   }
1025
1026   private SSJavaLattice<String> getMethodLattice(MethodDescriptor md) {
1027     if (!md2lattice.containsKey(md)) {
1028       md2lattice.put(md, new SSJavaLattice<String>(SSJavaAnalysis.TOP, SSJavaAnalysis.BOTTOM));
1029     }
1030     return md2lattice.get(md);
1031   }
1032
1033   private void setMethodLattice(MethodDescriptor md, SSJavaLattice<String> lattice) {
1034     md2lattice.put(md, lattice);
1035   }
1036
1037   private void extractRelationFromFieldFlows(ClassDescriptor cd, FlowNode srcNode,
1038       FlowNode dstNode, int idx) {
1039
1040
1041     if (srcNode.getDescTuple().get(idx).equals(dstNode.getDescTuple().get(idx))
1042         && srcNode.getDescTuple().size() > (idx + 1) && dstNode.getDescTuple().size() > (idx + 1)) {
1043       // value flow between fields: we don't need to add a binary relation
1044       // for this case
1045
1046       Descriptor desc = srcNode.getDescTuple().get(idx);
1047       ClassDescriptor classDesc;
1048
1049       if (idx == 0) {
1050         classDesc = ((VarDescriptor) desc).getType().getClassDesc();
1051       } else {
1052         classDesc = ((FieldDescriptor) desc).getType().getClassDesc();
1053       }
1054
1055       extractRelationFromFieldFlows(classDesc, srcNode, dstNode, idx + 1);
1056
1057     } else {
1058
1059       Descriptor srcFieldDesc = srcNode.getDescTuple().get(idx);
1060       Descriptor dstFieldDesc = dstNode.getDescTuple().get(idx);
1061
1062       // add a new binary relation of dstNode < srcNode
1063       SSJavaLattice<String> fieldLattice = getFieldLattice(cd);
1064       LocationInfo fieldInfo = getFieldLocationInfo(cd);
1065
1066
1067       String srcSymbol = fieldInfo.getFieldInferLocation(srcFieldDesc).getLocIdentifier();
1068       String dstSymbol = fieldInfo.getFieldInferLocation(dstFieldDesc).getLocIdentifier();
1069
1070       addRelationHigherToLower(fieldLattice, fieldInfo, srcSymbol, dstSymbol);
1071
1072     }
1073
1074   }
1075
1076   public SSJavaLattice<String> getFieldLattice(ClassDescriptor cd) {
1077     if (!cd2lattice.containsKey(cd)) {
1078       cd2lattice.put(cd, new SSJavaLattice<String>(SSJavaAnalysis.TOP, SSJavaAnalysis.BOTTOM));
1079     }
1080     return cd2lattice.get(cd);
1081   }
1082
1083   public void constructFlowGraph() {
1084
1085     setupToAnalyze();
1086
1087     while (!toAnalyzeIsEmpty()) {
1088       ClassDescriptor cd = toAnalyzeNext();
1089
1090       setupToAnalazeMethod(cd);
1091       while (!toAnalyzeMethodIsEmpty()) {
1092         MethodDescriptor md = toAnalyzeMethodNext();
1093         if (ssjava.needTobeAnnotated(md)) {
1094           if (state.SSJAVADEBUG) {
1095             System.out.println();
1096             System.out.println("SSJAVA: Constructing a flow graph: " + md);
1097           }
1098
1099           // creates a mapping from a method descriptor to virtual methods
1100           Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
1101           if (md.isStatic()) {
1102             setPossibleCallees.add(md);
1103           } else {
1104             setPossibleCallees.addAll(ssjava.getCallGraph().getMethods(md));
1105           }
1106           mapMethodDescToPossibleMethodDescSet.put(md, setPossibleCallees);
1107
1108           // creates a mapping from a parameter descriptor to its index
1109           Map<Descriptor, Integer> mapParamDescToIdx = new HashMap<Descriptor, Integer>();
1110           int offset = md.isStatic() ? 0 : 1;
1111           for (int i = 0; i < md.numParameters(); i++) {
1112             Descriptor paramDesc = (Descriptor) md.getParameter(i);
1113             mapParamDescToIdx.put(paramDesc, new Integer(i + offset));
1114           }
1115
1116           FlowGraph fg = new FlowGraph(md, mapParamDescToIdx);
1117           mapMethodDescriptorToFlowGraph.put(md, fg);
1118
1119           analyzeMethodBody(cd, md);
1120         }
1121       }
1122     }
1123
1124     _debug_printGraph();
1125   }
1126
1127   private void analyzeMethodBody(ClassDescriptor cd, MethodDescriptor md) {
1128     BlockNode bn = state.getMethodBody(md);
1129     NodeTupleSet implicitFlowTupleSet = new NodeTupleSet();
1130     analyzeFlowBlockNode(md, md.getParameterTable(), bn, implicitFlowTupleSet);
1131   }
1132
1133   private void analyzeFlowBlockNode(MethodDescriptor md, SymbolTable nametable, BlockNode bn,
1134       NodeTupleSet implicitFlowTupleSet) {
1135
1136     bn.getVarTable().setParent(nametable);
1137     for (int i = 0; i < bn.size(); i++) {
1138       BlockStatementNode bsn = bn.get(i);
1139       analyzeBlockStatementNode(md, bn.getVarTable(), bsn, implicitFlowTupleSet);
1140     }
1141
1142   }
1143
1144   private void analyzeBlockStatementNode(MethodDescriptor md, SymbolTable nametable,
1145       BlockStatementNode bsn, NodeTupleSet implicitFlowTupleSet) {
1146
1147     switch (bsn.kind()) {
1148     case Kind.BlockExpressionNode:
1149       analyzeBlockExpressionNode(md, nametable, (BlockExpressionNode) bsn, implicitFlowTupleSet);
1150       break;
1151
1152     case Kind.DeclarationNode:
1153       analyzeFlowDeclarationNode(md, nametable, (DeclarationNode) bsn, implicitFlowTupleSet);
1154       break;
1155
1156     case Kind.IfStatementNode:
1157       analyzeFlowIfStatementNode(md, nametable, (IfStatementNode) bsn, implicitFlowTupleSet);
1158       break;
1159
1160     case Kind.LoopNode:
1161       analyzeFlowLoopNode(md, nametable, (LoopNode) bsn, implicitFlowTupleSet);
1162       break;
1163
1164     case Kind.ReturnNode:
1165       analyzeFlowReturnNode(md, nametable, (ReturnNode) bsn, implicitFlowTupleSet);
1166       break;
1167
1168     case Kind.SubBlockNode:
1169       analyzeFlowSubBlockNode(md, nametable, (SubBlockNode) bsn, implicitFlowTupleSet);
1170       break;
1171
1172     case Kind.ContinueBreakNode:
1173       break;
1174
1175     case Kind.SwitchStatementNode:
1176       analyzeSwitchStatementNode(md, nametable, (SwitchStatementNode) bsn);
1177       break;
1178
1179     }
1180
1181   }
1182
1183   private void analyzeSwitchStatementNode(MethodDescriptor md, SymbolTable nametable,
1184       SwitchStatementNode bsn) {
1185     // TODO Auto-generated method stub
1186   }
1187
1188   private void analyzeFlowSubBlockNode(MethodDescriptor md, SymbolTable nametable,
1189       SubBlockNode sbn, NodeTupleSet implicitFlowTupleSet) {
1190     analyzeFlowBlockNode(md, nametable, sbn.getBlockNode(), implicitFlowTupleSet);
1191   }
1192
1193   private void analyzeFlowReturnNode(MethodDescriptor md, SymbolTable nametable, ReturnNode rn,
1194       NodeTupleSet implicitFlowTupleSet) {
1195
1196     ExpressionNode returnExp = rn.getReturnExpression();
1197
1198     NodeTupleSet nodeSet = new NodeTupleSet();
1199     analyzeFlowExpressionNode(md, nametable, returnExp, nodeSet, false);
1200
1201     FlowGraph fg = getFlowGraph(md);
1202
1203     // annotate the elements of the node set as the return location
1204     for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
1205       NTuple<Descriptor> returnDescTuple = (NTuple<Descriptor>) iterator.next();
1206       fg.setReturnFlowNode(returnDescTuple);
1207       for (Iterator iterator2 = implicitFlowTupleSet.iterator(); iterator2.hasNext();) {
1208         NTuple<Descriptor> implicitFlowDescTuple = (NTuple<Descriptor>) iterator2.next();
1209         fg.addValueFlowEdge(implicitFlowDescTuple, returnDescTuple);
1210       }
1211     }
1212
1213   }
1214
1215   private void analyzeFlowLoopNode(MethodDescriptor md, SymbolTable nametable, LoopNode ln,
1216       NodeTupleSet implicitFlowTupleSet) {
1217
1218     if (ln.getType() == LoopNode.WHILELOOP || ln.getType() == LoopNode.DOWHILELOOP) {
1219
1220       NodeTupleSet condTupleNode = new NodeTupleSet();
1221       analyzeFlowExpressionNode(md, nametable, ln.getCondition(), condTupleNode, null,
1222           implicitFlowTupleSet, false);
1223       condTupleNode.addTupleSet(implicitFlowTupleSet);
1224
1225       // add edges from condNodeTupleSet to all nodes of conditional nodes
1226       analyzeFlowBlockNode(md, nametable, ln.getBody(), condTupleNode);
1227
1228     } else {
1229       // check 'for loop' case
1230       BlockNode bn = ln.getInitializer();
1231       bn.getVarTable().setParent(nametable);
1232       for (int i = 0; i < bn.size(); i++) {
1233         BlockStatementNode bsn = bn.get(i);
1234         analyzeBlockStatementNode(md, bn.getVarTable(), bsn, implicitFlowTupleSet);
1235       }
1236
1237       NodeTupleSet condTupleNode = new NodeTupleSet();
1238       analyzeFlowExpressionNode(md, bn.getVarTable(), ln.getCondition(), condTupleNode, null,
1239           implicitFlowTupleSet, false);
1240       condTupleNode.addTupleSet(implicitFlowTupleSet);
1241
1242       analyzeFlowBlockNode(md, bn.getVarTable(), ln.getUpdate(), condTupleNode);
1243       analyzeFlowBlockNode(md, bn.getVarTable(), ln.getBody(), condTupleNode);
1244
1245     }
1246
1247   }
1248
1249   private void analyzeFlowIfStatementNode(MethodDescriptor md, SymbolTable nametable,
1250       IfStatementNode isn, NodeTupleSet implicitFlowTupleSet) {
1251
1252     NodeTupleSet condTupleNode = new NodeTupleSet();
1253     analyzeFlowExpressionNode(md, nametable, isn.getCondition(), condTupleNode, null,
1254         implicitFlowTupleSet, false);
1255
1256     // add edges from condNodeTupleSet to all nodes of conditional nodes
1257     condTupleNode.addTupleSet(implicitFlowTupleSet);
1258     analyzeFlowBlockNode(md, nametable, isn.getTrueBlock(), condTupleNode);
1259
1260     if (isn.getFalseBlock() != null) {
1261       analyzeFlowBlockNode(md, nametable, isn.getFalseBlock(), condTupleNode);
1262     }
1263
1264   }
1265
1266   private void analyzeFlowDeclarationNode(MethodDescriptor md, SymbolTable nametable,
1267       DeclarationNode dn, NodeTupleSet implicitFlowTupleSet) {
1268
1269     VarDescriptor vd = dn.getVarDescriptor();
1270     NTuple<Descriptor> tupleLHS = new NTuple<Descriptor>();
1271     tupleLHS.add(vd);
1272     getFlowGraph(md).createNewFlowNode(tupleLHS);
1273
1274     if (dn.getExpression() != null) {
1275
1276       NodeTupleSet tupleSetRHS = new NodeTupleSet();
1277       analyzeFlowExpressionNode(md, nametable, dn.getExpression(), tupleSetRHS, null,
1278           implicitFlowTupleSet, false);
1279
1280       // add a new flow edge from rhs to lhs
1281       for (Iterator<NTuple<Descriptor>> iter = tupleSetRHS.iterator(); iter.hasNext();) {
1282         NTuple<Descriptor> from = iter.next();
1283         addFlowGraphEdge(md, from, tupleLHS);
1284       }
1285
1286     }
1287
1288   }
1289
1290   private void analyzeBlockExpressionNode(MethodDescriptor md, SymbolTable nametable,
1291       BlockExpressionNode ben, NodeTupleSet implicitFlowTupleSet) {
1292     analyzeFlowExpressionNode(md, nametable, ben.getExpression(), null, null, implicitFlowTupleSet,
1293         false);
1294   }
1295
1296   private NTuple<Descriptor> analyzeFlowExpressionNode(MethodDescriptor md, SymbolTable nametable,
1297       ExpressionNode en, NodeTupleSet nodeSet, boolean isLHS) {
1298     return analyzeFlowExpressionNode(md, nametable, en, nodeSet, null, new NodeTupleSet(), isLHS);
1299   }
1300
1301   private NTuple<Descriptor> analyzeFlowExpressionNode(MethodDescriptor md, SymbolTable nametable,
1302       ExpressionNode en, NodeTupleSet nodeSet, NTuple<Descriptor> base,
1303       NodeTupleSet implicitFlowTupleSet, boolean isLHS) {
1304
1305     // note that expression node can create more than one flow node
1306     // nodeSet contains of flow nodes
1307     // base is always assigned to null except the case of a name node!
1308
1309     NTuple<Descriptor> flowTuple;
1310
1311     switch (en.kind()) {
1312
1313     case Kind.AssignmentNode:
1314       analyzeFlowAssignmentNode(md, nametable, (AssignmentNode) en, base, implicitFlowTupleSet);
1315       break;
1316
1317     case Kind.FieldAccessNode:
1318       flowTuple =
1319           analyzeFlowFieldAccessNode(md, nametable, (FieldAccessNode) en, nodeSet, base,
1320               implicitFlowTupleSet);
1321       nodeSet.addTuple(flowTuple);
1322       return flowTuple;
1323
1324     case Kind.NameNode:
1325       NodeTupleSet nameNodeSet = new NodeTupleSet();
1326       flowTuple =
1327           analyzeFlowNameNode(md, nametable, (NameNode) en, nameNodeSet, base, implicitFlowTupleSet);
1328       nodeSet.addTuple(flowTuple);
1329       return flowTuple;
1330
1331     case Kind.OpNode:
1332       analyzeFlowOpNode(md, nametable, (OpNode) en, nodeSet, implicitFlowTupleSet);
1333       break;
1334
1335     case Kind.CreateObjectNode:
1336       analyzeCreateObjectNode(md, nametable, (CreateObjectNode) en);
1337       break;
1338
1339     case Kind.ArrayAccessNode:
1340       analyzeFlowArrayAccessNode(md, nametable, (ArrayAccessNode) en, nodeSet, isLHS);
1341       break;
1342
1343     case Kind.LiteralNode:
1344       analyzeLiteralNode(md, nametable, (LiteralNode) en);
1345       break;
1346
1347     case Kind.MethodInvokeNode:
1348       analyzeFlowMethodInvokeNode(md, nametable, (MethodInvokeNode) en, implicitFlowTupleSet);
1349       break;
1350
1351     case Kind.TertiaryNode:
1352       analyzeFlowTertiaryNode(md, nametable, (TertiaryNode) en, nodeSet, implicitFlowTupleSet);
1353       break;
1354
1355     case Kind.CastNode:
1356       analyzeFlowCastNode(md, nametable, (CastNode) en, implicitFlowTupleSet);
1357       break;
1358
1359     // case Kind.InstanceOfNode:
1360     // checkInstanceOfNode(md, nametable, (InstanceOfNode) en, td);
1361     // return null;
1362
1363     // case Kind.ArrayInitializerNode:
1364     // checkArrayInitializerNode(md, nametable, (ArrayInitializerNode) en,
1365     // td);
1366     // return null;
1367
1368     // case Kind.ClassTypeNode:
1369     // checkClassTypeNode(md, nametable, (ClassTypeNode) en, td);
1370     // return null;
1371
1372     // case Kind.OffsetNode:
1373     // checkOffsetNode(md, nametable, (OffsetNode)en, td);
1374     // return null;
1375
1376     }
1377     return null;
1378
1379   }
1380
1381   private void analyzeFlowCastNode(MethodDescriptor md, SymbolTable nametable, CastNode cn,
1382       NodeTupleSet implicitFlowTupleSet) {
1383
1384     NodeTupleSet nodeTupleSet = new NodeTupleSet();
1385     analyzeFlowExpressionNode(md, nametable, cn.getExpression(), nodeTupleSet, false);
1386
1387   }
1388
1389   private void analyzeFlowTertiaryNode(MethodDescriptor md, SymbolTable nametable, TertiaryNode tn,
1390       NodeTupleSet nodeSet, NodeTupleSet implicitFlowTupleSet) {
1391
1392     NodeTupleSet tertiaryTupleNode = new NodeTupleSet();
1393     analyzeFlowExpressionNode(md, nametable, tn.getCond(), tertiaryTupleNode, null,
1394         implicitFlowTupleSet, false);
1395
1396     // add edges from tertiaryTupleNode to all nodes of conditional nodes
1397     tertiaryTupleNode.addTupleSet(implicitFlowTupleSet);
1398     analyzeFlowExpressionNode(md, nametable, tn.getTrueExpr(), tertiaryTupleNode, null,
1399         implicitFlowTupleSet, false);
1400
1401     analyzeFlowExpressionNode(md, nametable, tn.getFalseExpr(), tertiaryTupleNode, null,
1402         implicitFlowTupleSet, false);
1403
1404     nodeSet.addTupleSet(tertiaryTupleNode);
1405
1406   }
1407
1408   private void addMapCallerMethodDescToMethodInvokeNodeSet(MethodDescriptor caller,
1409       MethodInvokeNode min) {
1410     Set<MethodInvokeNode> set = mapMethodDescriptorToMethodInvokeNodeSet.get(caller);
1411     if (set == null) {
1412       set = new HashSet<MethodInvokeNode>();
1413       mapMethodDescriptorToMethodInvokeNodeSet.put(caller, set);
1414     }
1415     set.add(min);
1416   }
1417
1418   private void analyzeFlowMethodInvokeNode(MethodDescriptor md, SymbolTable nametable,
1419       MethodInvokeNode min, NodeTupleSet implicitFlowTupleSet) {
1420
1421     addMapCallerMethodDescToMethodInvokeNodeSet(md, min);
1422
1423     MethodDescriptor calleeMD = min.getMethod();
1424
1425     NameDescriptor baseName = min.getBaseName();
1426     boolean isSystemout = false;
1427     if (baseName != null) {
1428       isSystemout = baseName.getSymbol().equals("System.out");
1429     }
1430
1431     if (!ssjava.isSSJavaUtil(calleeMD.getClassDesc()) && !ssjava.isTrustMethod(calleeMD)
1432         && !calleeMD.getModifiers().isNative() && !isSystemout) {
1433
1434       // CompositeLocation baseLocation = null;
1435       if (min.getExpression() != null) {
1436
1437         NodeTupleSet baseNodeSet = new NodeTupleSet();
1438         analyzeFlowExpressionNode(calleeMD, nametable, min.getExpression(), baseNodeSet, null,
1439             implicitFlowTupleSet, false);
1440
1441       } else {
1442         if (min.getMethod().isStatic()) {
1443           // String globalLocId = ssjava.getMethodLattice(md).getGlobalLoc();
1444           // if (globalLocId == null) {
1445           // throw new
1446           // Error("Method lattice does not define global variable location at "
1447           // + generateErrorMessage(md.getClassDesc(), min));
1448           // }
1449           // baseLocation = new CompositeLocation(new Location(md,
1450           // globalLocId));
1451         } else {
1452           // 'this' var case
1453           // String thisLocId = ssjava.getMethodLattice(md).getThisLoc();
1454           // baseLocation = new CompositeLocation(new Location(md, thisLocId));
1455         }
1456       }
1457
1458       // constraint case:
1459       // if (constraint != null) {
1460       // int compareResult =
1461       // CompositeLattice.compare(constraint, baseLocation, true,
1462       // generateErrorMessage(cd, min));
1463       // if (compareResult != ComparisonResult.GREATER) {
1464       // // if the current constraint is higher than method's THIS location
1465       // // no need to check constraints!
1466       // CompositeLocation calleeConstraint =
1467       // translateCallerLocToCalleeLoc(calleeMD, baseLocation, constraint);
1468       // // System.out.println("check method body for constraint:" + calleeMD +
1469       // // " calleeConstraint="
1470       // // + calleeConstraint);
1471       // checkMethodBody(calleeMD.getClassDesc(), calleeMD, calleeConstraint);
1472       // }
1473       // }
1474
1475       analyzeFlowMethodParameters(md, nametable, min);
1476
1477       // checkCalleeConstraints(md, nametable, min, baseLocation, constraint);
1478
1479       // checkCallerArgumentLocationConstraints(md, nametable, min,
1480       // baseLocation, constraint);
1481
1482       if (!min.getMethod().getReturnType().isVoid()) {
1483         // If method has a return value, compute the highest possible return
1484         // location in the caller's perspective
1485         // CompositeLocation ceilingLoc =
1486         // computeCeilingLocationForCaller(md, nametable, min, baseLocation,
1487         // constraint);
1488         // return ceilingLoc;
1489       }
1490     }
1491
1492     // return new CompositeLocation(Location.createTopLocation(md));
1493
1494   }
1495
1496   private NTuple<Descriptor> getArgTupleByArgIdx(MethodInvokeNode min, int idx) {
1497     return mapMethodInvokeNodeToArgIdxMap.get(min).get(new Integer(idx));
1498   }
1499
1500   private void addArgIdxMap(MethodInvokeNode min, int idx, NTuple<Descriptor> argTuple) {
1501     Map<Integer, NTuple<Descriptor>> mapIdxToArgTuple = mapMethodInvokeNodeToArgIdxMap.get(min);
1502     if (mapIdxToArgTuple == null) {
1503       mapIdxToArgTuple = new HashMap<Integer, NTuple<Descriptor>>();
1504       mapMethodInvokeNodeToArgIdxMap.put(min, mapIdxToArgTuple);
1505     }
1506     mapIdxToArgTuple.put(new Integer(idx), argTuple);
1507   }
1508
1509   private void analyzeFlowMethodParameters(MethodDescriptor callermd, SymbolTable nametable,
1510       MethodInvokeNode min) {
1511
1512     if (min.numArgs() > 0) {
1513
1514       int offset = min.getMethod().isStatic() ? 0 : 1;
1515
1516       for (int i = 0; i < min.numArgs(); i++) {
1517         ExpressionNode en = min.getArg(i);
1518         NTuple<Descriptor> argTuple =
1519             analyzeFlowExpressionNode(callermd, nametable, en, new NodeTupleSet(), false);
1520
1521         addArgIdxMap(min, i + offset, argTuple);
1522       }
1523
1524     }
1525
1526   }
1527
1528   private void analyzeLiteralNode(MethodDescriptor md, SymbolTable nametable, LiteralNode en) {
1529     // TODO Auto-generated method stub
1530
1531   }
1532
1533   private void analyzeFlowArrayAccessNode(MethodDescriptor md, SymbolTable nametable,
1534       ArrayAccessNode aan, NodeTupleSet nodeSet, boolean isLHS) {
1535
1536     NodeTupleSet expNodeTupleSet = new NodeTupleSet();
1537     analyzeFlowExpressionNode(md, nametable, aan.getExpression(), expNodeTupleSet, isLHS);
1538
1539     NodeTupleSet idxNodeTupleSet = new NodeTupleSet();
1540     analyzeFlowExpressionNode(md, nametable, aan.getIndex(), idxNodeTupleSet, isLHS);
1541
1542     if (isLHS) {
1543       // need to create an edge from idx to array
1544
1545       for (Iterator<NTuple<Descriptor>> idxIter = idxNodeTupleSet.iterator(); idxIter.hasNext();) {
1546         NTuple<Descriptor> idxTuple = idxIter.next();
1547         for (Iterator<NTuple<Descriptor>> arrIter = expNodeTupleSet.iterator(); arrIter.hasNext();) {
1548           NTuple<Descriptor> arrTuple = arrIter.next();
1549           getFlowGraph(md).addValueFlowEdge(idxTuple, arrTuple);
1550         }
1551       }
1552
1553       nodeSet.addTupleSet(expNodeTupleSet);
1554     } else {
1555       nodeSet.addTupleSet(expNodeTupleSet);
1556       nodeSet.addTupleSet(idxNodeTupleSet);
1557     }
1558
1559   }
1560
1561   private void analyzeCreateObjectNode(MethodDescriptor md, SymbolTable nametable,
1562       CreateObjectNode en) {
1563     // TODO Auto-generated method stub
1564
1565   }
1566
1567   private void analyzeFlowOpNode(MethodDescriptor md, SymbolTable nametable, OpNode on,
1568       NodeTupleSet nodeSet, NodeTupleSet implicitFlowTupleSet) {
1569
1570     NodeTupleSet leftOpSet = new NodeTupleSet();
1571     NodeTupleSet rightOpSet = new NodeTupleSet();
1572
1573     // left operand
1574     analyzeFlowExpressionNode(md, nametable, on.getLeft(), leftOpSet, null, implicitFlowTupleSet,
1575         false);
1576
1577     if (on.getRight() != null) {
1578       // right operand
1579       analyzeFlowExpressionNode(md, nametable, on.getRight(), rightOpSet, null,
1580           implicitFlowTupleSet, false);
1581     }
1582
1583     Operation op = on.getOp();
1584
1585     switch (op.getOp()) {
1586
1587     case Operation.UNARYPLUS:
1588     case Operation.UNARYMINUS:
1589     case Operation.LOGIC_NOT:
1590       // single operand
1591       nodeSet.addTupleSet(leftOpSet);
1592       break;
1593
1594     case Operation.LOGIC_OR:
1595     case Operation.LOGIC_AND:
1596     case Operation.COMP:
1597     case Operation.BIT_OR:
1598     case Operation.BIT_XOR:
1599     case Operation.BIT_AND:
1600     case Operation.ISAVAILABLE:
1601     case Operation.EQUAL:
1602     case Operation.NOTEQUAL:
1603     case Operation.LT:
1604     case Operation.GT:
1605     case Operation.LTE:
1606     case Operation.GTE:
1607     case Operation.ADD:
1608     case Operation.SUB:
1609     case Operation.MULT:
1610     case Operation.DIV:
1611     case Operation.MOD:
1612     case Operation.LEFTSHIFT:
1613     case Operation.RIGHTSHIFT:
1614     case Operation.URIGHTSHIFT:
1615
1616       // there are two operands
1617       nodeSet.addTupleSet(leftOpSet);
1618       nodeSet.addTupleSet(rightOpSet);
1619       break;
1620
1621     default:
1622       throw new Error(op.toString());
1623     }
1624   }
1625
1626   private NTuple<Descriptor> analyzeFlowNameNode(MethodDescriptor md, SymbolTable nametable,
1627       NameNode nn, NodeTupleSet nodeSet, NTuple<Descriptor> base, NodeTupleSet implicitFlowTupleSet) {
1628
1629     if (base == null) {
1630       base = new NTuple<Descriptor>();
1631     }
1632
1633     NameDescriptor nd = nn.getName();
1634
1635     if (nd.getBase() != null) {
1636       analyzeFlowExpressionNode(md, nametable, nn.getExpression(), nodeSet, base,
1637           implicitFlowTupleSet, false);
1638     } else {
1639       String varname = nd.toString();
1640       if (varname.equals("this")) {
1641         // 'this' itself!
1642         base.add(md.getThis());
1643         return base;
1644       }
1645
1646       Descriptor d = (Descriptor) nametable.get(varname);
1647
1648       if (d instanceof VarDescriptor) {
1649         VarDescriptor vd = (VarDescriptor) d;
1650         base.add(vd);
1651       } else if (d instanceof FieldDescriptor) {
1652         // the type of field descriptor has a location!
1653         FieldDescriptor fd = (FieldDescriptor) d;
1654         if (fd.isStatic()) {
1655           if (fd.isFinal()) {
1656             // if it is 'static final', the location has TOP since no one can
1657             // change its value
1658             // loc.addLocation(Location.createTopLocation(md));
1659             // return loc;
1660           } else {
1661             // if 'static', the location has pre-assigned global loc
1662             // MethodLattice<String> localLattice = ssjava.getMethodLattice(md);
1663             // String globalLocId = localLattice.getGlobalLoc();
1664             // if (globalLocId == null) {
1665             // throw new
1666             // Error("Global location element is not defined in the method " +
1667             // md);
1668             // }
1669             // Location globalLoc = new Location(md, globalLocId);
1670             //
1671             // loc.addLocation(globalLoc);
1672           }
1673         } else {
1674           // the location of field access starts from this, followed by field
1675           // location
1676           base.add(md.getThis());
1677         }
1678
1679         base.add(fd);
1680       } else if (d == null) {
1681         // access static field
1682         // FieldDescriptor fd = nn.getField();addFlowGraphEdge
1683         //
1684         // MethodLattice<String> localLattice = ssjava.getMethodLattice(md);
1685         // String globalLocId = localLattice.getGlobalLoc();
1686         // if (globalLocId == null) {
1687         // throw new
1688         // Error("Method lattice does not define global variable location at "
1689         // + generateErrorMessage(md.getClassDesc(), nn));
1690         // }
1691         // loc.addLocation(new Location(md, globalLocId));
1692         //
1693         // Location fieldLoc = (Location) fd.getType().getExtension();
1694         // loc.addLocation(fieldLoc);
1695         //
1696         // return loc;
1697
1698       }
1699     }
1700
1701     getFlowGraph(md).createNewFlowNode(base);
1702
1703     return base;
1704
1705   }
1706
1707   private NTuple<Descriptor> analyzeFlowFieldAccessNode(MethodDescriptor md, SymbolTable nametable,
1708       FieldAccessNode fan, NodeTupleSet nodeSet, NTuple<Descriptor> base,
1709       NodeTupleSet implicitFlowTupleSet) {
1710
1711     ExpressionNode left = fan.getExpression();
1712     TypeDescriptor ltd = left.getType();
1713     FieldDescriptor fd = fan.getField();
1714
1715     String varName = null;
1716     if (left.kind() == Kind.NameNode) {
1717       NameDescriptor nd = ((NameNode) left).getName();
1718       varName = nd.toString();
1719     }
1720
1721     if (ltd.isClassNameRef() || (varName != null && varName.equals("this"))) {
1722       // using a class name directly or access using this
1723       if (fd.isStatic() && fd.isFinal()) {
1724         // loc.addLocation(Location.createTopLocation(md));
1725         // return loc;
1726       }
1727     }
1728
1729     // if (left instanceof ArrayAccessNode) {
1730     // ArrayAccessNode aan = (ArrayAccessNode) left;
1731     // left = aan.getExpression();
1732     // }
1733     // fanNodeSet
1734     base =
1735         analyzeFlowExpressionNode(md, nametable, left, nodeSet, base, implicitFlowTupleSet, false);
1736
1737     if (!left.getType().isPrimitive()) {
1738
1739       if (fd.getSymbol().equals("length")) {
1740         // TODO
1741         // array.length access, return the location of the array
1742         // return loc;
1743       }
1744
1745       base.add(fd);
1746     }
1747
1748     getFlowGraph(md).createNewFlowNode(base);
1749     return base;
1750
1751   }
1752
1753   private void analyzeFlowAssignmentNode(MethodDescriptor md, SymbolTable nametable,
1754       AssignmentNode an, NTuple<Descriptor> base, NodeTupleSet implicitFlowTupleSet) {
1755
1756     // System.out.println("#an=" + an.printNode(0) + " an src=" +
1757     // an.getSrc().printNode(0) + " dst="
1758     // + an.getDest().printNode(0));
1759     NodeTupleSet nodeSetRHS = new NodeTupleSet();
1760     NodeTupleSet nodeSetLHS = new NodeTupleSet();
1761
1762     boolean postinc = true;
1763     if (an.getOperation().getBaseOp() == null
1764         || (an.getOperation().getBaseOp().getOp() != Operation.POSTINC && an.getOperation()
1765             .getBaseOp().getOp() != Operation.POSTDEC)) {
1766       postinc = false;
1767     }
1768     // if LHS is array access node, need to capture value flows between an array
1769     // and its index value
1770     analyzeFlowExpressionNode(md, nametable, an.getDest(), nodeSetLHS, null, implicitFlowTupleSet,
1771         true);
1772
1773     if (!postinc) {
1774       // analyze value flows of rhs expression
1775       analyzeFlowExpressionNode(md, nametable, an.getSrc(), nodeSetRHS, null, implicitFlowTupleSet,
1776           false);
1777
1778       // creates edges from RHS to LHS
1779       for (Iterator<NTuple<Descriptor>> iter = nodeSetRHS.iterator(); iter.hasNext();) {
1780         NTuple<Descriptor> fromTuple = iter.next();
1781         for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
1782           NTuple<Descriptor> toTuple = iter2.next();
1783           addFlowGraphEdge(md, fromTuple, toTuple);
1784         }
1785       }
1786
1787       // creates edges from implicitFlowTupleSet to LHS
1788       for (Iterator<NTuple<Descriptor>> iter = implicitFlowTupleSet.iterator(); iter.hasNext();) {
1789         NTuple<Descriptor> fromTuple = iter.next();
1790         for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
1791           NTuple<Descriptor> toTuple = iter2.next();
1792           addFlowGraphEdge(md, fromTuple, toTuple);
1793         }
1794       }
1795
1796     } else {
1797       // postinc case
1798       for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
1799         NTuple<Descriptor> tuple = iter2.next();
1800         addFlowGraphEdge(md, tuple, tuple);
1801       }
1802
1803     }
1804
1805   }
1806
1807   public FlowGraph getFlowGraph(MethodDescriptor md) {
1808     return mapMethodDescriptorToFlowGraph.get(md);
1809   }
1810
1811   private boolean addFlowGraphEdge(MethodDescriptor md, NTuple<Descriptor> from,
1812       NTuple<Descriptor> to) {
1813     // TODO
1814     // return true if it adds a new edge
1815     FlowGraph graph = getFlowGraph(md);
1816     graph.addValueFlowEdge(from, to);
1817     return true;
1818   }
1819
1820   public void _debug_printGraph() {
1821     Set<MethodDescriptor> keySet = mapMethodDescriptorToFlowGraph.keySet();
1822
1823     for (Iterator<MethodDescriptor> iterator = keySet.iterator(); iterator.hasNext();) {
1824       MethodDescriptor md = (MethodDescriptor) iterator.next();
1825       FlowGraph fg = mapMethodDescriptorToFlowGraph.get(md);
1826       try {
1827         fg.writeGraph();
1828       } catch (IOException e) {
1829         e.printStackTrace();
1830       }
1831     }
1832
1833   }
1834
1835 }