Search in sources :

Example 1 with VariableDeclarationOrBinding

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

the class ForFactory method buildForPlain.

private static ComplexNode buildForPlain(ReentrantASTIterator astpp, ForStatement forStmt) {
    ComplexNode cNode = new ComplexNode(astpp.container(), forStmt);
    List<Node> initNodes = new LinkedList<>();
    Node entryNode = new HelperNode(NodeNames.ENTRY, astpp.pos(), forStmt);
    Node conditionNode = null;
    Node bodyNode = null;
    Node updatesNode = null;
    if (forStmt.getVarDeclsOrBindings() != null) {
        int i = 0;
        for (VariableDeclarationOrBinding vdob : forStmt.getVarDeclsOrBindings()) {
            Node initNode = DelegatingNodeFactory.create(astpp, "init_" + i, forStmt, vdob);
            initNodes.add(initNode);
            i++;
        }
    }
    if (forStmt.getInitExpr() != null) {
        Node initNode = DelegatingNodeFactory.create(astpp, NodeNames.INITS, forStmt, forStmt.getInitExpr());
        initNodes.add(initNode);
    }
    if (forStmt.getExpression() != null) {
        conditionNode = DelegatingNodeFactory.create(astpp, NodeNames.CONDITION, forStmt, forStmt.getExpression());
    }
    Node conditionForkNode = new HelperNode(NodeNames.CONDITION_FORK, astpp.pos(), forStmt);
    bodyNode = DelegatingNodeFactory.createOrHelper(astpp, NodeNames.BODY, forStmt, forStmt.getStatement());
    Node continueCatchNode = new HelperNode(NodeNames.CONTINUE_CATCH, astpp.pos(), forStmt);
    if (forStmt.getUpdateExpr() != null) {
        updatesNode = DelegatingNodeFactory.create(astpp, NodeNames.UPDATES, forStmt, forStmt.getUpdateExpr());
    }
    Node exitNode = new HelperNode(NodeNames.EXIT, astpp.pos(), forStmt);
    cNode.addNode(entryNode);
    cNode.addNode(exitNode);
    for (Node initNode : initNodes) cNode.addNode(initNode);
    cNode.addNode(conditionNode);
    cNode.addNode(conditionForkNode);
    cNode.addNode(bodyNode);
    cNode.addNode(continueCatchNode);
    cNode.addNode(updatesNode);
    List<Node> nodes = new LinkedList<>();
    nodes.add(entryNode);
    nodes.addAll(initNodes);
    nodes.add(conditionNode);
    nodes.add(conditionForkNode);
    cNode.connectInternalSucc(nodes);
    cNode.connectInternalSucc(ControlFlowType.LoopEnter, conditionForkNode, bodyNode);
    if (conditionNode != null) {
        cNode.connectInternalSucc(ControlFlowType.LoopExit, conditionForkNode, exitNode);
        cNode.connectInternalSucc(bodyNode, continueCatchNode, updatesNode);
        Node beforeConditionNode = ListUtils.filterNulls(bodyNode, continueCatchNode, updatesNode).getLast();
        cNode.connectInternalSucc(ControlFlowType.LoopRepeat, beforeConditionNode, conditionNode);
    } else {
        cNode.connectInternalSucc(bodyNode, continueCatchNode, updatesNode);
        LinkedList<Node> loopCycle = ListUtils.filterNulls(continueCatchNode, updatesNode);
        Node loopSrc = loopCycle.getLast();
        cNode.connectInternalSucc(ControlFlowType.LoopInfinite, loopSrc, conditionForkNode);
        cNode.connectInternalSucc(ControlFlowType.DeadCode, loopSrc, exitNode);
    }
    // catch for short-circuits
    conditionForkNode.addCatchToken(new CatchToken(ControlFlowType.IfTrue));
    exitNode.addCatchToken(new CatchToken(ControlFlowType.IfFalse, ControlFlowType.LoopExit));
    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)

Example 2 with VariableDeclarationOrBinding

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

the class VariableStatementFactory method buildComplexNode.

