changes.
[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 is driven from a paramter
21   private boolean isParameter;
22
23   // set true if this node stores a return value
24   private boolean isReturn;
25
26   public Set<FlowNode> getFieldNodeSet() {
27     return fieldNodeSet;
28   }
29
30   private Set<FlowEdge> outEdgeSet;
31
32   public FlowNode(NTuple<Descriptor> tuple, boolean isParam) {
33
34     this.isParameter = isParam;
35
36     NTuple<Descriptor> base = null;
37     Descriptor desc = null;
38     if (tuple.size() > 1) {
39       base = tuple.subList(0, tuple.size() - 1);
40       desc = tuple.get(tuple.size() - 1);
41     } else {
42       base = tuple;
43     }
44     fieldNodeSet = new HashSet<FlowNode>();
45     descTuple = new NTuple<Descriptor>();
46     if (base != null) {
47       descTuple.addAll(base);
48     }
49     if (desc != null) {
50       descTuple.add(desc);
51     }
52     outEdgeSet = new HashSet<FlowEdge>();
53   }
54
55   public void addFieldNode(FlowNode node) {
56     fieldNodeSet.add(node);
57   }
58
59   public boolean isParameter() {
60     return isParameter;
61   }
62
63   public NTuple<Descriptor> getDescTuple() {
64     return descTuple;
65   }
66
67   public Descriptor getOwnDescriptor() {
68     return descTuple.get(descTuple.size() - 1);
69   }
70
71   public boolean isReturn() {
72     return isReturn;
73   }
74
75   public void setReturn(boolean isReturn) {
76     this.isReturn = isReturn;
77   }
78
79   public boolean isPrimitiveType() {
80     Descriptor desc = descTuple.get(descTuple.size() - 1);
81     if (desc instanceof VarDescriptor) {
82       return ((VarDescriptor) desc).getType().isPrimitive();
83     } else if (desc instanceof FieldDescriptor) {
84       return ((FieldDescriptor) desc).getType().isPrimitive();
85     }
86     return false;
87   }
88
89   public String toString() {
90     String rtr = "[FlowNode]:";
91     if (isParameter()) {
92       rtr += "param:";
93     }
94     rtr += ":" + descTuple;
95     return rtr;
96   }
97
98   public Iterator<FlowEdge> iteratorOfOutEdges() {
99     return outEdgeSet.iterator();
100   }
101
102   public void addOutEdge(FlowEdge out) {
103     outEdgeSet.add(out);
104   }
105
106   public Set<FlowEdge> getOutEdgeSet() {
107     return outEdgeSet;
108   }
109
110   public int hashCode() {
111     return 7 + descTuple.hashCode();
112   }
113
114   public boolean equals(Object obj) {
115
116     if (obj instanceof FlowNode) {
117       FlowNode in = (FlowNode) obj;
118       if (descTuple.equals(in.getDescTuple())) {
119         return true;
120       }
121     }
122
123     return false;
124
125   }
126
127   public String getID() {
128     String id = "";
129     for (int i = 0; i < descTuple.size(); i++) {
130       id += descTuple.get(i).getSymbol();
131     }
132     return id;
133   }
134
135   public String getPrettyID() {
136     String id = "<";
137     for (int i = 0; i < descTuple.size(); i++) {
138       if (i != 0) {
139         id += ",";
140       }
141       id += descTuple.get(i).getSymbol();
142     }
143     id += ">";
144     return id;
145   }
146 }