-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathPredicate.java
More file actions
258 lines (222 loc) · 8.91 KB
/
Predicate.java
File metadata and controls
258 lines (222 loc) · 8.91 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
package liquidjava.rj_language;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import liquidjava.diagnostics.errors.ArgumentMismatchError;
import liquidjava.diagnostics.errors.LJError;
import liquidjava.diagnostics.errors.SyntaxError;
import liquidjava.processor.context.AliasWrapper;
import liquidjava.processor.context.Context;
import liquidjava.processor.context.GhostFunction;
import liquidjava.processor.context.GhostState;
import liquidjava.processor.facade.AliasDTO;
import liquidjava.rj_language.ast.BinaryExpression;
import liquidjava.rj_language.ast.Expression;
import liquidjava.rj_language.ast.FunctionInvocation;
import liquidjava.rj_language.ast.GroupExpression;
import liquidjava.rj_language.ast.Ite;
import liquidjava.rj_language.ast.LiteralBoolean;
import liquidjava.rj_language.ast.LiteralInt;
import liquidjava.rj_language.ast.LiteralLong;
import liquidjava.rj_language.ast.LiteralReal;
import liquidjava.rj_language.ast.UnaryExpression;
import liquidjava.rj_language.ast.Var;
import liquidjava.rj_language.opt.derivation_node.ValDerivationNode;
import liquidjava.rj_language.opt.ExpressionSimplifier;
import liquidjava.rj_language.parsing.RefinementsParser;
import liquidjava.utils.Utils;
import liquidjava.utils.constants.Keys;
import liquidjava.utils.constants.Ops;
import liquidjava.utils.constants.Types;
import spoon.reflect.cu.SourcePosition;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.declaration.CtType;
import spoon.reflect.factory.Factory;
/**
* Acts as a wrapper for Expression AST
*
* @author cgamboa
*/
public class Predicate {
protected Expression exp;
protected String prefix;
/** Create a predicate with the expression true */
public Predicate() {
exp = new LiteralBoolean(true);
}
/**
* Create a new predicate with a refinement
*
* @param ref
* @param element
*/
public Predicate(String ref, CtElement element) throws LJError {
this(ref, element, element.getParent(CtType.class).getQualifiedName());
}
/**
* Create a new predicate with a refinement and a given prefix for the class
*
* @param ref
* @param element
* @param prefix
*/
public Predicate(String ref, CtElement element, String prefix) throws LJError {
this.prefix = prefix;
exp = parse(ref, element);
if (!(exp instanceof GroupExpression)) {
exp = new GroupExpression(exp);
}
}
/** Create a predicate with the expression true */
public Predicate(Expression e) {
exp = e;
}
protected Expression parse(String ref, CtElement element) throws LJError {
try {
return RefinementsParser.createAST(ref, prefix);
} catch (LJError e) {
// add location info to error
SourcePosition pos = Utils.getRefinementAnnotationPosition(element, ref);
e.setPosition(pos);
throw e;
}
}
protected Expression innerParse(String ref, String prefix) throws LJError {
return RefinementsParser.createAST(ref, prefix);
}
public Predicate changeAliasToRefinement(Context context, Factory f) throws LJError {
Expression ref = getExpression();
Map<String, AliasDTO> alias = new HashMap<>();
for (AliasWrapper aw : context.getAlias()) {
alias.put(aw.getName(), aw.createAliasDTO());
}
ref = ref.changeAlias(alias, context, f);
ref.validateGhostInvocations(context, f);
return new Predicate(ref);
}
public Predicate negate() {
return new Predicate(new UnaryExpression("!", exp));
}
public Predicate substituteVariable(String from, String to) {
Expression ec = exp.clone();
ec = ec.substitute(new Var(from), new Var(to));
return new Predicate(ec);
}
public List<String> getVariableNames() {
List<String> l = new ArrayList<>();
exp.getVariableNames(l);
return l;
}
public List<GhostState> getStateInvocations(List<GhostState> lgs) {
if (lgs == null)
return new ArrayList<>();
List<String> all = lgs.stream().map(GhostFunction::getQualifiedName).collect(Collectors.toList());
List<String> toAdd = new ArrayList<>();
exp.getStateInvocations(toAdd, all);
List<GhostState> gh = new ArrayList<>();
for (String n : toAdd) {
for (GhostState g : lgs)
if (g.matches(n))
gh.add(g);
}
return gh;
}
/** Change old mentions of previous name to the new name e.g., old(previousName) -> newName */
public Predicate changeOldMentions(String previousName, String newName) {
Expression e = exp.clone();
Expression prev = createVar(previousName).getExpression();
List<Expression> le = new ArrayList<>();
le.add(createVar(newName).getExpression());
e.substituteFunction(Keys.OLD, le, prev);
return new Predicate(e);
}
public Predicate changeStatesToRefinements(List<GhostState> ghostState, String[] toChange) throws LJError {
Map<String, Expression> nameRefinementMap = new HashMap<>();
for (GhostState gs : ghostState) {
if (gs.getRefinement() != null) { // is a state and not a ghost state
String name = gs.getQualifiedName();
Expression exp = innerParse(gs.getRefinement().toString(), gs.getPrefix());
nameRefinementMap.put(name, exp);
// Also allow simple name lookup to enable hierarchy matching
String simple = Utils.getSimpleName(name);
nameRefinementMap.putIfAbsent(simple, exp);
}
}
Expression e = exp.substituteState(nameRefinementMap, toChange);
return new Predicate(e);
}
public boolean isBooleanTrue() {
return exp.isBooleanTrue();
}
@Override
public String toString() {
return exp.toString();
}
@Override
public Predicate clone() {
return new Predicate(exp.clone());
}
public Expression getExpression() {
return exp;
}
public ValDerivationNode simplify() {
return ExpressionSimplifier.simplify(exp.clone());
}
private static boolean isBooleanLiteral(Expression expr, boolean value) {
return expr instanceof LiteralBoolean && expr.isBooleanTrue() == value;
}
public static Predicate createConjunction(Predicate c1, Predicate c2) {
// simplification: (true && x) = x, (false && x) = false
if (isBooleanLiteral(c1.getExpression(), true))
return c2;
if (isBooleanLiteral(c2.getExpression(), true))
return c1;
if (isBooleanLiteral(c1.getExpression(), false))
return c1;
if (isBooleanLiteral(c2.getExpression(), false))
return c2;
return new Predicate(new BinaryExpression(c1.getExpression(), Ops.AND, c2.getExpression()));
}
public static Predicate createDisjunction(Predicate c1, Predicate c2) {
// simplification: (false || x) = x, (true || x) = true
if (isBooleanLiteral(c1.getExpression(), false))
return c2;
if (isBooleanLiteral(c2.getExpression(), false))
return c1;
if (isBooleanLiteral(c1.getExpression(), true))
return c1;
if (isBooleanLiteral(c2.getExpression(), true))
return c2;
return new Predicate(new BinaryExpression(c1.getExpression(), Ops.OR, c2.getExpression()));
}
public static Predicate createEquals(Predicate c1, Predicate c2) {
return new Predicate(new BinaryExpression(c1.getExpression(), Ops.EQ, c2.getExpression()));
}
public static Predicate createITE(Predicate c1, Predicate c2, Predicate c3) {
return new Predicate(new Ite(c1.getExpression(), c2.getExpression(), c3.getExpression()));
}
public static Predicate createLit(String value, String type) {
Expression exp = switch (type) {
case Types.BOOLEAN -> new LiteralBoolean(value);
case Types.INT, Types.SHORT -> new LiteralInt(value);
case Types.LONG -> new LiteralLong(value);
case Types.DOUBLE, Types.FLOAT -> new LiteralReal(value);
default -> throw new IllegalArgumentException("Unsupported literal type: " + type);
};
return new Predicate(exp);
}
public static Predicate createOperation(Predicate c1, String op, Predicate c2) {
return new Predicate(new BinaryExpression(c1.getExpression(), op, c2.getExpression()));
}
public static Predicate createVar(String name) {
return new Predicate(new Var(name));
}
public static Predicate createInvocation(String name, Predicate... Predicates) {
List<Expression> le = new ArrayList<>();
for (Predicate c : Predicates)
le.add(c.getExpression());
return new Predicate(new FunctionInvocation(name, le));
}
}