Search in sources :

Example 1 with AssignStmtImpl

use of com.android.tools.idea.experimental.codeanalysis.datastructs.stmt.impl.AssignStmtImpl in project android by JetBrains.

the class CFGBuilder method dfsDeclarationStatementBuilder.

/**
   * Declaration Statement will not return a value, therefore always return null;
   *
   * @param resultArray
   * @param currentDeclStmt
   * @return
   */
public Value dfsDeclarationStatementBuilder(PsiDeclarationStatement currentDeclStmt) {
    PsiElement[] retElements = currentDeclStmt.getDeclaredElements();
    for (PsiElement curElement : retElements) {
        if (curElement == null) {
            PsiCFGDebugUtil.LOG.warning("element in DeclarationStatement " + currentDeclStmt.getText() + " is null");
            continue;
        }
        if (curElement instanceof PsiLocalVariable) {
            //So it is a local variable
            PsiLocalVariable curLocal = (PsiLocalVariable) curElement;
            PsiType localType = curLocal.getType();
            //Generate the decl statement
            DeclarationStmtImpl newDecl = new DeclarationStmtImpl(localType, curLocal, currentDeclStmt);
            connectGeneratedStmt(newDecl);
            if (this.mGraph instanceof BlockGraph) {
                ((BlockGraph) (this.mGraph)).addLocal(curLocal, (LocalImpl) newDecl.getLocal());
            }
            if (curLocal.hasInitializer()) {
                //Generate Statement for the initializer
                PsiExpression initializer = curLocal.getInitializer();
                Value initExpr = dfsExpressionBuilder(initializer);
                AssignStmtImpl initializerStmt = new AssignStmtImpl(true, null, JavaTokenType.EQ);
                LocalImpl localExpr = (LocalImpl) newDecl.getLocal();
                initializerStmt.setROp(initExpr);
                initializerStmt.setLOp(localExpr);
                connectGeneratedStmt(initializerStmt);
            }
        } else if (curElement instanceof PsiClass) {
            //It declares a nested class
            mScene.getOrCreateNestedClass((PsiClass) curElement, this.containerClass, retrieveDeclaringMethod(), this.mGraph);
        } else {
            PsiCFGDebugUtil.LOG.warning("element " + curElement.getText() + " in DeclarationStmt " + currentDeclStmt.getText() + " cannot be resolved");
        }
    }
    return null;
}
Also used : AssignStmtImpl(com.android.tools.idea.experimental.codeanalysis.datastructs.stmt.impl.AssignStmtImpl) BlockGraph(com.android.tools.idea.experimental.codeanalysis.datastructs.graph.BlockGraph) DeclarationStmtImpl(com.android.tools.idea.experimental.codeanalysis.datastructs.stmt.impl.DeclarationStmtImpl)

Example 2 with AssignStmtImpl

use of com.android.tools.idea.experimental.codeanalysis.datastructs.stmt.impl.AssignStmtImpl in project android by JetBrains.

the class CFGBuilder method createSynthesizeTemporalVariable.

/**
   * Create a temp local for an expression
   * @param expr The reference to the expression
   * @return The temp local.
   */
public SynthesizedLocalImpl createSynthesizeTemporalVariable(Value expr) {
    SynthesizedLocalImpl synthesizedLocal = new SynthesizedLocalImpl(expr.getType(), expr.getPsiRef().getText(), null);
    AssignStmtImpl synthesizedAssign = new AssignStmtImpl(true, null, JavaTokenType.EQ);
    synthesizedAssign.setLOp(synthesizedLocal);
    synthesizedAssign.setROp(expr);
    synthesizedLocal.setAssignStmt(synthesizedAssign);
    connectGeneratedStmt(synthesizedAssign);
    return synthesizedLocal;
}
Also used : AssignStmtImpl(com.android.tools.idea.experimental.codeanalysis.datastructs.stmt.impl.AssignStmtImpl)

Example 3 with AssignStmtImpl

use of com.android.tools.idea.experimental.codeanalysis.datastructs.stmt.impl.AssignStmtImpl in project android by JetBrains.

the class CFGBuilder method dfsPolyadicExpressionBuilder.

/**
   * Handle the PsiPolyadicExpression. Like binOpExpress,
   * in PsiTree. The PolyadicExpression could contain
   * short-circuit logical operators. It is handled in this
   * Method
   *
   * @param polyadicExpress The polyadic expression
   * @return
   */
