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