Search in sources :

Example 6 with CatchToken

use of org.eclipse.n4js.flowgraphs.model.CatchToken in project n4js by eclipse.

the class SwitchFactory method buildComplexNode.

static ComplexNode buildComplexNode(ReentrantASTIterator astpp, SwitchStatement switchStmt) {
    ComplexNode cNode = new ComplexNode(astpp.container(), switchStmt);
    Node entryNode = new HelperNode(NodeNames.ENTRY, astpp.pos(), switchStmt);
    Node pivotNode = DelegatingNodeFactory.createOrHelper(astpp, NodeNames.PIVOT, switchStmt, switchStmt.getExpression());
    cNode.addNode(entryNode);
    cNode.addNode(pivotNode);
    List<Node> caseNodes = new LinkedList<>();
    // Assumption: clauses are ordered analog to the source code
    List<AbstractCaseClause> caseClauses = switchStmt.getCases();
    for (int n = 0; n < caseClauses.size(); n++) {
        AbstractCaseClause cc = caseClauses.get(n);
        Node caseNode = null;
        if (cc instanceof CaseClause) {
            caseNode = DelegatingNodeFactory.create(astpp, "case_" + n, switchStmt, cc);
        }
        if (cc instanceof DefaultClause) {
            caseNode = DelegatingNodeFactory.create(astpp, NodeNames.DEFAULT, switchStmt, cc);
        }
        caseNodes.add(caseNode);
        cNode.addNode(caseNode);
    }
    Node exitNode = new HelperNode(NodeNames.EXIT, astpp.pos(), switchStmt);
    cNode.addNode(exitNode);
    List<Node> cfs = new LinkedList<>();
    cfs.add(entryNode);
    cfs.add(pivotNode);
    cNode.connectInternalSucc(cfs);
    for (Node cnf : caseNodes) {
        cNode.connectInternalSucc(pivotNode, cnf);
    }
    cfs.clear();
    cfs.addAll(caseNodes);
    cfs.add(exitNode);
    // See {@link JumpFactory} how {@link BreakStatements} modify the control flow
    cNode.connectInternalSucc(cfs);
    if (switchStmt.getDefaultClause() == null)
        cNode.connectInternalSucc(pivotNode, exitNode);
    cNode.setEntryNode(entryNode);
    cNode.setExitNode(exitNode);
    LabelledStatement lblStmt = ASTUtils.getLabelledStatement(switchStmt);
    exitNode.addCatchToken(new CatchToken(ControlFlowType.Break, lblStmt));
    return cNode;
}
Also used : DefaultClause(org.eclipse.n4js.n4JS.DefaultClause) HelperNode(org.eclipse.n4js.flowgraphs.model.HelperNode) ComplexNode(org.eclipse.n4js.flowgraphs.model.ComplexNode) LabelledStatement(org.eclipse.n4js.n4JS.LabelledStatement) ComplexNode(org.eclipse.n4js.flowgraphs.model.ComplexNode) HelperNode(org.eclipse.n4js.flowgraphs.model.HelperNode) Node(org.eclipse.n4js.flowgraphs.model.Node) AbstractCaseClause(org.eclipse.n4js.n4JS.AbstractCaseClause) CaseClause(org.eclipse.n4js.n4JS.CaseClause) AbstractCaseClause(org.eclipse.n4js.n4JS.AbstractCaseClause) CatchToken(org.eclipse.n4js.flowgraphs.model.CatchToken) LinkedList(java.util.LinkedList)

Example 7 with CatchToken

use of org.eclipse.n4js.flowgraphs.model.CatchToken in project n4js by eclipse.

the class TryFactory method buildComplexNode.

