fixes on PCLOC generation, intermediate node insertion, etc...
[IRC.git] / Robust / src / Analysis / SSJava / LocationInference.java
1 package Analysis.SSJava;
2
3 import java.io.BufferedReader;
4 import java.io.BufferedWriter;
5 import java.io.FileReader;
6 import java.io.FileWriter;
7 import java.io.IOException;
8 import java.util.ArrayList;
9 import java.util.Collections;
10 import java.util.Comparator;
11 import java.util.HashMap;
12 import java.util.HashSet;
13 import java.util.Iterator;
14 import java.util.LinkedList;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Set;
18 import java.util.Stack;
19 import java.util.Vector;
20
21 import IR.ClassDescriptor;
22 import IR.Descriptor;
23 import IR.FieldDescriptor;
24 import IR.MethodDescriptor;
25 import IR.NameDescriptor;
26 import IR.Operation;
27 import IR.State;
28 import IR.SymbolTable;
29 import IR.TypeDescriptor;
30 import IR.VarDescriptor;
31 import IR.Tree.ArrayAccessNode;
32 import IR.Tree.AssignmentNode;
33 import IR.Tree.BlockExpressionNode;
34 import IR.Tree.BlockNode;
35 import IR.Tree.BlockStatementNode;
36 import IR.Tree.CastNode;
37 import IR.Tree.CreateObjectNode;
38 import IR.Tree.DeclarationNode;
39 import IR.Tree.ExpressionNode;
40 import IR.Tree.FieldAccessNode;
41 import IR.Tree.IfStatementNode;
42 import IR.Tree.Kind;
43 import IR.Tree.LiteralNode;
44 import IR.Tree.LoopNode;
45 import IR.Tree.MethodInvokeNode;
46 import IR.Tree.NameNode;
47 import IR.Tree.OpNode;
48 import IR.Tree.ReturnNode;
49 import IR.Tree.SubBlockNode;
50 import IR.Tree.SwitchBlockNode;
51 import IR.Tree.SwitchStatementNode;
52 import IR.Tree.TertiaryNode;
53 import IR.Tree.TreeNode;
54 import Util.Pair;
55
56 public class LocationInference {
57
58   State state;
59   SSJavaAnalysis ssjava;
60
61   List<ClassDescriptor> temp_toanalyzeList;
62   List<MethodDescriptor> temp_toanalyzeMethodList;
63   Map<MethodDescriptor, FlowGraph> mapMethodDescriptorToFlowGraph;
64
65   LinkedList<MethodDescriptor> toanalyze_methodDescList;
66
67   // map a method descriptor to its set of parameter descriptors
68   Map<MethodDescriptor, Set<Descriptor>> mapMethodDescriptorToParamDescSet;
69
70   // keep current descriptors to visit in fixed-point interprocedural analysis,
71   private Stack<MethodDescriptor> methodDescriptorsToVisitStack;
72
73   // map a class descriptor to a field lattice
74   private Map<ClassDescriptor, SSJavaLattice<String>> cd2lattice;
75
76   // map a method descriptor to a method lattice
77   private Map<MethodDescriptor, SSJavaLattice<String>> md2lattice;
78
79   // map a method/class descriptor to a hierarchy graph
80   private Map<Descriptor, HierarchyGraph> mapDescriptorToHierarchyGraph;
81
82   // map a method/class descriptor to a skeleton hierarchy graph
83   private Map<Descriptor, HierarchyGraph> mapDescriptorToSkeletonHierarchyGraph;
84
85   private Map<Descriptor, HierarchyGraph> mapDescriptorToSimpleHierarchyGraph;
86
87   // map a method/class descriptor to a skeleton hierarchy graph with combination nodes
88   private Map<Descriptor, HierarchyGraph> mapDescriptorToCombineSkeletonHierarchyGraph;
89
90   // map a descriptor to a simple lattice
91   private Map<Descriptor, SSJavaLattice<String>> mapDescriptorToSimpleLattice;
92
93   // map a method descriptor to the set of method invocation nodes which are
94   // invoked by the method descriptor
95   private Map<MethodDescriptor, Set<MethodInvokeNode>> mapMethodDescriptorToMethodInvokeNodeSet;
96
97   private Map<MethodInvokeNode, Map<Integer, NTuple<Descriptor>>> mapMethodInvokeNodeToArgIdxMap;
98
99   private Map<MethodInvokeNode, NTuple<Descriptor>> mapMethodInvokeNodeToBaseTuple;
100
101   private Map<MethodDescriptor, MethodLocationInfo> mapMethodDescToMethodLocationInfo;
102
103   private Map<ClassDescriptor, LocationInfo> mapClassToLocationInfo;
104
105   private Map<MethodDescriptor, Set<MethodDescriptor>> mapMethodToCalleeSet;
106
107   private Map<MethodDescriptor, Set<FlowNode>> mapMethodDescToParamNodeFlowsToReturnValue;
108
109   private Map<String, Vector<String>> mapFileNameToLineVector;
110
111   private Map<Descriptor, Integer> mapDescToDefinitionLine;
112
113   private Map<Descriptor, LocationSummary> mapDescToLocationSummary;
114
115   // maps a method descriptor to a sub global flow graph that captures all value flows caused by the
116   // set of callees reachable from the method
117   private Map<MethodDescriptor, GlobalFlowGraph> mapMethodDescriptorToSubGlobalFlowGraph;
118
119   private Map<MethodInvokeNode, Map<NTuple<Descriptor>, NTuple<Descriptor>>> mapMethodInvokeNodeToMapCallerArgToCalleeArg;
120
121   public static final String GLOBALLOC = "GLOBALLOC";
122
123   public static final String TOPLOC = "TOPLOC";
124
125   public static final String INTERLOC = "INTERLOC";
126
127   public static final String PCLOC = "PCLOC";
128
129   public static final String RLOC = "RLOC";
130
131   public static final Descriptor GLOBALDESC = new NameDescriptor(GLOBALLOC);
132
133   public static final Descriptor TOPDESC = new NameDescriptor(TOPLOC);
134
135   public static final Descriptor RETURNLOC = new NameDescriptor(RLOC);
136
137   public static String newline = System.getProperty("line.separator");
138
139   LocationInfo curMethodInfo;
140
141   boolean debug = true;
142
143   public static int locSeed = 0;
144
145   public LocationInference(SSJavaAnalysis ssjava, State state) {
146     this.ssjava = ssjava;
147     this.state = state;
148     this.temp_toanalyzeList = new ArrayList<ClassDescriptor>();
149     this.temp_toanalyzeMethodList = new ArrayList<MethodDescriptor>();
150     this.mapMethodDescriptorToFlowGraph = new HashMap<MethodDescriptor, FlowGraph>();
151     this.cd2lattice = new HashMap<ClassDescriptor, SSJavaLattice<String>>();
152     this.md2lattice = new HashMap<MethodDescriptor, SSJavaLattice<String>>();
153     this.methodDescriptorsToVisitStack = new Stack<MethodDescriptor>();
154     this.mapMethodDescriptorToMethodInvokeNodeSet =
155         new HashMap<MethodDescriptor, Set<MethodInvokeNode>>();
156     this.mapMethodInvokeNodeToArgIdxMap =
157         new HashMap<MethodInvokeNode, Map<Integer, NTuple<Descriptor>>>();
158     this.mapMethodDescToMethodLocationInfo = new HashMap<MethodDescriptor, MethodLocationInfo>();
159     this.mapMethodToCalleeSet = new HashMap<MethodDescriptor, Set<MethodDescriptor>>();
160     this.mapClassToLocationInfo = new HashMap<ClassDescriptor, LocationInfo>();
161
162     this.mapFileNameToLineVector = new HashMap<String, Vector<String>>();
163     this.mapDescToDefinitionLine = new HashMap<Descriptor, Integer>();
164     this.mapMethodDescToParamNodeFlowsToReturnValue =
165         new HashMap<MethodDescriptor, Set<FlowNode>>();
166
167     this.mapDescriptorToHierarchyGraph = new HashMap<Descriptor, HierarchyGraph>();
168     this.mapMethodInvokeNodeToBaseTuple = new HashMap<MethodInvokeNode, NTuple<Descriptor>>();
169
170     this.mapDescriptorToSkeletonHierarchyGraph = new HashMap<Descriptor, HierarchyGraph>();
171     this.mapDescriptorToCombineSkeletonHierarchyGraph = new HashMap<Descriptor, HierarchyGraph>();
172     this.mapDescriptorToSimpleHierarchyGraph = new HashMap<Descriptor, HierarchyGraph>();
173
174     this.mapDescriptorToSimpleLattice = new HashMap<Descriptor, SSJavaLattice<String>>();
175
176     this.mapDescToLocationSummary = new HashMap<Descriptor, LocationSummary>();
177
178     this.mapMethodDescriptorToSubGlobalFlowGraph = new HashMap<MethodDescriptor, GlobalFlowGraph>();
179
180     this.mapMethodInvokeNodeToMapCallerArgToCalleeArg =
181         new HashMap<MethodInvokeNode, Map<NTuple<Descriptor>, NTuple<Descriptor>>>();
182
183   }
184
185   public void setupToAnalyze() {
186     SymbolTable classtable = state.getClassSymbolTable();
187     temp_toanalyzeList.clear();
188     temp_toanalyzeList.addAll(classtable.getValueSet());
189     // Collections.sort(toanalyzeList, new Comparator<ClassDescriptor>() {
190     // public int compare(ClassDescriptor o1, ClassDescriptor o2) {
191     // return o1.getClassName().compareToIgnoreCase(o2.getClassName());
192     // }
193     // });
194   }
195
196   public void setupToAnalazeMethod(ClassDescriptor cd) {
197
198     SymbolTable methodtable = cd.getMethodTable();
199     temp_toanalyzeMethodList.clear();
200     temp_toanalyzeMethodList.addAll(methodtable.getValueSet());
201     Collections.sort(temp_toanalyzeMethodList, new Comparator<MethodDescriptor>() {
202       public int compare(MethodDescriptor o1, MethodDescriptor o2) {
203         return o1.getSymbol().compareToIgnoreCase(o2.getSymbol());
204       }
205     });
206   }
207
208   public boolean toAnalyzeMethodIsEmpty() {
209     return temp_toanalyzeMethodList.isEmpty();
210   }
211
212   public boolean toAnalyzeIsEmpty() {
213     return temp_toanalyzeList.isEmpty();
214   }
215
216   public ClassDescriptor toAnalyzeNext() {
217     return temp_toanalyzeList.remove(0);
218   }
219
220   public MethodDescriptor toAnalyzeMethodNext() {
221     return temp_toanalyzeMethodList.remove(0);
222   }
223
224   public void inference() {
225
226     // construct value flow graph
227     constructFlowGraph();
228
229     assignCompositeLocation();
230
231     // System.exit(0);
232
233     constructHierarchyGraph();
234
235     debug_writeHierarchyDotFiles();
236
237     // calculate RETURNLOC,PCLOC
238     calculateExtraLocations();
239
240     simplifyHierarchyGraph();
241
242     debug_writeSimpleHierarchyDotFiles();
243
244     constructSkeletonHierarchyGraph();
245
246     debug_writeSkeletonHierarchyDotFiles();
247
248     insertCombinationNodes();
249
250     debug_writeSkeletonCombinationHierarchyDotFiles();
251
252     buildLattice();
253
254     debug_writeLattices();
255
256     updateCompositeLocationAssignments();
257
258     generateMethodSummary();
259
260     generateAnnoatedCode();
261
262     System.exit(0);
263
264   }
265
266   public Map<NTuple<Descriptor>, NTuple<Descriptor>> getMapCallerArgToCalleeParam(
267       MethodInvokeNode min) {
268
269     if (!mapMethodInvokeNodeToMapCallerArgToCalleeArg.containsKey(min)) {
270       mapMethodInvokeNodeToMapCallerArgToCalleeArg.put(min,
271           new HashMap<NTuple<Descriptor>, NTuple<Descriptor>>());
272     }
273
274     return mapMethodInvokeNodeToMapCallerArgToCalleeArg.get(min);
275   }
276
277   public void addMapCallerArgToCalleeParam(MethodInvokeNode min, NTuple<Descriptor> callerArg,
278       NTuple<Descriptor> calleeParam) {
279     getMapCallerArgToCalleeParam(min).put(callerArg, calleeParam);
280   }
281
282   private void assignCompositeLocation() {
283     calculateGlobalValueFlowCompositeLocation();
284     translateCompositeLocationAssignmentToFlowGraph();
285   }
286
287   private void translateCompositeLocationAssignmentToFlowGraph() {
288     System.out.println("\nSSJAVA: Translate composite location assignments to flow graphs:");
289     MethodDescriptor methodEventLoopDesc = ssjava.getMethodContainingSSJavaLoop();
290     translateCompositeLocationAssignmentToFlowGraph(methodEventLoopDesc);
291     _debug_printGraph();
292   }
293
294   private void updateCompositeLocationAssignments() {
295
296     LinkedList<MethodDescriptor> methodDescList =
297         (LinkedList<MethodDescriptor>) toanalyze_methodDescList.clone();
298
299     while (!methodDescList.isEmpty()) {
300       MethodDescriptor md = methodDescList.removeLast();
301
302       System.out.println("\n#updateCompositeLocationAssignments=" + md);
303
304       FlowGraph flowGraph = getFlowGraph(md);
305
306       MethodSummary methodSummary = getMethodSummary(md);
307
308       Set<FlowNode> nodeSet = flowGraph.getNodeSet();
309       for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
310         FlowNode node = (FlowNode) iterator.next();
311         System.out.println("-node=" + node + "   node.getDescTuple=" + node.getDescTuple());
312         if (node.getCompositeLocation() != null) {
313           CompositeLocation compLoc = node.getCompositeLocation();
314           CompositeLocation updatedCompLoc = updateCompositeLocation(compLoc);
315           node.setCompositeLocation(updatedCompLoc);
316           System.out.println("---updatedCompLoc1=" + updatedCompLoc);
317         } else {
318           NTuple<Descriptor> descTuple = node.getDescTuple();
319           CompositeLocation compLoc = convertToCompositeLocation(md, descTuple);
320           compLoc = updateCompositeLocation(compLoc);
321           node.setCompositeLocation(compLoc);
322           System.out.println("---updatedCompLoc2=" + compLoc);
323         }
324
325         if (node.isDeclaratonNode()) {
326           Descriptor localVarDesc = node.getDescTuple().get(0);
327           CompositeLocation compLoc = updateCompositeLocation(node.getCompositeLocation());
328           methodSummary.addMapVarNameToInferCompLoc(localVarDesc, compLoc);
329         }
330       }
331
332       // update PCLOC and RETURNLOC if they have a composite location assignment
333       if (methodSummary.getRETURNLoc() != null) {
334         methodSummary.setRETURNLoc(updateCompositeLocation(methodSummary.getRETURNLoc()));
335       }
336       if (methodSummary.getPCLoc() != null) {
337         methodSummary.setPCLoc(updateCompositeLocation(methodSummary.getPCLoc()));
338       }
339
340     }
341
342   }
343
344   private CompositeLocation updateCompositeLocation(CompositeLocation compLoc) {
345     CompositeLocation updatedCompLoc = new CompositeLocation();
346     for (int i = 0; i < compLoc.getSize(); i++) {
347       Location loc = compLoc.get(i);
348       String nodeIdentifier = loc.getLocIdentifier();
349       Descriptor enclosingDesc = loc.getDescriptor();
350       String locName;
351       if (!enclosingDesc.equals(GLOBALDESC)) {
352         LocationSummary locSummary = getLocationSummary(enclosingDesc);
353         HierarchyGraph hierarchyGraph = getSimpleHierarchyGraph(enclosingDesc);
354         if (hierarchyGraph != null) {
355
356           HNode curNode = hierarchyGraph.getCurrentHNode(nodeIdentifier);
357           if (curNode != null) {
358             nodeIdentifier = curNode.getName();
359           }
360         }
361         locName = locSummary.getLocationName(nodeIdentifier);
362       } else {
363         locName = nodeIdentifier;
364       }
365       Location updatedLoc = new Location(enclosingDesc, locName);
366       updatedCompLoc.addLocation(updatedLoc);
367     }
368
369     return updatedCompLoc;
370   }
371
372   private void translateCompositeLocationAssignmentToFlowGraph(MethodDescriptor mdCaller) {
373
374     // First, assign a composite location to a node in the flow graph
375     GlobalFlowGraph callerGlobalFlowGraph = getSubGlobalFlowGraph(mdCaller);
376
377     FlowGraph callerFlowGraph = getFlowGraph(mdCaller);
378     Map<Location, CompositeLocation> callerMapLocToCompLoc =
379         callerGlobalFlowGraph.getMapLocationToInferCompositeLocation();
380     Set<Location> methodLocSet = callerMapLocToCompLoc.keySet();
381     for (Iterator iterator = methodLocSet.iterator(); iterator.hasNext();) {
382       Location methodLoc = (Location) iterator.next();
383       if (methodLoc.getDescriptor().equals(mdCaller)) {
384         CompositeLocation inferCompLoc = callerMapLocToCompLoc.get(methodLoc);
385         assignCompositeLocationToFlowGraph(callerFlowGraph, methodLoc, inferCompLoc);
386       }
387     }
388
389     Set<MethodInvokeNode> minSet = mapMethodDescriptorToMethodInvokeNodeSet.get(mdCaller);
390
391     Set<MethodDescriptor> calleeSet = new HashSet<MethodDescriptor>();
392     for (Iterator iterator = minSet.iterator(); iterator.hasNext();) {
393       MethodInvokeNode min = (MethodInvokeNode) iterator.next();
394       // need to translate a composite location that is started with the base
395       // tuple of 'min'.
396       translateMapLocationToInferCompositeLocationToCalleeGraph(callerGlobalFlowGraph, min);
397       calleeSet.add(min.getMethod());
398     }
399
400     for (Iterator iterator = calleeSet.iterator(); iterator.hasNext();) {
401       MethodDescriptor callee = (MethodDescriptor) iterator.next();
402       translateCompositeLocationAssignmentToFlowGraph(callee);
403     }
404
405   }
406
407   public void assignCompositeLocationToFlowGraph(FlowGraph flowGraph, Location loc,
408       CompositeLocation inferCompLoc) {
409     Descriptor localDesc = loc.getLocDescriptor();
410
411     Set<FlowNode> nodeSet = flowGraph.getNodeSet();
412     for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
413       FlowNode node = (FlowNode) iterator.next();
414       if (node.getDescTuple().startsWith(localDesc)) {
415         // need to assign the inferred composite location to this node
416         CompositeLocation newCompLoc = generateCompositeLocation(node.getDescTuple(), inferCompLoc);
417         node.setCompositeLocation(newCompLoc);
418         System.out.println("SET Node=" + node + "  inferCompLoc=" + newCompLoc);
419       }
420     }
421   }
422
423   private CompositeLocation generateCompositeLocation(NTuple<Descriptor> nodeDescTuple,
424       CompositeLocation inferCompLoc) {
425
426     System.out.println("generateCompositeLocation=" + nodeDescTuple + " with inferCompLoc="
427         + inferCompLoc);
428
429     CompositeLocation newCompLoc = new CompositeLocation();
430     for (int i = 0; i < inferCompLoc.getSize(); i++) {
431       newCompLoc.addLocation(inferCompLoc.get(i));
432     }
433
434     Descriptor lastDescOfPrefix = nodeDescTuple.get(0);
435     Descriptor enclosingDescriptor;
436     if (lastDescOfPrefix instanceof InterDescriptor) {
437       enclosingDescriptor = null;
438     } else {
439       enclosingDescriptor = ((VarDescriptor) lastDescOfPrefix).getType().getClassDesc();
440     }
441
442     for (int i = 1; i < nodeDescTuple.size(); i++) {
443       Descriptor desc = nodeDescTuple.get(i);
444       Location locElement = new Location(enclosingDescriptor, desc);
445       newCompLoc.addLocation(locElement);
446
447       enclosingDescriptor = ((FieldDescriptor) desc).getClassDescriptor();
448     }
449
450     return newCompLoc;
451   }
452
453   private void translateMapLocationToInferCompositeLocationToCalleeGraph(
454       GlobalFlowGraph callerGraph, MethodInvokeNode min) {
455
456     MethodDescriptor mdCallee = min.getMethod();
457     MethodDescriptor mdCaller = callerGraph.getMethodDescriptor();
458     Map<Location, CompositeLocation> callerMapLocToCompLoc =
459         callerGraph.getMapLocationToInferCompositeLocation();
460
461     FlowGraph calleeFlowGraph = getFlowGraph(mdCallee);
462     GlobalFlowGraph calleeGlobalGraph = getSubGlobalFlowGraph(mdCallee);
463
464     NTuple<Location> baseLocTuple = null;
465     if (mapMethodInvokeNodeToBaseTuple.containsKey(min)) {
466       baseLocTuple = translateToLocTuple(mdCaller, mapMethodInvokeNodeToBaseTuple.get(min));
467     }
468
469     // System.out.println("\n-translate caller infer composite loc to callee=" + mdCallee
470     // + " baseLocTuple=" + baseLocTuple);
471     Set<Location> keySet = callerMapLocToCompLoc.keySet();
472     for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
473       Location key = (Location) iterator.next();
474       CompositeLocation callerCompLoc = callerMapLocToCompLoc.get(key);
475
476       if (!key.getDescriptor().equals(mdCaller)) {
477
478         CompositeLocation newCalleeCompLoc;
479         if (baseLocTuple != null && callerCompLoc.getTuple().startsWith(baseLocTuple)) {
480           // System.out.println("---need to translate callerCompLoc=" + callerCompLoc
481           // + " with baseTuple=" + baseLocTuple);
482           newCalleeCompLoc =
483               translateCompositeLocationToCallee(callerCompLoc, baseLocTuple, mdCallee);
484
485           calleeGlobalGraph.addMapLocationToInferCompositeLocation(key, newCalleeCompLoc);
486           // System.out.println("---callee loc=" + key + "  newCalleeCompLoc=" + newCalleeCompLoc);
487         } else {
488           // check if it is the global access
489           Location compLocFirstElement = callerCompLoc.getTuple().get(0);
490           if (compLocFirstElement.getDescriptor().equals(mdCallee)
491               && compLocFirstElement.getLocDescriptor().equals(GLOBALDESC)) {
492
493             newCalleeCompLoc = new CompositeLocation();
494             Location newMethodLoc = new Location(mdCallee, GLOBALDESC);
495
496             newCalleeCompLoc.addLocation(newMethodLoc);
497             for (int i = 1; i < callerCompLoc.getSize(); i++) {
498               newCalleeCompLoc.addLocation(callerCompLoc.get(i));
499             }
500             calleeGlobalGraph.addMapLocationToInferCompositeLocation(key, newCalleeCompLoc);
501
502           }
503
504         }
505
506       }
507     }
508
509     // System.out.println("-----*AFTER TRANSLATING COMP LOC MAPPING, CALLEE MAPPING="
510     // + calleeGlobalGraph.getMapLocationToInferCompositeLocation());
511
512     // If the location of an argument has a composite location
513     // need to assign a proper composite location to the corresponding callee parameter
514     // System.out.println("---translate arg composite location to callee param. min="
515     // + min.printNode(0));
516     Map<Integer, NTuple<Descriptor>> mapIdxToArgTuple = mapMethodInvokeNodeToArgIdxMap.get(min);
517     Set<Integer> idxSet = mapIdxToArgTuple.keySet();
518     for (Iterator iterator = idxSet.iterator(); iterator.hasNext();) {
519       Integer idx = (Integer) iterator.next();
520
521       if (idx == 0 && !min.getMethod().isStatic()) {
522         continue;
523       }
524
525       NTuple<Descriptor> argTuple = mapIdxToArgTuple.get(idx);
526       if (argTuple.size() > 0) {
527         // check if an arg tuple has been already assigned to a composite location
528         NTuple<Location> argLocTuple = translateToLocTuple(mdCaller, argTuple);
529         Location argLocalLoc = argLocTuple.get(0);
530
531         // if (!isPrimitiveType(argTuple)) {
532         if (callerMapLocToCompLoc.containsKey(argLocalLoc)) {
533
534           CompositeLocation callerCompLoc = callerMapLocToCompLoc.get(argLocalLoc);
535           for (int i = 1; i < argLocTuple.size(); i++) {
536             callerCompLoc.addLocation(argLocTuple.get(i));
537           }
538
539           if (baseLocTuple != null && callerCompLoc.getTuple().startsWith(baseLocTuple)) {
540
541             FlowNode calleeParamFlowNode = calleeFlowGraph.getParamFlowNode(idx);
542             NTuple<Descriptor> calleeParamDescTuple = calleeParamFlowNode.getDescTuple();
543             NTuple<Location> calleeParamLocTuple =
544                 translateToLocTuple(mdCallee, calleeParamDescTuple);
545
546             System.out.println("---need to translate callerCompLoc=" + callerCompLoc
547                 + " with baseTuple=" + baseLocTuple + "   calleeParamLocTuple="
548                 + calleeParamLocTuple);
549
550             CompositeLocation newCalleeCompLoc =
551                 translateCompositeLocationToCallee(callerCompLoc, baseLocTuple, mdCallee);
552
553             calleeGlobalGraph.addMapLocationToInferCompositeLocation(calleeParamLocTuple.get(0),
554                 newCalleeCompLoc);
555
556             System.out.println("---callee loc=" + calleeParamLocTuple.get(0)
557                 + "  newCalleeCompLoc=" + newCalleeCompLoc);
558
559             // System.out.println("###need to assign composite location to=" + calleeParamDescTuple
560             // + " with baseTuple=" + baseLocTuple);
561           }
562
563         }
564       }
565
566     }
567
568   }
569
570   private boolean isPrimitiveType(NTuple<Descriptor> argTuple) {
571
572     Descriptor lastDesc = argTuple.get(argTuple.size() - 1);
573
574     if (lastDesc instanceof FieldDescriptor) {
575       return ((FieldDescriptor) lastDesc).getType().isPrimitive();
576     } else if (lastDesc instanceof VarDescriptor) {
577       return ((VarDescriptor) lastDesc).getType().isPrimitive();
578     }
579
580     return true;
581   }
582
583   private CompositeLocation translateCompositeLocationToCallee(CompositeLocation callerCompLoc,
584       NTuple<Location> baseLocTuple, MethodDescriptor mdCallee) {
585
586     CompositeLocation newCalleeCompLoc = new CompositeLocation();
587
588     Location calleeThisLoc = new Location(mdCallee, mdCallee.getThis());
589     newCalleeCompLoc.addLocation(calleeThisLoc);
590
591     // remove the base tuple from the caller
592     // ex; In the method invoation foo.bar.methodA(), the callee will have the composite location
593     // ,which is relative to the 'this' variable, <THIS,...>
594     for (int i = baseLocTuple.size(); i < callerCompLoc.getSize(); i++) {
595       newCalleeCompLoc.addLocation(callerCompLoc.get(i));
596     }
597
598     return newCalleeCompLoc;
599
600   }
601
602   private void calculateGlobalValueFlowCompositeLocation() {
603
604     System.out.println("SSJAVA: Calculate composite locations in the global value flow graph");
605     MethodDescriptor methodDescEventLoop = ssjava.getMethodContainingSSJavaLoop();
606     GlobalFlowGraph globalFlowGraph = getSubGlobalFlowGraph(methodDescEventLoop);
607
608     Set<Location> calculatedPrefixSet = new HashSet<Location>();
609
610     Set<GlobalFlowNode> nodeSet = globalFlowGraph.getNodeSet();
611
612     next: for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
613       GlobalFlowNode node = (GlobalFlowNode) iterator.next();
614
615       Location prefixLoc = node.getLocTuple().get(0);
616
617       if (calculatedPrefixSet.contains(prefixLoc)) {
618         // the prefix loc has been already assigned to a composite location
619         continue;
620       }
621
622       calculatedPrefixSet.add(prefixLoc);
623
624       // Set<GlobalFlowNode> incomingNodeSet = globalFlowGraph.getIncomingNodeSet(node);
625       List<NTuple<Location>> prefixList = calculatePrefixList(globalFlowGraph, node);
626
627       Set<GlobalFlowNode> reachableNodeSet =
628           globalFlowGraph.getReachableNodeSetByPrefix(node.getLocTuple().get(0));
629       // Set<GlobalFlowNode> reachNodeSet = globalFlowGraph.getReachableNodeSetFrom(node);
630
631       // System.out.println("node=" + node + "    prefixList=" + prefixList + "   reachableNodeSet="
632       // + reachableNodeSet);
633
634       for (int i = 0; i < prefixList.size(); i++) {
635         NTuple<Location> curPrefix = prefixList.get(i);
636         Set<NTuple<Location>> reachableCommonPrefixSet = new HashSet<NTuple<Location>>();
637
638         for (Iterator iterator2 = reachableNodeSet.iterator(); iterator2.hasNext();) {
639           GlobalFlowNode reachNode = (GlobalFlowNode) iterator2.next();
640           if (reachNode.getLocTuple().startsWith(curPrefix)) {
641             reachableCommonPrefixSet.add(reachNode.getLocTuple());
642           }
643         }
644
645         if (!reachableCommonPrefixSet.isEmpty()) {
646
647           MethodDescriptor curPrefixFirstElementMethodDesc =
648               (MethodDescriptor) curPrefix.get(0).getDescriptor();
649
650           MethodDescriptor nodePrefixLocFirstElementMethodDesc =
651               (MethodDescriptor) prefixLoc.getDescriptor();
652
653           if (curPrefixFirstElementMethodDesc.equals(nodePrefixLocFirstElementMethodDesc)
654               || isTransitivelyCalledFrom(nodePrefixLocFirstElementMethodDesc,
655                   curPrefixFirstElementMethodDesc)) {
656
657             // TODO
658             // if (!node.getLocTuple().startsWith(curPrefix.get(0))) {
659
660             Location curPrefixLocalLoc = curPrefix.get(0);
661             if (globalFlowGraph.mapLocationToInferCompositeLocation.containsKey(curPrefixLocalLoc)) {
662               // in this case, the local variable of the current prefix has already got a composite
663               // location
664               // so we just ignore the current composite location.
665
666               // System.out.println("HERE WE DO NOT ASSIGN A COMPOSITE LOCATION TO =" + node
667               // + " DUE TO " + curPrefix);
668
669               continue next;
670             }
671
672             Location targetLocalLoc = node.getLocTuple().get(0);
673             // CompositeLocation curCompLoc = globalFlowGraph.getCompositeLocation(targetLocalLoc);
674             // if ((curPrefix.size() + 1) > curCompLoc.getSize()) {
675
676             CompositeLocation newCompLoc = generateCompositeLocation(curPrefix);
677             System.out.println("NEED TO ASSIGN COMP LOC TO " + node + " with prefix=" + curPrefix);
678             System.out.println("- newCompLoc=" + newCompLoc);
679             globalFlowGraph.addMapLocationToInferCompositeLocation(targetLocalLoc, newCompLoc);
680             // }
681
682             continue next;
683             // }
684
685           }
686
687         }
688
689       }
690
691     }
692     // Set<GlobalFlowNode> inNodeSet =
693     // graph.getIncomingNodeSetWithPrefix(prefix);
694     // System.out.println("inNodeSet=" + inNodeSet + "  from=" + node);
695   }
696
697   private void assignCompositeLocation(CompositeLocation compLocPrefix, GlobalFlowNode node) {
698     CompositeLocation newCompLoc = compLocPrefix.clone();
699     NTuple<Location> locTuple = node.getLocTuple();
700     for (int i = 1; i < locTuple.size(); i++) {
701       newCompLoc.addLocation(locTuple.get(i));
702     }
703     node.setInferCompositeLocation(newCompLoc);
704   }
705
706   private List<NTuple<Location>> calculatePrefixList(GlobalFlowGraph graph, GlobalFlowNode node) {
707
708     System.out.println("\n##### calculatePrefixList node=" + node);
709
710     Set<GlobalFlowNode> incomingNodeSetPrefix =
711         graph.getIncomingNodeSetByPrefix(node.getLocTuple().get(0));
712     // System.out.println("incomingNodeSetPrefix=" + incomingNodeSetPrefix);
713     //
714     // Set<GlobalFlowNode> reachableNodeSetPrefix =
715     // graph.getReachableNodeSetByPrefix(node.getLocTuple().get(0));
716     // System.out.println("reachableNodeSetPrefix=" + reachableNodeSetPrefix);
717
718     List<NTuple<Location>> prefixList = new ArrayList<NTuple<Location>>();
719
720     for (Iterator iterator = incomingNodeSetPrefix.iterator(); iterator.hasNext();) {
721       GlobalFlowNode inNode = (GlobalFlowNode) iterator.next();
722       NTuple<Location> inNodeTuple = inNode.getLocTuple();
723
724       for (int i = 1; i < inNodeTuple.size(); i++) {
725         NTuple<Location> prefix = inNodeTuple.subList(0, i);
726         if (!prefixList.contains(prefix)) {
727           prefixList.add(prefix);
728         }
729       }
730     }
731
732     Collections.sort(prefixList, new Comparator<NTuple<Location>>() {
733       public int compare(NTuple<Location> arg0, NTuple<Location> arg1) {
734         int s0 = arg0.size();
735         int s1 = arg1.size();
736         if (s0 > s1) {
737           return -1;
738         } else if (s0 == s1) {
739           return 0;
740         } else {
741           return 1;
742         }
743       }
744     });
745
746     // remove a prefix which is not suitable for generating composite location
747     Location localVarLoc = node.getLocTuple().get(0);
748     MethodDescriptor md = (MethodDescriptor) localVarLoc.getDescriptor();
749     ClassDescriptor cd = md.getClassDesc();
750
751     int idx = 0;
752
753     Set<NTuple<Location>> toberemoved = new HashSet<NTuple<Location>>();
754     for (int i = 0; i < prefixList.size(); i++) {
755       NTuple<Location> prefixLocTuple = prefixList.get(i);
756       if (!containsClassDesc(cd, prefixLocTuple)) {
757         toberemoved.add(prefixLocTuple);
758       }
759     }
760
761     prefixList.removeAll(toberemoved);
762
763     return prefixList;
764
765     // List<NTuple<Location>> prefixList = new ArrayList<NTuple<Location>>();
766     //
767     // for (Iterator iterator = incomingNodeSet.iterator(); iterator.hasNext();) {
768     // GlobalFlowNode inNode = (GlobalFlowNode) iterator.next();
769     // NTuple<Location> inNodeTuple = inNode.getLocTuple();
770     //
771     // for (int i = 1; i < inNodeTuple.size(); i++) {
772     // NTuple<Location> prefix = inNodeTuple.subList(0, i);
773     // if (!prefixList.contains(prefix)) {
774     // prefixList.add(prefix);
775     // }
776     // }
777     // }
778     //
779     // Collections.sort(prefixList, new Comparator<NTuple<Location>>() {
780     // public int compare(NTuple<Location> arg0, NTuple<Location> arg1) {
781     // int s0 = arg0.size();
782     // int s1 = arg1.size();
783     // if (s0 > s1) {
784     // return -1;
785     // } else if (s0 == s1) {
786     // return 0;
787     // } else {
788     // return 1;
789     // }
790     // }
791     // });
792     // return prefixList;
793   }
794
795   private boolean containsClassDesc(ClassDescriptor cd, NTuple<Location> prefixLocTuple) {
796     for (int i = 0; i < prefixLocTuple.size(); i++) {
797       Location loc = prefixLocTuple.get(i);
798       Descriptor locDesc = loc.getLocDescriptor();
799       if (locDesc != null) {
800         ClassDescriptor type = getClassTypeDescriptor(locDesc);
801         if (type != null && type.equals(cd)) {
802           return true;
803         }
804       }
805     }
806     return false;
807   }
808
809   private GlobalFlowGraph constructSubGlobalFlowGraph(FlowGraph flowGraph) {
810
811     MethodDescriptor md = flowGraph.getMethodDescriptor();
812
813     GlobalFlowGraph globalGraph = new GlobalFlowGraph(md);
814
815     // Set<FlowNode> nodeSet = flowGraph.getNodeSet();
816     Set<FlowEdge> edgeSet = flowGraph.getEdgeSet();
817
818     for (Iterator iterator = edgeSet.iterator(); iterator.hasNext();) {
819
820       FlowEdge edge = (FlowEdge) iterator.next();
821       NTuple<Descriptor> srcDescTuple = edge.getInitTuple();
822       NTuple<Descriptor> dstDescTuple = edge.getEndTuple();
823
824       // here only keep the first element(method location) of the descriptor
825       // tuple
826       NTuple<Location> srcLocTuple = translateToLocTuple(md, srcDescTuple);
827       // Location srcMethodLoc = srcLocTuple.get(0);
828       // Descriptor srcVarDesc = srcMethodLoc.getLocDescriptor();
829       // // if (flowGraph.isParamDesc(srcVarDesc) &&
830       // (!srcVarDesc.equals(md.getThis()))) {
831       // if (!srcVarDesc.equals(md.getThis())) {
832       // srcLocTuple = new NTuple<Location>();
833       // Location loc = new Location(md, srcVarDesc);
834       // srcLocTuple.add(loc);
835       // }
836       //
837       NTuple<Location> dstLocTuple = translateToLocTuple(md, dstDescTuple);
838       // Location dstMethodLoc = dstLocTuple.get(0);
839       // Descriptor dstVarDesc = dstMethodLoc.getLocDescriptor();
840       // if (!dstVarDesc.equals(md.getThis())) {
841       // dstLocTuple = new NTuple<Location>();
842       // Location loc = new Location(md, dstVarDesc);
843       // dstLocTuple.add(loc);
844       // }
845
846       globalGraph.addValueFlowEdge(srcLocTuple, dstLocTuple);
847
848     }
849
850     return globalGraph;
851   }
852
853   private NTuple<Location> translateToLocTuple(MethodDescriptor md, NTuple<Descriptor> descTuple) {
854
855     NTuple<Location> locTuple = new NTuple<Location>();
856
857     Descriptor enclosingDesc = md;
858     // System.out.println("md=" + md + "  descTuple=" + descTuple);
859     for (int i = 0; i < descTuple.size(); i++) {
860       Descriptor desc = descTuple.get(i);
861
862       Location loc = new Location(enclosingDesc, desc);
863       locTuple.add(loc);
864
865       if (desc instanceof VarDescriptor) {
866         enclosingDesc = ((VarDescriptor) desc).getType().getClassDesc();
867       } else if (desc instanceof FieldDescriptor) {
868         enclosingDesc = ((FieldDescriptor) desc).getType().getClassDesc();
869       } else {
870         // TODO: inter descriptor case
871         enclosingDesc = desc;
872       }
873
874     }
875
876     return locTuple;
877
878   }
879
880   private void addValueFlowsFromCalleeSubGlobalFlowGraph(MethodDescriptor mdCaller,
881       GlobalFlowGraph subGlobalFlowGraph) {
882
883     // the transformation for a call site propagates flows through parameters
884     // if the method is virtual, it also grab all relations from any possible
885     // callees
886
887     Set<MethodInvokeNode> setMethodInvokeNode = getMethodInvokeNodeSet(mdCaller);
888
889     for (Iterator iterator = setMethodInvokeNode.iterator(); iterator.hasNext();) {
890       MethodInvokeNode min = (MethodInvokeNode) iterator.next();
891       MethodDescriptor mdCallee = min.getMethod();
892       Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
893       if (mdCallee.isStatic()) {
894         setPossibleCallees.add(mdCallee);
895       } else {
896         Set<MethodDescriptor> calleeSet = ssjava.getCallGraph().getMethods(mdCallee);
897         // removes method descriptors that are not invoked by the caller
898         calleeSet.retainAll(mapMethodToCalleeSet.get(mdCaller));
899         setPossibleCallees.addAll(calleeSet);
900       }
901
902       for (Iterator iterator2 = setPossibleCallees.iterator(); iterator2.hasNext();) {
903         MethodDescriptor possibleMdCallee = (MethodDescriptor) iterator2.next();
904         propagateValueFlowsToCallerFromSubGlobalFlowGraph(min, mdCaller, possibleMdCallee);
905       }
906
907     }
908
909   }
910
911   private void propagateValueFlowsToCallerFromSubGlobalFlowGraph(MethodInvokeNode min,
912       MethodDescriptor mdCaller, MethodDescriptor possibleMdCallee) {
913
914     System.out.println("---propagate from " + min.printNode(0) + " to caller=" + mdCaller);
915     FlowGraph calleeFlowGraph = getFlowGraph(possibleMdCallee);
916     Map<Integer, NTuple<Descriptor>> mapIdxToArg = mapMethodInvokeNodeToArgIdxMap.get(min);
917
918     System.out.println("-----mapMethodInvokeNodeToArgIdxMap.get(min)="
919         + mapMethodInvokeNodeToArgIdxMap.get(min));
920     Set<Integer> keySet = mapIdxToArg.keySet();
921     for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
922       Integer idx = (Integer) iterator.next();
923       NTuple<Descriptor> argDescTuple = mapIdxToArg.get(idx);
924       if (argDescTuple.size() > 0) {
925         NTuple<Location> argLocTuple = translateToLocTuple(mdCaller, argDescTuple);
926         NTuple<Descriptor> paramDescTuple = calleeFlowGraph.getParamFlowNode(idx).getDescTuple();
927         NTuple<Location> paramLocTuple = translateToLocTuple(possibleMdCallee, paramDescTuple);
928         addMapCallerArgToCalleeParam(min, argDescTuple, paramDescTuple);
929       }
930     }
931
932     NTuple<Descriptor> baseTuple = mapMethodInvokeNodeToBaseTuple.get(min);
933     GlobalFlowGraph calleeSubGlobalGraph = getSubGlobalFlowGraph(possibleMdCallee);
934     Set<GlobalFlowNode> calleeNodeSet = calleeSubGlobalGraph.getNodeSet();
935     for (Iterator iterator = calleeNodeSet.iterator(); iterator.hasNext();) {
936       GlobalFlowNode calleeNode = (GlobalFlowNode) iterator.next();
937       addValueFlowFromCalleeNode(min, mdCaller, possibleMdCallee, calleeNode);
938     }
939
940     // int numParam = calleeFlowGraph.getNumParameters();
941     // for (int idx = 0; idx < numParam; idx++) {
942     //
943     // FlowNode paramNode = calleeFlowGraph.getParamFlowNode(idx);
944     //
945     // NTuple<Location> paramLocTuple =
946     // translateToLocTuple(possibleMdCallee, paramNode.getCurrentDescTuple());
947     //
948     // GlobalFlowNode globalParamNode =
949     // calleeSubGlobalGraph.getFlowNode(paramLocTuple);
950     //
951     // NTuple<Descriptor> argTuple =
952     // mapMethodInvokeNodeToArgIdxMap.get(min).get(idx);
953     //
954     // NTuple<Location> argLocTuple = translateToLocTuple(mdCaller, argTuple);
955     //
956     // System.out.println("argTupleSet=" + argLocTuple + "   param=" +
957     // paramLocTuple);
958     // // here, it adds all value flows reachable from the paramNode in the
959     // callee's flow graph
960     //
961     // addValueFlowsFromCalleeParam(mdCaller, argLocTuple, baseLocTuple,
962     // possibleMdCallee,
963     // globalParamNode);
964     // }
965     //
966     // // TODO
967     // // FlowGraph callerSubGlobalGraph = getSubGlobalFlowGraph(mdCaller);
968     // // FlowGraph calleeSubGlobalGraph =
969     // getSubGlobalFlowGraph(possibleMdCallee);
970     // //
971     // // int numParam = calleeSubGlobalGraph.getNumParameters();
972     // // for (int idx = 0; idx < numParam; idx++) {
973     // // FlowNode paramNode = calleeSubGlobalGraph.getParamFlowNode(idx);
974     // // NTuple<Descriptor> argTuple =
975     // mapMethodInvokeNodeToArgIdxMap.get(min).get(idx);
976     // // System.out.println("argTupleSet=" + argTuple + "   param=" +
977     // paramNode);
978     // // // here, it adds all value flows reachable from the paramNode in the
979     // callee's flow graph
980     // // addValueFlowsFromCalleeParam(min, calleeSubGlobalGraph, paramNode,
981     // callerSubGlobalGraph,
982     // // argTuple, baseTuple);
983     // // }
984
985   }
986
987   private void addValueFlowFromCalleeNode(MethodInvokeNode min, MethodDescriptor mdCaller,
988       MethodDescriptor mdCallee, GlobalFlowNode calleeSrcNode) {
989
990     GlobalFlowGraph calleeSubGlobalGraph = getSubGlobalFlowGraph(mdCallee);
991     GlobalFlowGraph callerSubGlobalGraph = getSubGlobalFlowGraph(mdCaller);
992
993     NTuple<Location> callerSrcNodeLocTuple =
994         translateToCallerLocTuple(min, mdCallee, mdCaller, calleeSrcNode.getLocTuple());
995
996     if (callerSrcNodeLocTuple != null) {
997       Set<GlobalFlowNode> outNodeSet = calleeSubGlobalGraph.getOutNodeSet(calleeSrcNode);
998
999       for (Iterator iterator = outNodeSet.iterator(); iterator.hasNext();) {
1000         GlobalFlowNode outNode = (GlobalFlowNode) iterator.next();
1001         NTuple<Location> callerDstNodeLocTuple =
1002             translateToCallerLocTuple(min, mdCallee, mdCaller, outNode.getLocTuple());
1003         if (callerDstNodeLocTuple != null) {
1004           callerSubGlobalGraph.addValueFlowEdge(callerSrcNodeLocTuple, callerDstNodeLocTuple);
1005         }
1006       }
1007     }
1008
1009   }
1010
1011   private NTuple<Location> translateToCallerLocTuple(MethodInvokeNode min,
1012       MethodDescriptor mdCallee, MethodDescriptor mdCaller, NTuple<Location> nodeLocTuple) {
1013     // this method will return the same nodeLocTuple if the corresponding argument is literal
1014     // value.
1015
1016     FlowGraph calleeFlowGraph = getFlowGraph(mdCallee);
1017
1018     NTuple<Descriptor> nodeDescTuple = translateToDescTuple(nodeLocTuple);
1019     if (calleeFlowGraph.isParameter(nodeDescTuple)) {
1020       int paramIdx = calleeFlowGraph.getParamIdx(nodeDescTuple);
1021       NTuple<Descriptor> argDescTuple = mapMethodInvokeNodeToArgIdxMap.get(min).get(paramIdx);
1022
1023       if (isPrimitive(nodeLocTuple.get(0).getLocDescriptor())) {
1024         // the type of argument is primitive.
1025         return nodeLocTuple.clone();
1026       }
1027       NTuple<Location> argLocTuple = translateToLocTuple(mdCaller, argDescTuple);
1028
1029       NTuple<Location> callerLocTuple = new NTuple<Location>();
1030
1031       callerLocTuple.addAll(argLocTuple);
1032       for (int i = 1; i < nodeLocTuple.size(); i++) {
1033         callerLocTuple.add(nodeLocTuple.get(i));
1034       }
1035       return callerLocTuple;
1036     } else {
1037       return nodeLocTuple.clone();
1038     }
1039
1040   }
1041
1042   public static boolean isPrimitive(Descriptor desc) {
1043
1044     if (desc instanceof FieldDescriptor) {
1045       return ((FieldDescriptor) desc).getType().isPrimitive();
1046     } else if (desc instanceof VarDescriptor) {
1047       return ((VarDescriptor) desc).getType().isPrimitive();
1048     } else if (desc instanceof InterDescriptor) {
1049       return true;
1050     }
1051
1052     return false;
1053   }
1054
1055   private NTuple<Descriptor> translateToDescTuple(NTuple<Location> locTuple) {
1056
1057     NTuple<Descriptor> descTuple = new NTuple<Descriptor>();
1058     for (int i = 0; i < locTuple.size(); i++) {
1059       descTuple.add(locTuple.get(i).getLocDescriptor());
1060     }
1061     return descTuple;
1062
1063   }
1064
1065   private void addValueFlowsFromCalleeParam(MethodDescriptor mdCaller,
1066       NTuple<Location> argLocTuple, NTuple<Location> baseLocTuple, MethodDescriptor mdCallee,
1067       GlobalFlowNode globalParamNode) {
1068
1069     Set<GlobalFlowNode> visited = new HashSet<GlobalFlowNode>();
1070     visited.add(globalParamNode);
1071     recurAddValueFlowsFromCalleeParam(mdCaller, argLocTuple, baseLocTuple, mdCallee,
1072         globalParamNode);
1073
1074   }
1075
1076   private void recurAddValueFlowsFromCalleeParam(MethodDescriptor mdCaller,
1077       NTuple<Location> argLocTuple, NTuple<Location> baseLocTuple, MethodDescriptor mdCallee,
1078       GlobalFlowNode calleeCurNode) {
1079
1080     // FlowGraph calleeFlowGraph = getFlowGraph(mdCallee);
1081     // GlobalFlowGraph calleeSubGlobalGraph = getSubGlobalFlowGraph(mdCallee);
1082     //
1083     // NTuple<Location> curNodeLocTuple = calleeCurNode.getLocTuple();
1084     // NTuple<Descriptor> curNodeDescTuple = calleeCurNode.getDescTuple();
1085     // if (calleeFlowGraph.isParameter(curNodeDescTuple)) {
1086     // curNodeLocTuple = translateToCaller(argLocTuple, curNodeLocTuple);
1087     // }
1088     //
1089     // Set<GlobalFlowNode> outNodeSet =
1090     // calleeSubGlobalGraph.getOutNodeSet(calleeCurNode);
1091     // for (Iterator iterator = outNodeSet.iterator(); iterator.hasNext();) {
1092     // GlobalFlowNode outNode = (GlobalFlowNode) iterator.next();
1093     //
1094     // NTuple<Location> curNodeLocTuple = calleeCurNode.getLocTuple();
1095     // NTuple<Descriptor> curNodeDescTuple = calleeCurNode.getDescTuple();
1096     // if (calleeFlowGraph.isParameter(curNodeDescTuple)) {
1097     // curNodeLocTuple = translateToCaller(argLocTuple, curNodeLocTuple);
1098     // }
1099     //
1100     // outNode.getDescTuple();
1101     //
1102     // if (calleeFlowGraph.is)
1103     //
1104     // if (calleeSubGlobalGraph.isParameter(srcDescTuple)) {
1105     // // destination node is started with 'parameter'
1106     // // need to translate it in terms of the caller's a node
1107     // srcDescTuple =
1108     // translateToCaller(min, calleeSubGlobalGraph.getParamIdx(srcDescTuple),
1109     // srcDescTuple);
1110     // }
1111     //
1112     // }
1113     //
1114     // Set<FlowEdge> edgeSet =
1115     // calleeSubGlobalGraph.getOutEdgeSetStartingFrom(calleeSrcNode);
1116     // for (Iterator iterator = edgeSet.iterator(); iterator.hasNext();) {
1117     // FlowEdge flowEdge = (FlowEdge) iterator.next();
1118     //
1119     // NTuple<Descriptor> srcDescTuple = flowEdge.getInitTuple();
1120     // NTuple<Descriptor> dstDescTuple = flowEdge.getEndTuple();
1121     //
1122     // FlowNode dstNode = calleeSubGlobalGraph.getFlowNode(dstDescTuple);
1123     //
1124     // if (calleeSubGlobalGraph.isParameter(srcDescTuple)) {
1125     // // destination node is started with 'parameter'
1126     // // need to translate it in terms of the caller's a node
1127     // srcDescTuple =
1128     // translateToCaller(min, calleeSubGlobalGraph.getParamIdx(srcDescTuple),
1129     // srcDescTuple);
1130     // }
1131     //
1132     // if (calleeSubGlobalGraph.isParameter(dstDescTuple)) {
1133     // // destination node is started with 'parameter'
1134     // // need to translate it in terms of the caller's a node
1135     // dstDescTuple =
1136     // translateToCaller(min, calleeSubGlobalGraph.getParamIdx(dstDescTuple),
1137     // dstDescTuple);
1138     // }
1139     //
1140     // callerSubGlobalGraph.addValueFlowEdge(srcDescTuple, dstDescTuple);
1141     //
1142     // if (!visited.contains(dstNode)) {
1143     // visited.add(dstNode);
1144     // recurAddValueFlowsFromCalleeParam(min, calleeSubGlobalGraph, dstNode,
1145     // callerSubGlobalGraph,
1146     // dstDescTuple, visited, baseTuple);
1147     // }
1148     //
1149     // }
1150
1151   }
1152
1153   private NTuple<Location> translateToCaller(NTuple<Location> argLocTuple,
1154       NTuple<Location> curNodeLocTuple) {
1155
1156     NTuple<Location> callerLocTuple = new NTuple<Location>();
1157
1158     callerLocTuple.addAll(argLocTuple);
1159     for (int i = 1; i < curNodeLocTuple.size(); i++) {
1160       callerLocTuple.add(curNodeLocTuple.get(i));
1161     }
1162
1163     return callerLocTuple;
1164   }
1165
1166   private void recurAddValueFlowsFromCalleeParam(MethodInvokeNode min,
1167       FlowGraph calleeSubGlobalGraph, FlowNode calleeSrcNode, FlowGraph callerSubGlobalGraph,
1168       NTuple<Descriptor> callerSrcTuple, Set<FlowNode> visited, NTuple<Descriptor> baseTuple) {
1169
1170     MethodDescriptor mdCallee = calleeSubGlobalGraph.getMethodDescriptor();
1171
1172     // Set<FlowEdge> edgeSet =
1173     // calleeSubGlobalGraph.getOutEdgeSet(calleeSrcNode);
1174     Set<FlowEdge> edgeSet = calleeSubGlobalGraph.getOutEdgeSetStartingFrom(calleeSrcNode);
1175     for (Iterator iterator = edgeSet.iterator(); iterator.hasNext();) {
1176       FlowEdge flowEdge = (FlowEdge) iterator.next();
1177
1178       NTuple<Descriptor> srcDescTuple = flowEdge.getInitTuple();
1179       NTuple<Descriptor> dstDescTuple = flowEdge.getEndTuple();
1180
1181       FlowNode dstNode = calleeSubGlobalGraph.getFlowNode(dstDescTuple);
1182
1183       if (calleeSubGlobalGraph.isParameter(srcDescTuple)) {
1184         // destination node is started with 'parameter'
1185         // need to translate it in terms of the caller's a node
1186         srcDescTuple =
1187             translateToCaller(min, calleeSubGlobalGraph.getParamIdx(srcDescTuple), srcDescTuple);
1188       }
1189
1190       if (calleeSubGlobalGraph.isParameter(dstDescTuple)) {
1191         // destination node is started with 'parameter'
1192         // need to translate it in terms of the caller's a node
1193         dstDescTuple =
1194             translateToCaller(min, calleeSubGlobalGraph.getParamIdx(dstDescTuple), dstDescTuple);
1195       }
1196
1197       callerSubGlobalGraph.addValueFlowEdge(srcDescTuple, dstDescTuple);
1198
1199       if (!visited.contains(dstNode)) {
1200         visited.add(dstNode);
1201         recurAddValueFlowsFromCalleeParam(min, calleeSubGlobalGraph, dstNode, callerSubGlobalGraph,
1202             dstDescTuple, visited, baseTuple);
1203       }
1204
1205     }
1206
1207   }
1208
1209   private NTuple<Descriptor> translateToCaller(MethodInvokeNode min, int paramIdx,
1210       NTuple<Descriptor> srcDescTuple) {
1211
1212     NTuple<Descriptor> callerTuple = new NTuple<Descriptor>();
1213
1214     NTuple<Descriptor> argTuple = mapMethodInvokeNodeToArgIdxMap.get(min).get(paramIdx);
1215
1216     for (int i = 0; i < argTuple.size(); i++) {
1217       callerTuple.add(argTuple.get(i));
1218     }
1219
1220     for (int i = 1; i < srcDescTuple.size(); i++) {
1221       callerTuple.add(srcDescTuple.get(i));
1222     }
1223
1224     return callerTuple;
1225   }
1226
1227   private NTuple<Descriptor> traslateToCalleeParamTupleToCallerArgTuple(
1228       NTuple<Descriptor> calleeInitTuple, NTuple<Descriptor> callerSrcTuple) {
1229
1230     NTuple<Descriptor> callerInitTuple = new NTuple<Descriptor>();
1231
1232     for (int i = 0; i < callerSrcTuple.size(); i++) {
1233       callerInitTuple.add(callerSrcTuple.get(i));
1234     }
1235
1236     for (int i = 1; i < calleeInitTuple.size(); i++) {
1237       callerInitTuple.add(calleeInitTuple.get(i));
1238     }
1239
1240     return callerInitTuple;
1241   }
1242
1243   public LocationSummary getLocationSummary(Descriptor d) {
1244     if (!mapDescToLocationSummary.containsKey(d)) {
1245       if (d instanceof MethodDescriptor) {
1246         mapDescToLocationSummary.put(d, new MethodSummary((MethodDescriptor) d));
1247       } else if (d instanceof ClassDescriptor) {
1248         mapDescToLocationSummary.put(d, new FieldSummary());
1249       }
1250     }
1251     return mapDescToLocationSummary.get(d);
1252   }
1253
1254   private void generateMethodSummary() {
1255
1256     Set<MethodDescriptor> keySet = md2lattice.keySet();
1257     for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1258       MethodDescriptor md = (MethodDescriptor) iterator.next();
1259
1260       System.out.println("\nSSJAVA: generate method summary: " + md);
1261
1262       FlowGraph flowGraph = getFlowGraph(md);
1263       MethodSummary methodSummary = getMethodSummary(md);
1264
1265       HierarchyGraph scGraph = getSkeletonCombinationHierarchyGraph(md);
1266
1267       // set the 'this' reference location
1268       if (!md.isStatic()) {
1269         System.out.println("setThisLocName=" + scGraph.getHNode(md.getThis()).getName());
1270         methodSummary.setThisLocName(scGraph.getHNode(md.getThis()).getName());
1271       }
1272
1273       // set the 'global' reference location if needed
1274       if (methodSummary.hasGlobalAccess()) {
1275         methodSummary.setGlobalLocName(scGraph.getHNode(GLOBALDESC).getName());
1276       }
1277
1278       // construct a parameter mapping that maps a parameter descriptor to an
1279       // inferred composite location
1280       for (int paramIdx = 0; paramIdx < flowGraph.getNumParameters(); paramIdx++) {
1281         FlowNode flowNode = flowGraph.getParamFlowNode(paramIdx);
1282         CompositeLocation inferredCompLoc = flowNode.getCompositeLocation();
1283         // NTuple<Descriptor> descTuple = flowNode.getDescTuple();
1284         //
1285         // CompositeLocation assignedCompLoc = flowNode.getCompositeLocation();
1286         // CompositeLocation inferredCompLoc;
1287         // if (assignedCompLoc != null) {
1288         // inferredCompLoc = translateCompositeLocation(assignedCompLoc);
1289         // } else {
1290         // Descriptor locDesc = descTuple.get(0);
1291         // Location loc = new Location(md, locDesc.getSymbol());
1292         // loc.setLocDescriptor(locDesc);
1293         // inferredCompLoc = new CompositeLocation(loc);
1294         // }
1295         System.out.println("-paramIdx=" + paramIdx + "   infer=" + inferredCompLoc);
1296         System.out.println("-flowNode inferLoc=" + flowNode.getCompositeLocation());
1297
1298         Descriptor localVarDesc = flowNode.getDescTuple().get(0);
1299         methodSummary.addMapVarNameToInferCompLoc(localVarDesc, inferredCompLoc);
1300         methodSummary.addMapParamIdxToInferLoc(paramIdx, inferredCompLoc);
1301       }
1302
1303     }
1304
1305   }
1306
1307   private boolean hasOrderingRelation(NTuple<Location> locTuple1, NTuple<Location> locTuple2) {
1308
1309     int size = locTuple1.size() >= locTuple2.size() ? locTuple2.size() : locTuple1.size();
1310
1311     for (int idx = 0; idx < size; idx++) {
1312       Location loc1 = locTuple1.get(idx);
1313       Location loc2 = locTuple2.get(idx);
1314
1315       Descriptor desc1 = loc1.getDescriptor();
1316       Descriptor desc2 = loc2.getDescriptor();
1317
1318       if (!desc1.equals(desc2)) {
1319         throw new Error("Fail to compare " + locTuple1 + " and " + locTuple2);
1320       }
1321
1322       Descriptor locDesc1 = loc1.getLocDescriptor();
1323       Descriptor locDesc2 = loc2.getLocDescriptor();
1324
1325       HierarchyGraph hierarchyGraph = getHierarchyGraph(desc1);
1326
1327       HNode node1 = hierarchyGraph.getHNode(locDesc1);
1328       HNode node2 = hierarchyGraph.getHNode(locDesc2);
1329
1330       System.out.println("---node1=" + node1 + "  node2=" + node2);
1331       System.out.println("---hierarchyGraph.getIncomingNodeSet(node2)="
1332           + hierarchyGraph.getIncomingNodeSet(node2));
1333
1334       if (locDesc1.equals(locDesc2)) {
1335         continue;
1336       } else if (!hierarchyGraph.getIncomingNodeSet(node2).contains(node1)
1337           && !hierarchyGraph.getIncomingNodeSet(node1).contains(node2)) {
1338         return false;
1339       } else {
1340         return true;
1341       }
1342
1343     }
1344
1345     return false;
1346
1347   }
1348
1349   private boolean isHigherThan(NTuple<Location> locTuple1, NTuple<Location> locTuple2) {
1350
1351     int size = locTuple1.size() >= locTuple2.size() ? locTuple2.size() : locTuple1.size();
1352
1353     for (int idx = 0; idx < size; idx++) {
1354       Location loc1 = locTuple1.get(idx);
1355       Location loc2 = locTuple2.get(idx);
1356
1357       Descriptor desc1 = loc1.getDescriptor();
1358       Descriptor desc2 = loc2.getDescriptor();
1359
1360       if (!desc1.equals(desc2)) {
1361         throw new Error("Fail to compare " + locTuple1 + " and " + locTuple2);
1362       }
1363
1364       Descriptor locDesc1 = loc1.getLocDescriptor();
1365       Descriptor locDesc2 = loc2.getLocDescriptor();
1366
1367       HierarchyGraph hierarchyGraph = getHierarchyGraph(desc1);
1368
1369       HNode node1 = hierarchyGraph.getHNode(locDesc1);
1370       HNode node2 = hierarchyGraph.getHNode(locDesc2);
1371
1372       System.out.println("---node1=" + node1 + "  node2=" + node2);
1373       System.out.println("---hierarchyGraph.getIncomingNodeSet(node2)="
1374           + hierarchyGraph.getIncomingNodeSet(node2));
1375
1376       if (locDesc1.equals(locDesc2)) {
1377         continue;
1378       } else if (hierarchyGraph.getIncomingNodeSet(node2).contains(node1)) {
1379         return true;
1380       } else {
1381         return false;
1382       }
1383
1384     }
1385
1386     return false;
1387   }
1388
1389   private CompositeLocation translateCompositeLocation(CompositeLocation compLoc) {
1390     CompositeLocation newCompLoc = new CompositeLocation();
1391
1392     // System.out.println("compLoc=" + compLoc);
1393     for (int i = 0; i < compLoc.getSize(); i++) {
1394       Location loc = compLoc.get(i);
1395       Descriptor enclosingDescriptor = loc.getDescriptor();
1396       Descriptor locDescriptor = loc.getLocDescriptor();
1397
1398       HNode hnode = getHierarchyGraph(enclosingDescriptor).getHNode(locDescriptor);
1399       // System.out.println("-hnode=" + hnode + "    from=" + locDescriptor +
1400       // " enclosingDescriptor="
1401       // + enclosingDescriptor);
1402       // System.out.println("-getLocationSummary(enclosingDescriptor)="
1403       // + getLocationSummary(enclosingDescriptor));
1404       String locName = getLocationSummary(enclosingDescriptor).getLocationName(hnode.getName());
1405       // System.out.println("-locName=" + locName);
1406       Location newLoc = new Location(enclosingDescriptor, locName);
1407       newLoc.setLocDescriptor(locDescriptor);
1408       newCompLoc.addLocation(newLoc);
1409     }
1410
1411     return newCompLoc;
1412   }
1413
1414   private void debug_writeLattices() {
1415
1416     Set<Descriptor> keySet = mapDescriptorToSimpleLattice.keySet();
1417     for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1418       Descriptor key = (Descriptor) iterator.next();
1419       SSJavaLattice<String> simpleLattice = mapDescriptorToSimpleLattice.get(key);
1420       // HierarchyGraph simpleHierarchyGraph = getSimpleHierarchyGraph(key);
1421       HierarchyGraph scHierarchyGraph = getSkeletonCombinationHierarchyGraph(key);
1422       if (key instanceof ClassDescriptor) {
1423         writeInferredLatticeDotFile((ClassDescriptor) key, scHierarchyGraph, simpleLattice,
1424             "_SIMPLE");
1425       } else if (key instanceof MethodDescriptor) {
1426         MethodDescriptor md = (MethodDescriptor) key;
1427         writeInferredLatticeDotFile(md.getClassDesc(), md, scHierarchyGraph, simpleLattice,
1428             "_SIMPLE");
1429       }
1430
1431       LocationSummary ls = getLocationSummary(key);
1432       System.out.println("####LOC SUMMARY=" + key + "\n" + ls.getMapHNodeNameToLocationName());
1433     }
1434
1435     Set<ClassDescriptor> cdKeySet = cd2lattice.keySet();
1436     for (Iterator iterator = cdKeySet.iterator(); iterator.hasNext();) {
1437       ClassDescriptor cd = (ClassDescriptor) iterator.next();
1438       writeInferredLatticeDotFile((ClassDescriptor) cd, getSkeletonCombinationHierarchyGraph(cd),
1439           cd2lattice.get(cd), "");
1440     }
1441
1442     Set<MethodDescriptor> mdKeySet = md2lattice.keySet();
1443     for (Iterator iterator = mdKeySet.iterator(); iterator.hasNext();) {
1444       MethodDescriptor md = (MethodDescriptor) iterator.next();
1445       writeInferredLatticeDotFile(md.getClassDesc(), md, getSkeletonCombinationHierarchyGraph(md),
1446           md2lattice.get(md), "");
1447     }
1448
1449   }
1450
1451   private void buildLattice() {
1452
1453     BuildLattice buildLattice = new BuildLattice(this);
1454
1455     Set<Descriptor> keySet = mapDescriptorToCombineSkeletonHierarchyGraph.keySet();
1456     for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1457       Descriptor desc = (Descriptor) iterator.next();
1458
1459       SSJavaLattice<String> simpleLattice = buildLattice.buildLattice(desc);
1460
1461       addMapDescToSimpleLattice(desc, simpleLattice);
1462
1463       HierarchyGraph simpleHierarchyGraph = getSimpleHierarchyGraph(desc);
1464       System.out.println("\n## insertIntermediateNodesToStraightLine:"
1465           + simpleHierarchyGraph.getName());
1466       SSJavaLattice<String> lattice =
1467           buildLattice.insertIntermediateNodesToStraightLine(desc, simpleLattice);
1468       lattice.removeRedundantEdges();
1469
1470       if (desc instanceof ClassDescriptor) {
1471         // field lattice
1472         cd2lattice.put((ClassDescriptor) desc, lattice);
1473         // ssjava.writeLatticeDotFile((ClassDescriptor) desc, null, lattice);
1474       } else if (desc instanceof MethodDescriptor) {
1475         // method lattice
1476         md2lattice.put((MethodDescriptor) desc, lattice);
1477         MethodDescriptor md = (MethodDescriptor) desc;
1478         ClassDescriptor cd = md.getClassDesc();
1479         // ssjava.writeLatticeDotFile(cd, md, lattice);
1480       }
1481
1482       // System.out.println("\nSSJAVA: Insering Combination Nodes:" + desc);
1483       // HierarchyGraph skeletonGraph = getSkeletonHierarchyGraph(desc);
1484       // HierarchyGraph skeletonGraphWithCombinationNode =
1485       // skeletonGraph.clone();
1486       // skeletonGraphWithCombinationNode.setName(desc + "_SC");
1487       //
1488       // HierarchyGraph simpleHierarchyGraph = getSimpleHierarchyGraph(desc);
1489       // System.out.println("Identifying Combination Nodes:");
1490       // skeletonGraphWithCombinationNode.insertCombinationNodesToGraph(simpleHierarchyGraph);
1491       // skeletonGraphWithCombinationNode.simplifySkeletonCombinationHierarchyGraph();
1492       // mapDescriptorToCombineSkeletonHierarchyGraph.put(desc,
1493       // skeletonGraphWithCombinationNode);
1494     }
1495
1496   }
1497
1498   public void addMapDescToSimpleLattice(Descriptor desc, SSJavaLattice<String> lattice) {
1499     mapDescriptorToSimpleLattice.put(desc, lattice);
1500   }
1501
1502   public SSJavaLattice<String> getSimpleLattice(Descriptor desc) {
1503     return mapDescriptorToSimpleLattice.get(desc);
1504   }
1505
1506   private void simplifyHierarchyGraph() {
1507     Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
1508     for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1509       Descriptor desc = (Descriptor) iterator.next();
1510       HierarchyGraph simpleHierarchyGraph = getHierarchyGraph(desc).clone();
1511       simpleHierarchyGraph.setName(desc + "_SIMPLE");
1512       simpleHierarchyGraph.removeRedundantEdges();
1513       // simpleHierarchyGraph.simplifyHierarchyGraph();
1514       mapDescriptorToSimpleHierarchyGraph.put(desc, simpleHierarchyGraph);
1515     }
1516   }
1517
1518   private void insertCombinationNodes() {
1519     Set<Descriptor> keySet = mapDescriptorToSkeletonHierarchyGraph.keySet();
1520     for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1521       Descriptor desc = (Descriptor) iterator.next();
1522       System.out.println("\nSSJAVA: Insering Combination Nodes:" + desc);
1523       HierarchyGraph skeletonGraph = getSkeletonHierarchyGraph(desc);
1524       HierarchyGraph skeletonGraphWithCombinationNode = skeletonGraph.clone();
1525       skeletonGraphWithCombinationNode.setName(desc + "_SC");
1526
1527       HierarchyGraph simpleHierarchyGraph = getSimpleHierarchyGraph(desc);
1528       System.out.println("Identifying Combination Nodes:");
1529       skeletonGraphWithCombinationNode.insertCombinationNodesToGraph(simpleHierarchyGraph);
1530       skeletonGraphWithCombinationNode.simplifySkeletonCombinationHierarchyGraph();
1531       mapDescriptorToCombineSkeletonHierarchyGraph.put(desc, skeletonGraphWithCombinationNode);
1532     }
1533   }
1534
1535   private void constructSkeletonHierarchyGraph() {
1536     Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
1537     for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1538       Descriptor desc = (Descriptor) iterator.next();
1539       HierarchyGraph simpleGraph = getSimpleHierarchyGraph(desc);
1540       HierarchyGraph skeletonGraph = simpleGraph.generateSkeletonGraph();
1541       skeletonGraph.setMapDescToHNode(simpleGraph.getMapDescToHNode());
1542       skeletonGraph.setMapHNodeToDescSet(simpleGraph.getMapHNodeToDescSet());
1543       skeletonGraph.simplifyHierarchyGraph();
1544       // skeletonGraph.combineRedundantNodes(false);
1545       // skeletonGraph.removeRedundantEdges();
1546       mapDescriptorToSkeletonHierarchyGraph.put(desc, skeletonGraph);
1547     }
1548   }
1549
1550   private void debug_writeHierarchyDotFiles() {
1551
1552     Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
1553     for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1554       Descriptor desc = (Descriptor) iterator.next();
1555       getHierarchyGraph(desc).writeGraph();
1556     }
1557
1558   }
1559
1560   private void debug_writeSimpleHierarchyDotFiles() {
1561
1562     Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
1563     for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1564       Descriptor desc = (Descriptor) iterator.next();
1565       getHierarchyGraph(desc).writeGraph();
1566       getSimpleHierarchyGraph(desc).writeGraph();
1567     }
1568
1569   }
1570
1571   private void debug_writeSkeletonHierarchyDotFiles() {
1572
1573     Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
1574     for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1575       Descriptor desc = (Descriptor) iterator.next();
1576       getSkeletonHierarchyGraph(desc).writeGraph();
1577     }
1578
1579   }
1580
1581   private void debug_writeSkeletonCombinationHierarchyDotFiles() {
1582
1583     Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
1584     for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1585       Descriptor desc = (Descriptor) iterator.next();
1586       getSkeletonCombinationHierarchyGraph(desc).writeGraph();
1587     }
1588
1589   }
1590
1591   public HierarchyGraph getSimpleHierarchyGraph(Descriptor d) {
1592     return mapDescriptorToSimpleHierarchyGraph.get(d);
1593   }
1594
1595   private HierarchyGraph getSkeletonHierarchyGraph(Descriptor d) {
1596     if (!mapDescriptorToSkeletonHierarchyGraph.containsKey(d)) {
1597       mapDescriptorToSkeletonHierarchyGraph.put(d, new HierarchyGraph(d));
1598     }
1599     return mapDescriptorToSkeletonHierarchyGraph.get(d);
1600   }
1601
1602   public HierarchyGraph getSkeletonCombinationHierarchyGraph(Descriptor d) {
1603     if (!mapDescriptorToCombineSkeletonHierarchyGraph.containsKey(d)) {
1604       mapDescriptorToCombineSkeletonHierarchyGraph.put(d, new HierarchyGraph(d));
1605     }
1606     return mapDescriptorToCombineSkeletonHierarchyGraph.get(d);
1607   }
1608
1609   private void constructHierarchyGraph() {
1610
1611     // do fixed-point analysis
1612
1613     ssjava.init();
1614     LinkedList<MethodDescriptor> descriptorListToAnalyze = ssjava.getSortedDescriptors();
1615
1616     // Collections.sort(descriptorListToAnalyze, new
1617     // Comparator<MethodDescriptor>() {
1618     // public int compare(MethodDescriptor o1, MethodDescriptor o2) {
1619     // return o1.getSymbol().compareToIgnoreCase(o2.getSymbol());
1620     // }
1621     // });
1622
1623     // current descriptors to visit in fixed-point interprocedural analysis,
1624     // prioritized by dependency in the call graph
1625     methodDescriptorsToVisitStack.clear();
1626
1627     Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
1628     methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
1629
1630     while (!descriptorListToAnalyze.isEmpty()) {
1631       MethodDescriptor md = descriptorListToAnalyze.removeFirst();
1632       methodDescriptorsToVisitStack.add(md);
1633     }
1634
1635     // analyze scheduled methods until there are no more to visit
1636     while (!methodDescriptorsToVisitStack.isEmpty()) {
1637       // start to analyze leaf node
1638       MethodDescriptor md = methodDescriptorsToVisitStack.pop();
1639
1640       HierarchyGraph hierarchyGraph = new HierarchyGraph(md);
1641       // MethodSummary methodSummary = new MethodSummary(md);
1642
1643       // MethodLocationInfo methodInfo = new MethodLocationInfo(md);
1644       // curMethodInfo = methodInfo;
1645
1646       System.out.println();
1647       System.out.println("SSJAVA: Construcing the hierarchy graph from " + md);
1648
1649       constructHierarchyGraph(md, hierarchyGraph);
1650
1651       HierarchyGraph prevHierarchyGraph = getHierarchyGraph(md);
1652       // MethodSummary prevMethodSummary = getMethodSummary(md);
1653
1654       if (!hierarchyGraph.equals(prevHierarchyGraph)) {
1655
1656         mapDescriptorToHierarchyGraph.put(md, hierarchyGraph);
1657         // mapDescToLocationSummary.put(md, methodSummary);
1658
1659         // results for callee changed, so enqueue dependents caller for
1660         // further analysis
1661         Iterator<MethodDescriptor> depsItr = ssjava.getDependents(md).iterator();
1662         while (depsItr.hasNext()) {
1663           MethodDescriptor methodNext = depsItr.next();
1664           if (!methodDescriptorsToVisitStack.contains(methodNext)
1665               && methodDescriptorToVistSet.contains(methodNext)) {
1666             methodDescriptorsToVisitStack.add(methodNext);
1667           }
1668         }
1669
1670       }
1671
1672     }
1673
1674     setupToAnalyze();
1675     while (!toAnalyzeIsEmpty()) {
1676       ClassDescriptor cd = toAnalyzeNext();
1677       HierarchyGraph graph = getHierarchyGraph(cd);
1678       for (Iterator iter = cd.getFields(); iter.hasNext();) {
1679         FieldDescriptor fieldDesc = (FieldDescriptor) iter.next();
1680         if (!(fieldDesc.isStatic() && fieldDesc.isFinal())) {
1681           graph.getHNode(fieldDesc);
1682         }
1683       }
1684     }
1685
1686   }
1687
1688   private HierarchyGraph getHierarchyGraph(Descriptor d) {
1689     if (!mapDescriptorToHierarchyGraph.containsKey(d)) {
1690       mapDescriptorToHierarchyGraph.put(d, new HierarchyGraph(d));
1691     }
1692     return mapDescriptorToHierarchyGraph.get(d);
1693   }
1694
1695   private void constructHierarchyGraph(MethodDescriptor md, HierarchyGraph methodGraph) {
1696
1697     // visit each node of method flow graph
1698     FlowGraph fg = getFlowGraph(md);
1699     Set<FlowNode> nodeSet = fg.getNodeSet();
1700
1701     Set<Descriptor> paramDescSet = fg.getMapParamDescToIdx().keySet();
1702     for (Iterator iterator = paramDescSet.iterator(); iterator.hasNext();) {
1703       Descriptor desc = (Descriptor) iterator.next();
1704       methodGraph.getHNode(desc).setSkeleton(true);
1705     }
1706
1707     // for the method lattice, we need to look at the first element of
1708     // NTuple<Descriptor>
1709     boolean hasGlobalAccess = false;
1710     for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
1711       FlowNode srcNode = (FlowNode) iterator.next();
1712
1713       // if the srcNode is started with the global descriptor
1714       // need to set as a skeleton node
1715       if (!hasGlobalAccess && srcNode.getDescTuple().startsWith(GLOBALDESC)) {
1716         hasGlobalAccess = true;
1717       }
1718
1719       Set<FlowEdge> outEdgeSet = fg.getOutEdgeSet(srcNode);
1720       for (Iterator iterator2 = outEdgeSet.iterator(); iterator2.hasNext();) {
1721         FlowEdge outEdge = (FlowEdge) iterator2.next();
1722         FlowNode dstNode = outEdge.getDst();
1723
1724         NTuple<Descriptor> srcNodeTuple = srcNode.getDescTuple();
1725         NTuple<Descriptor> dstNodeTuple = dstNode.getDescTuple();
1726
1727         if (outEdge.getInitTuple().equals(srcNodeTuple)
1728             && outEdge.getEndTuple().equals(dstNodeTuple)) {
1729
1730           NTuple<Descriptor> srcCurTuple = srcNode.getCurrentDescTuple();
1731           NTuple<Descriptor> dstCurTuple = dstNode.getCurrentDescTuple();
1732
1733           if ((srcCurTuple.size() > 1 && dstCurTuple.size() > 1)
1734               && srcCurTuple.get(0).equals(dstCurTuple.get(0))) {
1735
1736             // value flows between fields
1737             Descriptor desc = srcCurTuple.get(0);
1738             ClassDescriptor classDesc;
1739
1740             if (desc.equals(GLOBALDESC)) {
1741               classDesc = md.getClassDesc();
1742             } else {
1743               VarDescriptor varDesc = (VarDescriptor) srcCurTuple.get(0);
1744               classDesc = varDesc.getType().getClassDesc();
1745             }
1746             extractFlowsBetweenFields(classDesc, srcNode, dstNode, 1);
1747
1748           } else {
1749             // value flow between local var - local var or local var - field
1750
1751             Descriptor srcDesc = srcCurTuple.get(0);
1752             Descriptor dstDesc = dstCurTuple.get(0);
1753
1754             methodGraph.addEdge(srcDesc, dstDesc);
1755
1756             if (fg.isParamDesc(srcDesc)) {
1757               methodGraph.setParamHNode(srcDesc);
1758             }
1759             if (fg.isParamDesc(dstDesc)) {
1760               methodGraph.setParamHNode(dstDesc);
1761             }
1762
1763           }
1764
1765         }
1766       }
1767     }
1768
1769     // If the method accesses static fields
1770     // set hasGloabalAccess true in the method summary.
1771     if (hasGlobalAccess) {
1772       getMethodSummary(md).setHasGlobalAccess();
1773     }
1774     methodGraph.getHNode(GLOBALDESC).setSkeleton(true);
1775
1776   }
1777
1778   private MethodSummary getMethodSummary(MethodDescriptor md) {
1779     if (!mapDescToLocationSummary.containsKey(md)) {
1780       mapDescToLocationSummary.put(md, new MethodSummary(md));
1781     }
1782     return (MethodSummary) mapDescToLocationSummary.get(md);
1783   }
1784
1785   private void addMapClassDefinitionToLineNum(ClassDescriptor cd, String strLine, int lineNum) {
1786
1787     String classSymbol = cd.getSymbol();
1788     int idx = classSymbol.lastIndexOf("$");
1789     if (idx != -1) {
1790       classSymbol = classSymbol.substring(idx + 1);
1791     }
1792
1793     String pattern = "class " + classSymbol + " ";
1794     if (strLine.indexOf(pattern) != -1) {
1795       mapDescToDefinitionLine.put(cd, lineNum);
1796     }
1797   }
1798
1799   private void addMapMethodDefinitionToLineNum(Set<MethodDescriptor> methodSet, String strLine,
1800       int lineNum) {
1801     for (Iterator iterator = methodSet.iterator(); iterator.hasNext();) {
1802       MethodDescriptor md = (MethodDescriptor) iterator.next();
1803       String pattern = md.getMethodDeclaration();
1804       if (strLine.indexOf(pattern) != -1) {
1805         mapDescToDefinitionLine.put(md, lineNum);
1806         methodSet.remove(md);
1807         return;
1808       }
1809     }
1810
1811   }
1812
1813   private void readOriginalSourceFiles() {
1814
1815     SymbolTable classtable = state.getClassSymbolTable();
1816
1817     Set<ClassDescriptor> classDescSet = new HashSet<ClassDescriptor>();
1818     classDescSet.addAll(classtable.getValueSet());
1819
1820     try {
1821       // inefficient implement. it may re-visit the same file if the file
1822       // contains more than one class definitions.
1823       for (Iterator iterator = classDescSet.iterator(); iterator.hasNext();) {
1824         ClassDescriptor cd = (ClassDescriptor) iterator.next();
1825
1826         Set<MethodDescriptor> methodSet = new HashSet<MethodDescriptor>();
1827         methodSet.addAll(cd.getMethodTable().getValueSet());
1828
1829         String sourceFileName = cd.getSourceFileName();
1830         Vector<String> lineVec = new Vector<String>();
1831
1832         mapFileNameToLineVector.put(sourceFileName, lineVec);
1833
1834         BufferedReader in = new BufferedReader(new FileReader(sourceFileName));
1835         String strLine;
1836         int lineNum = 1;
1837         lineVec.add(""); // the index is started from 1.
1838         while ((strLine = in.readLine()) != null) {
1839           lineVec.add(lineNum, strLine);
1840           addMapClassDefinitionToLineNum(cd, strLine, lineNum);
1841           addMapMethodDefinitionToLineNum(methodSet, strLine, lineNum);
1842           lineNum++;
1843         }
1844
1845       }
1846
1847     } catch (IOException e) {
1848       e.printStackTrace();
1849     }
1850
1851   }
1852
1853   private String generateLatticeDefinition(Descriptor desc) {
1854
1855     Set<String> sharedLocSet = new HashSet<String>();
1856
1857     SSJavaLattice<String> lattice = getLattice(desc);
1858     String rtr = "@LATTICE(\"";
1859
1860     Map<String, Set<String>> map = lattice.getTable();
1861     Set<String> keySet = map.keySet();
1862     boolean first = true;
1863     for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1864       String key = (String) iterator.next();
1865       if (!key.equals(lattice.getTopItem())) {
1866         Set<String> connectedSet = map.get(key);
1867
1868         if (connectedSet.size() == 1) {
1869           if (connectedSet.iterator().next().equals(lattice.getBottomItem())) {
1870             if (!first) {
1871               rtr += ",";
1872             } else {
1873               rtr += "LOC,";
1874               first = false;
1875             }
1876             rtr += key;
1877             if (lattice.isSharedLoc(key)) {
1878               rtr += "," + key + "*";
1879             }
1880           }
1881         }
1882
1883         for (Iterator iterator2 = connectedSet.iterator(); iterator2.hasNext();) {
1884           String loc = (String) iterator2.next();
1885           if (!loc.equals(lattice.getBottomItem())) {
1886             if (!first) {
1887               rtr += ",";
1888             } else {
1889               rtr += "LOC,";
1890               first = false;
1891             }
1892             rtr += loc + "<" + key;
1893             if (lattice.isSharedLoc(key) && (!sharedLocSet.contains(key))) {
1894               rtr += "," + key + "*";
1895               sharedLocSet.add(key);
1896             }
1897             if (lattice.isSharedLoc(loc) && (!sharedLocSet.contains(loc))) {
1898               rtr += "," + loc + "*";
1899               sharedLocSet.add(loc);
1900             }
1901
1902           }
1903         }
1904       }
1905     }
1906
1907     rtr += "\")";
1908
1909     if (desc instanceof MethodDescriptor) {
1910       System.out.println("#EXTRA LOC DECLARATION GEN=" + desc);
1911
1912       MethodSummary methodSummary = getMethodSummary((MethodDescriptor) desc);
1913
1914       if (!ssjava.getMethodContainingSSJavaLoop().equals(desc)) {
1915         TypeDescriptor returnType = ((MethodDescriptor) desc).getReturnType();
1916         if (returnType != null && (!returnType.isVoid())) {
1917           rtr +=
1918               "\n@RETURNLOC(\"" + generateLocationAnnoatation(methodSummary.getRETURNLoc()) + "\")";
1919         }
1920         CompositeLocation pcLoc = methodSummary.getPCLoc();
1921         if ((pcLoc != null) && (!pcLoc.get(0).isTop())) {
1922           rtr += "\n@PCLOC(\"" + generateLocationAnnoatation(pcLoc) + "\")";
1923         }
1924       }
1925
1926       rtr += "\n@THISLOC(\"" + methodSummary.getThisLocName() + "\")";
1927       rtr += "\n@GLOBALLOC(\"" + methodSummary.getGlobalLocName() + "\")";
1928
1929     }
1930
1931     return rtr;
1932   }
1933
1934   private void generateAnnoatedCode() {
1935
1936     readOriginalSourceFiles();
1937
1938     setupToAnalyze();
1939     while (!toAnalyzeIsEmpty()) {
1940       ClassDescriptor cd = toAnalyzeNext();
1941
1942       setupToAnalazeMethod(cd);
1943
1944       String sourceFileName = cd.getSourceFileName();
1945
1946       if (cd.isInterface()) {
1947         continue;
1948       }
1949
1950       int classDefLine = mapDescToDefinitionLine.get(cd);
1951       Vector<String> sourceVec = mapFileNameToLineVector.get(sourceFileName);
1952
1953       LocationSummary fieldLocSummary = getLocationSummary(cd);
1954
1955       String fieldLatticeDefStr = generateLatticeDefinition(cd);
1956       String annoatedSrc = fieldLatticeDefStr + newline + sourceVec.get(classDefLine);
1957       sourceVec.set(classDefLine, annoatedSrc);
1958
1959       // generate annotations for field declarations
1960       // Map<Descriptor, CompositeLocation> inferLocMap = fieldLocInfo.getMapDescToInferLocation();
1961       Map<String, String> mapFieldNameToLocName = fieldLocSummary.getMapHNodeNameToLocationName();
1962
1963       for (Iterator iter = cd.getFields(); iter.hasNext();) {
1964         FieldDescriptor fd = (FieldDescriptor) iter.next();
1965
1966         String locAnnotationStr;
1967         // CompositeLocation inferLoc = inferLocMap.get(fd);
1968         String locName = mapFieldNameToLocName.get(fd.getSymbol());
1969
1970         if (locName != null) {
1971           // infer loc is null if the corresponding field is static and final
1972           // locAnnotationStr = "@LOC(\"" + generateLocationAnnoatation(inferLoc) + "\")";
1973           locAnnotationStr = "@LOC(\"" + locName + "\")";
1974           int fdLineNum = fd.getLineNum();
1975           String orgFieldDeclarationStr = sourceVec.get(fdLineNum);
1976           String fieldDeclaration = fd.toString();
1977           fieldDeclaration = fieldDeclaration.substring(0, fieldDeclaration.length() - 1);
1978           String annoatedStr = locAnnotationStr + " " + orgFieldDeclarationStr;
1979           sourceVec.set(fdLineNum, annoatedStr);
1980         }
1981
1982       }
1983
1984       while (!toAnalyzeMethodIsEmpty()) {
1985         MethodDescriptor md = toAnalyzeMethodNext();
1986
1987         if (!ssjava.needTobeAnnotated(md)) {
1988           continue;
1989         }
1990
1991         SSJavaLattice<String> methodLattice = md2lattice.get(md);
1992         if (methodLattice != null) {
1993
1994           int methodDefLine = md.getLineNum();
1995
1996           // MethodLocationInfo methodLocInfo = getMethodLocationInfo(md);
1997           // Map<Descriptor, CompositeLocation> methodInferLocMap =
1998           // methodLocInfo.getMapDescToInferLocation();
1999
2000           MethodSummary methodSummary = getMethodSummary(md);
2001
2002           Map<Descriptor, CompositeLocation> mapVarDescToInferLoc =
2003               methodSummary.getMapVarDescToInferCompositeLocation();
2004           System.out.println("-----md=" + md);
2005           System.out.println("-----mapVarDescToInferLoc=" + mapVarDescToInferLoc);
2006
2007           Set<Descriptor> localVarDescSet = mapVarDescToInferLoc.keySet();
2008
2009           Set<String> localLocElementSet = methodLattice.getElementSet();
2010
2011           for (Iterator iterator = localVarDescSet.iterator(); iterator.hasNext();) {
2012             Descriptor localVarDesc = (Descriptor) iterator.next();
2013             System.out.println("-------localVarDesc=" + localVarDesc);
2014             CompositeLocation inferLoc = mapVarDescToInferLoc.get(localVarDesc);
2015
2016             String localLocIdentifier = inferLoc.get(0).getLocIdentifier();
2017             if (!localLocElementSet.contains(localLocIdentifier)) {
2018               methodLattice.put(localLocIdentifier);
2019             }
2020
2021             String locAnnotationStr = "@LOC(\"" + generateLocationAnnoatation(inferLoc) + "\")";
2022
2023             if (!isParameter(md, localVarDesc)) {
2024               if (mapDescToDefinitionLine.containsKey(localVarDesc)) {
2025                 int varLineNum = mapDescToDefinitionLine.get(localVarDesc);
2026                 String orgSourceLine = sourceVec.get(varLineNum);
2027                 int idx =
2028                     orgSourceLine.indexOf(generateVarDeclaration((VarDescriptor) localVarDesc));
2029                 assert (idx != -1);
2030                 String annoatedStr =
2031                     orgSourceLine.substring(0, idx) + locAnnotationStr + " "
2032                         + orgSourceLine.substring(idx);
2033                 sourceVec.set(varLineNum, annoatedStr);
2034               }
2035             } else {
2036               String methodDefStr = sourceVec.get(methodDefLine);
2037
2038               int idx =
2039                   getParamLocation(methodDefStr,
2040                       generateVarDeclaration((VarDescriptor) localVarDesc));
2041
2042               assert (idx != -1);
2043
2044               String annoatedStr =
2045                   methodDefStr.substring(0, idx) + locAnnotationStr + " "
2046                       + methodDefStr.substring(idx);
2047               sourceVec.set(methodDefLine, annoatedStr);
2048             }
2049
2050           }
2051
2052           // check if the lattice has to have the location type for the this
2053           // reference...
2054
2055           // boolean needToAddthisRef = hasThisReference(md);
2056           // if (localLocElementSet.contains("this")) {
2057           // methodLattice.put("this");
2058           // }
2059
2060           String methodLatticeDefStr = generateLatticeDefinition(md);
2061           String annoatedStr = methodLatticeDefStr + newline + sourceVec.get(methodDefLine);
2062           sourceVec.set(methodDefLine, annoatedStr);
2063
2064         }
2065       }
2066
2067     }
2068
2069     codeGen();
2070   }
2071
2072   private boolean hasThisReference(MethodDescriptor md) {
2073
2074     FlowGraph fg = getFlowGraph(md);
2075     Set<FlowNode> nodeSet = fg.getNodeSet();
2076     for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
2077       FlowNode flowNode = (FlowNode) iterator.next();
2078       if (flowNode.getDescTuple().get(0).equals(md.getThis())) {
2079         return true;
2080       }
2081     }
2082
2083     return false;
2084   }
2085
2086   private int getParamLocation(String methodStr, String paramStr) {
2087
2088     String pattern = paramStr + ",";
2089
2090     int idx = methodStr.indexOf(pattern);
2091     if (idx != -1) {
2092       return idx;
2093     } else {
2094       pattern = paramStr + ")";
2095       return methodStr.indexOf(pattern);
2096     }
2097
2098   }
2099
2100   private String generateVarDeclaration(VarDescriptor varDesc) {
2101
2102     TypeDescriptor td = varDesc.getType();
2103     String rtr = td.toString();
2104     if (td.isArray()) {
2105       for (int i = 0; i < td.getArrayCount(); i++) {
2106         rtr += "[]";
2107       }
2108     }
2109     rtr += " " + varDesc.getName();
2110     return rtr;
2111
2112   }
2113
2114   private String generateLocationAnnoatation(CompositeLocation loc) {
2115     System.out.println("loc=" + loc);
2116     String rtr = "";
2117     // method location
2118     Location methodLoc = loc.get(0);
2119     rtr += methodLoc.getLocIdentifier();
2120
2121     for (int i = 1; i < loc.getSize(); i++) {
2122       Location element = loc.get(i);
2123       rtr += "," + element.getDescriptor().getSymbol() + "." + element.getLocIdentifier();
2124     }
2125
2126     return rtr;
2127   }
2128
2129   private boolean isParameter(MethodDescriptor md, Descriptor localVarDesc) {
2130     return getFlowGraph(md).isParamDesc(localVarDesc);
2131   }
2132
2133   private String extractFileName(String fileName) {
2134     int idx = fileName.lastIndexOf("/");
2135     if (idx == -1) {
2136       return fileName;
2137     } else {
2138       return fileName.substring(idx + 1);
2139     }
2140
2141   }
2142
2143   private void codeGen() {
2144
2145     Set<String> originalFileNameSet = mapFileNameToLineVector.keySet();
2146     for (Iterator iterator = originalFileNameSet.iterator(); iterator.hasNext();) {
2147       String orgFileName = (String) iterator.next();
2148       String outputFileName = extractFileName(orgFileName);
2149
2150       Vector<String> sourceVec = mapFileNameToLineVector.get(orgFileName);
2151
2152       try {
2153
2154         FileWriter fileWriter = new FileWriter("./infer/" + outputFileName);
2155         BufferedWriter out = new BufferedWriter(fileWriter);
2156
2157         for (int i = 0; i < sourceVec.size(); i++) {
2158           out.write(sourceVec.get(i));
2159           out.newLine();
2160         }
2161         out.close();
2162       } catch (IOException e) {
2163         e.printStackTrace();
2164       }
2165
2166     }
2167
2168   }
2169
2170   private void checkLattices() {
2171
2172     LinkedList<MethodDescriptor> descriptorListToAnalyze = ssjava.getSortedDescriptors();
2173
2174     // current descriptors to visit in fixed-point interprocedural analysis,
2175     // prioritized by
2176     // dependency in the call graph
2177     methodDescriptorsToVisitStack.clear();
2178
2179     // descriptorListToAnalyze.removeFirst();
2180
2181     Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
2182     methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
2183
2184     while (!descriptorListToAnalyze.isEmpty()) {
2185       MethodDescriptor md = descriptorListToAnalyze.removeFirst();
2186       checkLatticesOfVirtualMethods(md);
2187     }
2188
2189   }
2190
2191   private void debug_writeLatticeDotFile() {
2192     // generate lattice dot file
2193
2194     setupToAnalyze();
2195
2196     while (!toAnalyzeIsEmpty()) {
2197       ClassDescriptor cd = toAnalyzeNext();
2198
2199       setupToAnalazeMethod(cd);
2200
2201       SSJavaLattice<String> classLattice = cd2lattice.get(cd);
2202       if (classLattice != null) {
2203         ssjava.writeLatticeDotFile(cd, null, classLattice);
2204         debug_printDescriptorToLocNameMapping(cd);
2205       }
2206
2207       while (!toAnalyzeMethodIsEmpty()) {
2208         MethodDescriptor md = toAnalyzeMethodNext();
2209         SSJavaLattice<String> methodLattice = md2lattice.get(md);
2210         if (methodLattice != null) {
2211           ssjava.writeLatticeDotFile(cd, md, methodLattice);
2212           debug_printDescriptorToLocNameMapping(md);
2213         }
2214       }
2215     }
2216
2217   }
2218
2219   private void debug_printDescriptorToLocNameMapping(Descriptor desc) {
2220
2221     LocationInfo info = getLocationInfo(desc);
2222     System.out.println("## " + desc + " ##");
2223     System.out.println(info.getMapDescToInferLocation());
2224     LocationInfo locInfo = getLocationInfo(desc);
2225     System.out.println("mapping=" + locInfo.getMapLocSymbolToDescSet());
2226     System.out.println("###################");
2227
2228   }
2229
2230   private void inferLattices() {
2231   }
2232
2233   private void calculateExtraLocations() {
2234     LinkedList<MethodDescriptor> descriptorListToAnalyze = ssjava.getSortedDescriptors();
2235     for (Iterator iterator = descriptorListToAnalyze.iterator(); iterator.hasNext();) {
2236       MethodDescriptor md = (MethodDescriptor) iterator.next();
2237       if (!ssjava.getMethodContainingSSJavaLoop().equals(md)) {
2238         calculateExtraLocations(md);
2239       }
2240     }
2241   }
2242
2243   private void checkLatticesOfVirtualMethods(MethodDescriptor md) {
2244
2245     if (!md.isStatic()) {
2246       Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
2247       setPossibleCallees.addAll(ssjava.getCallGraph().getMethods(md));
2248
2249       for (Iterator iterator = setPossibleCallees.iterator(); iterator.hasNext();) {
2250         MethodDescriptor mdCallee = (MethodDescriptor) iterator.next();
2251         if (!md.equals(mdCallee)) {
2252           checkConsistency(md, mdCallee);
2253         }
2254       }
2255
2256     }
2257
2258   }
2259
2260   private void checkConsistency(MethodDescriptor md1, MethodDescriptor md2) {
2261
2262     // check that two lattice have the same relations between parameters(+PC
2263     // LOC, GLOBAL_LOC RETURN LOC)
2264
2265     List<CompositeLocation> list1 = new ArrayList<CompositeLocation>();
2266     List<CompositeLocation> list2 = new ArrayList<CompositeLocation>();
2267
2268     MethodLocationInfo locInfo1 = getMethodLocationInfo(md1);
2269     MethodLocationInfo locInfo2 = getMethodLocationInfo(md2);
2270
2271     Map<Integer, CompositeLocation> paramMap1 = locInfo1.getMapParamIdxToInferLoc();
2272     Map<Integer, CompositeLocation> paramMap2 = locInfo2.getMapParamIdxToInferLoc();
2273
2274     int numParam = locInfo1.getMapParamIdxToInferLoc().keySet().size();
2275
2276     // add location types of paramters
2277     for (int idx = 0; idx < numParam; idx++) {
2278       list1.add(paramMap1.get(Integer.valueOf(idx)));
2279       list2.add(paramMap2.get(Integer.valueOf(idx)));
2280     }
2281
2282     // add program counter location
2283     list1.add(locInfo1.getPCLoc());
2284     list2.add(locInfo2.getPCLoc());
2285
2286     if (!md1.getReturnType().isVoid()) {
2287       // add return value location
2288       CompositeLocation rtrLoc1 = getMethodLocationInfo(md1).getReturnLoc();
2289       CompositeLocation rtrLoc2 = getMethodLocationInfo(md2).getReturnLoc();
2290       list1.add(rtrLoc1);
2291       list2.add(rtrLoc2);
2292     }
2293
2294     // add global location type
2295     if (md1.isStatic()) {
2296       CompositeLocation globalLoc1 =
2297           new CompositeLocation(new Location(md1, locInfo1.getGlobalLocName()));
2298       CompositeLocation globalLoc2 =
2299           new CompositeLocation(new Location(md2, locInfo2.getGlobalLocName()));
2300       list1.add(globalLoc1);
2301       list2.add(globalLoc2);
2302     }
2303
2304     for (int i = 0; i < list1.size(); i++) {
2305       CompositeLocation locA1 = list1.get(i);
2306       CompositeLocation locA2 = list2.get(i);
2307       for (int k = 0; k < list1.size(); k++) {
2308         if (i != k) {
2309           CompositeLocation locB1 = list1.get(k);
2310           CompositeLocation locB2 = list2.get(k);
2311           boolean r1 = isGreaterThan(getLattice(md1), locA1, locB1);
2312
2313           boolean r2 = isGreaterThan(getLattice(md1), locA2, locB2);
2314
2315           if (r1 != r2) {
2316             throw new Error("The method " + md1 + " is not consistent with the method " + md2
2317                 + ".:: They have a different ordering relation between locations (" + locA1 + ","
2318                 + locB1 + ") and (" + locA2 + "," + locB2 + ").");
2319           }
2320         }
2321       }
2322     }
2323
2324   }
2325
2326   private String getSymbol(int idx, FlowNode node) {
2327     Descriptor desc = node.getDescTuple().get(idx);
2328     return desc.getSymbol();
2329   }
2330
2331   private Descriptor getDescriptor(int idx, FlowNode node) {
2332     Descriptor desc = node.getDescTuple().get(idx);
2333     return desc;
2334   }
2335
2336   private void calculatePCLOC(MethodDescriptor md) {
2337
2338     System.out.println("#calcualtePCLOC");
2339     MethodSummary methodSummary = getMethodSummary(md);
2340     FlowGraph fg = getFlowGraph(md);
2341     Map<Integer, CompositeLocation> mapParamToLoc = methodSummary.getMapParamIdxToInferLoc();
2342
2343     // calculate the initial program counter location
2344     // PC location is higher than location types of parameters which has incoming flows.
2345
2346     Set<NTuple<Location>> paramLocTupleHavingInFlowSet = new HashSet<NTuple<Location>>();
2347     Set<NTuple<Location>> paramLocTupleNOTHavingInFlowSet = new HashSet<NTuple<Location>>();
2348
2349     int numParams = fg.getNumParameters();
2350     for (int i = 0; i < numParams; i++) {
2351       FlowNode paramFlowNode = fg.getParamFlowNode(i);
2352       Descriptor prefix = paramFlowNode.getDescTuple().get(0);
2353       NTuple<Descriptor> paramDescTuple = paramFlowNode.getCurrentDescTuple();
2354       NTuple<Location> paramLocTuple = translateToLocTuple(md, paramDescTuple);
2355
2356       if (fg.getIncomingNodeSetByPrefix(prefix).size() > 0) {
2357         // parameter has in-value flows
2358         paramLocTupleHavingInFlowSet.add(paramLocTuple);
2359       } else {
2360         paramLocTupleNOTHavingInFlowSet.add(paramLocTuple);
2361       }
2362     }
2363
2364
2365     if (paramLocTupleHavingInFlowSet.size() > 0
2366         && !coversAllParamters(md, fg, paramLocTupleHavingInFlowSet)) {
2367
2368       // Here, generates a location in the method lattice that is higher than the
2369       // paramLocTupleHavingInFlowSet
2370       NTuple<Location> pcLocTuple =
2371           generateLocTupleRelativeTo(md, paramLocTupleHavingInFlowSet, PCLOC);
2372
2373       int pcLocTupleIdx = pcLocTuple.size() - 1;
2374       Location pcLoc = pcLocTuple.get(pcLocTupleIdx);
2375       Descriptor pcDesc = pcLoc.getLocDescriptor();
2376       Descriptor enclosingDesc = pcLocTuple.get(pcLocTupleIdx).getDescriptor();
2377
2378       HierarchyGraph hierarchyGraph = getHierarchyGraph(enclosingDesc);
2379       HNode pcNode = hierarchyGraph.getHNode(pcDesc);
2380       pcNode.setSkeleton(true);
2381
2382       // add ordering relations s.t. PCLOC is higher than all of parameters with incoming flows
2383       // and lower than paramters which do not have any incoming flows
2384       for (Iterator iterator = paramLocTupleHavingInFlowSet.iterator(); iterator.hasNext();) {
2385         NTuple<Location> paramLocTuple = (NTuple<Location>) iterator.next();
2386         if (paramLocTuple.size() > pcLocTupleIdx) {
2387           Descriptor lowerDesc = paramLocTuple.get(pcLocTupleIdx).getLocDescriptor();
2388           hierarchyGraph.addEdge(pcDesc, lowerDesc);
2389         }
2390       }
2391
2392       for (Iterator iterator = paramLocTupleNOTHavingInFlowSet.iterator(); iterator.hasNext();) {
2393         NTuple<Location> paramLocTuple = (NTuple<Location>) iterator.next();
2394         if (paramLocTuple.size() > pcLocTupleIdx) {
2395           Descriptor higherDesc = paramLocTuple.get(pcLocTupleIdx).getLocDescriptor();
2396           hierarchyGraph.addEdge(higherDesc, pcDesc);
2397         }
2398       }
2399
2400       System.out.println("pcLoc=" + pcLoc);
2401
2402       methodSummary.setPCLoc(new CompositeLocation(pcLocTuple));
2403     }
2404   }
2405
2406   private boolean coversAllParamters(MethodDescriptor md, FlowGraph fg,
2407       Set<NTuple<Location>> paramLocTupleHavingInFlowSet) {
2408
2409     int numParam = fg.getNumParameters();
2410     int size = paramLocTupleHavingInFlowSet.size();
2411
2412     if (!md.isStatic()) {
2413
2414       // if the method is not static && there is a parameter composite location &&
2415       // it is started with 'this',
2416       // paramLocTupleHavingInFlowSet need to have 'this' parameter.
2417
2418       FlowNode thisParamNode = fg.getParamFlowNode(0);
2419       NTuple<Location> thisParamLocTuple =
2420           translateToLocTuple(md, thisParamNode.getCurrentDescTuple());
2421
2422       if (!paramLocTupleHavingInFlowSet.contains(thisParamLocTuple)) {
2423
2424         for (Iterator iterator = paramLocTupleHavingInFlowSet.iterator(); iterator.hasNext();) {
2425           NTuple<Location> paramTuple = (NTuple<Location>) iterator.next();
2426           if (paramTuple.size() > 1 && paramTuple.get(0).getLocDescriptor().equals(md.getThis())) {
2427             // paramLocTupleHavingInFlowSet.add(thisParamLocTuple);
2428             // break;
2429             size++;
2430           }
2431         }
2432
2433       }
2434     }
2435
2436     if (size == numParam) {
2437       return true;
2438     } else {
2439       return false;
2440     }
2441
2442   }
2443
2444   private void calculateRETURNLOC(MethodDescriptor md) {
2445
2446     System.out.println("#calculateRETURNLOC= " + md);
2447     // calculate a return location:
2448     // the return location type is lower than all parameters and the location of return values
2449
2450     MethodSummary methodSummary = getMethodSummary(md);
2451
2452     FlowGraph fg = getFlowGraph(md);
2453
2454     Map<Integer, CompositeLocation> mapParamToLoc = methodSummary.getMapParamIdxToInferLoc();
2455     Set<Integer> paramIdxSet = mapParamToLoc.keySet();
2456
2457     if (!md.getReturnType().isVoid()) {
2458       // first, generate the set of return value location types that starts
2459       // with 'this' reference
2460
2461       Set<NTuple<Location>> inferFieldReturnLocSet = new HashSet<NTuple<Location>>();
2462
2463       Set<FlowNode> paramFlowNodeFlowingToReturnValueSet = getParamNodeFlowingToReturnValue(md);
2464       System.out.println("paramFlowNodeFlowingToReturnValueSet="
2465           + paramFlowNodeFlowingToReturnValueSet);
2466
2467       Set<NTuple<Location>> locFlowingToReturnValueSet = new HashSet<NTuple<Location>>();
2468       for (Iterator iterator = paramFlowNodeFlowingToReturnValueSet.iterator(); iterator.hasNext();) {
2469         FlowNode fn = (FlowNode) iterator.next();
2470
2471         NTuple<Descriptor> paramDescTuple = fn.getCurrentDescTuple();
2472         NTuple<Location> paramLocTuple = translateToLocTuple(md, paramDescTuple);
2473
2474         locFlowingToReturnValueSet.add(paramLocTuple);
2475       }
2476
2477       Set<FlowNode> returnNodeSet = fg.getReturnNodeSet();
2478       for (Iterator iterator = returnNodeSet.iterator(); iterator.hasNext();) {
2479         FlowNode returnNode = (FlowNode) iterator.next();
2480         NTuple<Descriptor> returnDescTuple = returnNode.getCurrentDescTuple();
2481         NTuple<Location> returnLocTuple = translateToLocTuple(md, returnDescTuple);
2482         locFlowingToReturnValueSet.add(returnLocTuple);
2483       }
2484       System.out.println("locFlowingToReturnValueSet=" + locFlowingToReturnValueSet);
2485
2486       // Here, generates a return location in the method lattice that is lower than the
2487       // locFlowingToReturnValueSet
2488       NTuple<Location> returnLocTuple =
2489           generateLocTupleRelativeTo(md, locFlowingToReturnValueSet, RLOC);
2490
2491       System.out.println("returnLocTuple=" + returnLocTuple);
2492
2493       int returnLocTupleIdx = returnLocTuple.size() - 1;
2494       Location returnLoc = returnLocTuple.get(returnLocTupleIdx);
2495       Descriptor returnDesc = returnLoc.getLocDescriptor();
2496       Descriptor enclosingDesc = returnLocTuple.get(returnLocTupleIdx).getDescriptor();
2497
2498       HierarchyGraph hierarchyGraph = getHierarchyGraph(enclosingDesc);
2499       HNode returnNode = hierarchyGraph.getHNode(returnDesc);
2500       returnNode.setSkeleton(true);
2501
2502       for (Iterator iterator = locFlowingToReturnValueSet.iterator(); iterator.hasNext();) {
2503         NTuple<Location> locTuple = (NTuple<Location>) iterator.next();
2504         Descriptor higherDesc = locTuple.get(returnLocTupleIdx).getLocDescriptor();
2505         hierarchyGraph.addEdge(higherDesc, returnDesc);
2506       }
2507
2508       methodSummary.setRETURNLoc(new CompositeLocation(returnLocTuple));
2509
2510       // skip: for (Iterator iterator = returnNodeSet.iterator(); iterator.hasNext();) {
2511       // FlowNode returnNode = (FlowNode) iterator.next();
2512       //
2513       // NTuple<Descriptor> returnDescTuple = returnNode.getCurrentDescTuple();
2514       // NTuple<Location> returnLocTuple = translateToLocTuple(md, returnDescTuple);
2515       //
2516       // if (returnLocTuple.get(0).getLocDescriptor().equals(md.getThis())) {
2517       // // if the location type of the return value matches "this" reference
2518       // // then, check whether this return value is equal to/lower than all
2519       // // of parameters that possibly flow into the return values
2520       // for (Iterator iterator2 = inferParamLocSet.iterator(); iterator2.hasNext();) {
2521       // CompositeLocation paramInferLoc = (CompositeLocation) iterator2.next();
2522       //
2523       // if ((!paramInferLoc.equals(returnLocTuple))
2524       // && !isGreaterThan(methodLattice, paramInferLoc, inferReturnLoc)) {
2525       // continue skip;
2526       // }
2527       // }
2528       // inferFieldReturnLocSet.add(returnLocTuple);
2529       //
2530       // }
2531       // }
2532
2533       // if (inferFieldReturnLocSet.size() > 0) {
2534       //
2535       // // CompositeLocation returnLoc = getLowest(methodLattice, inferFieldReturnLocSet);
2536       // CompositeLocation returnLoc = null;
2537       // if (returnLoc == null) {
2538       // // in this case, assign <'this',bottom> to the RETURNLOC
2539       // returnLoc = new CompositeLocation(new Location(md, md.getThis().getSymbol()));
2540       // returnLoc.addLocation(new Location(md.getClassDesc(), getLattice(md.getClassDesc())
2541       // .getBottomItem()));
2542       // }
2543       // methodInfo.setReturnLoc(returnLoc);
2544       //
2545       // } else {
2546       // String returnLocSymbol = "RETURNLOC";
2547       // CompositeLocation returnLocInferLoc =
2548       // new CompositeLocation(new Location(md, returnLocSymbol));
2549       // methodInfo.setReturnLoc(returnLocInferLoc);
2550       //
2551       // for (Iterator iterator = paramIdxSet.iterator(); iterator.hasNext();) {
2552       // Integer paramIdx = (Integer) iterator.next();
2553       // CompositeLocation inferLoc = mapParamToLoc.get(paramIdx);
2554       // String paramLocLocalSymbol = inferLoc.get(0).getLocIdentifier();
2555       // if (!methodLattice.isGreaterThan(paramLocLocalSymbol, returnLocSymbol)) {
2556       // // TODO
2557       // // addRelationHigherToLower(methodLattice, methodInfo,
2558       // // paramLocLocalSymbol,
2559       // // returnLocSymbol);
2560       // }
2561       // }
2562       //
2563       // for (Iterator iterator = returnNodeSet.iterator(); iterator.hasNext();) {
2564       // FlowNode returnNode = (FlowNode) iterator.next();
2565       // CompositeLocation inferLoc =
2566       // generateInferredCompositeLocation(methodInfo, fg.getLocationTuple(returnNode));
2567       // if (!isGreaterThan(methodLattice, inferLoc, returnLocInferLoc)) {
2568       // // TODO
2569       // // addRelation(methodLattice, methodInfo, inferLoc,
2570       // // returnLocInferLoc);
2571       // }
2572       // }
2573       //
2574       // }
2575
2576     }
2577   }
2578
2579   private void calculateExtraLocations(MethodDescriptor md) {
2580     // calcualte pcloc, returnloc,...
2581
2582     System.out.println("\nSSJAVA:Calculate PCLOC/RETURNLOC locations: " + md);
2583
2584     calculatePCLOC(md);
2585     calculateRETURNLOC(md);
2586
2587   }
2588
2589   private NTuple<Location> generateLocTupleRelativeTo(MethodDescriptor md,
2590       Set<NTuple<Location>> paramLocTupleHavingInFlowSet, String locNamePrefix) {
2591
2592     System.out.println("-generateLocTupleRelativeTo=" + paramLocTupleHavingInFlowSet);
2593
2594     NTuple<Location> higherLocTuple = new NTuple<Location>();
2595
2596     VarDescriptor thisVarDesc = md.getThis();
2597     // check if all paramter loc tuple is started with 'this' reference
2598     boolean hasParamNotStartedWithThisRef = false;
2599
2600     int minSize = 0;
2601
2602     Set<NTuple<Location>> paramLocTupleStartedWithThis = new HashSet<NTuple<Location>>();
2603
2604     for (Iterator iterator = paramLocTupleHavingInFlowSet.iterator(); iterator.hasNext();) {
2605       NTuple<Location> paramLocTuple = (NTuple<Location>) iterator.next();
2606       if (!paramLocTuple.get(0).getLocDescriptor().equals(thisVarDesc)) {
2607         hasParamNotStartedWithThisRef = true;
2608       } else if (paramLocTuple.size() > 1) {
2609         paramLocTupleStartedWithThis.add(paramLocTuple);
2610         if (minSize == 0 || minSize > paramLocTuple.size()) {
2611           minSize = paramLocTuple.size();
2612         }
2613       }
2614     }
2615
2616     System.out.println("---paramLocTupleStartedWithThis=" + paramLocTupleStartedWithThis);
2617     Descriptor enclosingDesc = md;
2618     if (hasParamNotStartedWithThisRef) {
2619       // in this case, PCLOC will be the local location
2620     } else {
2621       // all parameter is started with 'this', so PCLOC will be set relative to the composite
2622       // location started with 'this'.
2623       for (int idx = 0; idx < minSize - 1; idx++) {
2624         Set<Descriptor> locDescSet = new HashSet<Descriptor>();
2625         Location curLoc = null;
2626         NTuple<Location> paramLocTuple = null;
2627         for (Iterator iterator = paramLocTupleStartedWithThis.iterator(); iterator.hasNext();) {
2628           paramLocTuple = (NTuple<Location>) iterator.next();
2629           System.out.println("-----paramLocTuple=" + paramLocTuple + "  idx=" + idx);
2630           curLoc = paramLocTuple.get(idx);
2631           Descriptor locDesc = curLoc.getLocDescriptor();
2632           locDescSet.add(locDesc);
2633         }
2634         System.out.println("-----locDescSet=" + locDescSet + " idx=" + idx);
2635         if (locDescSet.size() != 1) {
2636           break;
2637         }
2638         Location newLocElement = new Location(curLoc.getDescriptor(), curLoc.getLocDescriptor());
2639         higherLocTuple.add(newLocElement);
2640         enclosingDesc = getClassTypeDescriptor(curLoc.getLocDescriptor());
2641       }
2642
2643     }
2644
2645     String pcLocIdentifier = locNamePrefix + (locSeed++);
2646     NameDescriptor pcLocDesc = new NameDescriptor(pcLocIdentifier);
2647     Location newLoc = new Location(enclosingDesc, pcLocDesc);
2648     higherLocTuple.add(newLoc);
2649
2650     System.out.println("---new loc tuple=" + higherLocTuple);
2651
2652     return higherLocTuple;
2653
2654   }
2655
2656   public ClassDescriptor getClassTypeDescriptor(Descriptor in) {
2657
2658     if (in instanceof VarDescriptor) {
2659       return ((VarDescriptor) in).getType().getClassDesc();
2660     } else if (in instanceof FieldDescriptor) {
2661       return ((FieldDescriptor) in).getType().getClassDesc();
2662     }
2663     // else if (in instanceof LocationDescriptor) {
2664     // // here is the case that the descriptor 'in' is the last element of the assigned composite
2665     // // location
2666     // return ((VarDescriptor) locTuple.get(0).getLocDescriptor()).getType().getClassDesc();
2667     // }
2668     else {
2669       return null;
2670     }
2671
2672   }
2673
2674   private Set<NTuple<Location>> calculateHighestLocTupleSet(
2675       Set<NTuple<Location>> paramLocTupleHavingInFlowSet) {
2676
2677     Set<NTuple<Location>> highestSet = new HashSet<NTuple<Location>>();
2678
2679     Iterator<NTuple<Location>> iterator = paramLocTupleHavingInFlowSet.iterator();
2680     NTuple<Location> highest = iterator.next();
2681
2682     for (; iterator.hasNext();) {
2683       NTuple<Location> curLocTuple = (NTuple<Location>) iterator.next();
2684       if (isHigherThan(curLocTuple, highest)) {
2685         System.out.println(curLocTuple + " is greater than " + highest);
2686         highest = curLocTuple;
2687       }
2688     }
2689
2690     highestSet.add(highest);
2691
2692     MethodDescriptor md = (MethodDescriptor) highest.get(0).getDescriptor();
2693     VarDescriptor thisVarDesc = md.getThis();
2694
2695     System.out.println("highest=" + highest);
2696
2697     for (Iterator<NTuple<Location>> iter = paramLocTupleHavingInFlowSet.iterator(); iter.hasNext();) {
2698       NTuple<Location> curLocTuple = iter.next();
2699
2700       if (!curLocTuple.equals(highest) && !hasOrderingRelation(highest, curLocTuple)) {
2701
2702         System.out.println("add it to the highest set=" + curLocTuple);
2703         highestSet.add(curLocTuple);
2704
2705       }
2706     }
2707
2708     return highestSet;
2709
2710   }
2711
2712   private void calculateExtraLocations2(MethodDescriptor md) {
2713     // calcualte pcloc, returnloc,...
2714
2715     SSJavaLattice<String> methodLattice = getMethodLattice(md);
2716     MethodLocationInfo methodInfo = getMethodLocationInfo(md);
2717     FlowGraph fg = getFlowGraph(md);
2718     Set<FlowNode> nodeSet = fg.getNodeSet();
2719
2720     for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
2721       FlowNode flowNode = (FlowNode) iterator.next();
2722       if (flowNode.isDeclaratonNode()) {
2723         CompositeLocation inferLoc = methodInfo.getInferLocation(flowNode.getDescTuple().get(0));
2724         String locIdentifier = inferLoc.get(0).getLocIdentifier();
2725         if (!methodLattice.containsKey(locIdentifier)) {
2726           methodLattice.put(locIdentifier);
2727         }
2728
2729       }
2730     }
2731
2732     Map<Integer, CompositeLocation> mapParamToLoc = methodInfo.getMapParamIdxToInferLoc();
2733     Set<Integer> paramIdxSet = mapParamToLoc.keySet();
2734
2735     if (!ssjava.getMethodContainingSSJavaLoop().equals(md)) {
2736       // calculate the initial program counter location
2737       // PC location is higher than location types of all parameters
2738       String pcLocSymbol = "PCLOC";
2739
2740       Set<CompositeLocation> paramInFlowSet = new HashSet<CompositeLocation>();
2741
2742       for (Iterator iterator = paramIdxSet.iterator(); iterator.hasNext();) {
2743         Integer paramIdx = (Integer) iterator.next();
2744
2745         FlowNode paramFlowNode = fg.getParamFlowNode(paramIdx);
2746
2747         if (fg.getIncomingFlowNodeSet(paramFlowNode).size() > 0) {
2748           // parameter has in-value flows
2749           CompositeLocation inferLoc = mapParamToLoc.get(paramIdx);
2750           paramInFlowSet.add(inferLoc);
2751         }
2752       }
2753
2754       if (paramInFlowSet.size() > 0) {
2755         CompositeLocation lowestLoc = getLowest(methodLattice, paramInFlowSet);
2756         assert (lowestLoc != null);
2757         methodInfo.setPCLoc(lowestLoc);
2758       }
2759
2760     }
2761
2762     // calculate a return location
2763     // the return location type is lower than all parameters and location
2764     // types
2765     // of return values
2766     if (!md.getReturnType().isVoid()) {
2767       // first, generate the set of return value location types that starts
2768       // with
2769       // 'this' reference
2770
2771       Set<CompositeLocation> inferFieldReturnLocSet = new HashSet<CompositeLocation>();
2772
2773       Set<FlowNode> paramFlowNode = getParamNodeFlowingToReturnValue(md);
2774       Set<CompositeLocation> inferParamLocSet = new HashSet<CompositeLocation>();
2775       if (paramFlowNode != null) {
2776         for (Iterator iterator = paramFlowNode.iterator(); iterator.hasNext();) {
2777           FlowNode fn = (FlowNode) iterator.next();
2778           CompositeLocation inferLoc =
2779               generateInferredCompositeLocation(methodInfo, getFlowGraph(md).getLocationTuple(fn));
2780           inferParamLocSet.add(inferLoc);
2781         }
2782       }
2783
2784       Set<FlowNode> returnNodeSet = fg.getReturnNodeSet();
2785
2786       skip: for (Iterator iterator = returnNodeSet.iterator(); iterator.hasNext();) {
2787         FlowNode returnNode = (FlowNode) iterator.next();
2788         CompositeLocation inferReturnLoc =
2789             generateInferredCompositeLocation(methodInfo, fg.getLocationTuple(returnNode));
2790         if (inferReturnLoc.get(0).getLocIdentifier().equals("this")) {
2791           // if the location type of the return value matches "this" reference
2792           // then, check whether this return value is equal to/lower than all
2793           // of
2794           // parameters that possibly flow into the return values
2795           for (Iterator iterator2 = inferParamLocSet.iterator(); iterator2.hasNext();) {
2796             CompositeLocation paramInferLoc = (CompositeLocation) iterator2.next();
2797
2798             if ((!paramInferLoc.equals(inferReturnLoc))
2799                 && !isGreaterThan(methodLattice, paramInferLoc, inferReturnLoc)) {
2800               continue skip;
2801             }
2802           }
2803           inferFieldReturnLocSet.add(inferReturnLoc);
2804
2805         }
2806       }
2807
2808       if (inferFieldReturnLocSet.size() > 0) {
2809
2810         CompositeLocation returnLoc = getLowest(methodLattice, inferFieldReturnLocSet);
2811         if (returnLoc == null) {
2812           // in this case, assign <'this',bottom> to the RETURNLOC
2813           returnLoc = new CompositeLocation(new Location(md, md.getThis().getSymbol()));
2814           returnLoc.addLocation(new Location(md.getClassDesc(), getLattice(md.getClassDesc())
2815               .getBottomItem()));
2816         }
2817         methodInfo.setReturnLoc(returnLoc);
2818
2819       } else {
2820         String returnLocSymbol = "RETURNLOC";
2821         CompositeLocation returnLocInferLoc =
2822             new CompositeLocation(new Location(md, returnLocSymbol));
2823         methodInfo.setReturnLoc(returnLocInferLoc);
2824
2825         for (Iterator iterator = paramIdxSet.iterator(); iterator.hasNext();) {
2826           Integer paramIdx = (Integer) iterator.next();
2827           CompositeLocation inferLoc = mapParamToLoc.get(paramIdx);
2828           String paramLocLocalSymbol = inferLoc.get(0).getLocIdentifier();
2829           if (!methodLattice.isGreaterThan(paramLocLocalSymbol, returnLocSymbol)) {
2830             // TODO
2831             // addRelationHigherToLower(methodLattice, methodInfo,
2832             // paramLocLocalSymbol,
2833             // returnLocSymbol);
2834           }
2835         }
2836
2837         for (Iterator iterator = returnNodeSet.iterator(); iterator.hasNext();) {
2838           FlowNode returnNode = (FlowNode) iterator.next();
2839           CompositeLocation inferLoc =
2840               generateInferredCompositeLocation(methodInfo, fg.getLocationTuple(returnNode));
2841           if (!isGreaterThan(methodLattice, inferLoc, returnLocInferLoc)) {
2842             // TODO
2843             // addRelation(methodLattice, methodInfo, inferLoc,
2844             // returnLocInferLoc);
2845           }
2846         }
2847
2848       }
2849
2850     }
2851   }
2852
2853   private Set<String> getHigherLocSymbolThan(SSJavaLattice<String> lattice, String loc) {
2854     Set<String> higherLocSet = new HashSet<String>();
2855
2856     Set<String> locSet = lattice.getTable().keySet();
2857     for (Iterator iterator = locSet.iterator(); iterator.hasNext();) {
2858       String element = (String) iterator.next();
2859       if (lattice.isGreaterThan(element, loc) && (!element.equals(lattice.getTopItem()))) {
2860         higherLocSet.add(element);
2861       }
2862     }
2863     return higherLocSet;
2864   }
2865
2866   private CompositeLocation getLowest(SSJavaLattice<String> methodLattice,
2867       Set<CompositeLocation> set) {
2868
2869     CompositeLocation lowest = set.iterator().next();
2870
2871     if (set.size() == 1) {
2872       return lowest;
2873     }
2874
2875     for (Iterator iterator = set.iterator(); iterator.hasNext();) {
2876       CompositeLocation loc = (CompositeLocation) iterator.next();
2877
2878       if ((!loc.equals(lowest)) && (!isComparable(methodLattice, lowest, loc))) {
2879         // if there is a case where composite locations are incomparable, just
2880         // return null
2881         return null;
2882       }
2883
2884       if ((!loc.equals(lowest)) && isGreaterThan(methodLattice, lowest, loc)) {
2885         lowest = loc;
2886       }
2887     }
2888     return lowest;
2889   }
2890
2891   private boolean isComparable(SSJavaLattice<String> methodLattice, CompositeLocation comp1,
2892       CompositeLocation comp2) {
2893
2894     int size = comp1.getSize() >= comp2.getSize() ? comp2.getSize() : comp1.getSize();
2895
2896     for (int idx = 0; idx < size; idx++) {
2897       Location loc1 = comp1.get(idx);
2898       Location loc2 = comp2.get(idx);
2899
2900       Descriptor desc1 = loc1.getDescriptor();
2901       Descriptor desc2 = loc2.getDescriptor();
2902
2903       if (!desc1.equals(desc2)) {
2904         throw new Error("Fail to compare " + comp1 + " and " + comp2);
2905       }
2906
2907       String symbol1 = loc1.getLocIdentifier();
2908       String symbol2 = loc2.getLocIdentifier();
2909
2910       SSJavaLattice<String> lattice;
2911       if (idx == 0) {
2912         lattice = methodLattice;
2913       } else {
2914         lattice = getLattice(desc1);
2915       }
2916
2917       if (symbol1.equals(symbol2)) {
2918         continue;
2919       } else if (!lattice.isComparable(symbol1, symbol2)) {
2920         return false;
2921       }
2922
2923     }
2924
2925     return true;
2926   }
2927
2928   private boolean isGreaterThan(SSJavaLattice<String> methodLattice, CompositeLocation comp1,
2929       CompositeLocation comp2) {
2930
2931     int size = comp1.getSize() >= comp2.getSize() ? comp2.getSize() : comp1.getSize();
2932
2933     for (int idx = 0; idx < size; idx++) {
2934       Location loc1 = comp1.get(idx);
2935       Location loc2 = comp2.get(idx);
2936
2937       Descriptor desc1 = loc1.getDescriptor();
2938       Descriptor desc2 = loc2.getDescriptor();
2939
2940       if (!desc1.equals(desc2)) {
2941         throw new Error("Fail to compare " + comp1 + " and " + comp2);
2942       }
2943
2944       String symbol1 = loc1.getLocIdentifier();
2945       String symbol2 = loc2.getLocIdentifier();
2946
2947       SSJavaLattice<String> lattice;
2948       if (idx == 0) {
2949         lattice = methodLattice;
2950       } else {
2951         lattice = getLattice(desc1);
2952       }
2953
2954       if (symbol1.equals(symbol2)) {
2955         continue;
2956       } else if (lattice.isGreaterThan(symbol1, symbol2)) {
2957         return true;
2958       } else {
2959         return false;
2960       }
2961
2962     }
2963
2964     return false;
2965   }
2966
2967   private void contributeCalleeFlows(MethodInvokeNode min, MethodDescriptor mdCaller,
2968       MethodDescriptor mdCallee) {
2969
2970     System.out.println("\n##contributeCalleeFlows callee=" + mdCallee + "TO caller=" + mdCaller);
2971
2972     getSubGlobalFlowGraph(mdCallee);
2973
2974   }
2975
2976   private GlobalFlowGraph getSubGlobalFlowGraph(MethodDescriptor md) {
2977     return mapMethodDescriptorToSubGlobalFlowGraph.get(md);
2978   }
2979
2980   private void propagateFlowsToCallerWithNoCompositeLocation(MethodInvokeNode min,
2981       MethodDescriptor mdCaller, MethodDescriptor mdCallee) {
2982
2983     // if the parameter A reaches to the parameter B
2984     // then, add an edge the argument A -> the argument B to the caller's flow
2985     // graph
2986
2987     FlowGraph calleeFlowGraph = getFlowGraph(mdCallee);
2988     FlowGraph callerFlowGraph = getFlowGraph(mdCaller);
2989     int numParam = calleeFlowGraph.getNumParameters();
2990
2991     for (int i = 0; i < numParam; i++) {
2992       for (int k = 0; k < numParam; k++) {
2993
2994         if (i != k) {
2995
2996           FlowNode paramNode1 = calleeFlowGraph.getParamFlowNode(i);
2997           FlowNode paramNode2 = calleeFlowGraph.getParamFlowNode(k);
2998
2999           NTuple<Descriptor> arg1Tuple = getNodeTupleByArgIdx(min, i);
3000           NTuple<Descriptor> arg2Tuple = getNodeTupleByArgIdx(min, k);
3001
3002           // check if the callee propagates an ordering constraints through
3003           // parameters
3004
3005           Set<FlowNode> localReachSet = calleeFlowGraph.getLocalReachFlowNodeSetFrom(paramNode1);
3006           // System.out.println("-param1=" + paramNode1 + " is higher than param2=" + paramNode2);
3007           // System.out.println("-- localReachSet from param1=" + localReachSet);
3008
3009           if (arg1Tuple.size() > 0 && arg2Tuple.size() > 0 && localReachSet.contains(paramNode2)) {
3010             // need to propagate an ordering relation s.t. arg1 is higher
3011             // than arg2
3012
3013             // System.out
3014             // .println("-arg1Tuple=" + arg1Tuple + " is higher than arg2Tuple=" + arg2Tuple);
3015
3016             // otherwise, flows between method/field locations...
3017             callerFlowGraph.addValueFlowEdge(arg1Tuple, arg2Tuple);
3018             // System.out.println("arg1=" + arg1Tuple + "   arg2=" + arg2Tuple);
3019
3020           }
3021
3022           System.out.println();
3023         }
3024       }
3025     }
3026     // System.out.println("##\n");
3027
3028   }
3029
3030   private void propagateFlowsToCaller(MethodInvokeNode min, MethodDescriptor mdCaller,
3031       MethodDescriptor mdCallee) {
3032
3033     System.out.println("\n##PROPAGATE callee=" + mdCallee + "TO caller=" + mdCaller);
3034
3035     // if the parameter A reaches to the parameter B
3036     // then, add an edge the argument A -> the argument B to the caller's flow
3037     // graph
3038
3039     // TODO
3040     // also if a parameter is a composite location and is started with "this"
3041     // reference,
3042     // need to make sure that the corresponding argument is higher than the
3043     // translated location of
3044     // the parameter.
3045
3046     FlowGraph calleeFlowGraph = getFlowGraph(mdCallee);
3047     FlowGraph callerFlowGraph = getFlowGraph(mdCaller);
3048     int numParam = calleeFlowGraph.getNumParameters();
3049
3050     for (int i = 0; i < numParam; i++) {
3051       for (int k = 0; k < numParam; k++) {
3052
3053         if (i != k) {
3054
3055           FlowNode paramNode1 = calleeFlowGraph.getParamFlowNode(i);
3056           FlowNode paramNode2 = calleeFlowGraph.getParamFlowNode(k);
3057
3058           System.out.println("param1=" + paramNode1 + " curDescTuple="
3059               + paramNode1.getCurrentDescTuple());
3060           System.out.println("param2=" + paramNode2 + " curDescTuple="
3061               + paramNode2.getCurrentDescTuple());
3062
3063           // TODO: deprecated method
3064           // NodeTupleSet tupleSetArg1 = getNodeTupleSetByArgIdx(min, i);
3065           // NodeTupleSet tupleSetArg2 = getNodeTupleSetByArgIdx(min, k);
3066           NodeTupleSet tupleSetArg1 = null;
3067           NodeTupleSet tupleSetArg2 = null;
3068
3069           for (Iterator<NTuple<Descriptor>> iter1 = tupleSetArg1.iterator(); iter1.hasNext();) {
3070             NTuple<Descriptor> arg1Tuple = iter1.next();
3071
3072             for (Iterator<NTuple<Descriptor>> iter2 = tupleSetArg2.iterator(); iter2.hasNext();) {
3073               NTuple<Descriptor> arg2Tuple = iter2.next();
3074
3075               // check if the callee propagates an ordering constraints through
3076               // parameters
3077
3078               Set<FlowNode> localReachSet =
3079                   calleeFlowGraph.getLocalReachFlowNodeSetFrom(paramNode1);
3080
3081               if (localReachSet.contains(paramNode2)) {
3082                 // need to propagate an ordering relation s.t. arg1 is higher
3083                 // than arg2
3084
3085                 System.out
3086                     .println("-param1=" + paramNode1 + " is higher than param2=" + paramNode2);
3087                 System.out.println("-arg1Tuple=" + arg1Tuple + " is higher than arg2Tuple="
3088                     + arg2Tuple);
3089
3090                 if (!min.getMethod().isStatic()) {
3091                   // check if this is the case that values flow to/from the
3092                   // current object reference 'this'
3093
3094                   NTuple<Descriptor> baseTuple = mapMethodInvokeNodeToBaseTuple.get(min);
3095                   Descriptor baseRef = baseTuple.get(baseTuple.size() - 1);
3096
3097                   System.out.println("paramNode1.getCurrentDescTuple()="
3098                       + paramNode1.getCurrentDescTuple());
3099                   // calculate the prefix of the argument
3100
3101                   if (arg2Tuple.size() == 1 && arg2Tuple.get(0).equals(baseRef)) {
3102                     // in this case, the callee flow causes a caller flow to the
3103                     // object whose method
3104                     // is invoked.
3105
3106                     if (!paramNode1.getCurrentDescTuple().startsWith(mdCallee.getThis())) {
3107                       // check whether ???
3108
3109                       NTuple<Descriptor> param1Prefix =
3110                           calculatePrefixForParam(callerFlowGraph, calleeFlowGraph, min, arg1Tuple,
3111                               paramNode1);
3112
3113                       if (param1Prefix != null && param1Prefix.startsWith(mdCallee.getThis())) {
3114                         // in this case, we need to create a new edge
3115                         // 'this.FIELD'->'this'
3116                         // but we couldn't... instead we assign a new composite
3117                         // location started
3118                         // with 'this' reference to the corresponding parameter
3119
3120                         CompositeLocation compLocForParam1 =
3121                             generateCompositeLocation(mdCallee, param1Prefix);
3122
3123                         System.out
3124                             .println("set comp loc=" + compLocForParam1 + " to " + paramNode1);
3125                         paramNode1.setCompositeLocation(compLocForParam1);
3126
3127                         // then, we need to make sure that the corresponding
3128                         // argument in the caller
3129                         // is required to be higher than or equal to the
3130                         // translated parameter
3131                         // location
3132
3133                         NTuple<Descriptor> translatedParamTuple =
3134                             translateCompositeLocationToCaller(min, compLocForParam1);
3135
3136                         // TODO : check if the arg >= the tranlated parameter
3137
3138                         System.out.println("add a flow edge= " + arg1Tuple + "->"
3139                             + translatedParamTuple);
3140                         callerFlowGraph.addValueFlowEdge(arg1Tuple, translatedParamTuple);
3141
3142                         continue;
3143
3144                       }
3145
3146                     } else {
3147                       // param1 has already been assigned a composite location
3148
3149                       System.out.println("--param1 has already been assigned a composite location");
3150                       CompositeLocation compLocForParam1 = paramNode1.getCompositeLocation();
3151                       NTuple<Descriptor> translatedParamTuple =
3152                           translateCompositeLocationToCaller(min, compLocForParam1);
3153
3154                       // TODO : check if the arg >= the tranlated parameter
3155
3156                       System.out.println("add a flow edge= " + arg1Tuple + "->"
3157                           + translatedParamTuple);
3158                       callerFlowGraph.addValueFlowEdge(arg1Tuple, translatedParamTuple);
3159
3160                       continue;
3161
3162                     }
3163
3164                   } else if (arg1Tuple.size() == 1 && arg1Tuple.get(0).equals(baseRef)) {
3165                     // in this case, the callee flow causes a caller flow
3166                     // originated from the object
3167                     // whose
3168                     // method is invoked.
3169
3170                     System.out.println("###FROM CASE");
3171
3172                     if (!paramNode2.getCurrentDescTuple().startsWith(mdCallee.getThis())) {
3173
3174                       NTuple<Descriptor> param2Prefix =
3175                           calculatePrefixForParam(callerFlowGraph, calleeFlowGraph, min, arg2Tuple,
3176                               paramNode2);
3177
3178                       if (param2Prefix != null && param2Prefix.startsWith(mdCallee.getThis())) {
3179                         // in this case, we need to create a new edge 'this' ->
3180                         // 'this.FIELD' but we couldn't... instead we assign the
3181                         // corresponding
3182                         // parameter a new composite location started with
3183                         // 'this' reference
3184
3185                         CompositeLocation compLocForParam2 =
3186                             generateCompositeLocation(mdCallee, param2Prefix);
3187
3188                         // System.out.println("set comp loc=" + compLocForParam2
3189                         // +
3190                         // " to " + paramNode2);
3191                         paramNode1.setCompositeLocation(compLocForParam2);
3192                         continue;
3193                       }
3194                     }
3195
3196                   }
3197                 }
3198
3199                 // otherwise, flows between method/field locations...
3200                 callerFlowGraph.addValueFlowEdge(arg1Tuple, arg2Tuple);
3201                 System.out.println("arg1=" + arg1Tuple + "   arg2=" + arg2Tuple);
3202
3203               }
3204
3205             }
3206
3207           }
3208           System.out.println();
3209         }
3210       }
3211     }
3212     System.out.println("##\n");
3213   }
3214
3215   private NTuple<Descriptor> translateCompositeLocationToCaller(MethodInvokeNode min,
3216       CompositeLocation compLocForParam1) {
3217     NTuple<Descriptor> baseTuple = mapMethodInvokeNodeToBaseTuple.get(min);
3218
3219     NTuple<Descriptor> tuple = new NTuple<Descriptor>();
3220
3221     for (int i = 0; i < baseTuple.size(); i++) {
3222       tuple.add(baseTuple.get(i));
3223     }
3224
3225     for (int i = 1; i < compLocForParam1.getSize(); i++) {
3226       Location loc = compLocForParam1.get(i);
3227       tuple.add(loc.getLocDescriptor());
3228     }
3229
3230     return tuple;
3231   }
3232
3233   private CompositeLocation generateCompositeLocation(NTuple<Location> prefixLocTuple) {
3234
3235     System.out.println("generateCompositeLocation=" + prefixLocTuple);
3236
3237     CompositeLocation newCompLoc = new CompositeLocation();
3238     for (int i = 0; i < prefixLocTuple.size(); i++) {
3239       newCompLoc.addLocation(prefixLocTuple.get(i));
3240     }
3241
3242     Descriptor lastDescOfPrefix = prefixLocTuple.get(prefixLocTuple.size() - 1).getLocDescriptor();
3243
3244     ClassDescriptor enclosingDescriptor;
3245     if (lastDescOfPrefix instanceof FieldDescriptor) {
3246       enclosingDescriptor = ((FieldDescriptor) lastDescOfPrefix).getType().getClassDesc();
3247       // System.out.println("enclosingDescriptor0=" + enclosingDescriptor);
3248     } else if (lastDescOfPrefix.equals(GLOBALDESC)) {
3249       MethodDescriptor currentMethodDesc = (MethodDescriptor) prefixLocTuple.get(0).getDescriptor();
3250       enclosingDescriptor = currentMethodDesc.getClassDesc();
3251     } else {
3252       // var descriptor case
3253       enclosingDescriptor = ((VarDescriptor) lastDescOfPrefix).getType().getClassDesc();
3254     }
3255     // System.out.println("enclosingDescriptor=" + enclosingDescriptor);
3256
3257     LocationDescriptor newLocDescriptor = generateNewLocationDescriptor();
3258     newLocDescriptor.setEnclosingClassDesc(enclosingDescriptor);
3259
3260     Location newLoc = new Location(enclosingDescriptor, newLocDescriptor.getSymbol());
3261     newLoc.setLocDescriptor(newLocDescriptor);
3262     newCompLoc.addLocation(newLoc);
3263
3264     // System.out.println("--newCompLoc=" + newCompLoc);
3265     return newCompLoc;
3266   }
3267
3268   private CompositeLocation generateCompositeLocation(MethodDescriptor md,
3269       NTuple<Descriptor> paramPrefix) {
3270
3271     System.out.println("generateCompositeLocation=" + paramPrefix);
3272
3273     CompositeLocation newCompLoc = convertToCompositeLocation(md, paramPrefix);
3274
3275     Descriptor lastDescOfPrefix = paramPrefix.get(paramPrefix.size() - 1);
3276     // System.out.println("lastDescOfPrefix=" + lastDescOfPrefix + "  kind="
3277     // + lastDescOfPrefix.getClass());
3278     ClassDescriptor enclosingDescriptor;
3279     if (lastDescOfPrefix instanceof FieldDescriptor) {
3280       enclosingDescriptor = ((FieldDescriptor) lastDescOfPrefix).getType().getClassDesc();
3281       // System.out.println("enclosingDescriptor0=" + enclosingDescriptor);
3282     } else {
3283       // var descriptor case
3284       enclosingDescriptor = ((VarDescriptor) lastDescOfPrefix).getType().getClassDesc();
3285     }
3286     // System.out.println("enclosingDescriptor=" + enclosingDescriptor);
3287
3288     LocationDescriptor newLocDescriptor = generateNewLocationDescriptor();
3289     newLocDescriptor.setEnclosingClassDesc(enclosingDescriptor);
3290
3291     Location newLoc = new Location(enclosingDescriptor, newLocDescriptor.getSymbol());
3292     newLoc.setLocDescriptor(newLocDescriptor);
3293     newCompLoc.addLocation(newLoc);
3294
3295     // System.out.println("--newCompLoc=" + newCompLoc);
3296     return newCompLoc;
3297   }
3298
3299   private NTuple<Descriptor> calculatePrefixForParam(FlowGraph callerFlowGraph,
3300       FlowGraph calleeFlowGraph, MethodInvokeNode min, NTuple<Descriptor> arg1Tuple,
3301       FlowNode paramNode1) {
3302
3303     NTuple<Descriptor> baseTuple = mapMethodInvokeNodeToBaseTuple.get(min);
3304     Descriptor baseRef = baseTuple.get(baseTuple.size() - 1);
3305     System.out.println("baseRef=" + baseRef);
3306
3307     FlowNode flowNodeArg1 = callerFlowGraph.getFlowNode(arg1Tuple);
3308     List<NTuple<Descriptor>> callerPrefixList = calculatePrefixList(callerFlowGraph, flowNodeArg1);
3309     System.out.println("callerPrefixList=" + callerPrefixList);
3310
3311     List<NTuple<Descriptor>> prefixList = calculatePrefixList(calleeFlowGraph, paramNode1);
3312     System.out.println("###prefixList from node=" + paramNode1 + " =" + prefixList);
3313
3314     List<NTuple<Descriptor>> calleePrefixList =
3315         translatePrefixListToCallee(baseRef, min.getMethod(), callerPrefixList);
3316
3317     System.out.println("calleePrefixList=" + calleePrefixList);
3318
3319     Set<FlowNode> reachNodeSetFromParam1 = calleeFlowGraph.getReachFlowNodeSetFrom(paramNode1);
3320     System.out.println("reachNodeSetFromParam1=" + reachNodeSetFromParam1);
3321
3322     for (int i = 0; i < calleePrefixList.size(); i++) {
3323       NTuple<Descriptor> curPrefix = calleePrefixList.get(i);
3324       Set<NTuple<Descriptor>> reachableCommonPrefixSet = new HashSet<NTuple<Descriptor>>();
3325
3326       for (Iterator iterator2 = reachNodeSetFromParam1.iterator(); iterator2.hasNext();) {
3327         FlowNode reachNode = (FlowNode) iterator2.next();
3328         if (reachNode.getCurrentDescTuple().startsWith(curPrefix)) {
3329           reachableCommonPrefixSet.add(reachNode.getCurrentDescTuple());
3330         }
3331       }
3332
3333       if (!reachableCommonPrefixSet.isEmpty()) {
3334         System.out.println("###REACHABLECOMONPREFIX=" + reachableCommonPrefixSet
3335             + " with curPreFix=" + curPrefix);
3336         return curPrefix;
3337       }
3338
3339     }
3340
3341     return null;
3342   }
3343
3344   private List<NTuple<Descriptor>> translatePrefixListToCallee(Descriptor baseRef,
3345       MethodDescriptor mdCallee, List<NTuple<Descriptor>> callerPrefixList) {
3346
3347     List<NTuple<Descriptor>> calleePrefixList = new ArrayList<NTuple<Descriptor>>();
3348
3349     for (int i = 0; i < callerPrefixList.size(); i++) {
3350       NTuple<Descriptor> prefix = callerPrefixList.get(i);
3351       if (prefix.startsWith(baseRef)) {
3352         NTuple<Descriptor> calleePrefix = new NTuple<Descriptor>();
3353         calleePrefix.add(mdCallee.getThis());
3354         for (int k = 1; k < prefix.size(); k++) {
3355           calleePrefix.add(prefix.get(k));
3356         }
3357         calleePrefixList.add(calleePrefix);
3358       }
3359     }
3360
3361     return calleePrefixList;
3362
3363   }
3364
3365   private List<NTuple<Descriptor>> calculatePrefixList(FlowGraph flowGraph, FlowNode flowNode) {
3366
3367     System.out.println("\n##### calculatePrefixList=" + flowNode);
3368
3369     Set<FlowNode> inNodeSet = flowGraph.getIncomingFlowNodeSet(flowNode);
3370     inNodeSet.add(flowNode);
3371
3372     System.out.println("inNodeSet=" + inNodeSet);
3373
3374     List<NTuple<Descriptor>> prefixList = new ArrayList<NTuple<Descriptor>>();
3375
3376     for (Iterator iterator = inNodeSet.iterator(); iterator.hasNext();) {
3377       FlowNode inNode = (FlowNode) iterator.next();
3378
3379       NTuple<Descriptor> inNodeTuple = inNode.getCurrentDescTuple();
3380
3381       // CompositeLocation inNodeInferredLoc =
3382       // generateInferredCompositeLocation(methodInfo, inNodeTuple);
3383       // NTuple<Location> inNodeInferredLocTuple = inNodeInferredLoc.getTuple();
3384
3385       for (int i = 1; i < inNodeTuple.size(); i++) {
3386         NTuple<Descriptor> prefix = inNodeTuple.subList(0, i);
3387         if (!prefixList.contains(prefix)) {
3388           prefixList.add(prefix);
3389         }
3390       }
3391     }
3392
3393     Collections.sort(prefixList, new Comparator<NTuple<Descriptor>>() {
3394       public int compare(NTuple<Descriptor> arg0, NTuple<Descriptor> arg1) {
3395         int s0 = arg0.size();
3396         int s1 = arg1.size();
3397         if (s0 > s1) {
3398           return -1;
3399         } else if (s0 == s1) {
3400           return 0;
3401         } else {
3402           return 1;
3403         }
3404       }
3405     });
3406
3407     return prefixList;
3408
3409   }
3410
3411   public CompositeLocation convertToCompositeLocation(MethodDescriptor md, NTuple<Descriptor> tuple) {
3412
3413     CompositeLocation compLoc = new CompositeLocation();
3414
3415     Descriptor enclosingDescriptor = md;
3416
3417     for (int i = 0; i < tuple.size(); i++) {
3418       Descriptor curDescriptor = tuple.get(i);
3419       Location locElement = new Location(enclosingDescriptor, curDescriptor.getSymbol());
3420       locElement.setLocDescriptor(curDescriptor);
3421       compLoc.addLocation(locElement);
3422
3423       if (curDescriptor instanceof VarDescriptor) {
3424         enclosingDescriptor = md.getClassDesc();
3425       } else if (curDescriptor instanceof NameDescriptor) {
3426         // it is "GLOBAL LOC" case!
3427         enclosingDescriptor = GLOBALDESC;
3428       } else if (curDescriptor instanceof InterDescriptor) {
3429         enclosingDescriptor = null;
3430       } else {
3431         enclosingDescriptor = ((FieldDescriptor) curDescriptor).getClassDescriptor();
3432       }
3433
3434     }
3435
3436     return compLoc;
3437   }
3438
3439   private LocationDescriptor generateNewLocationDescriptor() {
3440     return new LocationDescriptor("Loc" + (locSeed++));
3441   }
3442
3443   private int getPrefixIndex(NTuple<Descriptor> tuple1, NTuple<Descriptor> tuple2) {
3444
3445     // return the index where the prefix shared by tuple1 and tuple2 is ended
3446     // if there is no prefix shared by both of them, return -1
3447
3448     int minSize = tuple1.size();
3449     if (minSize > tuple2.size()) {
3450       minSize = tuple2.size();
3451     }
3452
3453     int idx = -1;
3454     for (int i = 0; i < minSize; i++) {
3455       if (!tuple1.get(i).equals(tuple2.get(i))) {
3456         break;
3457       } else {
3458         idx++;
3459       }
3460     }
3461
3462     return idx;
3463   }
3464
3465   private CompositeLocation generateInferredCompositeLocation(MethodLocationInfo methodInfo,
3466       NTuple<Location> tuple) {
3467
3468     // first, retrieve inferred location by the local var descriptor
3469     CompositeLocation inferLoc = new CompositeLocation();
3470
3471     CompositeLocation localVarInferLoc =
3472         methodInfo.getInferLocation(tuple.get(0).getLocDescriptor());
3473
3474     localVarInferLoc.get(0).setLocDescriptor(tuple.get(0).getLocDescriptor());
3475
3476     for (int i = 0; i < localVarInferLoc.getSize(); i++) {
3477       inferLoc.addLocation(localVarInferLoc.get(i));
3478     }
3479
3480     for (int i = 1; i < tuple.size(); i++) {
3481       Location cur = tuple.get(i);
3482       Descriptor enclosingDesc = cur.getDescriptor();
3483       Descriptor curDesc = cur.getLocDescriptor();
3484
3485       Location inferLocElement;
3486       if (curDesc == null) {
3487         // in this case, we have a newly generated location.
3488         inferLocElement = new Location(enclosingDesc, cur.getLocIdentifier());
3489       } else {
3490         String fieldLocSymbol =
3491             getLocationInfo(enclosingDesc).getInferLocation(curDesc).get(0).getLocIdentifier();
3492         inferLocElement = new Location(enclosingDesc, fieldLocSymbol);
3493         inferLocElement.setLocDescriptor(curDesc);
3494       }
3495
3496       inferLoc.addLocation(inferLocElement);
3497
3498     }
3499
3500     assert (inferLoc.get(0).getLocDescriptor().getSymbol() == inferLoc.get(0).getLocIdentifier());
3501     return inferLoc;
3502   }
3503
3504   public LocationInfo getLocationInfo(Descriptor d) {
3505     if (d instanceof MethodDescriptor) {
3506       return getMethodLocationInfo((MethodDescriptor) d);
3507     } else {
3508       return getFieldLocationInfo((ClassDescriptor) d);
3509     }
3510   }
3511
3512   private MethodLocationInfo getMethodLocationInfo(MethodDescriptor md) {
3513
3514     if (!mapMethodDescToMethodLocationInfo.containsKey(md)) {
3515       mapMethodDescToMethodLocationInfo.put(md, new MethodLocationInfo(md));
3516     }
3517
3518     return mapMethodDescToMethodLocationInfo.get(md);
3519
3520   }
3521
3522   private LocationInfo getFieldLocationInfo(ClassDescriptor cd) {
3523
3524     if (!mapClassToLocationInfo.containsKey(cd)) {
3525       mapClassToLocationInfo.put(cd, new LocationInfo(cd));
3526     }
3527
3528     return mapClassToLocationInfo.get(cd);
3529
3530   }
3531
3532   private void addPrefixMapping(Map<NTuple<Location>, Set<NTuple<Location>>> map,
3533       NTuple<Location> prefix, NTuple<Location> element) {
3534
3535     if (!map.containsKey(prefix)) {
3536       map.put(prefix, new HashSet<NTuple<Location>>());
3537     }
3538     map.get(prefix).add(element);
3539   }
3540
3541   private boolean containsNonPrimitiveElement(Set<Descriptor> descSet) {
3542     for (Iterator iterator = descSet.iterator(); iterator.hasNext();) {
3543       Descriptor desc = (Descriptor) iterator.next();
3544
3545       if (desc.equals(LocationInference.GLOBALDESC)) {
3546         return true;
3547       } else if (desc instanceof VarDescriptor) {
3548         if (!((VarDescriptor) desc).getType().isPrimitive()) {
3549           return true;
3550         }
3551       } else if (desc instanceof FieldDescriptor) {
3552         if (!((FieldDescriptor) desc).getType().isPrimitive()) {
3553           return true;
3554         }
3555       }
3556
3557     }
3558     return false;
3559   }
3560
3561   private SSJavaLattice<String> getLattice(Descriptor d) {
3562     if (d instanceof MethodDescriptor) {
3563       return getMethodLattice((MethodDescriptor) d);
3564     } else {
3565       return getFieldLattice((ClassDescriptor) d);
3566     }
3567   }
3568
3569   private SSJavaLattice<String> getMethodLattice(MethodDescriptor md) {
3570     if (!md2lattice.containsKey(md)) {
3571       md2lattice.put(md, new SSJavaLattice<String>(SSJavaAnalysis.TOP, SSJavaAnalysis.BOTTOM));
3572     }
3573     return md2lattice.get(md);
3574   }
3575
3576   private void setMethodLattice(MethodDescriptor md, SSJavaLattice<String> lattice) {
3577     md2lattice.put(md, lattice);
3578   }
3579
3580   private void extractFlowsBetweenFields(ClassDescriptor cd, FlowNode srcNode, FlowNode dstNode,
3581       int idx) {
3582
3583     NTuple<Descriptor> srcCurTuple = srcNode.getCurrentDescTuple();
3584     NTuple<Descriptor> dstCurTuple = dstNode.getCurrentDescTuple();
3585
3586     if (srcCurTuple.get(idx).equals(dstCurTuple.get(idx)) && srcCurTuple.size() > (idx + 1)
3587         && dstCurTuple.size() > (idx + 1)) {
3588       // value flow between fields: we don't need to add a binary relation
3589       // for this case
3590
3591       Descriptor desc = srcCurTuple.get(idx);
3592       ClassDescriptor classDesc;
3593
3594       if (idx == 0) {
3595         classDesc = ((VarDescriptor) desc).getType().getClassDesc();
3596       } else {
3597         if (desc instanceof FieldDescriptor) {
3598           classDesc = ((FieldDescriptor) desc).getType().getClassDesc();
3599         } else {
3600           // this case is that the local variable has a composite location assignment
3601           // the following element after the composite location to the local variable
3602           // has the enclosing descriptor of the local variable
3603           Descriptor localDesc = srcNode.getDescTuple().get(0);
3604           classDesc = ((VarDescriptor) localDesc).getType().getClassDesc();
3605         }
3606       }
3607       extractFlowsBetweenFields(classDesc, srcNode, dstNode, idx + 1);
3608
3609     } else {
3610
3611       Descriptor srcFieldDesc = srcCurTuple.get(idx);
3612       Descriptor dstFieldDesc = dstCurTuple.get(idx);
3613
3614       // add a new edge
3615       getHierarchyGraph(cd).addEdge(srcFieldDesc, dstFieldDesc);
3616
3617     }
3618
3619   }
3620
3621   public SSJavaLattice<String> getFieldLattice(ClassDescriptor cd) {
3622     if (!cd2lattice.containsKey(cd)) {
3623       cd2lattice.put(cd, new SSJavaLattice<String>(SSJavaAnalysis.TOP, SSJavaAnalysis.BOTTOM));
3624     }
3625     return cd2lattice.get(cd);
3626   }
3627
3628   public LinkedList<MethodDescriptor> computeMethodList() {
3629
3630     Set<MethodDescriptor> toSort = new HashSet<MethodDescriptor>();
3631
3632     setupToAnalyze();
3633
3634     Set<MethodDescriptor> visited = new HashSet<MethodDescriptor>();
3635     Set<MethodDescriptor> reachableCallee = new HashSet<MethodDescriptor>();
3636
3637     while (!toAnalyzeIsEmpty()) {
3638       ClassDescriptor cd = toAnalyzeNext();
3639
3640       setupToAnalazeMethod(cd);
3641       temp_toanalyzeMethodList.removeAll(visited);
3642
3643       while (!toAnalyzeMethodIsEmpty()) {
3644         MethodDescriptor md = toAnalyzeMethodNext();
3645         if ((!visited.contains(md))
3646             && (ssjava.needTobeAnnotated(md) || reachableCallee.contains(md))) {
3647
3648           // creates a mapping from a method descriptor to virtual methods
3649           Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
3650           if (md.isStatic()) {
3651             setPossibleCallees.add(md);
3652           } else {
3653             setPossibleCallees.addAll(ssjava.getCallGraph().getMethods(md));
3654           }
3655
3656           Set<MethodDescriptor> calleeSet = ssjava.getCallGraph().getCalleeSet(md);
3657           Set<MethodDescriptor> needToAnalyzeCalleeSet = new HashSet<MethodDescriptor>();
3658
3659           for (Iterator iterator = calleeSet.iterator(); iterator.hasNext();) {
3660             MethodDescriptor calleemd = (MethodDescriptor) iterator.next();
3661             if ((!ssjava.isTrustMethod(calleemd))
3662                 && (!ssjava.isSSJavaUtil(calleemd.getClassDesc()))) {
3663               if (!visited.contains(calleemd)) {
3664                 temp_toanalyzeMethodList.add(calleemd);
3665               }
3666               reachableCallee.add(calleemd);
3667               needToAnalyzeCalleeSet.add(calleemd);
3668             }
3669           }
3670
3671           mapMethodToCalleeSet.put(md, needToAnalyzeCalleeSet);
3672
3673           visited.add(md);
3674
3675           toSort.add(md);
3676         }
3677       }
3678     }
3679
3680     return ssjava.topologicalSort(toSort);
3681
3682   }
3683
3684   public boolean isTransitivelyCalledFrom(MethodDescriptor callee, MethodDescriptor caller) {
3685     // if the callee is transitively invoked from the caller
3686     // return true;
3687
3688     int callerIdx = toanalyze_methodDescList.indexOf(caller);
3689     int calleeIdx = toanalyze_methodDescList.indexOf(callee);
3690
3691     if (callerIdx < calleeIdx) {
3692       return true;
3693     }
3694
3695     return false;
3696
3697   }
3698
3699   public void constructFlowGraph() {
3700
3701     System.out.println("");
3702     toanalyze_methodDescList = computeMethodList();
3703
3704     LinkedList<MethodDescriptor> methodDescList =
3705         (LinkedList<MethodDescriptor>) toanalyze_methodDescList.clone();
3706
3707     System.out.println("@@@methodDescList=" + methodDescList);
3708     // System.exit(0);
3709
3710     while (!methodDescList.isEmpty()) {
3711       MethodDescriptor md = methodDescList.removeLast();
3712       if (state.SSJAVADEBUG) {
3713         System.out.println();
3714         System.out.println("SSJAVA: Constructing a flow graph: " + md);
3715
3716         // creates a mapping from a parameter descriptor to its index
3717         Map<Descriptor, Integer> mapParamDescToIdx = new HashMap<Descriptor, Integer>();
3718         int offset = 0;
3719         if (!md.isStatic()) {
3720           offset = 1;
3721           mapParamDescToIdx.put(md.getThis(), 0);
3722         }
3723
3724         for (int i = 0; i < md.numParameters(); i++) {
3725           Descriptor paramDesc = (Descriptor) md.getParameter(i);
3726           mapParamDescToIdx.put(paramDesc, new Integer(i + offset));
3727         }
3728
3729         FlowGraph fg = new FlowGraph(md, mapParamDescToIdx);
3730         mapMethodDescriptorToFlowGraph.put(md, fg);
3731
3732         analyzeMethodBody(md.getClassDesc(), md);
3733
3734         // System.out.println("##constructSubGlobalFlowGraph");
3735         // GlobalFlowGraph subGlobalFlowGraph = constructSubGlobalFlowGraph(fg);
3736         // mapMethodDescriptorToSubGlobalFlowGraph.put(md, subGlobalFlowGraph);
3737         //
3738         // // TODO
3739         // System.out.println("##addValueFlowsFromCalleeSubGlobalFlowGraph");
3740         // addValueFlowsFromCalleeSubGlobalFlowGraph(md, subGlobalFlowGraph);
3741         // subGlobalFlowGraph.writeGraph("_SUBGLOBAL");
3742         //
3743         // propagateFlowsFromCalleesWithNoCompositeLocation(md);
3744
3745       }
3746     }
3747     // _debug_printGraph();
3748
3749     methodDescList = (LinkedList<MethodDescriptor>) toanalyze_methodDescList.clone();
3750
3751     while (!methodDescList.isEmpty()) {
3752       MethodDescriptor md = methodDescList.removeLast();
3753       if (state.SSJAVADEBUG) {
3754         System.out.println();
3755         System.out.println("SSJAVA: Constructing a sub global flow graph: " + md);
3756
3757         GlobalFlowGraph subGlobalFlowGraph = constructSubGlobalFlowGraph(getFlowGraph(md));
3758         mapMethodDescriptorToSubGlobalFlowGraph.put(md, subGlobalFlowGraph);
3759
3760         // TODO
3761         System.out.println("-add Value Flows From CalleeSubGlobalFlowGraph");
3762         addValueFlowsFromCalleeSubGlobalFlowGraph(md, subGlobalFlowGraph);
3763         subGlobalFlowGraph.writeGraph("_SUBGLOBAL");
3764
3765         System.out.println("-propagate Flows From Callees With No CompositeLocation");
3766         propagateFlowsFromCalleesWithNoCompositeLocation(md);
3767
3768       }
3769     }
3770
3771   }
3772
3773   private Set<MethodInvokeNode> getMethodInvokeNodeSet(MethodDescriptor md) {
3774     if (!mapMethodDescriptorToMethodInvokeNodeSet.containsKey(md)) {
3775       mapMethodDescriptorToMethodInvokeNodeSet.put(md, new HashSet<MethodInvokeNode>());
3776     }
3777     return mapMethodDescriptorToMethodInvokeNodeSet.get(md);
3778   }
3779
3780   private void constructSubGlobalFlowGraph(MethodDescriptor md) {
3781
3782     FlowGraph flowGraph = getFlowGraph(md);
3783
3784     Set<MethodInvokeNode> setMethodInvokeNode = getMethodInvokeNodeSet(md);
3785
3786     for (Iterator<MethodInvokeNode> iter = setMethodInvokeNode.iterator(); iter.hasNext();) {
3787       MethodInvokeNode min = iter.next();
3788       propagateFlowsFromMethodInvokeNode(md, min);
3789     }
3790
3791   }
3792
3793   private void propagateFlowsFromMethodInvokeNode(MethodDescriptor mdCaller, MethodInvokeNode min) {
3794     // the transformation for a call site propagates flows through parameters
3795     // if the method is virtual, it also grab all relations from any possible
3796     // callees
3797
3798     MethodDescriptor mdCallee = min.getMethod();
3799     Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
3800     if (mdCallee.isStatic()) {
3801       setPossibleCallees.add(mdCallee);
3802     } else {
3803       Set<MethodDescriptor> calleeSet = ssjava.getCallGraph().getMethods(mdCallee);
3804       // removes method descriptors that are not invoked by the caller
3805       calleeSet.retainAll(mapMethodToCalleeSet.get(mdCaller));
3806       setPossibleCallees.addAll(calleeSet);
3807     }
3808
3809     for (Iterator iterator2 = setPossibleCallees.iterator(); iterator2.hasNext();) {
3810       MethodDescriptor possibleMdCallee = (MethodDescriptor) iterator2.next();
3811       contributeCalleeFlows(min, mdCaller, possibleMdCallee);
3812     }
3813
3814   }
3815
3816   private void assignCompositeLocation(MethodDescriptor md) {
3817
3818     FlowGraph flowGraph = getFlowGraph(md);
3819
3820     Set<FlowNode> nodeSet = flowGraph.getNodeSet();
3821
3822     next: for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
3823       FlowNode flowNode = (FlowNode) iterator.next();
3824
3825       // assign a composite location only to the local variable
3826       if (flowNode.getCurrentDescTuple().size() == 1) {
3827
3828         List<NTuple<Descriptor>> prefixList = calculatePrefixList(flowGraph, flowNode);
3829         Set<FlowNode> reachSet = flowGraph.getReachFlowNodeSetFrom(flowNode);
3830
3831         for (int i = 0; i < prefixList.size(); i++) {
3832           NTuple<Descriptor> curPrefix = prefixList.get(i);
3833           Set<NTuple<Descriptor>> reachableCommonPrefixSet = new HashSet<NTuple<Descriptor>>();
3834
3835           for (Iterator iterator2 = reachSet.iterator(); iterator2.hasNext();) {
3836             FlowNode reachNode = (FlowNode) iterator2.next();
3837             if (reachNode.getCurrentDescTuple().startsWith(curPrefix)) {
3838               reachableCommonPrefixSet.add(reachNode.getCurrentDescTuple());
3839             }
3840           }
3841
3842           if (!reachableCommonPrefixSet.isEmpty()) {
3843             System.out.println("NEED TO ASSIGN COMP LOC TO " + flowNode + " with prefix="
3844                 + curPrefix);
3845             CompositeLocation newCompLoc = generateCompositeLocation(md, curPrefix);
3846             flowNode.setCompositeLocation(newCompLoc);
3847             continue next;
3848           }
3849
3850         }
3851       }
3852
3853     }
3854
3855   }
3856
3857   private void propagateFlowsFromCalleesWithNoCompositeLocation(MethodDescriptor mdCaller) {
3858
3859     // the transformation for a call site propagates flows through parameters
3860     // if the method is virtual, it also grab all relations from any possible
3861     // callees
3862
3863     Set<MethodInvokeNode> setMethodInvokeNode =
3864         mapMethodDescriptorToMethodInvokeNodeSet.get(mdCaller);
3865
3866     if (setMethodInvokeNode != null) {
3867
3868       for (Iterator iterator = setMethodInvokeNode.iterator(); iterator.hasNext();) {
3869         MethodInvokeNode min = (MethodInvokeNode) iterator.next();
3870         MethodDescriptor mdCallee = min.getMethod();
3871         Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
3872         if (mdCallee.isStatic()) {
3873           setPossibleCallees.add(mdCallee);
3874         } else {
3875           Set<MethodDescriptor> calleeSet = ssjava.getCallGraph().getMethods(mdCallee);
3876           // removes method descriptors that are not invoked by the caller
3877           calleeSet.retainAll(mapMethodToCalleeSet.get(mdCaller));
3878           setPossibleCallees.addAll(calleeSet);
3879         }
3880
3881         for (Iterator iterator2 = setPossibleCallees.iterator(); iterator2.hasNext();) {
3882           MethodDescriptor possibleMdCallee = (MethodDescriptor) iterator2.next();
3883           propagateFlowsToCallerWithNoCompositeLocation(min, mdCaller, possibleMdCallee);
3884         }
3885
3886       }
3887     }
3888
3889   }
3890
3891   private void propagateFlowsFromCallees(MethodDescriptor mdCaller) {
3892
3893     // the transformation for a call site propagates flows through parameters
3894     // if the method is virtual, it also grab all relations from any possible
3895     // callees
3896
3897     Set<MethodInvokeNode> setMethodInvokeNode =
3898         mapMethodDescriptorToMethodInvokeNodeSet.get(mdCaller);
3899
3900     if (setMethodInvokeNode != null) {
3901
3902       for (Iterator iterator = setMethodInvokeNode.iterator(); iterator.hasNext();) {
3903         MethodInvokeNode min = (MethodInvokeNode) iterator.next();
3904         MethodDescriptor mdCallee = min.getMethod();
3905         Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
3906         if (mdCallee.isStatic()) {
3907           setPossibleCallees.add(mdCallee);
3908         } else {
3909           Set<MethodDescriptor> calleeSet = ssjava.getCallGraph().getMethods(mdCallee);
3910           // removes method descriptors that are not invoked by the caller
3911           calleeSet.retainAll(mapMethodToCalleeSet.get(mdCaller));
3912           setPossibleCallees.addAll(calleeSet);
3913         }
3914
3915         for (Iterator iterator2 = setPossibleCallees.iterator(); iterator2.hasNext();) {
3916           MethodDescriptor possibleMdCallee = (MethodDescriptor) iterator2.next();
3917           propagateFlowsToCaller(min, mdCaller, possibleMdCallee);
3918         }
3919
3920       }
3921     }
3922
3923   }
3924
3925   private void analyzeMethodBody(ClassDescriptor cd, MethodDescriptor md) {
3926     BlockNode bn = state.getMethodBody(md);
3927     NodeTupleSet implicitFlowTupleSet = new NodeTupleSet();
3928     analyzeFlowBlockNode(md, md.getParameterTable(), bn, implicitFlowTupleSet);
3929   }
3930
3931   private void analyzeFlowBlockNode(MethodDescriptor md, SymbolTable nametable, BlockNode bn,
3932       NodeTupleSet implicitFlowTupleSet) {
3933
3934     bn.getVarTable().setParent(nametable);
3935     for (int i = 0; i < bn.size(); i++) {
3936       BlockStatementNode bsn = bn.get(i);
3937       analyzeBlockStatementNode(md, bn.getVarTable(), bsn, implicitFlowTupleSet);
3938     }
3939
3940   }
3941
3942   private void analyzeBlockStatementNode(MethodDescriptor md, SymbolTable nametable,
3943       BlockStatementNode bsn, NodeTupleSet implicitFlowTupleSet) {
3944
3945     switch (bsn.kind()) {
3946     case Kind.BlockExpressionNode:
3947       analyzeBlockExpressionNode(md, nametable, (BlockExpressionNode) bsn, implicitFlowTupleSet);
3948       break;
3949
3950     case Kind.DeclarationNode:
3951       analyzeFlowDeclarationNode(md, nametable, (DeclarationNode) bsn, implicitFlowTupleSet);
3952       break;
3953
3954     case Kind.IfStatementNode:
3955       analyzeFlowIfStatementNode(md, nametable, (IfStatementNode) bsn, implicitFlowTupleSet);
3956       break;
3957
3958     case Kind.LoopNode:
3959       analyzeFlowLoopNode(md, nametable, (LoopNode) bsn, implicitFlowTupleSet);
3960       break;
3961
3962     case Kind.ReturnNode:
3963       analyzeFlowReturnNode(md, nametable, (ReturnNode) bsn, implicitFlowTupleSet);
3964       break;
3965
3966     case Kind.SubBlockNode:
3967       analyzeFlowSubBlockNode(md, nametable, (SubBlockNode) bsn, implicitFlowTupleSet);
3968       break;
3969
3970     case Kind.ContinueBreakNode:
3971       break;
3972
3973     case Kind.SwitchStatementNode:
3974       analyzeSwitchStatementNode(md, nametable, (SwitchStatementNode) bsn, implicitFlowTupleSet);
3975       break;
3976
3977     }
3978
3979   }
3980
3981   private void analyzeSwitchBlockNode(MethodDescriptor md, SymbolTable nametable,
3982       SwitchBlockNode sbn, NodeTupleSet implicitFlowTupleSet) {
3983
3984     analyzeFlowBlockNode(md, nametable, sbn.getSwitchBlockStatement(), implicitFlowTupleSet);
3985
3986   }
3987
3988   private void analyzeSwitchStatementNode(MethodDescriptor md, SymbolTable nametable,
3989       SwitchStatementNode ssn, NodeTupleSet implicitFlowTupleSet) {
3990
3991     NodeTupleSet condTupleNode = new NodeTupleSet();
3992     analyzeFlowExpressionNode(md, nametable, ssn.getCondition(), condTupleNode, null,
3993         implicitFlowTupleSet, false);
3994
3995     NodeTupleSet newImplicitTupleSet = new NodeTupleSet();
3996
3997     newImplicitTupleSet.addTupleSet(implicitFlowTupleSet);
3998     newImplicitTupleSet.addTupleSet(condTupleNode);
3999
4000     if (newImplicitTupleSet.size() > 1) {
4001       // need to create an intermediate node for the GLB of conditional
4002       // locations & implicit flows
4003       NTuple<Descriptor> interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
4004       for (Iterator<NTuple<Descriptor>> idxIter = newImplicitTupleSet.iterator(); idxIter.hasNext();) {
4005         NTuple<Descriptor> tuple = idxIter.next();
4006         addFlowGraphEdge(md, tuple, interTuple);
4007       }
4008       newImplicitTupleSet.clear();
4009       newImplicitTupleSet.addTuple(interTuple);
4010     }
4011
4012     BlockNode sbn = ssn.getSwitchBody();
4013     for (int i = 0; i < sbn.size(); i++) {
4014       analyzeSwitchBlockNode(md, nametable, (SwitchBlockNode) sbn.get(i), newImplicitTupleSet);
4015     }
4016
4017   }
4018
4019   private void analyzeFlowSubBlockNode(MethodDescriptor md, SymbolTable nametable,
4020       SubBlockNode sbn, NodeTupleSet implicitFlowTupleSet) {
4021     analyzeFlowBlockNode(md, nametable, sbn.getBlockNode(), implicitFlowTupleSet);
4022   }
4023
4024   private void analyzeFlowReturnNode(MethodDescriptor md, SymbolTable nametable, ReturnNode rn,
4025       NodeTupleSet implicitFlowTupleSet) {
4026
4027     ExpressionNode returnExp = rn.getReturnExpression();
4028
4029     if (returnExp != null) {
4030       NodeTupleSet nodeSet = new NodeTupleSet();
4031       // if a return expression returns a literal value, nodeSet is empty
4032       analyzeFlowExpressionNode(md, nametable, returnExp, nodeSet, false);
4033       FlowGraph fg = getFlowGraph(md);
4034
4035       // if (implicitFlowTupleSet.size() == 1
4036       // &&
4037       // fg.getFlowNode(implicitFlowTupleSet.iterator().next()).isIntermediate())
4038       // {
4039       //
4040       // // since there is already an intermediate node for the GLB of implicit
4041       // flows
4042       // // we don't need to create another intermediate node.
4043       // // just re-use the intermediate node for implicit flows.
4044       //
4045       // FlowNode meetNode =
4046       // fg.getFlowNode(implicitFlowTupleSet.iterator().next());
4047       //
4048       // for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
4049       // NTuple<Descriptor> returnNodeTuple = (NTuple<Descriptor>)
4050       // iterator.next();
4051       // fg.addValueFlowEdge(returnNodeTuple, meetNode.getDescTuple());
4052       // }
4053       //
4054       // }
4055
4056       NodeTupleSet currentFlowTupleSet = new NodeTupleSet();
4057
4058       // add tuples from return node
4059       currentFlowTupleSet.addTupleSet(nodeSet);
4060
4061       // add tuples corresponding to the current implicit flows
4062       currentFlowTupleSet.addTupleSet(implicitFlowTupleSet);
4063
4064       if (currentFlowTupleSet.size() > 1) {
4065         FlowNode meetNode = fg.createIntermediateNode();
4066         for (Iterator iterator = currentFlowTupleSet.iterator(); iterator.hasNext();) {
4067           NTuple<Descriptor> currentFlowTuple = (NTuple<Descriptor>) iterator.next();
4068           fg.addValueFlowEdge(currentFlowTuple, meetNode.getDescTuple());
4069         }
4070         fg.addReturnFlowNode(meetNode.getDescTuple());
4071       } else if (currentFlowTupleSet.size() == 1) {
4072         NTuple<Descriptor> tuple = currentFlowTupleSet.iterator().next();
4073         fg.addReturnFlowNode(tuple);
4074       }
4075
4076     }
4077
4078   }
4079
4080   private void analyzeFlowLoopNode(MethodDescriptor md, SymbolTable nametable, LoopNode ln,
4081       NodeTupleSet implicitFlowTupleSet) {
4082
4083     if (ln.getType() == LoopNode.WHILELOOP || ln.getType() == LoopNode.DOWHILELOOP) {
4084
4085       NodeTupleSet condTupleNode = new NodeTupleSet();
4086       analyzeFlowExpressionNode(md, nametable, ln.getCondition(), condTupleNode, null,
4087           implicitFlowTupleSet, false);
4088
4089       NodeTupleSet newImplicitTupleSet = new NodeTupleSet();
4090
4091       newImplicitTupleSet.addTupleSet(implicitFlowTupleSet);
4092       newImplicitTupleSet.addTupleSet(condTupleNode);
4093
4094       if (newImplicitTupleSet.size() > 1) {
4095         // need to create an intermediate node for the GLB of conditional
4096         // locations & implicit flows
4097         NTuple<Descriptor> interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
4098         for (Iterator<NTuple<Descriptor>> idxIter = newImplicitTupleSet.iterator(); idxIter
4099             .hasNext();) {
4100           NTuple<Descriptor> tuple = idxIter.next();
4101           addFlowGraphEdge(md, tuple, interTuple);
4102         }
4103         newImplicitTupleSet.clear();
4104         newImplicitTupleSet.addTuple(interTuple);
4105
4106       }
4107
4108       // ///////////
4109       // System.out.println("condTupleNode="+condTupleNode);
4110       // NTuple<Descriptor> interTuple =
4111       // getFlowGraph(md).createIntermediateNode().getDescTuple();
4112       //
4113       // for (Iterator<NTuple<Descriptor>> idxIter = condTupleNode.iterator();
4114       // idxIter.hasNext();) {
4115       // NTuple<Descriptor> tuple = idxIter.next();
4116       // addFlowGraphEdge(md, tuple, interTuple);
4117       // }
4118
4119       // for (Iterator<NTuple<Descriptor>> idxIter =
4120       // implicitFlowTupleSet.iterator(); idxIter
4121       // .hasNext();) {
4122       // NTuple<Descriptor> tuple = idxIter.next();
4123       // addFlowGraphEdge(md, tuple, interTuple);
4124       // }
4125
4126       // NodeTupleSet newImplicitSet = new NodeTupleSet();
4127       // newImplicitSet.addTuple(interTuple);
4128       analyzeFlowBlockNode(md, nametable, ln.getBody(), newImplicitTupleSet);
4129       // ///////////
4130
4131       // condTupleNode.addTupleSet(implicitFlowTupleSet);
4132
4133       // add edges from condNodeTupleSet to all nodes of conditional nodes
4134       // analyzeFlowBlockNode(md, nametable, ln.getBody(), condTupleNode);
4135
4136     } else {
4137       // check 'for loop' case
4138       BlockNode bn = ln.getInitializer();
4139       bn.getVarTable().setParent(nametable);
4140       for (int i = 0; i < bn.size(); i++) {
4141         BlockStatementNode bsn = bn.get(i);
4142         analyzeBlockStatementNode(md, bn.getVarTable(), bsn, implicitFlowTupleSet);
4143       }
4144
4145       NodeTupleSet condTupleNode = new NodeTupleSet();
4146       analyzeFlowExpressionNode(md, bn.getVarTable(), ln.getCondition(), condTupleNode, null,
4147           implicitFlowTupleSet, false);
4148
4149       // ///////////
4150       NTuple<Descriptor> interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
4151
4152       for (Iterator<NTuple<Descriptor>> idxIter = condTupleNode.iterator(); idxIter.hasNext();) {
4153         NTuple<Descriptor> tuple = idxIter.next();
4154         addFlowGraphEdge(md, tuple, interTuple);
4155       }
4156
4157       for (Iterator<NTuple<Descriptor>> idxIter = implicitFlowTupleSet.iterator(); idxIter
4158           .hasNext();) {
4159         NTuple<Descriptor> tuple = idxIter.next();
4160         addFlowGraphEdge(md, tuple, interTuple);
4161       }
4162
4163       NodeTupleSet newImplicitSet = new NodeTupleSet();
4164       newImplicitSet.addTuple(interTuple);
4165       analyzeFlowBlockNode(md, bn.getVarTable(), ln.getUpdate(), newImplicitSet);
4166       analyzeFlowBlockNode(md, bn.getVarTable(), ln.getBody(), newImplicitSet);
4167       // ///////////
4168
4169       // condTupleNode.addTupleSet(implicitFlowTupleSet);
4170       //
4171       // analyzeFlowBlockNode(md, bn.getVarTable(), ln.getUpdate(),
4172       // condTupleNode);
4173       // analyzeFlowBlockNode(md, bn.getVarTable(), ln.getBody(),
4174       // condTupleNode);
4175
4176     }
4177
4178   }
4179
4180   private void analyzeFlowIfStatementNode(MethodDescriptor md, SymbolTable nametable,
4181       IfStatementNode isn, NodeTupleSet implicitFlowTupleSet) {
4182
4183     // System.out.println("analyzeFlowIfStatementNode=" + isn.printNode(0));
4184
4185     NodeTupleSet condTupleNode = new NodeTupleSet();
4186     analyzeFlowExpressionNode(md, nametable, isn.getCondition(), condTupleNode, null,
4187         implicitFlowTupleSet, false);
4188
4189     NodeTupleSet newImplicitTupleSet = new NodeTupleSet();
4190
4191     newImplicitTupleSet.addTupleSet(implicitFlowTupleSet);
4192     newImplicitTupleSet.addTupleSet(condTupleNode);
4193
4194     // System.out.println("condTupleNode=" + condTupleNode);
4195     // System.out.println("implicitFlowTupleSet=" + implicitFlowTupleSet);
4196     // System.out.println("newImplicitTupleSet=" + newImplicitTupleSet);
4197
4198     if (newImplicitTupleSet.size() > 1) {
4199
4200       // need to create an intermediate node for the GLB of conditional locations & implicit flows
4201       NTuple<Descriptor> interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
4202       for (Iterator<NTuple<Descriptor>> idxIter = newImplicitTupleSet.iterator(); idxIter.hasNext();) {
4203         NTuple<Descriptor> tuple = idxIter.next();
4204         addFlowGraphEdge(md, tuple, interTuple);
4205       }
4206       newImplicitTupleSet.clear();
4207       newImplicitTupleSet.addTuple(interTuple);
4208     }
4209
4210     analyzeFlowBlockNode(md, nametable, isn.getTrueBlock(), newImplicitTupleSet);
4211
4212     if (isn.getFalseBlock() != null) {
4213       analyzeFlowBlockNode(md, nametable, isn.getFalseBlock(), newImplicitTupleSet);
4214     }
4215
4216   }
4217
4218   private void analyzeFlowDeclarationNode(MethodDescriptor md, SymbolTable nametable,
4219       DeclarationNode dn, NodeTupleSet implicitFlowTupleSet) {
4220
4221     VarDescriptor vd = dn.getVarDescriptor();
4222     mapDescToDefinitionLine.put(vd, dn.getNumLine());
4223     NTuple<Descriptor> tupleLHS = new NTuple<Descriptor>();
4224     tupleLHS.add(vd);
4225     FlowNode fn = getFlowGraph(md).createNewFlowNode(tupleLHS);
4226     fn.setDeclarationNode();
4227
4228     if (dn.getExpression() != null) {
4229
4230       NodeTupleSet nodeSetRHS = new NodeTupleSet();
4231       analyzeFlowExpressionNode(md, nametable, dn.getExpression(), nodeSetRHS, null,
4232           implicitFlowTupleSet, false);
4233
4234       // creates edges from RHS to LHS
4235       NTuple<Descriptor> interTuple = null;
4236       if (nodeSetRHS.size() > 1) {
4237         interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
4238       }
4239
4240       for (Iterator<NTuple<Descriptor>> iter = nodeSetRHS.iterator(); iter.hasNext();) {
4241         NTuple<Descriptor> fromTuple = iter.next();
4242         addFlowGraphEdge(md, fromTuple, interTuple, tupleLHS);
4243       }
4244
4245       // creates edges from implicitFlowTupleSet to LHS
4246       for (Iterator<NTuple<Descriptor>> iter = implicitFlowTupleSet.iterator(); iter.hasNext();) {
4247         NTuple<Descriptor> implicitTuple = iter.next();
4248         addFlowGraphEdge(md, implicitTuple, tupleLHS);
4249       }
4250
4251     }
4252
4253   }
4254
4255   private void analyzeBlockExpressionNode(MethodDescriptor md, SymbolTable nametable,
4256       BlockExpressionNode ben, NodeTupleSet implicitFlowTupleSet) {
4257     analyzeFlowExpressionNode(md, nametable, ben.getExpression(), null, null, implicitFlowTupleSet,
4258         false);
4259   }
4260
4261   private NTuple<Descriptor> analyzeFlowExpressionNode(MethodDescriptor md, SymbolTable nametable,
4262       ExpressionNode en, NodeTupleSet nodeSet, boolean isLHS) {
4263     return analyzeFlowExpressionNode(md, nametable, en, nodeSet, null, new NodeTupleSet(), isLHS);
4264   }
4265
4266   private NTuple<Descriptor> analyzeFlowExpressionNode(MethodDescriptor md, SymbolTable nametable,
4267       ExpressionNode en, NodeTupleSet nodeSet, NTuple<Descriptor> base,
4268       NodeTupleSet implicitFlowTupleSet, boolean isLHS) {
4269
4270     // note that expression node can create more than one flow node
4271     // nodeSet contains of flow nodes
4272     // base is always assigned to null except the case of a name node!
4273
4274     NTuple<Descriptor> flowTuple;
4275
4276     switch (en.kind()) {
4277
4278     case Kind.AssignmentNode:
4279       analyzeFlowAssignmentNode(md, nametable, (AssignmentNode) en, nodeSet, base,
4280           implicitFlowTupleSet);
4281       break;
4282
4283     case Kind.FieldAccessNode:
4284       flowTuple =
4285           analyzeFlowFieldAccessNode(md, nametable, (FieldAccessNode) en, nodeSet, base,
4286               implicitFlowTupleSet, isLHS);
4287       if (flowTuple != null) {
4288         nodeSet.addTuple(flowTuple);
4289       }
4290       return flowTuple;
4291
4292     case Kind.NameNode:
4293       NodeTupleSet nameNodeSet = new NodeTupleSet();
4294       flowTuple =
4295           analyzeFlowNameNode(md, nametable, (NameNode) en, nameNodeSet, base, implicitFlowTupleSet);
4296       if (flowTuple != null) {
4297         nodeSet.addTuple(flowTuple);
4298       }
4299       return flowTuple;
4300
4301     case Kind.OpNode:
4302       analyzeFlowOpNode(md, nametable, (OpNode) en, nodeSet, implicitFlowTupleSet);
4303       break;
4304
4305     case Kind.CreateObjectNode:
4306       analyzeCreateObjectNode(md, nametable, (CreateObjectNode) en);
4307       break;
4308
4309     case Kind.ArrayAccessNode:
4310       analyzeFlowArrayAccessNode(md, nametable, (ArrayAccessNode) en, nodeSet, isLHS);
4311       break;
4312
4313     case Kind.LiteralNode:
4314       analyzeLiteralNode(md, nametable, (LiteralNode) en);
4315       break;
4316
4317     case Kind.MethodInvokeNode:
4318       analyzeFlowMethodInvokeNode(md, nametable, (MethodInvokeNode) en, nodeSet,
4319           implicitFlowTupleSet);
4320       break;
4321
4322     case Kind.TertiaryNode:
4323       analyzeFlowTertiaryNode(md, nametable, (TertiaryNode) en, nodeSet, implicitFlowTupleSet);
4324       break;
4325
4326     case Kind.CastNode:
4327       analyzeFlowCastNode(md, nametable, (CastNode) en, nodeSet, base, implicitFlowTupleSet);
4328       break;
4329     // case Kind.InstanceOfNode:
4330     // checkInstanceOfNode(md, nametable, (InstanceOfNode) en, td);
4331     // return null;
4332
4333     // case Kind.ArrayInitializerNode:
4334     // checkArrayInitializerNode(md, nametable, (ArrayInitializerNode) en,
4335     // td);
4336     // return null;
4337
4338     // case Kind.ClassTypeNode:
4339     // checkClassTypeNode(md, nametable, (ClassTypeNode) en, td);
4340     // return null;
4341
4342     // case Kind.OffsetNode:
4343     // checkOffsetNode(md, nametable, (OffsetNode)en, td);
4344     // return null;
4345
4346     }
4347     return null;
4348
4349   }
4350
4351   private void analyzeFlowCastNode(MethodDescriptor md, SymbolTable nametable, CastNode cn,
4352       NodeTupleSet nodeSet, NTuple<Descriptor> base, NodeTupleSet implicitFlowTupleSet) {
4353
4354     analyzeFlowExpressionNode(md, nametable, cn.getExpression(), nodeSet, base,
4355         implicitFlowTupleSet, false);
4356
4357   }
4358
4359   private void analyzeFlowTertiaryNode(MethodDescriptor md, SymbolTable nametable, TertiaryNode tn,
4360       NodeTupleSet nodeSet, NodeTupleSet implicitFlowTupleSet) {
4361
4362     NodeTupleSet tertiaryTupleNode = new NodeTupleSet();
4363     analyzeFlowExpressionNode(md, nametable, tn.getCond(), tertiaryTupleNode, null,
4364         implicitFlowTupleSet, false);
4365
4366     // add edges from tertiaryTupleNode to all nodes of conditional nodes
4367     tertiaryTupleNode.addTupleSet(implicitFlowTupleSet);
4368     analyzeFlowExpressionNode(md, nametable, tn.getTrueExpr(), tertiaryTupleNode, null,
4369         implicitFlowTupleSet, false);
4370
4371     analyzeFlowExpressionNode(md, nametable, tn.getFalseExpr(), tertiaryTupleNode, null,
4372         implicitFlowTupleSet, false);
4373
4374     nodeSet.addTupleSet(tertiaryTupleNode);
4375
4376   }
4377
4378   private void addMapCallerMethodDescToMethodInvokeNodeSet(MethodDescriptor caller,
4379       MethodInvokeNode min) {
4380     Set<MethodInvokeNode> set = mapMethodDescriptorToMethodInvokeNodeSet.get(caller);
4381     if (set == null) {
4382       set = new HashSet<MethodInvokeNode>();
4383       mapMethodDescriptorToMethodInvokeNodeSet.put(caller, set);
4384     }
4385     set.add(min);
4386   }
4387
4388   private void addParamNodeFlowingToReturnValue(MethodDescriptor md, FlowNode fn) {
4389
4390     if (!mapMethodDescToParamNodeFlowsToReturnValue.containsKey(md)) {
4391       mapMethodDescToParamNodeFlowsToReturnValue.put(md, new HashSet<FlowNode>());
4392     }
4393     mapMethodDescToParamNodeFlowsToReturnValue.get(md).add(fn);
4394   }
4395
4396   private Set<FlowNode> getParamNodeFlowingToReturnValue(MethodDescriptor md) {
4397
4398     if (!mapMethodDescToParamNodeFlowsToReturnValue.containsKey(md)) {
4399       mapMethodDescToParamNodeFlowsToReturnValue.put(md, new HashSet<FlowNode>());
4400     }
4401
4402     return mapMethodDescToParamNodeFlowsToReturnValue.get(md);
4403   }
4404
4405   private void analyzeFlowMethodInvokeNode(MethodDescriptor md, SymbolTable nametable,
4406       MethodInvokeNode min, NodeTupleSet nodeSet, NodeTupleSet implicitFlowTupleSet) {
4407
4408     System.out.println("analyzeFlowMethodInvokeNode=" + min.printNode(0));
4409
4410     mapMethodInvokeNodeToArgIdxMap.put(min, new HashMap<Integer, NTuple<Descriptor>>());
4411
4412     if (nodeSet == null) {
4413       nodeSet = new NodeTupleSet();
4414     }
4415
4416     MethodDescriptor calleeMethodDesc = min.getMethod();
4417
4418     NameDescriptor baseName = min.getBaseName();
4419     boolean isSystemout = false;
4420     if (baseName != null) {
4421       isSystemout = baseName.getSymbol().equals("System.out");
4422     }
4423
4424     if (!ssjava.isSSJavaUtil(calleeMethodDesc.getClassDesc())
4425         && !ssjava.isTrustMethod(calleeMethodDesc) && !isSystemout) {
4426
4427       addMapCallerMethodDescToMethodInvokeNodeSet(md, min);
4428
4429       FlowGraph calleeFlowGraph = getFlowGraph(calleeMethodDesc);
4430       Set<FlowNode> calleeReturnSet = calleeFlowGraph.getReturnNodeSet();
4431
4432       // System.out.println("-calleeReturnSet=" + calleeReturnSet);
4433
4434       if (min.getExpression() != null) {
4435
4436         NodeTupleSet baseNodeSet = new NodeTupleSet();
4437         analyzeFlowExpressionNode(md, nametable, min.getExpression(), baseNodeSet, null,
4438             implicitFlowTupleSet, false);
4439
4440         assert (baseNodeSet.size() == 1);
4441         NTuple<Descriptor> baseTuple = baseNodeSet.iterator().next();
4442         mapMethodInvokeNodeToBaseTuple.put(min, baseTuple);
4443
4444         if (!min.getMethod().isStatic()) {
4445           addArgIdxMap(min, 0, baseTuple);
4446
4447           for (Iterator iterator = calleeReturnSet.iterator(); iterator.hasNext();) {
4448             FlowNode returnNode = (FlowNode) iterator.next();
4449             NTuple<Descriptor> returnDescTuple = returnNode.getDescTuple();
4450             if (returnDescTuple.startsWith(calleeMethodDesc.getThis())) {
4451               // the location type of the return value is started with 'this'
4452               // reference
4453               NTuple<Descriptor> inFlowTuple = new NTuple<Descriptor>(baseTuple.getList());
4454               inFlowTuple.addAll(returnDescTuple.subList(1, returnDescTuple.size()));
4455               nodeSet.addTuple(inFlowTuple);
4456             } else {
4457               // TODO
4458               Set<FlowNode> inFlowSet = calleeFlowGraph.getIncomingFlowNodeSet(returnNode);
4459               // System.out.println("inFlowSet=" + inFlowSet + "   from retrunNode=" + returnNode);
4460               for (Iterator iterator2 = inFlowSet.iterator(); iterator2.hasNext();) {
4461                 FlowNode inFlowNode = (FlowNode) iterator2.next();
4462                 if (inFlowNode.getDescTuple().startsWith(calleeMethodDesc.getThis())) {
4463                   nodeSet.addTupleSet(baseNodeSet);
4464                 }
4465               }
4466             }
4467           }
4468         }
4469
4470       }
4471
4472       // analyze parameter flows
4473
4474       if (min.numArgs() > 0) {
4475
4476         int offset;
4477         if (min.getMethod().isStatic()) {
4478           offset = 0;
4479         } else {
4480           offset = 1;
4481         }
4482
4483         for (int i = 0; i < min.numArgs(); i++) {
4484           ExpressionNode en = min.getArg(i);
4485           int idx = i + offset;
4486           NodeTupleSet argTupleSet = new NodeTupleSet();
4487           analyzeFlowExpressionNode(md, nametable, en, argTupleSet, false);
4488           // if argument is liternal node, argTuple is set to NULL
4489
4490           NTuple<Descriptor> argTuple = new NTuple<Descriptor>();
4491           if (argTupleSet.size() > 1) {
4492             NTuple<Descriptor> interTuple =
4493                 getFlowGraph(md).createIntermediateNode().getDescTuple();
4494             for (Iterator<NTuple<Descriptor>> idxIter = argTupleSet.iterator(); idxIter.hasNext();) {
4495               NTuple<Descriptor> tuple = idxIter.next();
4496               addFlowGraphEdge(md, tuple, interTuple);
4497             }
4498             argTuple = interTuple;
4499           } else if (argTupleSet.size() == 1) {
4500             argTuple = argTupleSet.iterator().next();
4501           } else {
4502             argTuple = new NTuple<Descriptor>();
4503           }
4504
4505           addArgIdxMap(min, idx, argTuple);
4506
4507           FlowNode paramNode = calleeFlowGraph.getParamFlowNode(idx);
4508           if (hasInFlowTo(calleeFlowGraph, paramNode, calleeReturnSet)
4509               || calleeMethodDesc.getModifiers().isNative()) {
4510             addParamNodeFlowingToReturnValue(calleeMethodDesc, paramNode);
4511             nodeSet.addTupleSet(argTupleSet);
4512           }
4513         }
4514
4515       }
4516
4517       // propagateFlowsFromCallee(min, md, min.getMethod());
4518
4519       System.out.println("min nodeSet=" + nodeSet);
4520     }
4521
4522   }
4523
4524   private boolean hasInFlowTo(FlowGraph fg, FlowNode inNode, Set<FlowNode> nodeSet) {
4525     // return true if inNode has in-flows to nodeSet
4526
4527     // Set<FlowNode> reachableSet = fg.getReachFlowNodeSetFrom(inNode);
4528     Set<FlowNode> reachableSet = fg.getReachableSetFrom(inNode.getDescTuple());
4529     System.out.println("inNode=" + inNode + "  reachalbeSet=" + reachableSet);
4530
4531     for (Iterator iterator = reachableSet.iterator(); iterator.hasNext();) {
4532       FlowNode fn = (FlowNode) iterator.next();
4533       if (nodeSet.contains(fn)) {
4534         return true;
4535       }
4536     }
4537     return false;
4538   }
4539
4540   private NTuple<Descriptor> getNodeTupleByArgIdx(MethodInvokeNode min, int idx) {
4541     return mapMethodInvokeNodeToArgIdxMap.get(min).get(new Integer(idx));
4542   }
4543
4544   private void addArgIdxMap(MethodInvokeNode min, int idx, NTuple<Descriptor> argTuple /*
4545                                                                                         * NodeTupleSet
4546                                                                                         * tupleSet
4547                                                                                         */) {
4548     Map<Integer, NTuple<Descriptor>> mapIdxToTuple = mapMethodInvokeNodeToArgIdxMap.get(min);
4549     if (mapIdxToTuple == null) {
4550       mapIdxToTuple = new HashMap<Integer, NTuple<Descriptor>>();
4551       mapMethodInvokeNodeToArgIdxMap.put(min, mapIdxToTuple);
4552     }
4553     mapIdxToTuple.put(new Integer(idx), argTuple);
4554   }
4555
4556   private void analyzeLiteralNode(MethodDescriptor md, SymbolTable nametable, LiteralNode en) {
4557
4558   }
4559
4560   private void analyzeFlowArrayAccessNode(MethodDescriptor md, SymbolTable nametable,
4561       ArrayAccessNode aan, NodeTupleSet nodeSet, boolean isLHS) {
4562
4563     NodeTupleSet expNodeTupleSet = new NodeTupleSet();
4564     NTuple<Descriptor> base =
4565         analyzeFlowExpressionNode(md, nametable, aan.getExpression(), expNodeTupleSet, isLHS);
4566
4567     NodeTupleSet idxNodeTupleSet = new NodeTupleSet();
4568     analyzeFlowExpressionNode(md, nametable, aan.getIndex(), idxNodeTupleSet, isLHS);
4569
4570     if (isLHS) {
4571       // need to create an edge from idx to array
4572       for (Iterator<NTuple<Descriptor>> idxIter = idxNodeTupleSet.iterator(); idxIter.hasNext();) {
4573         NTuple<Descriptor> idxTuple = idxIter.next();
4574         for (Iterator<NTuple<Descriptor>> arrIter = expNodeTupleSet.iterator(); arrIter.hasNext();) {
4575           NTuple<Descriptor> arrTuple = arrIter.next();
4576           getFlowGraph(md).addValueFlowEdge(idxTuple, arrTuple);
4577         }
4578       }
4579
4580       nodeSet.addTupleSet(expNodeTupleSet);
4581     } else {
4582       nodeSet.addTupleSet(expNodeTupleSet);
4583       nodeSet.addTupleSet(idxNodeTupleSet);
4584     }
4585   }
4586
4587   private void analyzeCreateObjectNode(MethodDescriptor md, SymbolTable nametable,
4588       CreateObjectNode en) {
4589     // TODO Auto-generated method stub
4590
4591   }
4592
4593   private void analyzeFlowOpNode(MethodDescriptor md, SymbolTable nametable, OpNode on,
4594       NodeTupleSet nodeSet, NodeTupleSet implicitFlowTupleSet) {
4595
4596     NodeTupleSet leftOpSet = new NodeTupleSet();
4597     NodeTupleSet rightOpSet = new NodeTupleSet();
4598
4599     // left operand
4600     analyzeFlowExpressionNode(md, nametable, on.getLeft(), leftOpSet, null, implicitFlowTupleSet,
4601         false);
4602
4603     if (on.getRight() != null) {
4604       // right operand
4605       analyzeFlowExpressionNode(md, nametable, on.getRight(), rightOpSet, null,
4606           implicitFlowTupleSet, false);
4607     }
4608
4609     Operation op = on.getOp();
4610
4611     switch (op.getOp()) {
4612
4613     case Operation.UNARYPLUS:
4614     case Operation.UNARYMINUS:
4615     case Operation.LOGIC_NOT:
4616       // single operand
4617       nodeSet.addTupleSet(leftOpSet);
4618       break;
4619
4620     case Operation.LOGIC_OR:
4621     case Operation.LOGIC_AND:
4622     case Operation.COMP:
4623     case Operation.BIT_OR:
4624     case Operation.BIT_XOR:
4625     case Operation.BIT_AND:
4626     case Operation.ISAVAILABLE:
4627     case Operation.EQUAL:
4628     case Operation.NOTEQUAL:
4629     case Operation.LT:
4630     case Operation.GT:
4631     case Operation.LTE:
4632     case Operation.GTE:
4633     case Operation.ADD:
4634     case Operation.SUB:
4635     case Operation.MULT:
4636     case Operation.DIV:
4637     case Operation.MOD:
4638     case Operation.LEFTSHIFT:
4639     case Operation.RIGHTSHIFT:
4640     case Operation.URIGHTSHIFT:
4641
4642       // there are two operands
4643       nodeSet.addTupleSet(leftOpSet);
4644       nodeSet.addTupleSet(rightOpSet);
4645       break;
4646
4647     default:
4648       throw new Error(op.toString());
4649     }
4650
4651   }
4652
4653   private NTuple<Descriptor> analyzeFlowNameNode(MethodDescriptor md, SymbolTable nametable,
4654       NameNode nn, NodeTupleSet nodeSet, NTuple<Descriptor> base, NodeTupleSet implicitFlowTupleSet) {
4655
4656     // System.out.println("analyzeFlowNameNode=" + nn.printNode(0));
4657
4658     if (base == null) {
4659       base = new NTuple<Descriptor>();
4660     }
4661
4662     NameDescriptor nd = nn.getName();
4663
4664     if (nd.getBase() != null) {
4665       base =
4666           analyzeFlowExpressionNode(md, nametable, nn.getExpression(), nodeSet, base,
4667               implicitFlowTupleSet, false);
4668       if (base == null) {
4669         // base node has the top location
4670         return base;
4671       }
4672     } else {
4673       String varname = nd.toString();
4674       if (varname.equals("this")) {
4675         // 'this' itself!
4676         base.add(md.getThis());
4677         return base;
4678       }
4679
4680       Descriptor d = (Descriptor) nametable.get(varname);
4681
4682       if (d instanceof VarDescriptor) {
4683         VarDescriptor vd = (VarDescriptor) d;
4684         base.add(vd);
4685       } else if (d instanceof FieldDescriptor) {
4686         // the type of field descriptor has a location!
4687         FieldDescriptor fd = (FieldDescriptor) d;
4688         if (fd.isStatic()) {
4689           if (fd.isFinal()) {
4690             // if it is 'static final', no need to have flow node for the TOP
4691             // location
4692             return null;
4693           } else {
4694             // if 'static', assign the default GLOBAL LOCATION to the first
4695             // element of the tuple
4696             base.add(GLOBALDESC);
4697           }
4698         } else {
4699           // the location of field access starts from this, followed by field
4700           // location
4701           base.add(md.getThis());
4702         }
4703
4704         base.add(fd);
4705       } else if (d == null) {
4706         // access static field
4707         base.add(GLOBALDESC);
4708         base.add(nn.getField());
4709         return base;
4710
4711         // FieldDescriptor fd = nn.getField();addFlowGraphEdge
4712         //
4713         // MethodLattice<String> localLattice = ssjava.getMethodLattice(md);
4714         // String globalLocId = localLattice.getGlobalLoc();
4715         // if (globalLocId == null) {
4716         // throw new
4717         // Error("Method lattice does not define global variable location at "
4718         // + generateErrorMessage(md.getClassDesc(), nn));
4719         // }
4720         // loc.addLocation(new Location(md, globalLocId));
4721         //
4722         // Location fieldLoc = (Location) fd.getType().getExtension();
4723         // loc.addLocation(fieldLoc);
4724         //
4725         // return loc;
4726
4727       }
4728     }
4729     getFlowGraph(md).createNewFlowNode(base);
4730
4731     return base;
4732
4733   }
4734
4735   private NTuple<Descriptor> analyzeFlowFieldAccessNode(MethodDescriptor md, SymbolTable nametable,
4736       FieldAccessNode fan, NodeTupleSet nodeSet, NTuple<Descriptor> base,
4737       NodeTupleSet implicitFlowTupleSet, boolean isLHS) {
4738
4739     ExpressionNode left = fan.getExpression();
4740     TypeDescriptor ltd = left.getType();
4741     FieldDescriptor fd = fan.getField();
4742
4743     String varName = null;
4744     if (left.kind() == Kind.NameNode) {
4745       NameDescriptor nd = ((NameNode) left).getName();
4746       varName = nd.toString();
4747     }
4748
4749     if (ltd.isClassNameRef() || (varName != null && varName.equals("this"))) {
4750       // using a class name directly or access using this
4751       if (fd.isStatic() && fd.isFinal()) {
4752         return null;
4753       }
4754     }
4755
4756     NodeTupleSet idxNodeTupleSet = new NodeTupleSet();
4757
4758     if (left instanceof ArrayAccessNode) {
4759
4760       ArrayAccessNode aan = (ArrayAccessNode) left;
4761       left = aan.getExpression();
4762       analyzeFlowExpressionNode(md, nametable, aan.getIndex(), idxNodeTupleSet, base,
4763           implicitFlowTupleSet, isLHS);
4764
4765       nodeSet.addTupleSet(idxNodeTupleSet);
4766     }
4767     base =
4768         analyzeFlowExpressionNode(md, nametable, left, nodeSet, base, implicitFlowTupleSet, isLHS);
4769
4770     if (base == null) {
4771       // in this case, field is TOP location
4772       return null;
4773     } else {
4774
4775       NTuple<Descriptor> flowFieldTuple = new NTuple<Descriptor>(base.toList());
4776
4777       if (!left.getType().isPrimitive()) {
4778
4779         if (!fd.getSymbol().equals("length")) {
4780           // array.length access, just have the location of the array
4781           flowFieldTuple.add(fd);
4782           nodeSet.removeTuple(base);
4783         }
4784
4785       }
4786       getFlowGraph(md).createNewFlowNode(flowFieldTuple);
4787
4788       if (isLHS) {
4789         for (Iterator<NTuple<Descriptor>> idxIter = idxNodeTupleSet.iterator(); idxIter.hasNext();) {
4790           NTuple<Descriptor> idxTuple = idxIter.next();
4791           getFlowGraph(md).addValueFlowEdge(idxTuple, flowFieldTuple);
4792         }
4793       }
4794       return flowFieldTuple;
4795
4796     }
4797
4798   }
4799
4800   private void debug_printTreeNode(TreeNode tn) {
4801
4802     System.out.println("DEBUG: " + tn.printNode(0) + "                line#=" + tn.getNumLine());
4803
4804   }
4805
4806   private void analyzeFlowAssignmentNode(MethodDescriptor md, SymbolTable nametable,
4807       AssignmentNode an, NodeTupleSet nodeSet, NTuple<Descriptor> base,
4808       NodeTupleSet implicitFlowTupleSet) {
4809
4810     NodeTupleSet nodeSetRHS = new NodeTupleSet();
4811     NodeTupleSet nodeSetLHS = new NodeTupleSet();
4812
4813     boolean postinc = true;
4814     if (an.getOperation().getBaseOp() == null
4815         || (an.getOperation().getBaseOp().getOp() != Operation.POSTINC && an.getOperation()
4816             .getBaseOp().getOp() != Operation.POSTDEC)) {
4817       postinc = false;
4818     }
4819     // if LHS is array access node, need to capture value flows between an array
4820     // and its index value
4821     analyzeFlowExpressionNode(md, nametable, an.getDest(), nodeSetLHS, null, implicitFlowTupleSet,
4822         true);
4823
4824     if (!postinc) {
4825       // analyze value flows of rhs expression
4826       analyzeFlowExpressionNode(md, nametable, an.getSrc(), nodeSetRHS, null, implicitFlowTupleSet,
4827           false);
4828
4829       // System.out.println("-analyzeFlowAssignmentNode=" + an.printNode(0));
4830       // System.out.println("-nodeSetLHS=" + nodeSetLHS);
4831       // System.out.println("-nodeSetRHS=" + nodeSetRHS);
4832       // System.out.println("-implicitFlowTupleSet=" + implicitFlowTupleSet);
4833       // System.out.println("-");
4834
4835       if (an.getOperation().getOp() >= 2 && an.getOperation().getOp() <= 12) {
4836         // if assignment contains OP+EQ operator, creates edges from LHS to LHS
4837
4838         for (Iterator<NTuple<Descriptor>> iter = nodeSetLHS.iterator(); iter.hasNext();) {
4839           NTuple<Descriptor> fromTuple = iter.next();
4840           for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
4841             NTuple<Descriptor> toTuple = iter2.next();
4842             addFlowGraphEdge(md, fromTuple, toTuple);
4843           }
4844         }
4845       }
4846
4847       // creates edges from RHS to LHS
4848       NTuple<Descriptor> interTuple = null;
4849       if (nodeSetRHS.size() > 1) {
4850         interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
4851       }
4852
4853       for (Iterator<NTuple<Descriptor>> iter = nodeSetRHS.iterator(); iter.hasNext();) {
4854         NTuple<Descriptor> fromTuple = iter.next();
4855         for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
4856           NTuple<Descriptor> toTuple = iter2.next();
4857           addFlowGraphEdge(md, fromTuple, interTuple, toTuple);
4858         }
4859       }
4860
4861       // creates edges from implicitFlowTupleSet to LHS
4862       for (Iterator<NTuple<Descriptor>> iter = implicitFlowTupleSet.iterator(); iter.hasNext();) {
4863         NTuple<Descriptor> fromTuple = iter.next();
4864         for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
4865           NTuple<Descriptor> toTuple = iter2.next();
4866           addFlowGraphEdge(md, fromTuple, toTuple);
4867         }
4868       }
4869
4870     } else {
4871       // postinc case
4872
4873       for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
4874         NTuple<Descriptor> tuple = iter2.next();
4875         addFlowGraphEdge(md, tuple, tuple);
4876       }
4877
4878       // creates edges from implicitFlowTupleSet to LHS
4879       for (Iterator<NTuple<Descriptor>> iter = implicitFlowTupleSet.iterator(); iter.hasNext();) {
4880         NTuple<Descriptor> fromTuple = iter.next();
4881         for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
4882           NTuple<Descriptor> toTuple = iter2.next();
4883           addFlowGraphEdge(md, fromTuple, toTuple);
4884         }
4885       }
4886
4887     }
4888
4889     if (nodeSet != null) {
4890       nodeSet.addTupleSet(nodeSetLHS);
4891     }
4892   }
4893
4894   public FlowGraph getFlowGraph(MethodDescriptor md) {
4895     return mapMethodDescriptorToFlowGraph.get(md);
4896   }
4897
4898   private boolean addFlowGraphEdge(MethodDescriptor md, NTuple<Descriptor> from,
4899       NTuple<Descriptor> to) {
4900     FlowGraph graph = getFlowGraph(md);
4901     graph.addValueFlowEdge(from, to);
4902     return true;
4903   }
4904
4905   private void addFlowGraphEdge(MethodDescriptor md, NTuple<Descriptor> from,
4906       NTuple<Descriptor> inter, NTuple<Descriptor> to) {
4907
4908     FlowGraph graph = getFlowGraph(md);
4909
4910     if (inter != null) {
4911       graph.addValueFlowEdge(from, inter);
4912       graph.addValueFlowEdge(inter, to);
4913     } else {
4914       graph.addValueFlowEdge(from, to);
4915     }
4916
4917   }
4918
4919   public void writeInferredLatticeDotFile(ClassDescriptor cd, HierarchyGraph simpleHierarchyGraph,
4920       SSJavaLattice<String> locOrder, String nameSuffix) {
4921     writeInferredLatticeDotFile(cd, null, simpleHierarchyGraph, locOrder, nameSuffix);
4922   }
4923
4924   public void writeInferredLatticeDotFile(ClassDescriptor cd, MethodDescriptor md,
4925       HierarchyGraph simpleHierarchyGraph, SSJavaLattice<String> locOrder, String nameSuffix) {
4926
4927     String fileName = "lattice_";
4928     if (md != null) {
4929       fileName +=
4930           cd.getSymbol().replaceAll("[\\W_]", "") + "_" + md.toString().replaceAll("[\\W_]", "");
4931     } else {
4932       fileName += cd.getSymbol().replaceAll("[\\W_]", "");
4933     }
4934
4935     fileName += nameSuffix;
4936
4937     Set<Pair<String, String>> pairSet = locOrder.getOrderingPairSet();
4938
4939     Set<String> addedLocSet = new HashSet<String>();
4940
4941     if (pairSet.size() > 0) {
4942       try {
4943         BufferedWriter bw = new BufferedWriter(new FileWriter(fileName + ".dot"));
4944
4945         bw.write("digraph " + fileName + " {\n");
4946
4947         for (Iterator iterator = pairSet.iterator(); iterator.hasNext();) {
4948           // pair is in the form of <higher, lower>
4949           Pair<String, String> pair = (Pair<String, String>) iterator.next();
4950
4951           String highLocId = pair.getFirst();
4952           String lowLocId = pair.getSecond();
4953
4954           if (!addedLocSet.contains(highLocId)) {
4955             addedLocSet.add(highLocId);
4956             drawNode(bw, locOrder, simpleHierarchyGraph, highLocId);
4957           }
4958
4959           if (!addedLocSet.contains(lowLocId)) {
4960             addedLocSet.add(lowLocId);
4961             drawNode(bw, locOrder, simpleHierarchyGraph, lowLocId);
4962           }
4963
4964           bw.write(highLocId + " -> " + lowLocId + ";\n");
4965         }
4966         bw.write("}\n");
4967         bw.close();
4968
4969       } catch (IOException e) {
4970         e.printStackTrace();
4971       }
4972
4973     }
4974
4975   }
4976
4977   private String convertMergeSetToString(HierarchyGraph graph, Set<HNode> mergeSet) {
4978     String str = "";
4979     for (Iterator iterator = mergeSet.iterator(); iterator.hasNext();) {
4980       HNode merged = (HNode) iterator.next();
4981       if (merged.isMergeNode()) {
4982         str += convertMergeSetToString(graph, graph.getMapHNodetoMergeSet().get(merged));
4983       } else {
4984         str += " " + merged.getName();
4985       }
4986     }
4987     return str;
4988   }
4989
4990   private void drawNode(BufferedWriter bw, SSJavaLattice<String> lattice, HierarchyGraph graph,
4991       String locName) throws IOException {
4992
4993     HNode node = graph.getHNode(locName);
4994
4995     if (node == null) {
4996       return;
4997     }
4998
4999     String prettyStr;
5000     if (lattice.isSharedLoc(locName)) {
5001       prettyStr = locName + "*";
5002     } else {
5003       prettyStr = locName;
5004     }
5005
5006     if (node.isMergeNode()) {
5007       Set<HNode> mergeSet = graph.getMapHNodetoMergeSet().get(node);
5008       prettyStr += ":" + convertMergeSetToString(graph, mergeSet);
5009     }
5010     bw.write(locName + " [label=\"" + prettyStr + "\"]" + ";\n");
5011   }
5012
5013   public void _debug_printGraph() {
5014     Set<MethodDescriptor> keySet = mapMethodDescriptorToFlowGraph.keySet();
5015
5016     for (Iterator<MethodDescriptor> iterator = keySet.iterator(); iterator.hasNext();) {
5017       MethodDescriptor md = (MethodDescriptor) iterator.next();
5018       FlowGraph fg = mapMethodDescriptorToFlowGraph.get(md);
5019       try {
5020         fg.writeGraph();
5021       } catch (IOException e) {
5022         e.printStackTrace();
5023       }
5024     }
5025
5026   }
5027
5028 }
5029
5030 class CyclicFlowException extends Exception {
5031
5032 }
5033
5034 class InterDescriptor extends Descriptor {
5035
5036   public InterDescriptor(String name) {
5037     super(name);
5038   }
5039
5040 }