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