static ComplexNode buildComplexNode(ReentrantASTIterator astpp, TryStatement tryStmt) {
    ComplexNode cNode = new ComplexNode(astpp.container(), tryStmt);
    Node entryNode = new HelperNode(NodeNames.ENTRY, astpp.pos(), tryStmt);
    Node tryNode = null;
    Node catchNode = null;
    Node finallyNode = null;
    if (tryStmt.getBlock() != null) {
        tryNode = DelegatingNodeFactory.create(astpp, NodeNames.TRY, tryStmt, tryStmt.getBlock());
    }
    if (tryStmt.getCatch() != null && tryStmt.getCatch().getBlock() != null) {
        CatchBlock catchClause = tryStmt.getCatch();
        CatchToken ct = new CatchToken(ControlFlowType.Throw);
        catchNode = DelegatingNodeFactory.create(astpp, NodeNames.CATCH, tryStmt, catchClause.getBlock());
        catchNode.addCatchToken(ct);
    }
    if (tryStmt.getFinally() != null && tryStmt.getFinally().getBlock() != null) {
        FinallyBlock finallyElem = tryStmt.getFinally();
        CatchToken ct = new CatchToken(ControlFlowType.CatchesAll);
        finallyNode = DelegatingNodeFactory.create(astpp, NodeNames.FINALLY, tryStmt, finallyElem.getBlock());
        finallyNode.addCatchToken(ct);
    }
    Node exitNode = new HelperNode(NodeNames.EXIT, astpp.pos(), tryStmt);
    cNode.addNode(entryNode);
    cNode.addNode(tryNode);
    cNode.addNode(catchNode);
    cNode.addNode(finallyNode);
    cNode.addNode(exitNode);
    List<Node> nodes = new LinkedList<>();
    nodes.add(entryNode);
    nodes.add(tryNode);
    nodes.add(finallyNode);
    nodes.add(exitNode);
    cNode.connectInternalSucc(nodes);
    // TODO: Consider to use a special edge type 'unsound'
    cNode.connectInternalSucc(entryNode, catchNode);
    LinkedList<Node> parts = ListUtils.filterNulls(finallyNode, exitNode);
    Node tgtFrgmt = parts.getFirst();
    // TODO: Consider to use a special edge type 'unsound'
    cNode.connectInternalSucc(tryNode, catchNode);
    cNode.connectInternalSucc(catchNode, tgtFrgmt);
    cNode.setEntryNode(entryNode);
    cNode.setExitNode(exitNode);
    return cNode;
}
Also used : HelperNode(org.eclipse.n4js.flowgraphs.model.HelperNode) ComplexNode(org.eclipse.n4js.flowgraphs.model.ComplexNode) CatchBlock(org.eclipse.n4js.n4JS.CatchBlock) FinallyBlock(org.eclipse.n4js.n4JS.FinallyBlock) ComplexNode(org.eclipse.n4js.flowgraphs.model.ComplexNode) HelperNode(org.eclipse.n4js.flowgraphs.model.HelperNode) Node(org.eclipse.n4js.flowgraphs.model.Node) CatchToken(org.eclipse.n4js.flowgraphs.model.CatchToken) LinkedList(java.util.LinkedList)

Example 8 with CatchToken

use of org.eclipse.n4js.flowgraphs.model.CatchToken in project n4js by eclipse.

the class WhileFactory method buildComplexNode.

static ComplexNode buildComplexNode(ReentrantASTIterator astpp, WhileStatement whileStmt) {
    ComplexNode cNode = new ComplexNode(astpp.container(), whileStmt);
    Node entryNode = new HelperNode(NodeNames.ENTRY, astpp.pos(), whileStmt);
    Node conditionNode = DelegatingNodeFactory.createOrHelper(astpp, NodeNames.CONDITION, whileStmt, whileStmt.getExpression());
    Node conditionForkNode = new HelperNode(NodeNames.CONDITION_FORK, astpp.pos(), whileStmt);
    Node bodyNode = DelegatingNodeFactory.createOrHelper(astpp, NodeNames.BODY, whileStmt, whileStmt.getStatement());
    Node continueCatchNode = new HelperNode(NodeNames.CONTINUE_CATCH, astpp.pos(), whileStmt);
    Node exitNode = new HelperNode(NodeNames.EXIT, astpp.pos(), whileStmt);
    cNode.addNode(entryNode);
    cNode.addNode(conditionNode);
    cNode.addNode(conditionForkNode);
    cNode.addNode(bodyNode);
    cNode.addNode(continueCatchNode);
    cNode.addNode(exitNode);
    cNode.connectInternalSucc(entryNode, conditionNode, conditionForkNode);
    cNode.connectInternalSucc(ControlFlowType.LoopEnter, conditionForkNode, bodyNode);
    cNode.connectInternalSucc(ControlFlowType.LoopExit, conditionForkNode, exitNode);
    cNode.connectInternalSucc(bodyNode, continueCatchNode);
    cNode.connectInternalSucc(ControlFlowType.LoopRepeat, continueCatchNode, conditionNode);
    cNode.setEntryNode(entryNode);
    cNode.setExitNode(exitNode);
    // catch for short-circuits
    conditionForkNode.addCatchToken(new CatchToken(ControlFlowType.IfTrue));
    exitNode.addCatchToken(new CatchToken(ControlFlowType.IfFalse, ControlFlowType.LoopExit));
    LabelledStatement lblStmt = ASTUtils.getLabelledStatement(whileStmt);
    exitNode.addCatchToken(new CatchToken(ControlFlowType.Break, lblStmt));
    continueCatchNode.addCatchToken(new CatchToken(ControlFlowType.Continue, lblStmt));
    return cNode;
}
Also used : HelperNode(org.eclipse.n4js.flowgraphs.model.HelperNode) ComplexNode(org.eclipse.n4js.flowgraphs.model.ComplexNode) LabelledStatement(org.eclipse.n4js.n4JS.LabelledStatement) ComplexNode(org.eclipse.n4js.flowgraphs.model.ComplexNode) HelperNode(org.eclipse.n4js.flowgraphs.model.HelperNode) Node(org.eclipse.n4js.flowgraphs.model.Node) CatchToken(org.eclipse.n4js.flowgraphs.model.CatchToken)

