Search in sources :

Example 26 with ComplexNode

use of org.eclipse.n4js.flowgraphs.model.ComplexNode 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)

Example 27 with ComplexNode

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

the class ControlFlowGraphFactory method connectNode.

/**
 * Connects all nodes based on
 * <ul>
 * <li/>the delegating nodes, and
 * <li/>the internal successor information of each node.
 * </ul>
 */
private static void connectNode(ComplexNodeMapper cnMapper, Node mNode) {
    Node internalStartNode = mNode;
    ControlFlowElement subASTElem = mNode.getDelegatedControlFlowElement();
    if (subASTElem != null) {
        ComplexNode subCN = cnMapper.get(subASTElem);
        if (subCN != null) {
            // can be null in case of malformed AST
            EdgeUtils.connectCF(mNode, subCN.getEntry());
            internalStartNode = subCN.getExit();
        }
    }
    Set<Node> internalSuccs = mNode.getInternalSuccessors();
    for (Node internalSucc : internalSuccs) {
        ControlFlowType cfType = mNode.getInternalSuccessorControlFlowType(internalSucc);
        EdgeUtils.connectCF(internalStartNode, internalSucc, cfType);
    }
}
Also used : ComplexNode(org.eclipse.n4js.flowgraphs.model.ComplexNode) ControlFlowType(org.eclipse.n4js.flowgraphs.ControlFlowType) ComplexNode(org.eclipse.n4js.flowgraphs.model.ComplexNode) RepresentingNode(org.eclipse.n4js.flowgraphs.model.RepresentingNode) DelegatingNode(org.eclipse.n4js.flowgraphs.model.DelegatingNode) Node(org.eclipse.n4js.flowgraphs.model.Node) ControlFlowElement(org.eclipse.n4js.n4JS.ControlFlowElement)

Example 28 with ComplexNode

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

the class ControlFlowGraphFactory method connectToJumpTarget.

private static void connectToJumpTarget(ComplexNodeMapper cnMapper, Node jumpNode, JumpToken jumpToken) {
    Pair<Node, ControlFlowType> catcher = CatchNodeFinder.find(jumpToken, jumpNode, cnMapper);
    if (catcher == null) {
        String jumpTokenStr = getJumpTokenDetailString(jumpToken, jumpNode);
        System.err.println("Could not find catching node for jump token '" + jumpTokenStr + "'");
        return;
    }
    Node catchNode = catcher.getKey();
    ControlFlowType newEdgeType = catcher.getValue();
    FinallyBlock enteringFinallyBlock = getEnteringFinallyBlock(catchNode);
    boolean isExitingFinallyBlock = isExitingFinallyBlock(cnMapper, jumpNode);
    if (enteringFinallyBlock != null || isExitingFinallyBlock) {
        boolean equalEdgeExistsAlready = equalEdgeExistsAlready(jumpNode, jumpToken, catchNode);
        if (!equalEdgeExistsAlready) {
            EdgeUtils.connectCF(jumpNode, catchNode, jumpToken);
        }
    } else {
        EdgeUtils.connectCF(jumpNode, catchNode, newEdgeType);
    }
    if (enteringFinallyBlock != null) {
        // Iff finally block was entered abruptly, jump on from exit of finally block
        Block block = enteringFinallyBlock.getBlock();
        ComplexNode cnBlock = cnMapper.get(block);
        Node exitFinallyBlock = cnBlock.getExit();
        connectToJumpTarget(cnMapper, exitFinallyBlock, jumpToken);
    }
}
Also used : ControlFlowType(org.eclipse.n4js.flowgraphs.ControlFlowType) FinallyBlock(org.eclipse.n4js.n4JS.FinallyBlock) ComplexNode(org.eclipse.n4js.flowgraphs.model.ComplexNode) ComplexNode(org.eclipse.n4js.flowgraphs.model.ComplexNode) RepresentingNode(org.eclipse.n4js.flowgraphs.model.RepresentingNode) DelegatingNode(org.eclipse.n4js.flowgraphs.model.DelegatingNode) Node(org.eclipse.n4js.flowgraphs.model.Node) FinallyBlock(org.eclipse.n4js.n4JS.FinallyBlock) Block(org.eclipse.n4js.n4JS.Block)

Example 29 with ComplexNode

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

the class ControlFlowGraphFactory method removeNode.

private static void removeNode(ComplexNode cn, Node mNode) {
    ControlFlowEdge e1 = mNode.pred.first();
    ControlFlowEdge e2 = mNode.succ.first();
    Node pred = e1.start;
    Node succ = e2.end;
    EdgeUtils.removeCF(e1);
    EdgeUtils.removeCF(e2);
    cn.removeNodeChecks(mNode);
    cn.removeNode(mNode);
    for (Node intPred : mNode.getInternalPredecessors()) {
        intPred.removeInternalSuccessor(mNode);
    }
    for (Node intSucc : mNode.getInternalSuccessors()) {
        intSucc.removeInternalPredecessor(mNode);
    }
    mNode.getInternalPredecessors().clear();
    mNode.getInternalSuccessors().clear();
    EdgeUtils.connectCF(pred, succ);
}
Also used : ControlFlowEdge(org.eclipse.n4js.flowgraphs.model.ControlFlowEdge) ComplexNode(org.eclipse.n4js.flowgraphs.model.ComplexNode) RepresentingNode(org.eclipse.n4js.flowgraphs.model.RepresentingNode) DelegatingNode(org.eclipse.n4js.flowgraphs.model.DelegatingNode) Node(org.eclipse.n4js.flowgraphs.model.Node)