static ComplexNode buildComplexNode(ReentrantASTIterator astpp, VariableStatement varDeclStmt) {
    ComplexNode cNode = new ComplexNode(astpp.container(), varDeclStmt);
    Node entryNode = new HelperNode(NodeNames.ENTRY, astpp.pos(), varDeclStmt);
    List<Node> varDeclNodes = new LinkedList<>();
    for (int n = 0; n < varDeclStmt.getVarDeclsOrBindings().size(); n++) {
        VariableDeclarationOrBinding varDOB = varDeclStmt.getVarDeclsOrBindings().get(n);
        Node varDeclNode = DelegatingNodeFactory.create(astpp, "declaration_" + n, varDeclStmt, varDOB);
        varDeclNodes.add(varDeclNode);
    }
    Node exitNode = new RepresentingNode(NodeNames.EXIT, astpp.pos(), varDeclStmt);
    cNode.addNode(entryNode);
    for (Node varDeclNode : varDeclNodes) cNode.addNode(varDeclNode);
    cNode.addNode(exitNode);
    List<Node> nodes = new LinkedList<>();
    nodes.add(entryNode);
    nodes.addAll(varDeclNodes);
    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) ComplexNode(org.eclipse.n4js.flowgraphs.model.ComplexNode) HelperNode(org.eclipse.n4js.flowgraphs.model.HelperNode) RepresentingNode(org.eclipse.n4js.flowgraphs.model.RepresentingNode) Node(org.eclipse.n4js.flowgraphs.model.Node) RepresentingNode(org.eclipse.n4js.flowgraphs.model.RepresentingNode) VariableDeclarationOrBinding(org.eclipse.n4js.n4JS.VariableDeclarationOrBinding) LinkedList(java.util.LinkedList)

Example 3 with VariableDeclarationOrBinding

use of org.eclipse.n4js.n4JS.VariableDeclarationOrBinding 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)

Example 4 with VariableDeclarationOrBinding

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

the class N4JSStatementValidator method checkForInLoop.

/**
 * 9.1.4 Iteration Statements, Constraints 118 (For-In-Statement Constraints) Variable declaration must be a string
 */
@Check
public void checkForInLoop(ForStatement forStatement) {
    if (forStatement.isForIn()) {
        TypeRef loopVarType = null;
        EObject location = null;
        RuleEnvironment G = (RuleEnvironment) getContext().get(RuleEnvironment.class);
        if (G == null)
            // wrongly configured test
            return;
        if (!forStatement.getVarDeclsOrBindings().isEmpty()) {
            VariableDeclarationOrBinding varDeclOrBinding = forStatement.getVarDeclsOrBindings().iterator().next();
            location = varDeclOrBinding;
            if (varDeclOrBinding instanceof VariableDeclaration) {
                loopVarType = ((VariableDeclaration) varDeclOrBinding).getDeclaredTypeRef();
            } else {
                VariableBinding varBinding = (VariableBinding) varDeclOrBinding;
                Result<TypeRef> res = typeSystem.type(G, varBinding.getExpression());
                if (!res.failed()) {
                    loopVarType = res.getFirst();
                }
            }
        } else if (forStatement.getInitExpr() != null) {
            location = forStatement.getInitExpr();
            Result<TypeRef> res = typeSystem.type(G, forStatement.getInitExpr());
            if (!res.failed()) {
                loopVarType = res.getFirst();
            }
        }
        if (loopVarType != null) {
            Result<Boolean> res = typeSystem.subtype(G, RuleEnvironmentExtensions.stringTypeRef(G), loopVarType);
            if (res.failed() || !res.getFirst()) {
                addIssue(getMessageForTYS_FOR_IN_VAR_STRING(loopVarType.getTypeRefAsString()), location, TYS_FOR_IN_VAR_STRING);
            }
        }
    }
}
Also used : TypeRef(org.eclipse.n4js.ts.typeRefs.TypeRef) EObject(org.eclipse.emf.ecore.EObject) RuleEnvironment(org.eclipse.xsemantics.runtime.RuleEnvironment) VariableDeclaration(org.eclipse.n4js.n4JS.VariableDeclaration) VariableDeclarationOrBinding(org.eclipse.n4js.n4JS.VariableDeclarationOrBinding) VariableBinding(org.eclipse.n4js.n4JS.VariableBinding) Result(org.eclipse.xsemantics.runtime.Result) Check(org.eclipse.xtext.validation.Check)

Aggregations

VariableDeclarationOrBinding (org.eclipse.n4js.n4JS.VariableDeclarationOrBinding)4 LinkedList (java.util.LinkedList)3 ComplexNode (org.eclipse.n4js.flowgraphs.model.ComplexNode)3 HelperNode (org.eclipse.n4js.flowgraphs.model.HelperNode)3 Node (org.eclipse.n4js.flowgraphs.model.Node)3 CatchToken (org.eclipse.n4js.flowgraphs.model.CatchToken)2 LabelledStatement (org.eclipse.n4js.n4JS.LabelledStatement)2 EObject (org.eclipse.emf.ecore.EObject)1 RepresentingNode (org.eclipse.n4js.flowgraphs.model.RepresentingNode)1 VariableBinding (org.eclipse.n4js.n4JS.VariableBinding)1 VariableDeclaration (org.eclipse.n4js.n4JS.VariableDeclaration)1 TypeRef (org.eclipse.n4js.ts.typeRefs.TypeRef)1 Result (org.eclipse.xsemantics.runtime.Result)1 RuleEnvironment (org.eclipse.xsemantics.runtime.RuleEnvironment)1 Check (org.eclipse.xtext.validation.Check)1