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));
}
}
}
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);
}
}
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;
}
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;
}
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;
}
Aggregations