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