-
Notifications
You must be signed in to change notification settings - Fork 35
Code Refactoring #109
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
Code Refactoring #109
Changes from 3 commits
Commits
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
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
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
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 |
|---|---|---|
|
|
@@ -13,7 +13,9 @@ | |
| import liquidjava.rj_language.BuiltinFunctionPredicate; | ||
| import liquidjava.rj_language.Predicate; | ||
| import liquidjava.rj_language.parsing.ParsingException; | ||
| import liquidjava.utils.Utils; | ||
| import liquidjava.utils.constants.Formats; | ||
| import liquidjava.utils.constants.Keys; | ||
| import liquidjava.utils.constants.Types; | ||
|
|
||
| import org.apache.commons.lang3.NotImplementedException; | ||
| import spoon.reflect.code.CtArrayRead; | ||
|
|
@@ -172,22 +174,22 @@ public <T> void visitCtNewArray(CtNewArray<T> newArray) { | |
| } catch (ParsingException e) { | ||
| return; // error already in ErrorEmitter | ||
| } | ||
| String name = String.format(freshFormat, context.getCounter()); | ||
| if (c.getVariableNames().contains(WILD_VAR)) { | ||
| c = c.substituteVariable(WILD_VAR, name); | ||
| String name = String.format(Formats.RET, context.getCounter()); | ||
|
||
| if (c.getVariableNames().contains(Keys.WILDCARD)) { | ||
| c = c.substituteVariable(Keys.WILDCARD, name); | ||
| } else { | ||
| c = Predicate.createEquals(Predicate.createVar(name), c); | ||
| } | ||
| context.addVarToContext(name, factory.Type().INTEGER_PRIMITIVE, c, exp); | ||
| Predicate ep; | ||
| try { | ||
| ep = Predicate.createEquals( | ||
| BuiltinFunctionPredicate.builtin_length(WILD_VAR, newArray, getErrorEmitter()), | ||
| BuiltinFunctionPredicate.builtin_length(Keys.WILDCARD, newArray, getErrorEmitter()), | ||
| Predicate.createVar(name)); | ||
| } catch (ParsingException e) { | ||
| return; // error already in ErrorEmitter | ||
| } | ||
| newArray.putMetadata(REFINE_KEY, ep); | ||
| newArray.putMetadata(Keys.REFINEMENT, ep); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -201,9 +203,9 @@ public <T> void visitCtThisAccess(CtThisAccess<T> thisAccess) { | |
| CtClass<?> c = thisAccess.getParent(CtClass.class); | ||
| String s = c.getSimpleName(); | ||
| if (thisAccess.getParent() instanceof CtReturn) { | ||
| String thisName = String.format(thisFormat, s); | ||
| thisAccess.putMetadata(REFINE_KEY, | ||
| Predicate.createEquals(Predicate.createVar(WILD_VAR), Predicate.createVar(thisName))); | ||
| String thisName = String.format(Formats.THIS, s); | ||
| thisAccess.putMetadata(Keys.REFINEMENT, | ||
| Predicate.createEquals(Predicate.createVar(Keys.WILDCARD), Predicate.createVar(thisName))); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -227,7 +229,7 @@ public <T, A extends T> void visitCtAssignment(CtAssignment<T, A> assignment) { | |
| CtFieldWrite<?> fw = ((CtFieldWrite<?>) ex); | ||
| CtFieldReference<?> cr = fw.getVariable(); | ||
| CtField<?> f = fw.getVariable().getDeclaration(); | ||
| String updatedVarName = String.format(thisFormat, cr.getSimpleName()); | ||
| String updatedVarName = String.format(Formats.THIS, cr.getSimpleName()); | ||
| checkAssignment(updatedVarName, cr.getType(), ex, assignment.getAssignment(), assignment, f); | ||
|
|
||
| // corresponding ghost function update | ||
|
|
@@ -249,9 +251,9 @@ public <T> void visitCtArrayRead(CtArrayRead<T> arrayRead) { | |
| } | ||
|
|
||
| super.visitCtArrayRead(arrayRead); | ||
| String name = String.format(instanceFormat, "arrayAccess", context.getCounter()); | ||
| String name = String.format(Formats.INSTANCE, "arrayAccess", context.getCounter()); | ||
| context.addVarToContext(name, arrayRead.getType(), new Predicate(), arrayRead); | ||
| arrayRead.putMetadata(REFINE_KEY, Predicate.createVar(name)); | ||
| arrayRead.putMetadata(Keys.REFINEMENT, Predicate.createVar(name)); | ||
| // TODO predicate for now is always TRUE | ||
| } | ||
|
|
||
|
|
@@ -261,15 +263,15 @@ public <T> void visitCtLiteral(CtLiteral<T> lit) { | |
| return; | ||
| } | ||
|
|
||
| List<String> types = Arrays.asList(implementedTypes); | ||
| List<String> types = Arrays.asList(Types.IMPLEMENTED); | ||
| String type = lit.getType().getQualifiedName(); | ||
| if (types.contains(type)) { | ||
| lit.putMetadata(REFINE_KEY, Predicate.createEquals(Predicate.createVar(WILD_VAR), | ||
| lit.putMetadata(Keys.REFINEMENT, Predicate.createEquals(Predicate.createVar(Keys.WILDCARD), | ||
| Predicate.createLit(lit.getValue().toString(), type))); | ||
|
|
||
| } else if (lit.getType().getQualifiedName().equals("java.lang.String")) { | ||
| // Only taking care of strings inside refinements | ||
| } else if (type.equals(Utils.NULL_TYPE)) { | ||
| } else if (type.equals(Types.NULL)) { | ||
| // Skip null literals | ||
| } else { | ||
| throw new NotImplementedException( | ||
|
|
@@ -293,10 +295,10 @@ public <T> void visitCtField(CtField<T> f) { | |
| // context.addVarToContext(f.getSimpleName(), f.getType(), | ||
| // c.map( i -> i.substituteVariable(WILD_VAR, f.getSimpleName()).orElse(new | ||
| // Predicate()) ); | ||
| String nname = String.format(thisFormat, f.getSimpleName()); | ||
| String nname = String.format(Formats.THIS, f.getSimpleName()); | ||
| Predicate ret = new Predicate(); | ||
| if (c.isPresent()) { | ||
| ret = c.get().substituteVariable(WILD_VAR, nname).substituteVariable(f.getSimpleName(), nname); | ||
| ret = c.get().substituteVariable(Keys.WILDCARD, nname).substituteVariable(f.getSimpleName(), nname); | ||
| } | ||
| RefinedVariable v = context.addVarToContext(nname, f.getType(), ret, f); | ||
| if (v instanceof Variable) { | ||
|
|
@@ -315,27 +317,27 @@ public <T> void visitCtFieldRead(CtFieldRead<T> fieldRead) { | |
| RefinedVariable rv = context.getVariableByName(fieldName); | ||
| if (rv instanceof Variable && ((Variable) rv).getLocation().isPresent() | ||
| && ((Variable) rv).getLocation().get().equals(fieldRead.getTarget().toString())) { | ||
| fieldRead.putMetadata(REFINE_KEY, context.getVariableRefinements(fieldName)); | ||
| fieldRead.putMetadata(Keys.REFINEMENT, context.getVariableRefinements(fieldName)); | ||
| } else { | ||
| fieldRead.putMetadata(REFINE_KEY, | ||
| Predicate.createEquals(Predicate.createVar(WILD_VAR), Predicate.createVar(fieldName))); | ||
| fieldRead.putMetadata(Keys.REFINEMENT, | ||
| Predicate.createEquals(Predicate.createVar(Keys.WILDCARD), Predicate.createVar(fieldName))); | ||
| } | ||
|
|
||
| } else if (context.hasVariable(String.format(thisFormat, fieldName))) { | ||
| String thisName = String.format(thisFormat, fieldName); | ||
| fieldRead.putMetadata(REFINE_KEY, | ||
| Predicate.createEquals(Predicate.createVar(WILD_VAR), Predicate.createVar(thisName))); | ||
| } else if (context.hasVariable(String.format(Formats.THIS, fieldName))) { | ||
| String thisName = String.format(Formats.THIS, fieldName); | ||
| fieldRead.putMetadata(Keys.REFINEMENT, | ||
| Predicate.createEquals(Predicate.createVar(Keys.WILDCARD), Predicate.createVar(thisName))); | ||
|
|
||
| } else if (fieldRead.getVariable().getSimpleName().equals("length")) { | ||
| String targetName = fieldRead.getTarget().toString(); | ||
| try { | ||
| fieldRead.putMetadata(REFINE_KEY, Predicate.createEquals(Predicate.createVar(WILD_VAR), | ||
| fieldRead.putMetadata(Keys.REFINEMENT, Predicate.createEquals(Predicate.createVar(Keys.WILDCARD), | ||
| BuiltinFunctionPredicate.builtin_length(targetName, fieldRead, getErrorEmitter()))); | ||
| } catch (ParsingException e) { | ||
| return; // error already in ErrorEmitter | ||
| } | ||
| } else { | ||
| fieldRead.putMetadata(REFINE_KEY, new Predicate()); | ||
| fieldRead.putMetadata(Keys.REFINEMENT, new Predicate()); | ||
| // TODO DO WE WANT THIS OR TO SHOW ERROR MESSAGE | ||
| } | ||
|
|
||
|
|
@@ -417,8 +419,8 @@ public void visitCtIf(CtIf ifElement) { | |
| } catch (ParsingException e) { | ||
| return; // error already in ErrorEmitter | ||
| } | ||
| String freshVarName = String.format(freshFormat, context.getCounter()); | ||
| expRefs = expRefs.substituteVariable(WILD_VAR, freshVarName); | ||
| String freshVarName = String.format(Formats.FRESH, context.getCounter()); | ||
| expRefs = expRefs.substituteVariable(Keys.WILDCARD, freshVarName); | ||
| Predicate lastExpRefs = substituteAllVariablesForLastInstance(expRefs); | ||
| expRefs = Predicate.createConjunction(expRefs, lastExpRefs); | ||
|
|
||
|
|
@@ -470,11 +472,11 @@ public <T> void visitCtArrayWrite(CtArrayWrite<T> arrayWrite) { | |
| BuiltinFunctionPredicate fp; | ||
| try { | ||
| fp = BuiltinFunctionPredicate.builtin_addToIndex(arrayWrite.getTarget().toString(), index.toString(), | ||
| WILD_VAR, arrayWrite, getErrorEmitter()); | ||
| Keys.WILDCARD, arrayWrite, getErrorEmitter()); | ||
| } catch (ParsingException e) { | ||
| return; // error already in ErrorEmitter | ||
| } | ||
| arrayWrite.putMetadata(REFINE_KEY, fp); | ||
| arrayWrite.putMetadata(Keys.REFINEMENT, fp); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -487,7 +489,7 @@ public <T> void visitCtConditional(CtConditional<T> conditional) { | |
| Predicate cond = getRefinement(conditional.getCondition()); | ||
| Predicate c = Predicate.createITE(cond, getRefinement(conditional.getThenExpression()), | ||
| getRefinement(conditional.getElseExpression())); | ||
| conditional.putMetadata(REFINE_KEY, c); | ||
| conditional.putMetadata(Keys.REFINEMENT, c); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -582,12 +584,12 @@ private Predicate substituteAllVariablesForLastInstance(Predicate c) { | |
| * Cannot be null | ||
| */ | ||
| private <T> void getPutVariableMetadada(CtElement elem, String name) { | ||
| Predicate cref = Predicate.createEquals(Predicate.createVar(WILD_VAR), Predicate.createVar(name)); | ||
| Predicate cref = Predicate.createEquals(Predicate.createVar(Keys.WILDCARD), Predicate.createVar(name)); | ||
| Optional<VariableInstance> ovi = context.getLastVariableInstance(name); | ||
| if (ovi.isPresent()) { | ||
| cref = Predicate.createEquals(Predicate.createVar(WILD_VAR), Predicate.createVar(ovi.get().getName())); | ||
| cref = Predicate.createEquals(Predicate.createVar(Keys.WILDCARD), Predicate.createVar(ovi.get().getName())); | ||
| } | ||
|
|
||
| elem.putMetadata(REFINE_KEY, cref); | ||
| elem.putMetadata(Keys.REFINEMENT, cref); | ||
| } | ||
| } | ||
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
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.
We could even improve this message to something like "Found multiple disjoint states in state transition - State transition can only go to one state of each state set"