changes.
[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     if (!mapLocSymbolToDescSet.containsKey(locSymbol)) {
67       mapLocSymbolToDescSet.put(locSymbol, new HashSet<Descriptor>());
68     }
69     mapLocSymbolToDescSet.get(locSymbol).add(desc);
70   }
71
72   public Location getFieldInferLocation(Descriptor desc) {
73     return getInferLocation(desc).get(0);
74   }
75
76   public Set<Descriptor> getDescSet(String locSymbol) {
77     if (!mapLocSymbolToDescSet.containsKey(locSymbol)) {
78       mapLocSymbolToDescSet.put(locSymbol, new HashSet<Descriptor>());
79     }
80     return mapLocSymbolToDescSet.get(locSymbol);
81   }
82
83   public void mergeMapping(String oldLocSymbol, String newSharedLoc) {
84     Set<Descriptor> descSet = getDescSet(oldLocSymbol);
85     getDescSet(newSharedLoc).addAll(descSet);
86     mapLocSymbolToDescSet.remove(oldLocSymbol);
87
88     Set<Descriptor> keySet = mapDescToInferCompositeLocation.keySet();
89     for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
90       Descriptor key = (Descriptor) iterator.next();
91       CompositeLocation inferLoc = getInferLocation(key);
92
93       CompositeLocation newInferLoc = new CompositeLocation();
94       if (inferLoc.getSize() > 1) {
95         // local variable has a composite location [refLoc.inferedLoc]
96
97         Location oldLoc = inferLoc.get(inferLoc.getSize() - 1);
98         // oldLoc corresponds to infered loc.
99
100         if (oldLoc.getLocIdentifier().equals(oldLocSymbol)) {
101           for (int i = 0; i < inferLoc.getSize() - 1; i++) {
102             Location loc = inferLoc.get(i);
103             newInferLoc.addLocation(loc);
104           }
105           Location newLoc = new Location(oldLoc.getDescriptor(), newSharedLoc);
106           newInferLoc.addLocation(newLoc);
107           mapDescriptorToLocation(key, newInferLoc);
108         }
109         // else {
110         // return;
111         // }
112       } else {
113         // local var has a local location
114         Location oldLoc = inferLoc.get(0);
115         if (oldLoc.getLocIdentifier().equals(oldLocSymbol)) {
116           Location newLoc = new Location(oldLoc.getDescriptor(), newSharedLoc);
117           newInferLoc.addLocation(newLoc);
118           mapDescriptorToLocation(key, newInferLoc);
119         }
120         // else {
121         // return;
122         // }
123       }
124
125     }
126   }
127
128 }