forked from typetools/checker-framework-inference
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathGraphBuilder.java
More file actions
216 lines (199 loc) · 8.27 KB
/
Copy pathGraphBuilder.java
File metadata and controls
216 lines (199 loc) · 8.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package checkers.inference.solver.constraintgraph;
import org.checkerframework.javacutil.AnnotationUtils;
import org.checkerframework.javacutil.BugInCF;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import javax.lang.model.element.AnnotationMirror;
import checkers.inference.InferenceMain;
import checkers.inference.model.AnnotationLocation.Kind;
import checkers.inference.model.ConstantSlot;
import checkers.inference.model.Constraint;
import checkers.inference.model.ExistentialConstraint;
import checkers.inference.model.Slot;
import checkers.inference.model.SubtypeConstraint;
import checkers.inference.model.VariableSlot;
import dataflow.DataflowAnnotatedTypeFactory;
import dataflow.DataflowVisitor;
/**
* GraphBuilder builds the constraint graph and runs graph traversal algorithms to separate the
* graph in different components.
*/
public class GraphBuilder {
private final Collection<Constraint> constraints;
private final ConstraintGraph graph;
private final AnnotationMirror top;
public GraphBuilder(
Collection<Slot> slots, Collection<Constraint> constraints, AnnotationMirror top) {
this.constraints = constraints;
this.graph = new ConstraintGraph();
this.top = top;
}
public ConstraintGraph buildGraph() {
for (Constraint constraint : constraints) {
if (constraint instanceof SubtypeConstraint) {
addSubtypeEdge((SubtypeConstraint) constraint);
} else if (constraint instanceof ExistentialConstraint) {
continue;
} else {
ArrayList<Slot> slots = new ArrayList<Slot>();
slots.addAll(constraint.getSlots());
addEdges(slots, constraint);
}
}
addConstant();
calculateIndependentPath();
calculateConstantPath();
// printEdges();
// printGraph();
return getGraph();
}
/** Find all constant vertices. */
private void addConstant() {
for (Vertex vertex : graph.getVerticies()) {
if (vertex.isConstant()) {
this.graph.addConstant(vertex);
}
}
}
/** This method runs BFS based algorithm, and calculates all independent components. */
private void calculateIndependentPath() {
Set<Vertex> visited = new HashSet<Vertex>();
for (Vertex vertex : this.graph.getVerticies()) {
if (!visited.contains(vertex)) {
Set<Constraint> independentPath = new HashSet<Constraint>();
Queue<Vertex> queue = new LinkedList<Vertex>();
queue.add(vertex);
while (!queue.isEmpty()) {
Vertex current = queue.remove();
visited.add(current);
for (Edge edge : current.getEdges()) {
independentPath.add(edge.getConstraint());
Vertex next =
edge.getFromVertex().equals(current)
? edge.getToVertex()
: edge.getFromVertex();
if (!visited.contains(next)) {
queue.add(next);
}
}
}
this.graph.addIndependentPath(independentPath);
}
}
}
/**
* For each constant vertex, this method run BFS based algorithm on it, and and put all edges
* that can be reached by the vertex into one set.
*/
private void calculateConstantPath() {
for (Vertex vertex : this.graph.getConstantVerticies()) {
Set<Constraint> constantPathConstraints = BFSSearch(vertex);
this.graph.addConstantPath(vertex, constantPathConstraints);
}
}
private Set<Constraint> BFSSearch(Vertex vertex) {
Set<Constraint> constantPathConstraints = new HashSet<Constraint>();
Queue<Vertex> queue = new LinkedList<Vertex>();
queue.add(vertex);
Set<Vertex> visited = new HashSet<Vertex>();
while (!queue.isEmpty()) {
Vertex current = queue.remove();
visited.add(current);
for (Edge edge : current.getEdges()) {
if ((edge instanceof SubtypeEdge) && current.equals(edge.to)) {
continue;
}
Vertex next =
current.equals(edge.getToVertex())
? edge.getFromVertex()
: edge.getToVertex();
constantPathConstraints.add(edge.getConstraint());
if (!visited.contains(next)) {
if (next.isConstant()) {
if (AnnotationUtils.areSame(top, next.getValue())) {
continue;
} else if (InferenceMain.getInstance().getVisitor()
instanceof DataflowVisitor) {
// TODO: find a proper way to adapt this behavior in type-system
// specific subclasses.
DataflowAnnotatedTypeFactory typeFactory =
(DataflowAnnotatedTypeFactory)
InferenceMain.getInstance().getRealTypeFactory();
List<String> typeNames =
typeFactory.dataflowUtils.getTypeNames(next.getValue());
if (typeNames.size() == 1 && typeNames.get(0).isEmpty()) {
continue;
}
}
} else {
VariableSlot slot = (VariableSlot) next.getSlot();
if (slot.getLocation() != null
&& slot.getLocation().getKind().equals(Kind.MISSING)) {
if (InferenceMain.isHackMode()) {
continue;
} else {
throw new BugInCF(
"In GraphBuilder.BFSSearch: find a slot of which "
+ "the location is either null or MISSING_LOCATION!");
}
}
}
queue.add(next);
}
}
}
return constantPathConstraints;
}
private void addEdges(ArrayList<Slot> slots, Constraint constraint) {
Slot first = slots.remove(0);
for (int i = 0; i < slots.size(); i++) {
Slot next = slots.get(i);
if (first instanceof ConstantSlot && next instanceof ConstantSlot) {
continue;
}
this.graph.createEdge(first, next, constraint);
addEdges(slots, constraint);
}
}
/**
* The order of subtype and supertype matters, first one has to be subtype, second one has to be
* supertype.
*
* @param subtypeConstraint
*/
private void addSubtypeEdge(SubtypeConstraint subtypeConstraint) {
Slot subtype = subtypeConstraint.getSubtype();
Slot supertype = subtypeConstraint.getSupertype();
if (subtype instanceof ConstantSlot && supertype instanceof ConstantSlot) {
return;
}
this.graph.createEdge(subtype, supertype, subtypeConstraint);
}
public ConstraintGraph getGraph() {
return this.graph;
}
private void printEdges() {
System.out.println("new graph!");
for (Map.Entry<Vertex, Set<Constraint>> entry : this.graph.getConstantPath().entrySet()) {
System.out.println(entry.getKey().getSlot());
for (Constraint constraint : entry.getValue()) {
System.out.println(constraint);
}
System.out.println("**************");
}
}
private void printGraph() {
for (Edge edge : this.graph.getEdges()) {
System.out.println(edge);
}
for (Vertex vertex : this.graph.getVerticies()) {
System.out.println(vertex.getId());
}
}
}