changes: building field/method hierarchy graph + inserting combination nodes at the...
[IRC.git] / Robust / src / Analysis / SSJava / FlowNode.java
1 package Analysis.SSJava;
2
3 import java.util.HashSet;
4 import java.util.Iterator;
5 import java.util.Set;
6
7 import IR.Descriptor;
8 import IR.FieldDescriptor;
9 import IR.VarDescriptor;
10
11 public class FlowNode {
12
13   // descriptor tuple is a unique identifier of the flow node
14   private NTuple<Descriptor> descTuple;
15
16   // if the infer node represents the base type of field access,
17   // this set contains fields of the base type
18   private Set<FlowNode> fieldNodeSet;
19
20   // set true if this node stores a return value
21   private boolean isReturn;
22
23   private boolean isDeclarationNode = false;
24
25   private boolean isIntermediate;
26
27   private CompositeLocation compLoc;
28
29   private boolean isSkeleton;
30
31   public boolean isIntermediate() {
32     return isIntermediate;
33   }
34
35   public void setIntermediate(boolean isIntermediate) {
36     this.isIntermediate = isIntermediate;
37   }
38
39   public Set<FlowNode> getFieldNodeSet() {
40     return fieldNodeSet;
41   }
42
43   private Set<FlowEdge> outEdgeSet;
44
45   public FlowNode(NTuple<Descriptor> tuple) {
46
47     this.isSkeleton = false;
48     this.isIntermediate = false;
49
50     NTuple<Descriptor> base = null;
51     Descriptor desc = null;
52     if (tuple.size() > 1) {
53       base = tuple.subList(0, tuple.size() - 1);
54       desc = tuple.get(tuple.size() - 1);
55     } else {
56       base = tuple;
57     }
58     fieldNodeSet = new HashSet<FlowNode>();
59     descTuple = new NTuple<Descriptor>();
60     if (base != null) {
61       descTuple.addAll(base);
62     }
63     if (desc != null) {
64       descTuple.add(desc);
65     }
66     outEdgeSet = new HashSet<FlowEdge>();
67
68   }
69
70   public void setCompositeLocation(CompositeLocation in) {
71     compLoc = in;
72   }
73
74   public CompositeLocation getCompositeLocation() {
75     return compLoc;
76   }
77
78   public void addFieldNode(FlowNode node) {
79     fieldNodeSet.add(node);
80   }
81
82   public NTuple<Descriptor> getDescTuple() {
83     return descTuple;
84   }
85
86   public Descriptor getOwnDescriptor() {
87     return descTuple.get(descTuple.size() - 1);
88   }
89
90   public boolean isReturn() {
91     return isReturn;
92   }
93
94   public void setReturn(boolean isReturn) {
95     this.isReturn = isReturn;
96   }
97
98   public boolean isPrimitiveType() {
99     Descriptor desc = descTuple.get(descTuple.size() - 1);
100     if (desc instanceof VarDescriptor) {
101       return ((VarDescriptor) desc).getType().isPrimitive();
102     } else if (desc instanceof FieldDescriptor) {
103       return ((FieldDescriptor) desc).getType().isPrimitive();
104     }
105     return false;
106   }
107
108   public String toString() {
109     String rtr = "[FlowNode]:";
110     if (isSkeleton()) {
111       rtr += "SKELETON:";
112     }
113     rtr += ":" + descTuple;
114     return rtr;
115   }
116
117   public Iterator<FlowEdge> iteratorOfOutEdges() {
118     return outEdgeSet.iterator();
119   }
120
121   public void addOutEdge(FlowEdge out) {
122     outEdgeSet.add(out);
123   }
124
125   public Set<FlowEdge> getOutEdgeSet() {
126     return outEdgeSet;
127   }
128
129   public int hashCode() {
130     return 7 + descTuple.hashCode();
131   }
132
133   public boolean equals(Object obj) {
134
135     if (obj instanceof FlowNode) {
136       FlowNode in = (FlowNode) obj;
137       if (descTuple.equals(in.getDescTuple())) {
138         return true;
139       }
140     }
141
142     return false;
143
144   }
145
146   public String getID() {
147     String id = "";
148     for (int i = 0; i < descTuple.size(); i++) {
149       id += descTuple.get(i).getSymbol();
150     }
151     return id;
152   }
153
154   public String getPrettyID() {
155     String id = "<";
156     for (int i = 0; i < descTuple.size(); i++) {
157       if (i != 0) {
158         id += ",";
159       }
160       id += descTuple.get(i).getSymbol();
161     }
162     id += ">";
163
164     if (compLoc != null) {
165       id += " " + compLoc;
166     }
167
168     return id;
169   }
170
171   public void setDeclarationNode() {
172     isDeclarationNode = true;
173   }
174
175   public boolean isDeclaratonNode() {
176     return isDeclarationNode;
177   }
178
179   public NTuple<Descriptor> getCurrentDescTuple() {
180
181     if (compLoc == null) {
182       return descTuple;
183     }
184
185     NTuple<Descriptor> curDescTuple = new NTuple<Descriptor>();
186     for (int i = 0; i < compLoc.getSize(); i++) {
187       Location locElement = compLoc.get(i);
188       curDescTuple.add(locElement.getLocDescriptor());
189     }
190     return curDescTuple;
191   }
192
193   public boolean isSkeleton() {
194     return isSkeleton;
195   }
196
197   public void setSkeleton(boolean isSkeleton) {
198     this.isSkeleton = isSkeleton;
199   }
200
201 }