public Value dfsPolyadicExpressionBuilder(PsiPolyadicExpression expression) {
    IElementType iOperator = expression.getOperationTokenType();
    if (expression.getType() == PsiType.BOOLEAN && (iOperator == JavaTokenType.ANDAND || iOperator == JavaTokenType.OROR)) {
        //Short circuit logical operators.
        ArrayList<GraphNode> returnedWorkingNode = Lists.newArrayList();
        PsiExpression[] expressionArray = expression.getOperands();
        if (iOperator == JavaTokenType.OROR) {
            //Short circuit logical OR
            SynthesizedLocal finalLocal = new SynthesizedLocalImpl(PsiType.BOOLEAN, expression.getText(), expression);
            for (int i = 0; i < expressionArray.length; i++) {
                //If this operand is false then evaluate next one
                Value curExprValue = dfsExpressionBuilder(expressionArray[i]);
                AssignStmtImpl synAssign = new AssignStmtImpl(true, null, JavaTokenType.EQ);
                synAssign.setLOp(finalLocal);
                synAssign.setROp(curExprValue);
                GraphNodeImpl assignNode = new GraphNodeImpl(this.mGraph);
                assignNode.getStmtList().add(synAssign);
                returnedWorkingNode.add(assignNode);
                //CreateConditionNode
                if (i != expressionArray.length - 1) {
                    ConditionCheckNodeImpl curCond = new ConditionCheckNodeImpl(this.mGraph, curExprValue);
                    connectCurrentWorkingNode(curCond);
                    curWorkingNodeList.clear();
                    curWorkingNodeList.add(curCond.getFalseBranch());
                    GraphNodeUtil.connectGraphNode(curCond.getTrueBranch(), assignNode);
                } else {
                    //Last one
                    for (GraphNode parent : curWorkingNodeList) {
                        GraphNodeUtil.connectGraphNode(parent, assignNode);
                    }
                }
            }
            curWorkingNodeList.clear();
            curWorkingNodeList.addAll(returnedWorkingNode);
            return finalLocal;
        }
        if (iOperator == JavaTokenType.ANDAND) {
            //Short circuit logical AND
            SynthesizedLocal finalLocal = new SynthesizedLocalImpl(PsiType.BOOLEAN, expression.getText(), expression);
            for (int i = 0; i < expressionArray.length; i++) {
                //If this operand is true then evaluate next one
                Value curExprValue = dfsExpressionBuilder(expressionArray[i]);
                AssignStmtImpl synAssign = new AssignStmtImpl(true, null, JavaTokenType.EQ);
                synAssign.setLOp(finalLocal);
                synAssign.setROp(curExprValue);
                GraphNodeImpl assignNode = new GraphNodeImpl(this.mGraph);
                assignNode.getStmtList().add(synAssign);
                returnedWorkingNode.add(assignNode);
                //CreateConditionNode
                if (i != expressionArray.length - 1) {
                    //Not last one
                    ConditionCheckNodeImpl curCond = new ConditionCheckNodeImpl(this.mGraph, curExprValue);
                    connectCurrentWorkingNode(curCond);
                    curWorkingNodeList.clear();
                    curWorkingNodeList.add(curCond.getTrueBranch());
                    GraphNodeUtil.connectGraphNode(curCond.getFalseBranch(), assignNode);
                } else {
                    //Last one
                    for (GraphNode parent : curWorkingNodeList) {
                        GraphNodeUtil.connectGraphNode(parent, assignNode);
                    }
                }
            }
            curWorkingNodeList.clear();
            curWorkingNodeList.addAll(returnedWorkingNode);
            return finalLocal;
        }
    //End of ANDAND
    }
    // End of short circuit Logical operators.
    PolyadicExprImpl mPolyadicExpr = new PolyadicExprImpl(iOperator, expression);
    PsiExpression[] allOperands = expression.getOperands();
    for (PsiExpression curPsiExpr : allOperands) {
        Value v = dfsExpressionBuilder(curPsiExpr);
        mPolyadicExpr.addOperand(v);
    }
    SynthesizedLocalImpl polyadicLocal = createSynthesizeTemporalVariable(mPolyadicExpr);
    return polyadicLocal;
}
Also used : AssignStmtImpl(com.android.tools.idea.experimental.codeanalysis.datastructs.stmt.impl.AssignStmtImpl) IElementType(com.intellij.psi.tree.IElementType)

Example 4 with AssignStmtImpl

