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 import Util.Pair;
13
14 public class LocationInfo {
15
16   Map<String, Set<Descriptor>> mapLocSymbolToDescSet;
17   Map<String, Set<Pair<Descriptor, Descriptor>>> mapLocSymbolToRelatedInferLocSet;
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     mapLocSymbolToRelatedInferLocSet = new HashMap<String, Set<Pair<Descriptor, Descriptor>>>();
26   }
27
28   public LocationInfo(ClassDescriptor cd) {
29     this();
30     this.cd = cd;
31   }
32
33   public Map<String, Set<Descriptor>> getMapLocSymbolToDescSet() {
34     return mapLocSymbolToDescSet;
35   }
36
37   public Map<Descriptor, CompositeLocation> getMapDescToInferLocation() {
38     return mapDescToInferCompositeLocation;
39   }
40
41   public void addMapLocSymbolToRelatedInferLoc(String locSymbol, Descriptor enclosingDesc,
42       Descriptor desc) {
43     if (!mapLocSymbolToRelatedInferLocSet.containsKey(locSymbol)) {
44       mapLocSymbolToRelatedInferLocSet.put(locSymbol, new HashSet<Pair<Descriptor, Descriptor>>());
45     }
46     mapLocSymbolToRelatedInferLocSet.get(locSymbol).add(
47         new Pair<Descriptor, Descriptor>(enclosingDesc, desc));
48   }
49
50   public Set<Pair<Descriptor, Descriptor>> getRelatedInferLocSet(String locSymbol) {
51     return mapLocSymbolToRelatedInferLocSet.get(locSymbol);
52   }
53
54   public void mapDescriptorToLocation(Descriptor desc, CompositeLocation inferLoc) {
55     mapDescToInferCompositeLocation.put(desc, inferLoc);
56   }
57
58   public CompositeLocation getInferLocation(Descriptor desc) {
59     if (!mapDescToInferCompositeLocation.containsKey(desc)) {
60       CompositeLocation newInferLoc = new CompositeLocation();
61       Location loc;
62       Descriptor enclosingDesc;
63       if (md != null) {
64         // method lattice
65         enclosingDesc = md;
66       } else {
67         enclosingDesc = cd;
68       }
69       loc = new Location(enclosingDesc, desc.getSymbol());
70
71       newInferLoc.addLocation(loc);
72       mapDescToInferCompositeLocation.put(desc, newInferLoc);
73       addMapLocSymbolToDescSet(desc.getSymbol(), desc);
74       addMapLocSymbolToRelatedInferLoc(desc.getSymbol(), enclosingDesc, desc);
75     }
76     return mapDescToInferCompositeLocation.get(desc);
77   }
78
79   public void addMapLocSymbolToDescSet(String locSymbol, Descriptor desc) {
80     if (!mapLocSymbolToDescSet.containsKey(locSymbol)) {
81       mapLocSymbolToDescSet.put(locSymbol, new HashSet<Descriptor>());
82     }
83     mapLocSymbolToDescSet.get(locSymbol).add(desc);
84   }
85
86   public Location getFieldInferLocation(Descriptor desc) {
87     return getInferLocation(desc).get(0);
88   }
89
90   public Set<Descriptor> getDescSet(String locSymbol) {
91     if (!mapLocSymbolToDescSet.containsKey(locSymbol)) {
92       mapLocSymbolToDescSet.put(locSymbol, new HashSet<Descriptor>());
93     }
94     return mapLocSymbolToDescSet.get(locSymbol);
95   }
96
97   public void removeRelatedInferLocSet(String oldLocSymbol, String newSharedLoc) {
98     Set<Descriptor> descSet = getDescSet(oldLocSymbol);
99     getDescSet(newSharedLoc).addAll(descSet);
100     mapLocSymbolToDescSet.remove(oldLocSymbol);
101     mapLocSymbolToRelatedInferLocSet.remove(oldLocSymbol);
102   }
103
104 }