Skip to content

Commit 4f0272b

Browse files
committed
Add File to GhostState
1 parent d05f418 commit 4f0272b

File tree

5 files changed

+24
-31
lines changed

5 files changed

+24
-31
lines changed

liquidjava-verifier/src/main/java/liquidjava/processor/context/ContextHistory.java

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ public class ContextHistory {
1414
private static ContextHistory instance;
1515

1616
private Map<String, Map<String, Set<RefinedVariable>>> fileScopeVars; // file -> (scope -> variables in scope)
17-
private Map<String, Set<GhostState>> ghosts; // file -> ghosts
18-
19-
// globals
17+
private Set<GhostState> ghosts;
2018
private Set<AliasWrapper> aliases;
2119
private Set<RefinedVariable> instanceVars;
2220
private Set<RefinedVariable> globalVars;
@@ -25,7 +23,7 @@ private ContextHistory() {
2523
fileScopeVars = new HashMap<>();
2624
instanceVars = new HashSet<>();
2725
globalVars = new HashSet<>();
28-
ghosts = new HashMap<>();
26+
ghosts = new HashSet<>();
2927
aliases = new HashSet<>();
3028
}
3129

@@ -44,7 +42,7 @@ public void clearHistory() {
4442
}
4543

4644
public void saveContext(CtElement element, Context context) {
47-
String file = getFile(element);
45+
String file = Utils.getFile(element);
4846
if (file == null)
4947
return;
5048

@@ -56,26 +54,11 @@ public void saveContext(CtElement element, Context context) {
5654
// add other elements in context (except ghosts)
5755
instanceVars.addAll(context.getCtxInstanceVars());
5856
globalVars.addAll(context.getCtxGlobalVars());
57+
ghosts.addAll(context.getGhostStates());
5958
aliases.addAll(context.getAliases());
6059
}
6160

62-
public void saveGhost(CtElement element, GhostState ghost) {
63-
String file = getFile(element);
64-
if (file == null)
65-
return;
66-
ghosts.putIfAbsent(file, new HashSet<>());
67-
ghosts.get(file).add(ghost);
68-
}
69-
70-
private String getFile(CtElement element) {
71-
SourcePosition pos = element.getPosition();
72-
if (pos == null || pos.getFile() == null)
73-
return null;
74-
75-
return pos.getFile().getAbsolutePath();
76-
}
77-
78-
public String getScopePosition(CtElement element) {
61+
private String getScopePosition(CtElement element) {
7962
CtElement startElement = element instanceof CtParameter<?> ? element.getParent() : element;
8063
SourcePosition annPosition = Utils.getFirstLJAnnotationPosition(startElement);
8164
SourcePosition pos = element.getPosition();
@@ -95,7 +78,7 @@ public Set<RefinedVariable> getGlobalVars() {
9578
return globalVars;
9679
}
9780

98-
public Map<String, Set<GhostState>> getGhosts() {
81+
public Set<GhostState> getGhosts() {
9982
return ghosts;
10083
}
10184

liquidjava-verifier/src/main/java/liquidjava/processor/context/GhostState.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ public class GhostState extends GhostFunction {
88

99
private GhostFunction parent;
1010
private Predicate refinement;
11+
private final String file;
1112

12-
public GhostState(String name, List<CtTypeReference<?>> list, CtTypeReference<?> returnType, String prefix) {
13+
public GhostState(String name, List<CtTypeReference<?>> list, CtTypeReference<?> returnType, String prefix,
14+
String file) {
1315
super(name, list, returnType, prefix);
16+
this.file = file;
1417
}
1518

1619
public void setGhostParent(GhostFunction parent) {
@@ -28,4 +31,8 @@ public GhostFunction getParent() {
2831
public Predicate getRefinement() {
2932
return refinement;
3033
}
34+
35+
public String getFile() {
36+
return file;
37+
}
3138
}

liquidjava-verifier/src/main/java/liquidjava/processor/refinement_checker/ExternalRefinementTypeChecker.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@
88
import liquidjava.diagnostics.warnings.ExternalClassNotFoundWarning;
99
import liquidjava.diagnostics.warnings.ExternalMethodNotFoundWarning;
1010
import liquidjava.processor.context.Context;
11-
import liquidjava.processor.context.ContextHistory;
1211
import liquidjava.processor.context.GhostFunction;
1312
import liquidjava.processor.facade.GhostDTO;
1413
import liquidjava.processor.refinement_checker.general_checkers.MethodsFunctionsChecker;
1514
import liquidjava.rj_language.Predicate;
16-
import liquidjava.rj_language.parsing.RefinementsParser;
1715
import liquidjava.utils.Utils;
1816
import spoon.reflect.code.CtLiteral;
1917
import spoon.reflect.cu.SourcePosition;
@@ -31,7 +29,6 @@
3129
public class ExternalRefinementTypeChecker extends TypeChecker {
3230
private String prefix;
3331
private final Diagnostics diagnostics = Diagnostics.getInstance();
34-
private final ContextHistory contextHistory = ContextHistory.getInstance();
3532

3633
public ExternalRefinementTypeChecker(Context context, Factory factory) {
3734
super(context, factory);

liquidjava-verifier/src/main/java/liquidjava/processor/refinement_checker/TypeChecker.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,11 @@ private void createStateSet(CtNewArray<String> e, int set, CtElement element) th
154154
CtLiteral<String> s = (CtLiteral<String>) ce;
155155
String f = s.getValue();
156156
GhostState gs = new GhostState(f, g.getParametersTypes(), factory.Type().BOOLEAN_PRIMITIVE,
157-
g.getPrefix());
157+
g.getPrefix(), Utils.getFile(element));
158158
gs.setGhostParent(g);
159159
gs.setRefinement(Predicate.createEquals(ip, Predicate.createLit(Integer.toString(order), Types.INT)));
160160
// open(THIS) -> state1(THIS) == 1
161161
context.addToGhostClass(g.getParentClassName(), gs);
162-
contextHistory.saveGhost(element, gs);
163162
}
164163
order++;
165164
}
@@ -192,9 +191,8 @@ private void createStateGhost(String string, CtElement element, SourcePosition p
192191
List<CtTypeReference<?>> param = Collections.singletonList(factory.Type().createReference(qn));
193192

194193
CtTypeReference<?> r = factory.Type().createReference(gd.returnType());
195-
GhostState gs = new GhostState(gd.name(), param, r, qn);
194+
GhostState gs = new GhostState(gd.name(), param, r, qn, Utils.getFile(element));
196195
context.addToGhostClass(sn, gs);
197-
contextHistory.saveGhost(element, gs);
198196
}
199197

200198
protected String getQualifiedClassName(CtElement element) {

liquidjava-verifier/src/main/java/liquidjava/utils/Utils.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ public static String qualifyName(String prefix, String name) {
3838
return String.format("%s.%s", prefix, name);
3939
}
4040

41+
public static String getFile(CtElement element) {
42+
SourcePosition pos = element.getPosition();
43+
if (pos == null || pos.getFile() == null)
44+
return null;
45+
46+
return pos.getFile().getAbsolutePath();
47+
}
48+
4149
public static SourcePosition getLJAnnotationPosition(CtElement element, String refinement) {
4250
return element.getAnnotations().stream()
4351
.filter(a -> isLiquidJavaAnnotation(a) && hasRefinementValue(a, "\"" + refinement + "\"")).findFirst()

0 commit comments

Comments
 (0)