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;
}
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;
}
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;
}
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);
}
}
}
}
Aggregations