Search in sources :

Example 1 with DfaInstance

use of org.jetbrains.plugins.groovy.lang.psi.dataFlow.DfaInstance in project intellij-community by JetBrains.

the class ControlFlowUtils method inferWriteAccessMap.

@NotNull
public static List<BitSet> inferWriteAccessMap(final Instruction[] flow, final GrVariable var) {
    final Semilattice<BitSet> sem = new Semilattice<BitSet>() {

        @NotNull
        @Override
        public BitSet join(@NotNull List<BitSet> ins) {
            BitSet result = new BitSet(flow.length);
            for (BitSet set : ins) {
                result.or(set);
            }
            return result;
        }

        @Override
        public boolean eq(@NotNull BitSet e1, @NotNull BitSet e2) {
            return e1.equals(e2);
        }
    };
    DfaInstance<BitSet> dfa = new DfaInstance<BitSet>() {

        @Override
        public void fun(@NotNull BitSet bitSet, @NotNull Instruction instruction) {
            if (!(instruction instanceof ReadWriteVariableInstruction))
                return;
            if (!((ReadWriteVariableInstruction) instruction).isWrite())
                return;
            final PsiElement element = instruction.getElement();
            if (element instanceof GrVariable && element != var)
                return;
            if (element instanceof GrReferenceExpression) {
                final GrReferenceExpression ref = (GrReferenceExpression) element;
                if (ref.isQualified() || ref.resolve() != var) {
                    return;
                }
            }
            if (!((ReadWriteVariableInstruction) instruction).getVariableName().equals(var.getName())) {
                return;
            }
            bitSet.clear();
            bitSet.set(instruction.num());
        }

        @NotNull
        @Override
        public BitSet initial() {
            return new BitSet(flow.length);
        }
    };
    return new DFAEngine<>(flow, dfa, sem).performForceDFA();
}
Also used : ReadWriteVariableInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction) Instruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction) MaybeReturnInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.MaybeReturnInstruction) AfterCallInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.AfterCallInstruction) ThrowingInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.ThrowingInstruction) IfEndInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.IfEndInstruction) ReadWriteVariableInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction) NotNull(org.jetbrains.annotations.NotNull) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) DfaInstance(org.jetbrains.plugins.groovy.lang.psi.dataFlow.DfaInstance) Semilattice(org.jetbrains.plugins.groovy.lang.psi.dataFlow.Semilattice) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PsiElement (com.intellij.psi.PsiElement)1 NotNull (org.jetbrains.annotations.NotNull)1 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)1 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)1 AfterCallInstruction (org.jetbrains.plugins.groovy.lang.psi.controlFlow.AfterCallInstruction)1 Instruction (org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction)1 ReadWriteVariableInstruction (org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction)1 IfEndInstruction (org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.IfEndInstruction)1 MaybeReturnInstruction (org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.MaybeReturnInstruction)1 ThrowingInstruction (org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.ThrowingInstruction)1 DfaInstance (org.jetbrains.plugins.groovy.lang.psi.dataFlow.DfaInstance)1 Semilattice (org.jetbrains.plugins.groovy.lang.psi.dataFlow.Semilattice)1