Search in sources :

Example 1 with SwitchCaseGraphImpl

use of com.android.tools.idea.experimental.codeanalysis.datastructs.graph.impl.SwitchCaseGraphImpl in project android by JetBrains.

the class CFGBuilder method dfsPsiSwitchLabelStatementBuilder.

/**
   * Build nodes for case "label" statement.
   * @param statement
   */
public void dfsPsiSwitchLabelStatementBuilder(PsiSwitchLabelStatement statement) {
    PsiExpression caseValueExpr = statement.getCaseValue();
    if (!(this.mGraph instanceof SwitchCaseGraph)) {
        //Not a inside a switch but encountered case?
        PsiCFGDebugUtil.LOG.warning("Case statement out side of switch in :");
        PsiCFGDebugUtil.debugOutputPsiElement(statement);
        return;
    }
    SwitchCaseGraphImpl switchGraph = (SwitchCaseGraphImpl) this.mGraph;
    SwitchBranchingNodeImpl switchNode = (SwitchBranchingNodeImpl) switchGraph.getSwitchBranchingNode();
    //Create a case node for this statement
    CaseNodeImpl curCaseNode = new CaseNodeImpl(this.mGraph);
    Value caseValue;
    //Evaluate the constant
    if (statement.isDefaultCase()) {
        //It is a default case
        curCaseNode.setDefault();
        switchNode.setDefaultTarget(curCaseNode);
    } else {
        //A "case value: " statement
        if (caseValueExpr == null) {
            PsiCFGDebugUtil.LOG.warning("CaseExpr is null in " + statement.getText());
            return;
        }
        if (caseValueExpr instanceof PsiLiteralExpression) {
            caseValue = dfsLiteralExpressionBuilder((PsiLiteralExpression) caseValueExpr);
        } else if (caseValueExpr instanceof PsiReferenceExpression) {
            //TODO: Add more sophisticate check for Reference Expression
            //Only primitive types are allowed in switch case.
            caseValue = dfsRHSReferenceExpressionBuilder((PsiReferenceExpression) caseValueExpr);
        } else {
            PsiCFGDebugUtil.LOG.warning("Case is not using a constant or reference");
            PsiCFGDebugUtil.debugOutputPsiElement(caseValueExpr);
            caseValue = new DummyRef(caseValueExpr.getType(), caseValueExpr);
        }
        curCaseNode.setLabelValue(caseValue);
        switchNode.setTargetViaKey(caseValue, curCaseNode);
    }
    switchGraph.addCase(curCaseNode);
    if (curWorkingNodeList.size() != 0) {
        connectCurrentWorkingNode(curCaseNode);
    }
    curWorkingNodeList.clear();
    curWorkingNodeList.add(curCaseNode);
}
Also used : SwitchCaseGraph(com.android.tools.idea.experimental.codeanalysis.datastructs.graph.SwitchCaseGraph) SwitchCaseGraphImpl(com.android.tools.idea.experimental.codeanalysis.datastructs.graph.impl.SwitchCaseGraphImpl)

Example 2 with SwitchCaseGraphImpl

use of com.android.tools.idea.experimental.codeanalysis.datastructs.graph.impl.SwitchCaseGraphImpl in project android by JetBrains.

the class CFGBuilder method dfsPsiSwitchStatementBuilder.

/**
   * Build the nodes for the switch statement
   * switch (value) {
   *
   * }
   * @param statement The switch statement
   */
public void dfsPsiSwitchStatementBuilder(PsiSwitchStatement statement) {
    PsiExpression checkedPsiExpr = statement.getExpression();
    PsiCodeBlock switchCodeBlock = statement.getBody();
    Value checkedValue = dfsExpressionBuilder(checkedPsiExpr);
    SwitchBranchingNodeImpl switchNode = new SwitchBranchingNodeImpl(this.mGraph);
    switchNode.setCheckedValue(checkedValue);
    this.mNestedStack.push(switchNode);
    //TODO: Support switch statement with label
    this.mLabelMap.put(switchNode, null);
    //Build the body for the switch Statement
    SwitchCaseGraphImpl switchGraph = new SwitchCaseGraphImpl();
    switchGraph.setParentGraph(this.mGraph);
    switchNode.setSwitchCaseGraph(switchGraph);
    switchGraph.setSwitchBranchingNode(switchNode);
    CFGBuilder switchGraphBuilder = new CFGBuilder(this.mScene, switchGraph, this.containerClass, switchCodeBlock);
    switchGraphBuilder.setNestedStack(this.mNestedStack);
    switchGraphBuilder.setNestedLabelMap(this.mLabelMap);
    switchGraphBuilder.build();
    connectCurrentWorkingNode(switchNode);
    curWorkingNodeList.clear();
    curWorkingNodeList.add(switchNode.getSwitchCaseGraph().getExitNode());
    //SwitchStatement construction finished.
    //Restore the stack
    this.mLabelMap.remove(switchNode);
    this.mNestedStack.pop();
}
Also used : SwitchCaseGraphImpl(com.android.tools.idea.experimental.codeanalysis.datastructs.graph.impl.SwitchCaseGraphImpl)

Aggregations

SwitchCaseGraphImpl (com.android.tools.idea.experimental.codeanalysis.datastructs.graph.impl.SwitchCaseGraphImpl)2 SwitchCaseGraph (com.android.tools.idea.experimental.codeanalysis.datastructs.graph.SwitchCaseGraph)1