1313import liquidjava .processor .context .*;
1414import liquidjava .rj_language .Predicate ;
1515import liquidjava .smt .SMTEvaluator ;
16- import liquidjava .smt .errors . TypeCheckError ;
16+ import liquidjava .smt .TypeCheckError ;
1717import liquidjava .utils .constants .Keys ;
1818import spoon .reflect .cu .SourcePosition ;
1919import spoon .reflect .declaration .CtElement ;
@@ -39,30 +39,64 @@ public void processSubtyping(Predicate expectedType, List<GhostState> list, CtEl
3939 TranslationTable map = new TranslationTable ();
4040 String [] s = { Keys .WILDCARD , Keys .THIS };
4141 Predicate premisesBeforeChange = joinPredicates (expectedType , mainVars , lrv , map ).toConjunctions ();
42- Predicate premises = new Predicate () ;
43- Predicate et ;
42+ Predicate premises ;
43+ Predicate expected ;
4444 try {
4545 List <GhostState > filtered = filterGhostStatesForVariables (list , mainVars , lrv );
4646 premises = premisesBeforeChange .changeStatesToRefinements (filtered , s ).changeAliasToRefinement (context , f );
47- et = expectedType .changeStatesToRefinements (filtered , s ).changeAliasToRefinement (context , f );
47+ expected = expectedType .changeStatesToRefinements (filtered , s ).changeAliasToRefinement (context , f );
4848 } catch (LJError e ) {
4949 // add location info to error
5050 e .setPosition (element .getPosition ());
5151 throw e ;
5252 }
53- try {
54- smtChecking (premises , et );
55- } catch (Exception e ) {
56- // To emit the message we use the constraints before the alias and state change
57- raiseError (e , element .getPosition (), premisesBeforeChange , expectedType , map );
58- }
53+ boolean isSubtype = smtChecks (expected , premises , element .getPosition ());
54+ if (!isSubtype )
55+ throw new RefinementError (element .getPosition (), expectedType .getExpression (),
56+ premisesBeforeChange .simplify (), map );
5957 }
6058
59+ /**
60+ * Check that type is a subtype of expectedType Throws RefinementError otherwise
61+ *
62+ * @param type
63+ * @param expectedType
64+ * @param list
65+ * @param element
66+ * @param f
67+ *
68+ * @throws LJError
69+ */
6170 public void processSubtyping (Predicate type , Predicate expectedType , List <GhostState > list , CtElement element ,
6271 Factory f ) throws LJError {
6372 boolean b = canProcessSubtyping (type , expectedType , list , element .getPosition (), f );
6473 if (!b )
65- raiseSubtypingError (element .getPosition (), expectedType , type );
74+ throwRefinementError (element .getPosition (), expectedType , type );
75+ }
76+
77+ /**
78+ * Checks the expected against the found constraint
79+ *
80+ * @param expected
81+ * @param found
82+ * @param position
83+ *
84+ * @return true if expected type is subtype of found type, false otherwise
85+ *
86+ * @throws LJError
87+ */
88+ public boolean smtChecks (Predicate expected , Predicate found , SourcePosition position ) throws LJError {
89+ try {
90+ new SMTEvaluator ().verifySubtype (found , expected , context );
91+ return true ;
92+ } catch (TypeCheckError e ) {
93+ return false ;
94+ } catch (LJError e ) {
95+ e .setPosition (position );
96+ throw e ;
97+ } catch (Exception e ) {
98+ throw new CustomError (e .getMessage (), position );
99+ }
66100 }
67101
68102 public boolean canProcessSubtyping (Predicate type , Predicate expectedType , List <GhostState > list ,
@@ -75,23 +109,14 @@ public boolean canProcessSubtyping(Predicate type, Predicate expectedType, List<
75109
76110 TranslationTable map = new TranslationTable ();
77111 String [] s = { Keys .WILDCARD , Keys .THIS };
78-
79- Predicate premises ;
80- Predicate et ;
81- try {
82- premises = joinPredicates (expectedType , mainVars , lrv , map ).toConjunctions ();
83- List <GhostState > filtered = filterGhostStatesForVariables (list , mainVars , lrv );
84- premises = Predicate .createConjunction (premises , type ).changeStatesToRefinements (filtered , s )
85- .changeAliasToRefinement (context , f );
86- et = expectedType .changeStatesToRefinements (filtered , s ).changeAliasToRefinement (context , f );
87- } catch (LJError e ) {
88- // add location info to error
89- e .setPosition (position );
90- throw e ;
91- } catch (Exception e ) {
92- return false ;
93- }
94- return smtChecks (et , premises , position , map );
112+ Predicate premises = joinPredicates (expectedType , mainVars , lrv , map ).toConjunctions ();
113+ List <GhostState > filtered = filterGhostStatesForVariables (list , mainVars , lrv );
114+ premises = Predicate .createConjunction (premises , type ).changeStatesToRefinements (filtered , s )
115+ .changeAliasToRefinement (context , f );
116+ Predicate expected = expectedType .changeStatesToRefinements (filtered , s ).changeAliasToRefinement (context , f );
117+
118+ // check subtyping
119+ return smtChecks (expected , premises , position );
95120 }
96121
97122 /**
@@ -196,7 +221,6 @@ private List<RefinedVariable> getVariables(Predicate c, String varName) {
196221 getVariablesFromContext (c .getVariableNames (), allVars , varName );
197222 List <String > pathNames = pathVariables .stream ().map (Refined ::getName ).collect (Collectors .toList ());
198223 getVariablesFromContext (pathNames , allVars , "" );
199-
200224 return allVars ;
201225 }
202226
@@ -217,30 +241,6 @@ private void recAuxGetVars(RefinedVariable var, List<RefinedVariable> newVars) {
217241 getVariablesFromContext (l , newVars , varName );
218242 }
219243
220- public boolean smtChecks (Predicate expected , Predicate found , SourcePosition position , TranslationTable map )
221- throws LJError {
222- try {
223- new SMTEvaluator ().verifySubtype (found , expected , context );
224- } catch (TypeCheckError e ) {
225- return false ;
226- } catch (Exception e ) {
227- raiseError (e , position , found , expected , map );
228- }
229- return true ;
230- }
231-
232- /**
233- * Checks the expectedType against the cSMT constraint. If the types do not check and error is sent and the program
234- * ends
235- *
236- * @param cSMT
237- * @param expectedType
238- *
239- */
240- private void smtChecking (Predicate cSMT , Predicate expectedType ) throws Exception {
241- new SMTEvaluator ().verifySubtype (cSMT , expectedType , context );
242- }
243-
244244 public void addPathVariable (RefinedVariable rv ) {
245245 pathVariables .add (rv );
246246 }
@@ -256,53 +256,36 @@ void removePathVariableThatIncludes(String otherVar) {
256256
257257 // Errors---------------------------------------------------------------------------------------------------
258258
259- private TranslationTable createMap (Predicate expectedType ) {
259+ protected void throwRefinementError (SourcePosition position , Predicate expected , Predicate found )
260+ throws RefinementError {
260261 List <RefinedVariable > lrv = new ArrayList <>(), mainVars = new ArrayList <>();
261- gatherVariables (expectedType , lrv , mainVars );
262+ gatherVariables (expected , lrv , mainVars );
263+ gatherVariables (found , lrv , mainVars );
262264 TranslationTable map = new TranslationTable ();
263- joinPredicates (expectedType , mainVars , lrv , map );
264- return map ;
265- }
266-
267- protected void raiseError (Exception e , SourcePosition position , Predicate found , Predicate expected ,
268- TranslationTable map ) throws LJError {
269- if (e instanceof TypeCheckError ) {
270- throw new RefinementError (position , expected .getExpression (), found .simplify (), map );
271- } else if (e instanceof liquidjava .smt .errors .NotFoundError nfe ) {
272- throw new NotFoundError (e .getMessage (), position , nfe .getName (), nfe .getKind (), map );
273- } else {
274- String msg = e .getLocalizedMessage ().toLowerCase ();
275- if (msg .contains ("wrong number of arguments" )) {
276- throw new ArgumentMismatchError ("Wrong number of arguments in ghost invocation" , position , map );
277- } else if (msg .contains ("sort mismatch" )) {
278- throw new ArgumentMismatchError ("Type mismatch in arguments of ghost invocation" , position , map );
279- } else {
280- throw new CustomError (e .getMessage (), position );
281- }
282- }
265+ Predicate premises = joinPredicates (expected , mainVars , lrv , map ).toConjunctions ();
266+ throw new RefinementError (position , expected .getExpression (), premises .simplify (), map );
283267 }
284268
285- protected void raiseSubtypingError (SourcePosition position , Predicate expected , Predicate found ) throws LJError {
269+ protected void throwStateRefinementError (SourcePosition position , Predicate found , Predicate expected )
270+ throws StateRefinementError {
286271 List <RefinedVariable > lrv = new ArrayList <>(), mainVars = new ArrayList <>();
287- gatherVariables (expected , lrv , mainVars );
288272 gatherVariables (found , lrv , mainVars );
289273 TranslationTable map = new TranslationTable ();
290- Predicate premises = joinPredicates (expected , mainVars , lrv , map ).toConjunctions ();
291- throw new RefinementError (position , expected .getExpression (), premises .simplify (), map );
274+ VCImplication foundState = joinPredicates (found , mainVars , lrv , map );
275+ throw new StateRefinementError (position , expected .getExpression (),
276+ foundState .toConjunctions ().simplify ().getValue (), map );
292277 }
293278
294- protected void raiseSameStateError (SourcePosition position , Predicate expected ) throws LJError {
279+ protected void throwStateConflictError (SourcePosition position , Predicate expected ) throws StateConflictError {
295280 TranslationTable map = createMap (expected );
296281 throw new StateConflictError (position , expected .getExpression (), map );
297282 }
298283
299- protected void raiseStateMismatchError (SourcePosition position , Predicate found , Predicate expected )
300- throws LJError {
284+ private TranslationTable createMap (Predicate expectedType ) {
301285 List <RefinedVariable > lrv = new ArrayList <>(), mainVars = new ArrayList <>();
302- gatherVariables (found , lrv , mainVars );
286+ gatherVariables (expectedType , lrv , mainVars );
303287 TranslationTable map = new TranslationTable ();
304- VCImplication foundState = joinPredicates (found , mainVars , lrv , map );
305- throw new StateRefinementError (position , expected .getExpression (),
306- foundState .toConjunctions ().simplify ().getValue (), map );
288+ joinPredicates (expectedType , mainVars , lrv , map );
289+ return map ;
307290 }
308291}
0 commit comments