Search in sources :

Example 1 with ScopeVariable

use of com.jetbrains.python.codeInsight.dataflow.scope.ScopeVariable in project intellij-community by JetBrains.

the class PyReachingDefsDfaInstance method processReducedMap.

private DFAMap<ScopeVariable> processReducedMap(DFAMap<ScopeVariable> map, final Instruction instruction, final PsiElement element) {
    String name = null;
    // Process readwrite instruction
    if (instruction instanceof ReadWriteInstruction && ((ReadWriteInstruction) instruction).getAccess().isWriteAccess()) {
        name = ((ReadWriteInstruction) instruction).getName();
    } else // Processing PyFunction
    if (element instanceof PyFunction) {
        name = ((PyFunction) element).getName();
    }
    if (name == null) {
        return map;
    }
    final ScopeVariable variable = map.get(name);
    // Parameter case
    final PsiElement parameterScope = ScopeUtil.getParameterScope(element);
    if (parameterScope != null) {
        final ScopeVariable scopeVariable = new ScopeVariableImpl(name, true, element);
        map = map.asWritable();
        map.put(name, scopeVariable);
    } else // Local variable case
    {
        final ScopeVariableImpl scopeVariable;
        final boolean isParameter = variable != null && variable.isParameter();
        if (variable == null) {
            scopeVariable = new ScopeVariableImpl(name, isParameter, element);
        } else {
            scopeVariable = new ScopeVariableImpl(name, isParameter, variable.getDeclarations());
        }
        map = map.asWritable();
        map.put(name, scopeVariable);
    }
    return map;
}
Also used : ReadWriteInstruction(com.jetbrains.python.codeInsight.controlflow.ReadWriteInstruction) PyFunction(com.jetbrains.python.psi.PyFunction) ScopeVariableImpl(com.jetbrains.python.codeInsight.dataflow.scope.impl.ScopeVariableImpl) ScopeVariable(com.jetbrains.python.codeInsight.dataflow.scope.ScopeVariable) PsiElement(com.intellij.psi.PsiElement)

Example 2 with ScopeVariable

use of com.jetbrains.python.codeInsight.dataflow.scope.ScopeVariable in project intellij-community by JetBrains.

the class PyReachingDefsSemilattice method join.

public DFAMap<ScopeVariable> join(ArrayList<DFAMap<ScopeVariable>> ins) {
    if (ins.isEmpty()) {
        return DFAMap.empty();
    }
    if (ins.size() == 1) {
        return ins.get(0);
    }
    final Set<String> resultNames = getResultNames(ins);
    if (resultNames == null || resultNames.isEmpty()) {
        return new DFAMap<>();
    }
    final DFAMap<ScopeVariable> result = new DFAMap<>();
    for (String name : resultNames) {
        boolean isParameter = true;
        Set<PsiElement> declarations = new HashSet<>();
        // iterating over all maps
        for (DFAMap<ScopeVariable> map : ins) {
            final ScopeVariable variable = map.get(name);
            if (variable == null) {
                continue;
            }
            isParameter = isParameter && variable.isParameter();
            declarations.addAll(variable.getDeclarations());
        }
        final ScopeVariable scopeVariable = new ScopeVariableImpl(name, isParameter, declarations);
        result.put(name, scopeVariable);
    }
    return result;
}
Also used : ScopeVariableImpl(com.jetbrains.python.codeInsight.dataflow.scope.impl.ScopeVariableImpl) ScopeVariable(com.jetbrains.python.codeInsight.dataflow.scope.ScopeVariable) PsiElement(com.intellij.psi.PsiElement) DFAMap(com.intellij.codeInsight.dataflow.map.DFAMap) HashSet(com.intellij.util.containers.HashSet)

Example 3 with ScopeVariable

use of com.jetbrains.python.codeInsight.dataflow.scope.ScopeVariable in project intellij-community by JetBrains.

the class ScopeImpl method computeScopeVariables.

private synchronized void computeScopeVariables() throws DFALimitExceededException {
    computeFlow();
    if (myCachedScopeVariables == null) {
        final PyReachingDefsDfaInstance dfaInstance = new PyReachingDefsDfaInstance();
        final PyReachingDefsSemilattice semilattice = new PyReachingDefsSemilattice();
        final DFAMapEngine<ScopeVariable> engine = new DFAMapEngine<>(myFlow, dfaInstance, semilattice);
        myCachedScopeVariables = engine.performDFA();
    }
}
Also used : PyReachingDefsDfaInstance(com.jetbrains.python.codeInsight.dataflow.PyReachingDefsDfaInstance) DFAMapEngine(com.intellij.codeInsight.dataflow.map.DFAMapEngine) ScopeVariable(com.jetbrains.python.codeInsight.dataflow.scope.ScopeVariable) PyReachingDefsSemilattice(com.jetbrains.python.codeInsight.dataflow.PyReachingDefsSemilattice)

Example 4 with ScopeVariable

use of com.jetbrains.python.codeInsight.dataflow.scope.ScopeVariable in project intellij-community by JetBrains.

the class PyReachingDefsDfaInstance method fun.

public DFAMap<ScopeVariable> fun(final DFAMap<ScopeVariable> map, final Instruction instruction) {
    final PsiElement element = instruction.getElement();
    if (element == null || !((PyFile) element.getContainingFile()).getLanguageLevel().isPy3K()) {
        return processReducedMap(map, instruction, element);
    }
    // Scope reduction
    final DFAMap<ScopeVariable> reducedMap = new DFAMap<>();
    for (Map.Entry<String, ScopeVariable> entry : map.entrySet()) {
        final ScopeVariable value = entry.getValue();
        // Support PEP-3110. (PY-1408)
        if (value.isParameter()) {
            final PsiElement declaration = value.getDeclarations().iterator().next();
            final PyExceptPart exceptPart = PyExceptPartNavigator.getPyExceptPartByTarget(declaration);
            if (exceptPart != null) {
                if (!PsiTreeUtil.isAncestor(exceptPart, element, false)) {
                    continue;
                }
            }
        }
        reducedMap.put(entry.getKey(), value);
    }
    return processReducedMap(reducedMap, instruction, element);
}
Also used : ScopeVariable(com.jetbrains.python.codeInsight.dataflow.scope.ScopeVariable) PyExceptPart(com.jetbrains.python.psi.PyExceptPart) DFAMap(com.intellij.codeInsight.dataflow.map.DFAMap) Map(java.util.Map) PsiElement(com.intellij.psi.PsiElement) DFAMap(com.intellij.codeInsight.dataflow.map.DFAMap)

Aggregations

ScopeVariable (com.jetbrains.python.codeInsight.dataflow.scope.ScopeVariable)4 PsiElement (com.intellij.psi.PsiElement)3 DFAMap (com.intellij.codeInsight.dataflow.map.DFAMap)2 ScopeVariableImpl (com.jetbrains.python.codeInsight.dataflow.scope.impl.ScopeVariableImpl)2 DFAMapEngine (com.intellij.codeInsight.dataflow.map.DFAMapEngine)1 HashSet (com.intellij.util.containers.HashSet)1 ReadWriteInstruction (com.jetbrains.python.codeInsight.controlflow.ReadWriteInstruction)1 PyReachingDefsDfaInstance (com.jetbrains.python.codeInsight.dataflow.PyReachingDefsDfaInstance)1 PyReachingDefsSemilattice (com.jetbrains.python.codeInsight.dataflow.PyReachingDefsSemilattice)1 PyExceptPart (com.jetbrains.python.psi.PyExceptPart)1 PyFunction (com.jetbrains.python.psi.PyFunction)1 Map (java.util.Map)1