-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathVCChecker.java
More file actions
296 lines (262 loc) · 12.1 KB
/
VCChecker.java
File metadata and controls
296 lines (262 loc) · 12.1 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package liquidjava.processor.refinement_checker;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.Stack;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import liquidjava.diagnostics.errors.*;
import liquidjava.diagnostics.TranslationTable;
import liquidjava.processor.VCImplication;
import liquidjava.processor.context.*;
import liquidjava.rj_language.Predicate;
import liquidjava.smt.SMTEvaluator;
import liquidjava.smt.TypeCheckError;
import liquidjava.utils.constants.Keys;
import spoon.reflect.cu.SourcePosition;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.factory.Factory;
import spoon.reflect.reference.CtTypeReference;
public class VCChecker {
private final Context context;
private final List<RefinedVariable> pathVariables;
public VCChecker() {
context = Context.getInstance();
pathVariables = new Stack<>();
}
public void processSubtyping(Predicate expectedType, List<GhostState> list, CtElement element, Factory f)
throws LJError {
processSubtyping(expectedType, list, element, f, null);
}
public void processSubtyping(Predicate expectedType, List<GhostState> list, CtElement element, Factory f,
String customMessage) throws LJError {
List<RefinedVariable> lrv = new ArrayList<>(), mainVars = new ArrayList<>();
gatherVariables(expectedType, lrv, mainVars);
if (expectedType.isBooleanTrue())
return;
TranslationTable map = new TranslationTable();
String[] s = { Keys.WILDCARD, Keys.THIS };
Predicate premisesBeforeChange = joinPredicates(expectedType, mainVars, lrv, map).toConjunctions();
Predicate premises;
Predicate expected;
try {
List<GhostState> filtered = filterGhostStatesForVariables(list, mainVars, lrv);
premises = premisesBeforeChange.changeStatesToRefinements(filtered, s).changeAliasToRefinement(context, f);
expected = expectedType.changeStatesToRefinements(filtered, s).changeAliasToRefinement(context, f);
} catch (LJError e) {
// add location info to error
e.setPosition(element.getPosition());
throw e;
}
boolean isSubtype = smtChecks(expected, premises, element.getPosition());
if (!isSubtype)
throw new RefinementError(element.getPosition(), expectedType.simplify(), premisesBeforeChange.simplify(),
map, customMessage);
}
/**
* Check that type is a subtype of expectedType Throws RefinementError otherwise
*
* @param type
* @param expectedType
* @param list
* @param element
* @param f
*
* @throws LJError
*/
public void processSubtyping(Predicate type, Predicate expectedType, List<GhostState> list, CtElement element,
Factory f) throws LJError {
boolean b = canProcessSubtyping(type, expectedType, list, element.getPosition(), f);
if (!b)
throwRefinementError(element.getPosition(), expectedType, type, null);
}
/**
* Checks the expected against the found constraint
*
* @param expected
* @param found
* @param position
*
* @return true if expected type is subtype of found type, false otherwise
*
* @throws LJError
*/
public boolean smtChecks(Predicate expected, Predicate found, SourcePosition position) throws LJError {
try {
new SMTEvaluator().verifySubtype(found, expected, context);
return true;
} catch (TypeCheckError e) {
return false;
} catch (LJError e) {
e.setPosition(position);
throw e;
} catch (Exception e) {
throw new CustomError(e.getMessage(), position);
}
}
public boolean canProcessSubtyping(Predicate type, Predicate expectedType, List<GhostState> list,
SourcePosition position, Factory f) throws LJError {
List<RefinedVariable> lrv = new ArrayList<>(), mainVars = new ArrayList<>();
gatherVariables(expectedType, lrv, mainVars);
gatherVariables(type, lrv, mainVars);
if (expectedType.isBooleanTrue() && type.isBooleanTrue())
return true;
TranslationTable map = new TranslationTable();
String[] s = { Keys.WILDCARD, Keys.THIS };
Predicate premises = joinPredicates(expectedType, mainVars, lrv, map).toConjunctions();
List<GhostState> filtered = filterGhostStatesForVariables(list, mainVars, lrv);
premises = Predicate.createConjunction(premises, type).changeStatesToRefinements(filtered, s)
.changeAliasToRefinement(context, f);
Predicate expected = expectedType.changeStatesToRefinements(filtered, s).changeAliasToRefinement(context, f);
// check subtyping
return smtChecks(expected, premises, position);
}
/**
* Reduce the ghost states list to those whose declaring class (prefix) matches any of the involved variable types
* or their supertypes This prevents ambiguous simple name substitutions across unrelated classes that share state
* names
*/
private List<GhostState> filterGhostStatesForVariables(List<GhostState> list, List<RefinedVariable> mainVars,
List<RefinedVariable> vars) {
if (list.isEmpty())
return list;
// Collect all relevant qualified type names (types + supertypes), keeping order and deduping
Set<String> allowedPrefixes = new java.util.LinkedHashSet<>();
Consumer<RefinedVariable> collect = rv -> {
if (rv.getType() != null) {
allowedPrefixes.add(rv.getType().getQualifiedName());
}
for (CtTypeReference<?> st : rv.getSuperTypes()) {
if (st != null) {
allowedPrefixes.add(st.getQualifiedName());
}
}
};
mainVars.forEach(collect);
vars.forEach(collect);
if (allowedPrefixes.isEmpty())
return list; // avoid over-filtering when types are unknown
List<GhostState> filtered = list.stream().filter(g -> {
String prefix = (g.getParent() != null) ? g.getParent().getPrefix() : g.getPrefix();
return allowedPrefixes.contains(prefix);
}).collect(Collectors.toList());
// If nothing matched, keep original to avoid accidental empties
return filtered.isEmpty() ? list : filtered;
}
private VCImplication joinPredicates(Predicate expectedType, List<RefinedVariable> mainVars,
List<RefinedVariable> vars, TranslationTable map) {
VCImplication firstSi = null;
VCImplication lastSi = null;
// Check
for (RefinedVariable var : mainVars) { // join main refinements of mainVars
addMap(var, map);
VCImplication si = new VCImplication(var.getName(), var.getType(), var.getMainRefinement());
if (lastSi != null) {
lastSi.setNext(si);
lastSi = si;
}
if (firstSi == null) {
firstSi = si;
lastSi = si;
}
}
for (RefinedVariable var : vars) { // join refinements of vars
addMap(var, map);
VCImplication si = new VCImplication(var.getName(), var.getType(), var.getRefinement());
if (lastSi != null) {
lastSi.setNext(si);
lastSi = si;
}
if (firstSi == null) {
firstSi = si;
lastSi = si;
}
}
VCImplication cSMT = new VCImplication(new Predicate());
if (firstSi != null) {
cSMT = firstSi.clone();
lastSi.setNext(new VCImplication(expectedType));
}
return cSMT;
}
private void addMap(RefinedVariable var, TranslationTable map) {
map.put(var.getName(), var.getPlacementInCode());
}
private void gatherVariables(Predicate expectedType, List<RefinedVariable> lrv, List<RefinedVariable> mainVars) {
for (String s : expectedType.getVariableNames()) {
if (context.hasVariable(s)) {
RefinedVariable rv = context.getVariableByName(s);
if (!mainVars.contains(rv) && !lrv.contains(rv))
mainVars.add(rv);
List<RefinedVariable> lm = getVariables(rv.getMainRefinement(), rv.getName());
addAllDifferent(lrv, lm, mainVars);
}
}
}
private void addAllDifferent(List<RefinedVariable> toExpand, List<RefinedVariable> from,
List<RefinedVariable> remove) {
from.stream().filter(rv -> !toExpand.contains(rv) && !remove.contains(rv)).forEach(toExpand::add);
}
private List<RefinedVariable> getVariables(Predicate c, String varName) {
List<RefinedVariable> allVars = new ArrayList<>();
getVariablesFromContext(c.getVariableNames(), allVars, varName);
List<String> pathNames = pathVariables.stream().map(Refined::getName).collect(Collectors.toList());
getVariablesFromContext(pathNames, allVars, "");
return allVars;
}
private void getVariablesFromContext(List<String> lvars, List<RefinedVariable> allVars, String notAdd) {
lvars.stream().filter(name -> !name.equals(notAdd) && context.hasVariable(name)).map(context::getVariableByName)
.filter(rv -> !allVars.contains(rv)).forEach(rv -> {
allVars.add(rv);
recAuxGetVars(rv, allVars);
});
}
private void recAuxGetVars(RefinedVariable var, List<RefinedVariable> newVars) {
if (!context.hasVariable(var.getName()))
return;
Predicate c = var.getRefinement();
String varName = var.getName();
List<String> l = c.getVariableNames();
getVariablesFromContext(l, newVars, varName);
}
public void addPathVariable(RefinedVariable rv) {
pathVariables.add(rv);
}
public void removePathVariable(RefinedVariable rv) {
pathVariables.remove(rv);
}
void removePathVariableThatIncludes(String otherVar) {
pathVariables.stream().filter(rv -> rv.getRefinement().getVariableNames().contains(otherVar)).toList()
.forEach(pathVariables::remove);
}
// Errors---------------------------------------------------------------------------------------------------
protected void throwRefinementError(SourcePosition position, Predicate expected, Predicate found,
String customMessage) throws RefinementError {
List<RefinedVariable> lrv = new ArrayList<>(), mainVars = new ArrayList<>();
gatherVariables(expected, lrv, mainVars);
gatherVariables(found, lrv, mainVars);
TranslationTable map = new TranslationTable();
Predicate premises = joinPredicates(expected, mainVars, lrv, map).toConjunctions();
throw new RefinementError(position, expected.simplify(), premises.simplify(), map, customMessage);
}
protected void throwStateRefinementError(SourcePosition position, Predicate found, Predicate expected,
String customMessage) throws StateRefinementError {
List<RefinedVariable> lrv = new ArrayList<>(), mainVars = new ArrayList<>();
gatherVariables(found, lrv, mainVars);
TranslationTable map = new TranslationTable();
VCImplication foundState = joinPredicates(found, mainVars, lrv, map);
throw new StateRefinementError(position, expected.getExpression(),
foundState.toConjunctions().simplify().getValue(), map, customMessage);
}
protected void throwStateConflictError(SourcePosition position, Predicate expected) throws StateConflictError {
TranslationTable map = createMap(expected);
throw new StateConflictError(position, expected.getExpression(), map);
}
private TranslationTable createMap(Predicate expectedType) {
List<RefinedVariable> lrv = new ArrayList<>(), mainVars = new ArrayList<>();
gatherVariables(expectedType, lrv, mainVars);
TranslationTable map = new TranslationTable();
joinPredicates(expectedType, mainVars, lrv, map);
return map;
}
}