-
Notifications
You must be signed in to change notification settings - Fork 35
Added enum types and verification #168
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 1 commit
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
c70cc5a
Added enum type support with tests
cheestree e77a33b
Added EnumRefinementMessage test
cheestree a359d1f
Applied fixes and suggestions for enums
cheestree a6a4055
Fixed grammar
cheestree 0ab2098
Revert "Fixed grammar"
cheestree 44758c7
Reapply "Fixed grammar"
cheestree b610928
Separated translation into phases
cheestree cdf192e
Refactored variable translation
cheestree d7dea40
Added enum tests
cheestree e3e49d2
Added enum tests, adjusted grammar
cheestree aafde53
Moved RefinementMessage test
cheestree 7a7e074
Changed Enumerate to Enum
cheestree 96905c1
Renamed fields and getters/setters
cheestree 8a4e507
Updated variable translation with suggestions
cheestree 2e46e5c
Merge branch 'liquid-java:main' into main
cheestree 2754337
Updated tests to fit with main changes
cheestree 2c23a2e
Extracted enum logic to its own method
cheestree c85ed97
Apply suggestions from code review
cheestree 2ad2747
Renamed usage
cheestree 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
Refactored variable translation
- Loading branch information
commit cdf192e01b23ed26a09ebe1de4c54ff88508f588
There are no files selected for viewing
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,65 +16,48 @@ | |
| import liquidjava.processor.context.GhostState; | ||
| import liquidjava.processor.context.RefinedVariable; | ||
| import spoon.reflect.declaration.CtEnum; | ||
| import spoon.reflect.declaration.CtType; | ||
| import spoon.reflect.reference.CtTypeReference; | ||
|
|
||
| public class TranslatorContextToZ3 { | ||
|
|
||
| static void translateVariables(Context z3, Map<String, CtTypeReference<?>> ctx, | ||
| Map<String, Expr<?>> varTranslation) { | ||
|
|
||
| Map<String, Expr<?>> varTranslation) { | ||
| // Translates all variables into z3 expressions, creating EnumSorts once per unique enum type. | ||
| Map<String, EnumSort<?>> enumSorts = new HashMap<>(); | ||
| translateEnums(z3, ctx, varTranslation, enumSorts); | ||
| translateNonEnumVariables(z3, ctx, varTranslation, enumSorts); | ||
|
|
||
| varTranslation.put("true", z3.mkBool(true)); | ||
| varTranslation.put("false", z3.mkBool(false)); | ||
| } | ||
|
|
||
| private static void translateEnums(Context z3, Map<String, CtTypeReference<?>> ctx, | ||
| Map<String, Expr<?>> varTranslation, Map<String, EnumSort<?>> enumSorts) { | ||
| // First pass: create one EnumSort per enum type and store actual enum constants | ||
| for (Map.Entry<String, CtTypeReference<?>> entry : ctx.entrySet()) { | ||
| String name = entry.getKey(); | ||
| CtTypeReference<?> type = entry.getValue(); | ||
| if (!type.isEnum()) | ||
| continue; | ||
| String typeName = type.getQualifiedName(); | ||
| if (enumSorts.containsKey(typeName)) | ||
|
|
||
| if (varTranslation.containsKey(name)) continue; | ||
|
|
||
| if (type.isEnum() && type.getDeclaration() instanceof CtEnum<?> enumDecl) { | ||
| EnumSort<?> enumSort = translateEnum(z3, varTranslation, enumSorts, type, enumDecl); | ||
| // translateEnum may have already registered name as a literal constant | ||
| // (e.g. Mode.Photo), no need to overwrite | ||
| if (!varTranslation.containsKey(name)) | ||
| varTranslation.put(name, z3.mkConst(name, enumSort)); | ||
| continue; | ||
| CtType<?> decl = type.getDeclaration(); | ||
| if (decl instanceof CtEnum<?> enumDecl) { | ||
| String[] enumValueNames = enumDecl.getEnumValues().stream().map(ev -> ev.getSimpleName()) | ||
| .toArray(String[]::new); | ||
| EnumSort<?> enumSort = z3.mkEnumSort(typeName, enumValueNames); | ||
| enumSorts.put(typeName, enumSort); | ||
|
|
||
| // Store actual enum constant values (not free variables) | ||
| Expr<?>[] consts = enumSort.getConsts(); | ||
| for (int i = 0; i < enumValueNames.length; i++) { | ||
| String varName = enumDecl.getSimpleName() + "." + enumValueNames[i]; | ||
| varTranslation.put(varName, consts[i]); | ||
| } | ||
| } | ||
| varTranslation.put(name, getExpr(z3, name, type)); | ||
| } | ||
|
|
||
| varTranslation.put("true", z3.mkBool(true)); | ||
| varTranslation.put("false", z3.mkBool(false)); | ||
| } | ||
|
|
||
| private static void translateNonEnumVariables(Context z3, Map<String, CtTypeReference<?>> ctx, | ||
| Map<String, Expr<?>> varTranslation, Map<String, EnumSort<?>> enumSorts) { | ||
| // Second pass: translate non-enum variables and enum-typed variables (not constants) | ||
| for (Map.Entry<String, CtTypeReference<?>> entry : ctx.entrySet()) { | ||
| String name = entry.getKey(); | ||
| if (varTranslation.containsKey(name)) | ||
| continue; // Already translated as an enum constant | ||
| CtTypeReference<?> type = entry.getValue(); | ||
| if (type.isEnum()) { | ||
| String typeName = type.getQualifiedName(); | ||
| EnumSort<?> enumSort = enumSorts.get(typeName); | ||
| varTranslation.put(name, z3.mkConst(name, enumSort)); | ||
| } else { | ||
| varTranslation.put(name, getExpr(z3, name, type)); | ||
| } | ||
| } | ||
| private static EnumSort<?> translateEnum(Context z3, Map<String, Expr<?>> varTranslation, Map<String, EnumSort<?>> enumSorts, CtTypeReference<?> type, CtEnum<?> enumDecl) { | ||
| // Creates (and caches if needed) a z3 EnumSort for a given enum type. Registers enum literal constants | ||
|
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. Lets put these comments as javadoc |
||
| // on first creation. | ||
| return enumSorts.computeIfAbsent(type.getQualifiedName(), k -> { | ||
| String[] enumValueNames = enumDecl.getEnumValues().stream() | ||
| .map(ev -> ev.getSimpleName()).toArray(String[]::new); | ||
| EnumSort<?> enumSort = z3.mkEnumSort(k, enumValueNames); | ||
| Expr<?>[] consts = enumSort.getConsts(); | ||
| for (int i = 0; i < enumValueNames.length; i++) | ||
| varTranslation.put(enumDecl.getSimpleName() + "." + enumValueNames[i], consts[i]); | ||
| return enumSort; | ||
| }); | ||
| } | ||
|
|
||
| public static void storeVariablesSubtypes(Context z3, List<RefinedVariable> variables, | ||
|
|
||
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.
Lets split this all function for enums in 2 steps.
Step one: go through the ctx.entrySet add to an enumList all the enums
Step two: use that list to translate to z3 using
mkEnumSort.The current implementation seems to work but its a bit complex to understand, lets simplify it.