Example 9 with CatchToken

use of org.eclipse.n4js.flowgraphs.model.CatchToken in project n4js by eclipse.

the class BinaryLogicalExpressionFactory method buildComplexNode.

static ComplexNode buildComplexNode(ReentrantASTIterator astpp, BinaryLogicalExpression lbExpr) {
    ComplexNode cNode = new ComplexNode(astpp.container(), lbExpr);
    HelperNode entryNode = new HelperNode(NodeNames.ENTRY, astpp.pos(), lbExpr);
    Node lhsNode = DelegatingNodeFactory.createOrHelper(astpp, NodeNames.LHS, lbExpr, lbExpr.getLhs());
    Node scJumpNode = new HelperNode(NodeNames.SHORT_CIRCUIT_JUMP, astpp.pos(), lbExpr);
    Node rhsNode = DelegatingNodeFactory.createOrHelper(astpp, NodeNames.RHS, lbExpr, lbExpr.getRhs());
    Node exitNode = new RepresentingNode(NodeNames.EXIT, astpp.pos(), lbExpr);
    cNode.addNode(entryNode);
    cNode.addNode(lhsNode);
    cNode.addNode(scJumpNode);
    cNode.addNode(rhsNode);
    cNode.addNode(exitNode);
    ControlFlowType thenCFT = null;
    ControlFlowType elseCFT = null;
    switch(lbExpr.getOp()) {
        case OR:
            thenCFT = ControlFlowType.IfFalse;
            elseCFT = ControlFlowType.IfTrue;
            break;
        case AND:
            thenCFT = ControlFlowType.IfTrue;
            elseCFT = ControlFlowType.IfFalse;
            break;
    }
    cNode.connectInternalSucc(entryNode, lhsNode, scJumpNode);
    cNode.connectInternalSucc(thenCFT, scJumpNode, rhsNode);
    cNode.connectInternalSucc(rhsNode, exitNode);
    // short-circuit evaluation
    scJumpNode.addJumpToken(new JumpToken(elseCFT));
    cNode.setJumpNode(scJumpNode);
    cNode.setEntryNode(entryNode);
    cNode.setExitNode(exitNode);
    rhsNode.addCatchToken(new CatchToken(thenCFT));
    boolean isCatchingLhs = isTopJumpCatcher(lbExpr);
    if (isCatchingLhs) {
        exitNode.addCatchToken(new CatchToken(elseCFT));
        exitNode.addCatchToken(new CatchToken(thenCFT));
    } else {
    // TODO: minor improvement: add the following jump node
    // exitNode.addJumpToken(new JumpToken(thenCFT)); // short-circuit evaluation
    // cNode.setJumpNode(scJumpNode);
    }
    return cNode;
}
Also used : JumpToken(org.eclipse.n4js.flowgraphs.model.JumpToken) HelperNode(org.eclipse.n4js.flowgraphs.model.HelperNode) ComplexNode(org.eclipse.n4js.flowgraphs.model.ComplexNode) ControlFlowType(org.eclipse.n4js.flowgraphs.ControlFlowType) HelperNode(org.eclipse.n4js.flowgraphs.model.HelperNode) RepresentingNode(org.eclipse.n4js.flowgraphs.model.RepresentingNode) ComplexNode(org.eclipse.n4js.flowgraphs.model.ComplexNode) Node(org.eclipse.n4js.flowgraphs.model.Node) RepresentingNode(org.eclipse.n4js.flowgraphs.model.RepresentingNode) CatchToken(org.eclipse.n4js.flowgraphs.model.CatchToken)

