11package liquidjava .processor .refinement_checker ;
22
33import java .util .ArrayList ;
4- import java .util .Arrays ;
54import java .util .List ;
65import java .util .Set ;
76import java .util .Stack ;
1817import liquidjava .processor .VCImplication ;
1918import liquidjava .processor .context .*;
2019import liquidjava .rj_language .Predicate ;
21- import liquidjava .rj_language .ast .Expression ;
2220import liquidjava .smt .SMTEvaluator ;
2321import liquidjava .smt .errors .TypeCheckError ;
2422import liquidjava .utils .constants .Keys ;
@@ -53,26 +51,26 @@ public void processSubtyping(Predicate expectedType, List<GhostState> list, CtEl
5351 premises = premisesBeforeChange .changeStatesToRefinements (filtered , s ).changeAliasToRefinement (context , f );
5452 et = expectedType .changeStatesToRefinements (filtered , s ).changeAliasToRefinement (context , f );
5553 } catch (Exception e ) {
56- throw new RefinementError (element , expectedType .getExpression (), premises .simplify (), map );
54+ throw new RefinementError (element . getPosition () , expectedType .getExpression (), premises .simplify (), map );
5755 }
5856
5957 try {
6058 smtChecking (premises , et );
6159 } catch (Exception e ) {
6260 // To emit the message we use the constraints before the alias and state change
63- raiseError (e , premisesBeforeChange , expectedType , element , map );
61+ raiseError (e , element . getPosition (), premisesBeforeChange , expectedType , map );
6462 }
6563 }
6664
6765 public void processSubtyping (Predicate type , Predicate expectedType , List <GhostState > list , CtElement element ,
6866 Factory f ) throws LJError {
6967 boolean b = canProcessSubtyping (type , expectedType , list , element .getPosition (), f );
7068 if (!b )
71- raiseSubtypingError (element , expectedType , type );
69+ raiseSubtypingError (element . getPosition () , expectedType , type );
7270 }
7371
74- public boolean canProcessSubtyping (Predicate type , Predicate expectedType , List <GhostState > list , SourcePosition p ,
75- Factory f ) throws LJError {
72+ public boolean canProcessSubtyping (Predicate type , Predicate expectedType , List <GhostState > list ,
73+ SourcePosition position , Factory f ) throws LJError {
7674 List <RefinedVariable > lrv = new ArrayList <>(), mainVars = new ArrayList <>();
7775 gatherVariables (expectedType , lrv , mainVars );
7876 gatherVariables (type , lrv , mainVars );
@@ -93,7 +91,7 @@ public boolean canProcessSubtyping(Predicate type, Predicate expectedType, List<
9391 } catch (Exception e ) {
9492 return false ;
9593 }
96- return smtChecks (premises , et , p );
94+ return smtChecks (et , premises , position , map );
9795 }
9896
9997 /**
@@ -219,22 +217,14 @@ private void recAuxGetVars(RefinedVariable var, List<RefinedVariable> newVars) {
219217 getVariablesFromContext (l , newVars , varName );
220218 }
221219
222- public boolean smtChecks (Predicate found , Predicate expectedType , SourcePosition p ) throws LJError {
220+ public boolean smtChecks (Predicate expected , Predicate found , SourcePosition position , TranslationTable map )
221+ throws LJError {
223222 try {
224- new SMTEvaluator ().verifySubtype (found , expectedType , context );
223+ new SMTEvaluator ().verifySubtype (found , expected , context );
225224 } catch (TypeCheckError e ) {
226225 return false ;
227- }
228- //TODO: handle NotFoundError properly
229- catch (Exception e ) {
230- String msg = e .getLocalizedMessage ().toLowerCase ();
231- if (msg .contains ("wrong number of arguments" )) {
232- throw new CustomError ("Wrong number of arguments in ghost invocation" , p );
233- } else if (msg .contains ("sort mismatch" )) {
234- throw new CustomError ("Type mismatch in arguments of ghost invocation" , p );
235- } else {
236- throw new CustomError (e .getMessage (), p );
237- }
226+ } catch (Exception e ) {
227+ raiseError (e , position , found , expected , map );
238228 }
239229 return true ;
240230 }
@@ -274,39 +264,45 @@ private TranslationTable createMap(Predicate expectedType) {
274264 return map ;
275265 }
276266
277- protected void raiseSubtypingError (CtElement element , Predicate expectedType , Predicate foundType ) throws LJError {
278- List <RefinedVariable > lrv = new ArrayList <>(), mainVars = new ArrayList <>();
279- gatherVariables (expectedType , lrv , mainVars );
280- gatherVariables (foundType , lrv , mainVars );
281- TranslationTable map = new TranslationTable ();
282- Predicate premises = joinPredicates (expectedType , mainVars , lrv , map ).toConjunctions ();
283- throw new RefinementError (element , expectedType .getExpression (), premises .simplify (), map );
284- }
285-
286- public void raiseSameStateError (CtElement element , Predicate expectedType , String klass ) throws LJError {
287- TranslationTable map = createMap (expectedType );
288- throw new StateConflictError (element , expectedType .getExpression (), map );
289- }
290-
291- private void raiseError (Exception e , Predicate premisesBeforeChange , Predicate expectedType , CtElement element ,
267+ protected void raiseError (Exception e , SourcePosition position , Predicate found , Predicate expected ,
292268 TranslationTable map ) throws LJError {
293269 if (e instanceof TypeCheckError ) {
294- throw new RefinementError (element , expectedType .getExpression (), premisesBeforeChange .simplify (), map );
270+ throw new RefinementError (position , expected .getExpression (), found .simplify (), map );
295271 } else if (e instanceof liquidjava .smt .errors .NotFoundError nfe ) {
296- throw new NotFoundError (element , e .getMessage (), nfe .getName (), map );
272+ throw new NotFoundError (position , e .getMessage (), nfe .getName (), nfe . getKind (), map );
297273 } else {
298- throw new CustomError (e .getMessage (), element );
274+ String msg = e .getLocalizedMessage ().toLowerCase ();
275+ if (msg .contains ("wrong number of arguments" )) {
276+ throw new CustomError ("Wrong number of arguments in ghost invocation" , position );
277+ } else if (msg .contains ("sort mismatch" )) {
278+ throw new CustomError ("Type mismatch in arguments of ghost invocation" , position );
279+ } else {
280+ throw new CustomError (e .getMessage (), position );
281+ }
299282 }
300283 }
301284
302- public void raiseStateMismatchError (CtElement element , String method , Predicate found , Predicate expected )
285+ protected void raiseSubtypingError (SourcePosition position , Predicate expected , Predicate found ) throws LJError {
286+ List <RefinedVariable > lrv = new ArrayList <>(), mainVars = new ArrayList <>();
287+ gatherVariables (expected , lrv , mainVars );
288+ gatherVariables (found , lrv , mainVars );
289+ TranslationTable map = new TranslationTable ();
290+ Predicate premises = joinPredicates (expected , mainVars , lrv , map ).toConjunctions ();
291+ throw new RefinementError (position , expected .getExpression (), premises .simplify (), map );
292+ }
293+
294+ protected void raiseSameStateError (SourcePosition position , Predicate expected , String klass ) throws LJError {
295+ TranslationTable map = createMap (expected );
296+ throw new StateConflictError (position , expected .getExpression (), map );
297+ }
298+
299+ protected void raiseStateMismatchError (SourcePosition position , String method , Predicate found , Predicate expected )
303300 throws LJError {
304301 List <RefinedVariable > lrv = new ArrayList <>(), mainVars = new ArrayList <>();
305302 gatherVariables (found , lrv , mainVars );
306303 TranslationTable map = new TranslationTable ();
307304 VCImplication foundState = joinPredicates (found , mainVars , lrv , map );
308- throw new StateRefinementError (element ,
309- expected .getExpression (),
305+ throw new StateRefinementError (position , expected .getExpression (),
310306 foundState .toConjunctions ().simplify ().getValue (), map );
311307 }
312308}
0 commit comments