-
Notifications
You must be signed in to change notification settings - Fork 35
Operator Assignment Support #214
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
liquidjava-example/src/main/java/testSuite/CorrectOperatorAssignments.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| package testSuite; | ||
|
|
||
| import liquidjava.specification.Refinement; | ||
|
|
||
| public class CorrectOperatorAssignments { | ||
|
|
||
| @Refinement("_ > 0") | ||
| int plus(@Refinement("_ >= 0") int x) { | ||
| x += 1; // x is now > 0 | ||
| return x; | ||
| } | ||
|
|
||
| @Refinement("_ > 0") | ||
| int plusInvocation(@Refinement("_ >= 0") int x) { | ||
| x += one(); // x is now > 0 | ||
| return x; | ||
| } | ||
|
|
||
| @Refinement("_ == 1") | ||
| int one() { | ||
| return 1; | ||
| } | ||
|
|
||
| @Refinement("_ < 0") | ||
| int minus(@Refinement("_ <= 0") int x) { | ||
| x -= 1; // x is now < 0 | ||
| return x; | ||
| } | ||
|
|
||
| @Refinement("_ >= 0") | ||
| int multiply(int x) { | ||
| x *= x; // x is now >= 0 | ||
| return x; | ||
| } | ||
|
|
||
| @Refinement("_ <= 1") | ||
| int divide(@Refinement("0 <= _ && _ <= 3") int x) { | ||
| x /= 2; // x is now <= 1 | ||
| return x; | ||
| } | ||
|
|
||
| @Refinement("_ == 0 || _ == 1") | ||
| int remainder(@Refinement("_ >= 0") int x) { | ||
| x %= 2; // x is now == 0 || x is now == 1 | ||
| return x; | ||
| } | ||
|
|
||
| int remainderInvocation(@Refinement("_ >= 0") int x) { | ||
| @Refinement("_ == 10 || _ == 11") | ||
| int y = 10; | ||
| y += remainder(x); // x is now >= 6 | ||
| return x; | ||
| } | ||
|
|
||
| @Refinement("_ == 9") | ||
| int plusUnaryInvocation() { | ||
| int y = 10; | ||
| y += -one(); | ||
| return y; | ||
| } | ||
|
|
||
| @Refinement("_ == 11") | ||
| int plusConditional(@Refinement("_ >= 0") int x) { | ||
| int y = 10; | ||
| y += x >= 0 ? one() : 2; | ||
| return y; | ||
| } | ||
|
|
||
| @Refinement("_ == 13") | ||
| int plusBinaryExpression() { | ||
| int y = 10; | ||
| y += one() + 2; | ||
| return y; | ||
| } | ||
|
|
||
| int plusArrayRead(int[] values) { | ||
| int y = 10; | ||
| y += values[0]; | ||
| return y; | ||
| } | ||
|
|
||
| @Refinement("_ == 11") | ||
| int plusCast() { | ||
| int y = 10; | ||
| y += (int) one(); | ||
| return y; | ||
| } | ||
| } | ||
60 changes: 60 additions & 0 deletions
60
liquidjava-example/src/main/java/testSuite/ErrorOperatorAssignments.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| package testSuite; | ||
|
|
||
| import liquidjava.specification.Refinement; | ||
|
|
||
| @SuppressWarnings("unused") | ||
| public class ErrorOperatorAssignments { | ||
|
|
||
| @Refinement("_ == 1") | ||
| int one() { | ||
| return 1; | ||
| } | ||
|
|
||
| @Refinement("_ == 0 || _ == 1") | ||
| int remainder(@Refinement("_ >= 0") int x) { | ||
| x %= 2; | ||
| return x; | ||
| } | ||
|
|
||
| @Refinement("_ == 12") | ||
| int plusInvocation(@Refinement("_ >= 0") int x) { | ||
| int y = 10; | ||
| y += remainder(x); | ||
| return y; // Refinement Error | ||
| } | ||
|
|
||
| @Refinement("_ == 10") | ||
| int plusUnaryInvocation() { | ||
| int y = 10; | ||
| y += -one(); | ||
| return y; // Refinement Error | ||
| } | ||
|
|
||
| @Refinement("_ == 12") | ||
| int plusConditional(@Refinement("_ >= 0") int x) { | ||
| int y = 10; | ||
| y += x >= 0 ? one() : 2; | ||
| return y; // Refinement Error | ||
| } | ||
|
|
||
| @Refinement("_ == 14") | ||
| int plusBinaryExpression() { | ||
| int y = 10; | ||
| y += one() + 2; | ||
| return y; // Refinement Error | ||
| } | ||
|
|
||
| @Refinement("_ == 10") | ||
| int plusArrayRead(int[] values) { | ||
| int y = 10; | ||
| y += values[0]; | ||
| return y; // Refinement Error | ||
| } | ||
|
|
||
| @Refinement("_ == 12") | ||
| int plusCast() { | ||
| int y = 10; | ||
| y += (int) one(); | ||
| return y; // Refinement Error | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,16 +16,21 @@ | |
| import liquidjava.utils.constants.Ops; | ||
| import liquidjava.utils.constants.Types; | ||
| import liquidjava.rj_language.Predicate; | ||
| import liquidjava.rj_language.ast.BinaryExpression; | ||
| import liquidjava.rj_language.ast.Expression; | ||
| import liquidjava.rj_language.ast.GroupExpression; | ||
| import org.apache.commons.lang3.NotImplementedException; | ||
| import spoon.reflect.code.BinaryOperatorKind; | ||
| import spoon.reflect.code.CtAssignment; | ||
| import spoon.reflect.code.CtConditional; | ||
| import spoon.reflect.code.CtBinaryOperator; | ||
| import spoon.reflect.code.CtExpression; | ||
| import spoon.reflect.code.CtFieldRead; | ||
| import spoon.reflect.code.CtIf; | ||
| import spoon.reflect.code.CtInvocation; | ||
| import spoon.reflect.code.CtLiteral; | ||
| import spoon.reflect.code.CtLocalVariable; | ||
| import spoon.reflect.code.CtOperatorAssignment; | ||
| import spoon.reflect.code.CtReturn; | ||
| import spoon.reflect.code.CtUnaryOperator; | ||
| import spoon.reflect.code.CtVariableRead; | ||
|
|
@@ -94,6 +99,18 @@ public <T> void getBinaryOpRefinements(CtBinaryOperator<T> operator) throws LJEr | |
| // TODO ADD TYPES | ||
| } | ||
|
|
||
| /** | ||
| * Builds the refinement for a operator assignment. Java operator assignments such as {@code x += y} are modeled as | ||
| * {@code x = x + y}; the returned predicate refines the assigned value as {@code _ == current(x) <op> rhs}. | ||
| */ | ||
| public Predicate getOperatorAssignmentRefinement(String assignedName, CtOperatorAssignment<?, ?> assignment) | ||
| throws LJError { | ||
| Predicate left = getCurrentVariableValue(assignedName); | ||
| Predicate right = getOperatorAssignmentRefinement(assignment.getAssignment()); | ||
| Predicate operation = Predicate.createOperation(left, getOperatorFromKind(assignment.getKind()), right); | ||
| return Predicate.createEquals(Predicate.createVar(Keys.WILDCARD), operation); | ||
| } | ||
|
|
||
| /** | ||
| * Finds and adds refinement metadata to the unary operation | ||
| * | ||
|
|
@@ -280,6 +297,91 @@ private Predicate getOperationRefinementFromExternalLib(CtInvocation<?> inv) thr | |
| return new Predicate(); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the latest symbolic value for a variable | ||
| */ | ||
| private Predicate getCurrentVariableValue(String name) { | ||
| Optional<VariableInstance> variableInstance = rtc.getContext().getLastVariableInstance(name); | ||
| return Predicate.createVar(variableInstance.map(VariableInstance::getName).orElse(name)); | ||
| } | ||
|
|
||
| /** | ||
| * Converts a operator assignment into an arithmetic predicate operand | ||
| */ | ||
| private Predicate getOperatorAssignmentRefinement(CtExpression<?> element) throws LJError { | ||
| if (element instanceof CtVariableRead<?> variableRead) { | ||
| String name = variableRead.getVariable().getSimpleName(); | ||
| if (variableRead instanceof CtFieldRead<?>) | ||
| name = String.format(Formats.THIS, name); | ||
| return getCurrentVariableValue(name); | ||
| } else if (element instanceof CtBinaryOperator<?> binaryOperator) { | ||
| Predicate left = getOperatorAssignmentRefinement(binaryOperator.getLeftHandOperand()); | ||
| Predicate right = getOperatorAssignmentRefinement(binaryOperator.getRightHandOperand()); | ||
| return Predicate.createOperation(left, getOperatorFromKind(binaryOperator.getKind()), right); | ||
| } else if (element instanceof CtConditional<?> conditional) { | ||
| Predicate condition = getConditionRefinement(conditional.getCondition()); | ||
| Predicate thenExpression = getOperatorAssignmentRefinement(conditional.getThenExpression()); | ||
| Predicate elseExpression = getOperatorAssignmentRefinement(conditional.getElseExpression()); | ||
| return Predicate.createITE(condition, thenExpression, elseExpression); | ||
| } else if (element instanceof CtLiteral<?> literal) { | ||
| if (literal.getValue() == null) | ||
| throw new CustomError("Null literals are not supported", literal.getPosition()); | ||
| return new Predicate(literal.getValue().toString(), element); | ||
| } else if (element instanceof CtInvocation<?>) { | ||
| VariableInstance invocationValue = (VariableInstance) element.getMetadata(Keys.TARGET); | ||
| if (invocationValue != null) | ||
| return Predicate.createVar(invocationValue.getName()); | ||
| } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if it is another kind of expression? we need more fallbacks. outputs |
||
| return valueFromRefinement(element, rtc.getRefinement(element)); | ||
| } | ||
|
|
||
| private Predicate getConditionRefinement(CtExpression<Boolean> condition) throws LJError { | ||
| Predicate refinement = rtc.getRefinement(condition); | ||
| Optional<Predicate> value = unwrapWildcardEquality(refinement); | ||
| if (value.isPresent()) | ||
| return value.get(); | ||
| return refinement; | ||
| } | ||
|
|
||
| private Optional<Predicate> unwrapWildcardEquality(Predicate refinement) { | ||
| Expression expression = unwrapGroupExpression(refinement.getExpression()); | ||
| if (expression instanceof BinaryExpression binaryExpression && Ops.EQ.equals(binaryExpression.getOperator()) | ||
| && Keys.WILDCARD.equals(binaryExpression.getFirstOperand().toString())) { | ||
| return Optional.of(new Predicate(binaryExpression.getSecondOperand())); | ||
| } | ||
| return Optional.empty(); | ||
| } | ||
|
|
||
| private Predicate valueFromRefinement(CtExpression<?> element, Predicate refinement) { | ||
| if (refinement == null) | ||
| return createFreshValue(element, new Predicate()); | ||
|
|
||
| Optional<Predicate> value = unwrapWildcardEquality(refinement); | ||
| if (value.isPresent()) | ||
| return value.get(); | ||
|
|
||
| Expression expression = unwrapGroupExpression(refinement.getExpression()); | ||
| boolean hasWildcard = refinement.getVariableNames().contains(Keys.WILDCARD); | ||
| if (!hasWildcard && !expression.isBooleanExpression()) | ||
| return new Predicate(expression); | ||
|
|
||
| Predicate constraint = hasWildcard ? refinement : new Predicate(); | ||
| return createFreshValue(element, constraint); | ||
| } | ||
|
|
||
| private Predicate createFreshValue(CtExpression<?> element, Predicate refinement) { | ||
| String newName = String.format(Formats.FRESH, rtc.getContext().getCounter()); | ||
| Predicate freshRefinement = refinement.substituteVariable(Keys.WILDCARD, newName); | ||
| rtc.getContext().addVarToContext(newName, element.getType(), freshRefinement, element); | ||
| return Predicate.createVar(newName); | ||
| } | ||
|
|
||
| private Expression unwrapGroupExpression(Expression expression) { | ||
| while (expression instanceof GroupExpression groupExpression) | ||
| expression = groupExpression.getExpression(); | ||
| return expression; | ||
| } | ||
|
|
||
| /** | ||
| * Retrieves the refinements for the variable write inside unary operation | ||
| * | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we also get some negative cases for examples?