use of org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction in project intellij-community by JetBrains.
the class ControlFlowUtils method findNearestInstruction.
@Nullable
public static Instruction findNearestInstruction(PsiElement place, Instruction[] flow) {
List<Instruction> applicable = new ArrayList<>();
for (Instruction instruction : flow) {
final PsiElement element = instruction.getElement();
if (element == null)
continue;
if (element == place)
return instruction;
if (PsiTreeUtil.isAncestor(element, place, true)) {
applicable.add(instruction);
}
}
if (applicable.isEmpty())
return null;
Collections.sort(applicable, (o1, o2) -> {
PsiElement e1 = o1.getElement();
PsiElement e2 = o2.getElement();
LOG.assertTrue(e1 != null);
LOG.assertTrue(e2 != null);
final TextRange t1 = e1.getTextRange();
final TextRange t2 = e2.getTextRange();
final int s1 = t1.getStartOffset();
final int s2 = t2.getStartOffset();
if (s1 == s2) {
return t1.getEndOffset() - t2.getEndOffset();
}
return s2 - s1;
});
return applicable.get(0);
}
use of org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction in project intellij-community by JetBrains.
the class InstructionImpl method toString.
public String toString() {
final StringBuilder builder = new StringBuilder();
builder.append(myNumber);
builder.append("(");
for (Instruction successor : mySuccessors) {
builder.append(successor.num());
builder.append(',');
}
if (!mySuccessors.isEmpty())
builder.delete(builder.length() - 1, builder.length());
builder.append(") ").append(getElementPresentation());
return builder.toString();
}
use of org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction in project intellij-community by JetBrains.
the class DFAEngine method performDFA.
@Nullable
private List<E> performDFA(boolean timeout) {
final int n = myFlow.length;
final List<E> info = new ArrayList<>(Collections.nCopies(n, myDfa.initial()));
final CallEnvironment env = new MyCallEnvironment(n);
final WorkList workList = new WorkList(getFlowOrder());
while (!workList.isEmpty()) {
ProgressManager.checkCanceled();
if (timeout && checkCounter())
return null;
final int num = workList.next();
final Instruction curr = myFlow[num];
// saved outbound state
final E oldE = info.get(num);
// inbound state
final E newE = getInboundState(curr, info, env);
// newly modified outbound state
myDfa.fun(newE, curr);
if (!mySemilattice.eq(newE, oldE)) {
// if outbound state changed
// save new state
info.set(num, newE);
for (Instruction next : getNext(curr, env)) {
workList.offer(next.num());
}
}
}
return info;
}
use of org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction in project intellij-community by JetBrains.
the class ReachingDefinitionsCollector method findPath.
/**
* return true if path is outside of fragment, null if there is no pathand false if path is inside fragment
*/
@Nullable
private static Boolean findPath(Instruction cur, int destination, LinkedHashSet<Integer> fragmentInsns, boolean wasOutside, HashMap<Instruction, Boolean> visited) {
wasOutside = wasOutside || !fragmentInsns.contains(cur.num());
visited.put(cur, null);
Iterable<? extends Instruction> instructions = cur.allSuccessors();
boolean pathExists = false;
for (Instruction i : instructions) {
if (i.num() == destination)
return wasOutside;
Boolean result;
if (visited.containsKey(i)) {
result = visited.get(i);
} else {
result = findPath(i, destination, fragmentInsns, wasOutside, visited);
visited.put(i, result);
}
if (result != null) {
if (result.booleanValue()) {
visited.put(cur, true);
return true;
}
pathExists = true;
}
}
if (pathExists) {
visited.put(cur, false);
return false;
} else {
visited.put(cur, null);
return null;
}
}
use of org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction in project intellij-community by JetBrains.
the class ReachingDefinitionsCollector method postprocess.
@NotNull
private static DefinitionMap postprocess(@NotNull final List<DefinitionMap> dfaResult, @NotNull Instruction[] flow, @NotNull ReachingDefinitionsDfaInstance dfaInstance) {
DefinitionMap result = new DefinitionMap();
for (int i = 0; i < flow.length; i++) {
Instruction insn = flow[i];
if (insn instanceof ReadWriteVariableInstruction) {
ReadWriteVariableInstruction rwInsn = (ReadWriteVariableInstruction) insn;
if (!rwInsn.isWrite()) {
int idx = dfaInstance.getVarIndex(rwInsn.getVariableName());
result.copyFrom(dfaResult.get(i), idx, i);
}
}
}
return result;
}
Aggregations