Example 10 with CatchToken

use of org.eclipse.n4js.flowgraphs.model.CatchToken in project n4js by eclipse.

the class BlockFactory method buildComplexNode.

static ComplexNode buildComplexNode(ReentrantASTIterator astpp, org.eclipse.n4js.n4JS.Block block) {
    ComplexNode cNode = new ComplexNode(astpp.container(), block);
    Node entryNode = new HelperNode(NodeNames.ENTRY, astpp.pos(), block);
    List<Node> blockNodes = new LinkedList<>();
    EList<Statement> stmts = block.getStatements();
    for (int i = 0; i < stmts.size(); i++) {
        Statement stmt = stmts.get(i);
        Node blockNode = DelegatingNodeFactory.create(astpp, "stmt_" + i, block, stmt);
        blockNodes.add(blockNode);
    }
    Node exitNode = new HelperNode(NodeNames.EXIT, astpp.pos(), block);
    cNode.addNode(entryNode);
    for (Node blockNode : blockNodes) cNode.addNode(blockNode);
    cNode.addNode(exitNode);
    List<Node> nodes = new LinkedList<>();
    nodes.add(entryNode);
    nodes.addAll(blockNodes);
    nodes.add(exitNode);
    cNode.connectInternalSucc(nodes);
    cNode.setEntryNode(entryNode);
    cNode.setExitNode(exitNode);
    if (FGUtils.isCFContainer(block)) {
        exitNode.addCatchToken(new CatchToken(ControlFlowType.CatchesAll));
    } else {
        LabelledStatement lblStmt = ASTUtils.getLabelledStatement(block);
        if (lblStmt != null) {
            exitNode.addCatchToken(new CatchToken(ControlFlowType.Break, lblStmt));
        }
    }
    return cNode;
}
Also used : HelperNode(org.eclipse.n4js.flowgraphs.model.HelperNode) ComplexNode(org.eclipse.n4js.flowgraphs.model.ComplexNode) Statement(org.eclipse.n4js.n4JS.Statement) LabelledStatement(org.eclipse.n4js.n4JS.LabelledStatement) LabelledStatement(org.eclipse.n4js.n4JS.LabelledStatement) ComplexNode(org.eclipse.n4js.flowgraphs.model.ComplexNode) HelperNode(org.eclipse.n4js.flowgraphs.model.HelperNode) Node(org.eclipse.n4js.flowgraphs.model.Node) CatchToken(org.eclipse.n4js.flowgraphs.model.CatchToken) LinkedList(java.util.LinkedList)

Aggregations

CatchToken (org.eclipse.n4js.flowgraphs.model.CatchToken)11 ComplexNode (org.eclipse.n4js.flowgraphs.model.ComplexNode)11 HelperNode (org.eclipse.n4js.flowgraphs.model.HelperNode)11 Node (org.eclipse.n4js.flowgraphs.model.Node)11 LinkedList (java.util.LinkedList)6 LabelledStatement (org.eclipse.n4js.n4JS.LabelledStatement)6 RepresentingNode (org.eclipse.n4js.flowgraphs.model.RepresentingNode)2 VariableDeclarationOrBinding (org.eclipse.n4js.n4JS.VariableDeclarationOrBinding)2 ControlFlowType (org.eclipse.n4js.flowgraphs.ControlFlowType)1 JumpToken (org.eclipse.n4js.flowgraphs.model.JumpToken)1 AbstractCaseClause (org.eclipse.n4js.n4JS.AbstractCaseClause)1 CaseClause (org.eclipse.n4js.n4JS.CaseClause)1 CatchBlock (org.eclipse.n4js.n4JS.CatchBlock)1 DefaultClause (org.eclipse.n4js.n4JS.DefaultClause)1 FinallyBlock (org.eclipse.n4js.n4JS.FinallyBlock)1 ScriptElement (org.eclipse.n4js.n4JS.ScriptElement)1 Statement (org.eclipse.n4js.n4JS.Statement)1