changes: building field/method hierarchy graph + inserting combination nodes at the...
[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     addMapLocSymbolToDescSet(locSymbol, desc);
58   }
59
60   public Set<Pair<Descriptor, Descriptor>> getRelatedInferLocSet(String locSymbol) {
61
62     if (!mapLocSymbolToRelatedInferLocSet.containsKey(locSymbol)) {
63       mapLocSymbolToRelatedInferLocSet.put(locSymbol, new HashSet<Pair<Descriptor, Descriptor>>());
64     }
65     return mapLocSymbolToRelatedInferLocSet.get(locSymbol);
66   }
67
68   public void mapDescriptorToLocation(Descriptor desc, CompositeLocation inferLoc) {
69     mapDescToInferCompositeLocation.put(desc, inferLoc);
70   }
71
72   public CompositeLocation getInferLocation(Descriptor desc) {
73     if (!mapDescToInferCompositeLocation.containsKey(desc)) {
74       CompositeLocation newInferLoc = new CompositeLocation();
75       Location loc;
76       Descriptor enclosingDesc;
77       if (md != null) {
78         // method lattice
79         enclosingDesc = md;
80       } else {
81         enclosingDesc = cd;
82       }
83       loc = new Location(enclosingDesc, desc.getSymbol());
84
85       newInferLoc.addLocation(loc);
86       mapDescToInferCompositeLocation.put(desc, newInferLoc);
87       // addMapLocSymbolToDescSet(desc.getSymbol(), desc);
88       addMapLocSymbolToRelatedInferLoc(desc.getSymbol(), enclosingDesc, desc);
89     }
90     return mapDescToInferCompositeLocation.get(desc);
91   }
92
93   public void addMapLocSymbolToDescSet(String locSymbol, Descriptor desc) {
94     if (!mapLocSymbolToDescSet.containsKey(locSymbol)) {
95       mapLocSymbolToDescSet.put(locSymbol, new HashSet<Descriptor>());
96     }
97     mapLocSymbolToDescSet.get(locSymbol).add(desc);
98   }
99
100   public Location getFieldInferLocation(Descriptor desc) {
101     return getInferLocation(desc).get(0);
102   }
103
104   public Set<Descriptor> getDescSet(String locSymbol) {
105     if (!mapLocSymbolToDescSet.containsKey(locSymbol)) {
106       mapLocSymbolToDescSet.put(locSymbol, new HashSet<Descriptor>());
107     }
108     return mapLocSymbolToDescSet.get(locSymbol);
109   }
110
111   public void removeRelatedInferLocSet(String oldLocSymbol, String newSharedLoc) {
112     Set<Descriptor> descSet = getDescSet(oldLocSymbol);
113     getDescSet(newSharedLoc).addAll(descSet);
114     // getRelatedInferLocSet(newSharedLoc).addAll(getRelatedInferLocSet(oldLocSymbol));
115     mapLocSymbolToDescSet.remove(oldLocSymbol);
116     mapLocSymbolToRelatedInferLocSet.remove(oldLocSymbol);
117   }
118
119 }