Search in sources :

Example 1 with AbstractCaseClause

use of org.eclipse.n4js.n4JS.AbstractCaseClause 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 2 with AbstractCaseClause

use of org.eclipse.n4js.n4JS.AbstractCaseClause in project n4js by eclipse.

the class AbstractCaseClauseFactory method buildComplexNode.

static ComplexNode buildComplexNode(ReentrantASTIterator astpp, AbstractCaseClause abstrCaseClause) {
    ComplexNode cNode = new ComplexNode(astpp.container(), abstrCaseClause);
    Node entryNode = new HelperNode(NodeNames.ENTRY, astpp.pos(), abstrCaseClause);
    List<Node> stmtNodes = new LinkedList<>();
    Node caseConditionNode = null;
    if (abstrCaseClause instanceof CaseClause) {
        CaseClause caseClause = (CaseClause) abstrCaseClause;
        caseConditionNode = DelegatingNodeFactory.create(astpp, NodeNames.CONDITION, caseClause, caseClause.getExpression());
    }
    EList<Statement> stmts = abstrCaseClause.getStatements();
    for (int i = 0; i < stmts.size(); i++) {
        Statement stmt = stmts.get(i);
        Node blockNode = new DelegatingNode("stmt_" + i, astpp.pos(), abstrCaseClause, stmt);
        stmtNodes.add(blockNode);
        astpp.visitUtil(blockNode.getDelegatedControlFlowElement());
    }
    Node exitNode = new HelperNode(NodeNames.EXIT, astpp.pos(), abstrCaseClause);
    cNode.addNode(entryNode);
    cNode.addNode(caseConditionNode);
    for (Node blockNode : stmtNodes) cNode.addNode(blockNode);
    cNode.addNode(exitNode);
    List<Node> nodes = new LinkedList<>();
    nodes.add(entryNode);
    nodes.add(caseConditionNode);
    nodes.addAll(stmtNodes);
    nodes.add(exitNode);
    cNode.connectInternalSucc(nodes);
    cNode.setEntryNode(entryNode);
    cNode.setExitNode(exitNode);
    return cNode;
}
Also used : HelperNode(org.eclipse.n4js.flowgraphs.model.HelperNode) ComplexNode(org.eclipse.n4js.flowgraphs.model.ComplexNode) Statement(org.eclipse.n4js.n4JS.Statement) ComplexNode(org.eclipse.n4js.flowgraphs.model.ComplexNode) DelegatingNode(org.eclipse.n4js.flowgraphs.model.DelegatingNode) HelperNode(org.eclipse.n4js.flowgraphs.model.HelperNode) Node(org.eclipse.n4js.flowgraphs.model.Node) CaseClause(org.eclipse.n4js.n4JS.CaseClause) AbstractCaseClause(org.eclipse.n4js.n4JS.AbstractCaseClause) DelegatingNode(org.eclipse.n4js.flowgraphs.model.DelegatingNode) LinkedList(java.util.LinkedList)

Example 3 with AbstractCaseClause

use of org.eclipse.n4js.n4JS.AbstractCaseClause in project n4js by eclipse.

the class SwitchStatementImpl method getDefaultClause.

/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
public DefaultClause getDefaultClause() {
    final Function1<AbstractCaseClause, Boolean> _function = new Function1<AbstractCaseClause, Boolean>() {

        public Boolean apply(final AbstractCaseClause it) {
            return Boolean.valueOf((it instanceof DefaultClause));
        }
    };
    AbstractCaseClause _findFirst = IterableExtensions.<AbstractCaseClause>findFirst(this.getCases(), _function);
    return ((DefaultClause) _findFirst);
}
Also used : DefaultClause(org.eclipse.n4js.n4JS.DefaultClause) AbstractCaseClause(org.eclipse.n4js.n4JS.AbstractCaseClause) Function1(org.eclipse.xtext.xbase.lib.Functions.Function1)

Aggregations

AbstractCaseClause (org.eclipse.n4js.n4JS.AbstractCaseClause)3 LinkedList (java.util.LinkedList)2 ComplexNode (org.eclipse.n4js.flowgraphs.model.ComplexNode)2 HelperNode (org.eclipse.n4js.flowgraphs.model.HelperNode)2 Node (org.eclipse.n4js.flowgraphs.model.Node)2 CaseClause (org.eclipse.n4js.n4JS.CaseClause)2 DefaultClause (org.eclipse.n4js.n4JS.DefaultClause)2 CatchToken (org.eclipse.n4js.flowgraphs.model.CatchToken)1 DelegatingNode (org.eclipse.n4js.flowgraphs.model.DelegatingNode)1 LabelledStatement (org.eclipse.n4js.n4JS.LabelledStatement)1 Statement (org.eclipse.n4js.n4JS.Statement)1 Function1 (org.eclipse.xtext.xbase.lib.Functions.Function1)1