67f426ed7bfcaaf1551b7c8878faadae379ced38
[IRC.git] / Robust / src / Analysis / Pointer / AllocFactory.java
1 package Analysis.Pointer;
2
3 import java.util.*;
4 import IR.*;
5 import IR.Flat.*;
6
7 public class AllocFactory {
8   public static class AllocNode {
9     int allocsite;
10     boolean summary;
11     TypeDescriptor type;
12     
13     public AllocNode(int allocsite, TypeDescriptor type, boolean summary) {
14       this.allocsite=allocsite;
15       this.summary=summary;
16       this.type=type;
17     }
18
19     public TypeDescriptor getType() {
20       return type;
21     }
22
23     public boolean isSummary() {
24       return summary;
25     }
26     
27     public int hashCode() {
28       return allocsite<<1^(summary?0:1);
29     }
30     
31     public boolean equals(Object o) {
32       if (o instanceof AllocNode) {
33         AllocNode an=(AllocNode)o;
34         return (allocsite==an.allocsite)&&(summary==an.summary);
35       }
36       return false;
37     }
38   }
39
40   public AllocFactory(State state, TypeUtil typeUtil) {
41     allocMap=new HashMap<FlatNew, Integer>();
42     allocNodeMap=new HashMap<AllocNode, AllocNode>();
43     this.typeUtil=typeUtil;
44     ClassDescriptor stringcd=typeUtil.getClass(TypeUtil.StringClass);
45     TypeDescriptor stringtd=new TypeDescriptor(stringcd);
46     TypeDescriptor stringarraytd=stringtd.makeArray(state);
47     StringArray=new AllocNode(0, stringarraytd, false);
48     Strings=new AllocNode(1, stringtd, true);
49   }
50
51   public int getSiteNumber(FlatNew node) {
52     if (allocMap.containsKey(node))
53       return allocMap.get(node);
54     int index=siteCounter++;
55     allocMap.put(node, index);
56     return index;
57   }
58
59   public AllocNode getAllocNode(FlatNew node, boolean isSummary) {
60     int site=getSiteNumber(node);
61     AllocNode key=new AllocNode(site, node.getType(), isSummary);
62     if (!allocNodeMap.containsKey(key)) {
63       allocNodeMap.put(key, key);
64       return key;
65     } else
66       return allocNodeMap.get(key);
67   }
68
69   HashMap<AllocNode, AllocNode> allocNodeMap;
70   HashMap<FlatNew, Integer> allocMap;
71   TypeUtil typeUtil;
72   int siteCounter=2;
73
74   public AllocNode StringArray;
75   public AllocNode Strings;
76 }