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