use of com.android.tools.idea.experimental.codeanalysis.datastructs.stmt.impl.AssignStmtImpl in project android by JetBrains.

the class CFGBuilder method dfsAssignmentExpressionBuilder.

/**
   * Build the nodes for assignment expression
   * @param expression the assignment expression
   * @return The local or the reference to the assignment expression
   */
public Value dfsAssignmentExpressionBuilder(PsiAssignmentExpression expression) {
    PsiExpression LExpr = expression.getLExpression();
    PsiExpression RExpr = expression.getRExpression();
    Value LhsValue = dfsLHSExpressionBuilder(LExpr);
    Value RhsValue = dfsExpressionBuilder(RExpr);
    AssignStmtImpl assignStmt = new AssignStmtImpl(false, expression, expression.getOperationTokenType());
    assignStmt.setLOp(LhsValue);
    assignStmt.setROp(RhsValue);
    connectGeneratedStmt(assignStmt);
    return LhsValue;
}
Also used : AssignStmtImpl(com.android.tools.idea.experimental.codeanalysis.datastructs.stmt.impl.AssignStmtImpl)

Example 5 with AssignStmtImpl

use of com.android.tools.idea.experimental.codeanalysis.datastructs.stmt.impl.AssignStmtImpl in project android by JetBrains.

the class CFGBuilder method dfsPsiConditionalExpressionBuilder.

public Value dfsPsiConditionalExpressionBuilder(PsiConditionalExpression expression) {
    PsiExpression conditionCheckExpression = expression.getCondition();
    PsiExpression trueBranchExpression = expression.getThenExpression();
    PsiExpression falseBranchExpression = expression.getElseExpression();
    //Note in condition express, the type of two branch might not branch.
    //There should be a method to determine the correct type.
    //Currently we just use the type from the PsiExpression
    Value conditionCheckExpr = dfsExpressionBuilder(conditionCheckExpression);
    ConditionCheckNodeImpl curConditionCheckNode = new ConditionCheckNodeImpl(this.mGraph, conditionCheckExpr);
    connectCurrentWorkingNode(curConditionCheckNode);
    SynthesizedLocalImpl synLocal = new SynthesizedLocalImpl(expression.getType(), expression.getText(), expression);
    //Evaluate True Branch
    curWorkingNodeList.clear();
    curWorkingNodeList.add(curConditionCheckNode.getTrueBranch());
    Value trueBrachValue = dfsExpressionBuilder(trueBranchExpression);
    AssignStmtImpl trueBranchAssign = new AssignStmtImpl(true, null, JavaTokenType.EQ);
    trueBranchAssign.setLOp(synLocal);
    trueBranchAssign.setROp(trueBrachValue);
    GraphNodeImpl trueBranchAssignNode = new GraphNodeImpl(this.mGraph);
    trueBranchAssignNode.getStmtList().add(trueBranchAssign);
    connectCurrentWorkingNode(trueBranchAssignNode);
    //Evaluate False Branch
    curWorkingNodeList.clear();
    curWorkingNodeList.add(curConditionCheckNode.getFalseBranch());
    Value falseBranchValue = dfsExpressionBuilder(falseBranchExpression);
    AssignStmtImpl falseBranchAssign = new AssignStmtImpl(true, null, JavaTokenType.EQ);
    falseBranchAssign.setLOp(synLocal);
    falseBranchAssign.setROp(falseBranchValue);
    GraphNodeImpl falseBranchAssignNode = new GraphNodeImpl(this.mGraph);
    falseBranchAssignNode.getStmtList().add(falseBranchAssign);
    connectCurrentWorkingNode(falseBranchAssignNode);
    //Setup curWorkingNodeList
    curWorkingNodeList.clear();
    curWorkingNodeList.add(trueBranchAssignNode);
    curWorkingNodeList.add(falseBranchAssignNode);
    return synLocal;
}
Also used : AssignStmtImpl(com.android.tools.idea.experimental.codeanalysis.datastructs.stmt.impl.AssignStmtImpl)

Aggregations

AssignStmtImpl (com.android.tools.idea.experimental.codeanalysis.datastructs.stmt.impl.AssignStmtImpl)8 IElementType (com.intellij.psi.tree.IElementType)2 BlockGraph (com.android.tools.idea.experimental.codeanalysis.datastructs.graph.BlockGraph)1 DeclarationStmtImpl (com.android.tools.idea.experimental.codeanalysis.datastructs.stmt.impl.DeclarationStmtImpl)1