From 8691569a6f6279b20c9470e576e7eae00c698dab Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Fri, 20 Dec 2019 17:48:03 +0700 Subject: [PATCH] DfaMemoryState#castTopOfStack moved to StandardInstructionVisitor and made private GitOrigin-RevId: e17486569a2472b4b36563209f8ee40bb2ba0b48 --- .../dataFlow/DfaMemoryState.java | 9 --------- .../dataFlow/DfaMemoryStateImpl.java | 15 -------------- .../dataFlow/StandardInstructionVisitor.java | 20 +++++++++++++++++-- 3 files changed, 18 insertions(+), 26 deletions(-) diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/DfaMemoryState.java b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/DfaMemoryState.java index c5c585986b2f..b0bf82a77062 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/DfaMemoryState.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/DfaMemoryState.java @@ -63,15 +63,6 @@ public interface DfaMemoryState { void setVarValue(DfaVariableValue var, DfaValue value); - /** - * Ensures that top-of-stack value is either null or belongs to the supplied type - * - * @param type the type to cast to - * @return true if cast is successful; false if top-of-stack value type is incompatible with supplied type - * @throws java.util.EmptyStackException if stack is empty - */ - boolean castTopOfStack(@NotNull TypeConstraint type); - /** * Returns a relation between given values within this state, if known * @param left first value diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/DfaMemoryStateImpl.java b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/DfaMemoryStateImpl.java index 5f4a0296fabe..7a3e49fdb587 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/DfaMemoryStateImpl.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/DfaMemoryStateImpl.java @@ -519,21 +519,6 @@ public class DfaMemoryStateImpl implements DfaMemoryState { return myStack.isEmpty(); } - @Override - public boolean castTopOfStack(@NotNull TypeConstraint type) { - DfaValue value = peek(); - DfType dfType = getDfType(value); - DfType result = dfType.meet(type.asDfType()); - if (!result.equals(dfType)) { - if (result == DfTypes.NULL || !meetDfType(value, result)) return false; - if (!(value instanceof DfaVariableValue)) { - pop(); - push(myFactory.fromDfType(result)); - } - } - return true; - } - private void convertReferenceEqualityToValueEquality(DfaValue value) { int id = canonicalize(value).getID(); Integer index = myIdToEqClassesIndices.get(id); diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/StandardInstructionVisitor.java b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/StandardInstructionVisitor.java index 3c14423b367b..ae9865fdf5fd 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/StandardInstructionVisitor.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/StandardInstructionVisitor.java @@ -282,7 +282,7 @@ public class StandardInstructionVisitor extends InstructionVisitor { if (transfer != null) { DfaMemoryState castFail = memState.createCopy(); if (fromType != null && type.isConvertibleFrom(fromType)) { - if (!memState.castTopOfStack(constraint)) { + if (!castTopOfStack(factory, memState, constraint)) { castPossible = false; } else { result.add(new DfaInstructionState(runner.getInstruction(instruction.getIndex() + 1), memState)); @@ -302,7 +302,7 @@ public class StandardInstructionVisitor extends InstructionVisitor { } } else { if (fromType != null && type.isConvertibleFrom(fromType)) { - if (!memState.castTopOfStack(constraint)) { + if (!castTopOfStack(factory, memState, constraint)) { castPossible = false; } } @@ -315,6 +315,22 @@ public class StandardInstructionVisitor extends InstructionVisitor { return result.toArray(DfaInstructionState.EMPTY_ARRAY); } + private static boolean castTopOfStack(@NotNull DfaValueFactory factory, + @NotNull DfaMemoryState state, + @NotNull TypeConstraint type) { + DfaValue value = state.peek(); + DfType dfType = state.getDfType(value); + DfType result = dfType.meet(type.asDfType()); + if (!result.equals(dfType)) { + if (result == DfTypes.NULL || !state.meetDfType(value, result)) return false; + if (!(value instanceof DfaVariableValue)) { + state.pop(); + state.push(factory.fromDfType(result)); + } + } + return true; + } + protected void onTypeCast(PsiTypeCastExpression castExpression, DfaMemoryState state, boolean castPossible) {} protected void beforeMethodCall(@NotNull PsiExpression expression,