Search in sources :

Example 6 with ReadWriteInstruction

use of com.jetbrains.python.codeInsight.controlflow.ReadWriteInstruction in project intellij-community by JetBrains.

the class PyUnusedLocalInspectionVisitor method collectUsedReads.

private void collectUsedReads(final ScopeOwner owner) {
    final Instruction[] instructions = ControlFlowCache.getControlFlow(owner).getInstructions();
    for (int i = 0; i < instructions.length; i++) {
        final Instruction instruction = instructions[i];
        if (instruction instanceof ReadWriteInstruction) {
            final ReadWriteInstruction readWriteInstruction = (ReadWriteInstruction) instruction;
            final ReadWriteInstruction.ACCESS access = readWriteInstruction.getAccess();
            if (!access.isReadAccess()) {
                continue;
            }
            final String name = readWriteInstruction.getName();
            if (name == null) {
                continue;
            }
            final PsiElement element = instruction.getElement();
            // Ignore elements out of scope
            if (element == null || !PsiTreeUtil.isAncestor(owner, element, false)) {
                continue;
            }
            final int startInstruction;
            if (access.isWriteAccess()) {
                final PyAugAssignmentStatement augAssignmentStatement = PyAugAssignmentStatementNavigator.getStatementByTarget(element);
                startInstruction = ControlFlowUtil.findInstructionNumberByElement(instructions, augAssignmentStatement);
            } else {
                startInstruction = i;
            }
            analyzeReadsInScope(name, owner, instructions, startInstruction, as(element, PyReferenceExpression.class));
        }
    }
}
Also used : ReadWriteInstruction(com.jetbrains.python.codeInsight.controlflow.ReadWriteInstruction) ReadWriteInstruction(com.jetbrains.python.codeInsight.controlflow.ReadWriteInstruction) Instruction(com.intellij.codeInsight.controlflow.Instruction) PsiElement(com.intellij.psi.PsiElement)

Example 7 with ReadWriteInstruction

use of com.jetbrains.python.codeInsight.controlflow.ReadWriteInstruction in project intellij-community by JetBrains.

the class PyDefUseUtil method getPostRefs.

private static void getPostRefs(PyTargetExpression var, Instruction[] instructions, int instr, boolean[] visited, Collection<PyElement> result) {
    // TODO: Use ControlFlowUtil.process() for forwards CFG traversal
    if (visited[instr])
        return;
    visited[instr] = true;
    if (instructions[instr] instanceof ReadWriteInstruction) {
        final ReadWriteInstruction instruction = (ReadWriteInstruction) instructions[instr];
        final PsiElement element = instruction.getElement();
        String name = elementName(element);
        if (Comparing.strEqual(name, var.getName())) {
            final ReadWriteInstruction.ACCESS access = instruction.getAccess();
            if (access.isWriteAccess()) {
                return;
            }
            result.add((PyElement) instruction.getElement());
        }
    }
    for (Instruction instruction : instructions[instr].allSucc()) {
        getPostRefs(var, instructions, instruction.num(), visited, result);
    }
}
Also used : ReadWriteInstruction(com.jetbrains.python.codeInsight.controlflow.ReadWriteInstruction) ReadWriteInstruction(com.jetbrains.python.codeInsight.controlflow.ReadWriteInstruction) Instruction(com.intellij.codeInsight.controlflow.Instruction) PsiElement(com.intellij.psi.PsiElement)

Example 8 with ReadWriteInstruction

use of com.jetbrains.python.codeInsight.controlflow.ReadWriteInstruction in project intellij-community by JetBrains.

the class PyDefUseUtil method getLatestDefs.

private static Collection<Instruction> getLatestDefs(final String varName, final Instruction[] instructions, final int instr, final boolean acceptTypeAssertions, final boolean acceptImplicitImports) {
    final Collection<Instruction> result = new LinkedHashSet<>();
    ControlFlowUtil.iteratePrev(instr, instructions, instruction -> {
        final PsiElement element = instruction.getElement();
        final PyImplicitImportNameDefiner implicit = PyUtil.as(element, PyImplicitImportNameDefiner.class);
        if (instruction instanceof ReadWriteInstruction) {
            final ReadWriteInstruction rwInstruction = (ReadWriteInstruction) instruction;
            final ReadWriteInstruction.ACCESS access = rwInstruction.getAccess();
            if (access.isWriteAccess() || acceptTypeAssertions && access.isAssertTypeAccess()) {
                final String name = elementName(element);
                if (Comparing.strEqual(name, varName)) {
                    result.add(rwInstruction);
                    return ControlFlowUtil.Operation.CONTINUE;
                }
            }
        } else if (acceptImplicitImports && implicit != null) {
            if (!implicit.multiResolveName(varName).isEmpty()) {
                result.add(instruction);
                return ControlFlowUtil.Operation.CONTINUE;
            }
        }
        return ControlFlowUtil.Operation.NEXT;
    });
    return result;
}
Also used : ReadWriteInstruction(com.jetbrains.python.codeInsight.controlflow.ReadWriteInstruction) ReadWriteInstruction(com.jetbrains.python.codeInsight.controlflow.ReadWriteInstruction) Instruction(com.intellij.codeInsight.controlflow.Instruction) PsiElement(com.intellij.psi.PsiElement)

