0ec825266f930d2aee8377bc003f2461ca4298e2
[IRC.git] / Robust / src / Analysis / SSJava / LocationInfo.java
1 package Analysis.SSJava;
2
3 import java.util.HashMap;
4 import java.util.HashSet;
5 import java.util.Iterator;
6 import java.util.Map;
7 import java.util.Set;
8
9 import IR.ClassDescriptor;
10 import IR.Descriptor;
11 import IR.MethodDescriptor;
12
13 public class LocationInfo {
14
15   // Map<Descriptor, String> mapDescToLocSymbol;
16   Map<String, Set<Descriptor>> mapLocSymbolToDescSet;
17
18   Map<Descriptor, CompositeLocation> mapDescToInferCompositeLocation;
19   MethodDescriptor md;
20   ClassDescriptor cd;
21
22   public LocationInfo() {
23     mapDescToInferCompositeLocation = new HashMap<Descriptor, CompositeLocation>();
24     mapLocSymbolToDescSet = new HashMap<String, Set<Descriptor>>();
25   }
26
27   public LocationInfo(ClassDescriptor cd) {
28     this();
29     this.cd = cd;
30   }
31
32   public Map<String, Set<Descriptor>> getMapLocSymbolToDescSet() {
33     return mapLocSymbolToDescSet;
34   }
35
36   public Map<Descriptor, CompositeLocation> getMapDescToInferLocation() {
37     return mapDescToInferCompositeLocation;
38   }
39
40   public void mapDescriptorToLocation(Descriptor desc, CompositeLocation inferLoc) {
41     mapDescToInferCompositeLocation.put(desc, inferLoc);
42   }
43
44   // public void mapDescSymbolToLocName(String descSymbol, String locName) {
45   // mapDescSymbolToLocName.put(descSymbol, locName);
46   // }
47
48   public CompositeLocation getInferLocation(Descriptor desc) {
49     if (!mapDescToInferCompositeLocation.containsKey(desc)) {
50       CompositeLocation newInferLoc = new CompositeLocation();
51       Location loc;
52       if (md != null) {
53         // method lattice
54         loc = new Location(md, desc.getSymbol());
55       } else {
56         loc = new Location(cd, desc.getSymbol());
57       }
58       newInferLoc.addLocation(loc);
59       mapDescToInferCompositeLocation.put(desc, newInferLoc);
60       addMapLocSymbolToDescSet(desc.getSymbol(), desc);
61     }
62     return mapDescToInferCompositeLocation.get(desc);
63   }
64
65   public void addMapLocSymbolToDescSet(String locSymbol, Descriptor desc) {
66     System.out.println("mapLocSymbolToDescSet=" + mapLocSymbolToDescSet);
67     if (!mapLocSymbolToDescSet.containsKey(locSymbol)) {
68       mapLocSymbolToDescSet.put(locSymbol, new HashSet<Descriptor>());
69     }
70     mapLocSymbolToDescSet.get(locSymbol).add(desc);
71   }
72
73   public Location getFieldInferLocation(Descriptor desc) {
74     return getInferLocation(desc).get(0);
75   }
76
77   public Set<Descriptor> getDescSet(String locSymbol) {
78     if (!mapLocSymbolToDescSet.containsKey(locSymbol)) {
79       mapLocSymbolToDescSet.put(locSymbol, new HashSet<Descriptor>());
80     }
81     return mapLocSymbolToDescSet.get(locSymbol);
82   }
83
84   public void mergeMapping(String oldLocSymbol, String newSharedLoc) {
85     Set<Descriptor> descSet = getDescSet(oldLocSymbol);
86     getDescSet(newSharedLoc).addAll(descSet);
87     mapLocSymbolToDescSet.remove(oldLocSymbol);
88
89     Set<Descriptor> keySet = mapDescToInferCompositeLocation.keySet();
90     for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
91       Descriptor key = (Descriptor) iterator.next();
92       CompositeLocation inferLoc = getInferLocation(key);
93
94       CompositeLocation newInferLoc = new CompositeLocation();
95       if (inferLoc.getSize() > 1) {
96         // local variable has a composite location [refLoc.inferedLoc]
97
98         Location oldLoc = inferLoc.get(inferLoc.getSize() - 1);
99         // oldLoc corresponds to infered loc.
100
101         if (oldLoc.getLocIdentifier().equals(oldLocSymbol)) {
102           for (int i = 0; i < inferLoc.getSize() - 1; i++) {
103             Location loc = inferLoc.get(i);
104             newInferLoc.addLocation(loc);
105           }
106           Location newLoc = new Location(oldLoc.getDescriptor(), newSharedLoc);
107           newInferLoc.addLocation(newLoc);
108           mapDescriptorToLocation(key, newInferLoc);
109         }
110         // else {
111         // return;
112         // }
113       } else {
114         // local var has a local location
115         Location oldLoc = inferLoc.get(0);
116         if (oldLoc.getLocIdentifier().equals(oldLocSymbol)) {
117           Location newLoc = new Location(oldLoc.getDescriptor(), newSharedLoc);
118           newInferLoc.addLocation(newLoc);
119           mapDescriptorToLocation(key, newInferLoc);
120         }
121         // else {
122         // return;
123         // }
124       }
125
126     }
127   }
128
129 }