Search in sources :

Example 11 with Instruction

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);
}
Also used : TextRange(com.intellij.openapi.util.TextRange) Instruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction) MaybeReturnInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.MaybeReturnInstruction) AfterCallInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.AfterCallInstruction) ThrowingInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.ThrowingInstruction) IfEndInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.IfEndInstruction) ReadWriteVariableInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 12 with Instruction

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();
}
Also used : Instruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction) NegatingGotoInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.NegatingGotoInstruction) CallInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.CallInstruction)

Example 13 with Instruction

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;
}
Also used : CallEnvironment(org.jetbrains.plugins.groovy.lang.psi.controlFlow.CallEnvironment) Instruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction) CallInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.CallInstruction) Nullable(org.jetbrains.annotations.Nullable)

Example 14 with Instruction

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;
    }
}
Also used : Instruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction) ReadWriteVariableInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction) Nullable(org.jetbrains.annotations.Nullable)

Example 15 with Instruction

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;
}
Also used : ReadWriteVariableInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction) Instruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction) ReadWriteVariableInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

Instruction (org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction)27 ReadWriteVariableInstruction (org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction)18 PsiElement (com.intellij.psi.PsiElement)10 Nullable (org.jetbrains.annotations.Nullable)8 GrControlFlowOwner (org.jetbrains.plugins.groovy.lang.psi.GrControlFlowOwner)7 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)7 IfEndInstruction (org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.IfEndInstruction)7 NotNull (org.jetbrains.annotations.NotNull)6 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)6 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)6 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)5 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)5 AfterCallInstruction (org.jetbrains.plugins.groovy.lang.psi.controlFlow.AfterCallInstruction)5 MaybeReturnInstruction (org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.MaybeReturnInstruction)5 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)4 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)4 ThrowingInstruction (org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.ThrowingInstruction)4 BitSet (java.util.BitSet)3 ControlFlowUtils (org.jetbrains.plugins.groovy.codeInspection.utils.ControlFlowUtils)3 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)3