Example 9 with ReadWriteInstruction

use of com.jetbrains.python.codeInsight.controlflow.ReadWriteInstruction in project intellij-community by JetBrains.

the class PyReferenceExpressionImpl method getTypeByControlFlow.

private static PyType getTypeByControlFlow(@NotNull String name, @NotNull TypeEvalContext context, @NotNull PyExpression anchor, @NotNull ScopeOwner scopeOwner) {
    final PyAugAssignmentStatement augAssignment = PsiTreeUtil.getParentOfType(anchor, PyAugAssignmentStatement.class);
    final PyElement element = augAssignment != null ? augAssignment : anchor;
    try {
        final List<Instruction> defs = PyDefUseUtil.getLatestDefs(scopeOwner, name, element, true, false);
        if (!defs.isEmpty()) {
            final ReadWriteInstruction firstInstruction = PyUtil.as(defs.get(0), ReadWriteInstruction.class);
            PyType type = firstInstruction != null ? firstInstruction.getType(context, anchor) : null;
            for (int i = 1; i < defs.size(); i++) {
                final ReadWriteInstruction instruction = PyUtil.as(defs.get(i), ReadWriteInstruction.class);
                type = PyUnionType.union(type, instruction != null ? instruction.getType(context, anchor) : null);
            }
            return type;
        }
    } catch (PyDefUseUtil.InstructionNotFoundException ignored) {
    }
    return null;
}
Also used : PyDefUseUtil(com.jetbrains.python.refactoring.PyDefUseUtil) ReadWriteInstruction(com.jetbrains.python.codeInsight.controlflow.ReadWriteInstruction) ReadWriteInstruction(com.jetbrains.python.codeInsight.controlflow.ReadWriteInstruction) Instruction(com.intellij.codeInsight.controlflow.Instruction)

Example 10 with ReadWriteInstruction

use of com.jetbrains.python.codeInsight.controlflow.ReadWriteInstruction in project intellij-community by JetBrains.

the class ScopeUtil method getReadWriteElements.

@NotNull
public static Collection<PsiElement> getReadWriteElements(@NotNull String name, @NotNull ScopeOwner scopeOwner, boolean isReadAccess, boolean isWriteAccess) {
    ControlFlow flow = ControlFlowCache.getControlFlow(scopeOwner);
    Collection<PsiElement> result = new ArrayList<>();
    for (Instruction instr : flow.getInstructions()) {
        if (instr instanceof ReadWriteInstruction) {
            ReadWriteInstruction rw = (ReadWriteInstruction) instr;
            if (name.equals(rw.getName())) {
                ReadWriteInstruction.ACCESS access = rw.getAccess();
                if ((isReadAccess && access.isReadAccess()) || (isWriteAccess && access.isWriteAccess())) {
                    result.add(rw.getElement());
                }
            }
        }
    }
    return result;
}
Also used : ReadWriteInstruction(com.jetbrains.python.codeInsight.controlflow.ReadWriteInstruction) ArrayList(java.util.ArrayList) ReadWriteInstruction(com.jetbrains.python.codeInsight.controlflow.ReadWriteInstruction) Instruction(com.intellij.codeInsight.controlflow.Instruction) ControlFlow(com.intellij.codeInsight.controlflow.ControlFlow) StubBasedPsiElement(com.intellij.psi.StubBasedPsiElement) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

ReadWriteInstruction (com.jetbrains.python.codeInsight.controlflow.ReadWriteInstruction)10 Instruction (com.intellij.codeInsight.controlflow.Instruction)8 PsiElement (com.intellij.psi.PsiElement)7 NotNull (org.jetbrains.annotations.NotNull)4 ControlFlow (com.intellij.codeInsight.controlflow.ControlFlow)2 Scope (com.jetbrains.python.codeInsight.dataflow.scope.Scope)2 StubBasedPsiElement (com.intellij.psi.StubBasedPsiElement)1 ScopeOwner (com.jetbrains.python.codeInsight.controlflow.ScopeOwner)1 ScopeVariable (com.jetbrains.python.codeInsight.dataflow.scope.ScopeVariable)1 ScopeVariableImpl (com.jetbrains.python.codeInsight.dataflow.scope.impl.ScopeVariableImpl)1 PyFunction (com.jetbrains.python.psi.PyFunction)1 PyDefUseUtil (com.jetbrains.python.refactoring.PyDefUseUtil)1 ArrayList (java.util.ArrayList)1