Example 30 with ComplexNode

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

the class ForFactory method buildForInOf.

private static ComplexNode buildForInOf(ReentrantASTIterator astpp, ForStatement forStmt, boolean forInSemantics) {
    ComplexNode cNode = new ComplexNode(astpp.container(), forStmt);
    Node entryNode = new HelperNode(NodeNames.ENTRY, astpp.pos(), forStmt);
    List<Node> declNodes = new LinkedList<>();
    List<Node> initNodes = new LinkedList<>();
    if (forStmt.getVarDeclsOrBindings() != null) {
        int i = 0;
        for (VariableDeclarationOrBinding vdob : forStmt.getVarDeclsOrBindings()) {
            Node initNode = DelegatingNodeFactory.create(astpp, "decl_" + i, forStmt, vdob);
            declNodes.add(initNode);
            i++;
        }
    }
    if (forStmt.getInitExpr() != null) {
        Node initNode = DelegatingNodeFactory.create(astpp, NodeNames.INITS, forStmt, forStmt.getInitExpr());
        initNodes.add(initNode);
    }
    Node expressionNode = DelegatingNodeFactory.create(astpp, NodeNames.EXPRESSION, forStmt, forStmt.getExpression());
    Node getObjectKeysNode = null;
    if (forInSemantics) {
        getObjectKeysNode = new HelperNode(NodeNames.GET_OBJECT_KEYS, astpp.pos(), forStmt);
    }
    Node getIteratorNode = new HelperNode(NodeNames.GET_ITERATOR, astpp.pos(), forStmt);
    Node hasNextNode = new HelperNode(NodeNames.HAS_NEXT, astpp.pos(), forStmt);
    Node nextNode = new HelperNode(NodeNames.NEXT, astpp.pos(), forStmt);
    Node bodyNode = DelegatingNodeFactory.createOrHelper(astpp, NodeNames.BODY, forStmt, forStmt.getStatement());
    Node continueCatchNode = new HelperNode(NodeNames.CONTINUE_CATCH, astpp.pos(), forStmt);
    Node exitNode = new HelperNode(NodeNames.EXIT, astpp.pos(), forStmt);
    cNode.addNode(entryNode);
    for (Node declNode : declNodes) cNode.addNode(declNode);
    for (Node initNode : initNodes) cNode.addNode(initNode);
    cNode.addNode(expressionNode);
    cNode.addNode(getObjectKeysNode);
    cNode.addNode(getIteratorNode);
    cNode.addNode(hasNextNode);
    cNode.addNode(nextNode);
    cNode.addNode(bodyNode);
    cNode.addNode(continueCatchNode);
    cNode.addNode(exitNode);
    List<Node> nodes = new LinkedList<>();
    nodes.add(entryNode);
    nodes.addAll(declNodes);
    nodes.addAll(initNodes);
    nodes.add(expressionNode);
    nodes.add(getObjectKeysNode);
    nodes.add(getIteratorNode);
    nodes.add(hasNextNode);
    cNode.connectInternalSucc(nodes);
    cNode.connectInternalSucc(ControlFlowType.LoopExit, hasNextNode, exitNode);
    cNode.connectInternalSucc(ControlFlowType.LoopEnter, hasNextNode, nextNode);
    cNode.connectInternalSucc(nextNode, bodyNode, continueCatchNode);
    cNode.connectInternalSucc(ControlFlowType.LoopRepeat, continueCatchNode, hasNextNode);
    cNode.setEntryNode(entryNode);
    cNode.setExitNode(exitNode);
    LabelledStatement lblStmt = ASTUtils.getLabelledStatement(forStmt);
    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) VariableDeclarationOrBinding(org.eclipse.n4js.n4JS.VariableDeclarationOrBinding) CatchToken(org.eclipse.n4js.flowgraphs.model.CatchToken) LinkedList(java.util.LinkedList)

Aggregations

ComplexNode (org.eclipse.n4js.flowgraphs.model.ComplexNode)33 Node (org.eclipse.n4js.flowgraphs.model.Node)27 HelperNode (org.eclipse.n4js.flowgraphs.model.HelperNode)17 LinkedList (java.util.LinkedList)13 RepresentingNode (org.eclipse.n4js.flowgraphs.model.RepresentingNode)12 CatchToken (org.eclipse.n4js.flowgraphs.model.CatchToken)11 ControlFlowElement (org.eclipse.n4js.n4JS.ControlFlowElement)7 LabelledStatement (org.eclipse.n4js.n4JS.LabelledStatement)6 DelegatingNode (org.eclipse.n4js.flowgraphs.model.DelegatingNode)5 ControlFlowEdge (org.eclipse.n4js.flowgraphs.model.ControlFlowEdge)4 HashSet (java.util.HashSet)3 ControlFlowType (org.eclipse.n4js.flowgraphs.ControlFlowType)3 FinallyBlock (org.eclipse.n4js.n4JS.FinallyBlock)3 VariableDeclarationOrBinding (org.eclipse.n4js.n4JS.VariableDeclarationOrBinding)3 HashMap (java.util.HashMap)2 LinkedHashSet (java.util.LinkedHashSet)2 AbstractCaseClause (org.eclipse.n4js.n4JS.AbstractCaseClause)2 Block (org.eclipse.n4js.n4JS.Block)2 CaseClause (org.eclipse.n4js.n4JS.CaseClause)2 Statement (org.eclipse.n4js.n4JS.Statement)2