/** * Assist in IR navigation. * * @param visitor IR navigating visitor. * * @return node or replacement */ @Override public Node accept(final LexicalContext lc, final NodeVisitor<? extends LexicalContext> visitor) { if (visitor.enterCallNode(this)) { final CallNode newCallNode = (CallNode)visitor.leaveCallNode( setFunction((Expression)function.accept(visitor)). setArgs(Node.accept(visitor, args)). setEvalArgs(evalArgs == null ? null : evalArgs.setArgs(Node.accept(visitor, evalArgs.getArgs())))); // Theoretically, we'd need to instead pass lc to every setter and do a replacement on each. In practice, // setType from TypeOverride can't accept a lc, and we don't necessarily want to go there now. if (this != newCallNode) { return Node.replaceInLexicalContext(lc, this, newCallNode); } } return this; }
/** * Assist in IR navigation. * @param visitor IR navigating visitor. */ @Override public Node accept(final NodeVisitor<? extends LexicalContext> visitor) { if (visitor.enterTryNode(this)) { // Need to do finallybody first for termination analysis. TODO still necessary? final Block newFinallyBody = finallyBody == null ? null : (Block)finallyBody.accept(visitor); final Block newBody = (Block)body.accept(visitor); return visitor.leaveTryNode( setBody(newBody). setFinallyBody(newFinallyBody). setCatchBlocks(Node.accept(visitor, catchBlocks)). setFinallyCatchAll(finallyCatchAll)); } return this; }
private static List<FunctionNode> directChildren(final FunctionNode functionNode) { final List<FunctionNode> dc = new ArrayList<>(); functionNode.accept(new NodeVisitor<LexicalContext>(new LexicalContext()) { @Override public boolean enterFunctionNode(final FunctionNode child) { if (child == functionNode) { return true; } if (lc.getParentFunction(child) == functionNode) { dc.add(child); } return false; } }); return dc; }
private boolean hasApplies(final FunctionNode functionNode) { try { functionNode.accept(new NodeVisitor<LexicalContext>(new LexicalContext()) { @Override public boolean enterFunctionNode(final FunctionNode fn) { return fn == functionNode; } @Override public boolean enterCallNode(final CallNode callNode) { if (isApply(callNode)) { throw HAS_APPLIES; } return true; } }); } catch (final AppliesFoundException e) { return true; } log.fine("There are no applies in ", DebugLogger.quote(functionNode.getName()), " - nothing to do."); return false; // no applies }
private void markEvalInArrowParameterList(final ParserContextBlockNode parameterBlock) { final Iterator<ParserContextFunctionNode> iter = lc.getFunctions(); final ParserContextFunctionNode current = iter.next(); final ParserContextFunctionNode parent = iter.next(); if (parent.getFlag(FunctionNode.HAS_EVAL) != 0) { // we might have flagged has-eval in the parent function during parsing the parameter list, // if the parameter list contains eval; must tag arrow function as has-eval. for (final Statement st : parameterBlock.getStatements()) { st.accept(new NodeVisitor<LexicalContext>(new LexicalContext()) { @Override public boolean enterCallNode(final CallNode callNode) { if (callNode.getFunction() instanceof IdentNode && ((IdentNode) callNode.getFunction()).getName().equals("eval")) { current.setFlag(FunctionNode.HAS_EVAL); } return true; } }); } // TODO: function containing the arrow function should not be flagged has-eval } }
/** * Assist in IR navigation. * @param visitor IR navigating visitor. */ @Override public Node accept(final LexicalContext lc, final NodeVisitor<? extends LexicalContext> visitor) { if (visitor.enterTryNode(this)) { // Need to do finallybody first for termination analysis. TODO still necessary? final Block newFinallyBody = finallyBody == null ? null : (Block)finallyBody.accept(visitor); final Block newBody = (Block)body.accept(visitor); return visitor.leaveTryNode( setBody(lc, newBody). setFinallyBody(lc, newFinallyBody). setCatchBlocks(lc, Node.accept(visitor, catchBlocks)). setInlinedFinallies(lc, Node.accept(visitor, inlinedFinallies))); } return this; }
/** * Assist in IR navigation. * @param visitor IR navigating visitor. */ @Override public Node accept(final NodeVisitor<? extends LexicalContext> visitor) { if (visitor.enterVarNode(this)) { // var is right associative, so visit init before name final Expression newInit = init == null ? null : (Expression)init.accept(visitor); final IdentNode newName = (IdentNode)name.accept(visitor); final VarNode newThis; if (name != newName || init != newInit) { newThis = new VarNode(this, newName, newInit, flags); } else { newThis = this; } return visitor.leaveVarNode(newThis); } return this; }
@Override public Node accept(final NodeVisitor<? extends LexicalContext> visitor) { if (visitor.enterReturnNode(this)) { if (expression != null) { return visitor.leaveReturnNode(setExpression((Expression)expression.accept(visitor))); } return visitor.leaveReturnNode(this); } return this; }
@Override public Node accept(final LexicalContext lc, final NodeVisitor<? extends LexicalContext> visitor) { if (visitor.enterFunctionNode(this)) { return visitor.leaveFunctionNode(setBody(lc, (Block)body.accept(visitor))); } return this; }
@Override public Node accept(final LexicalContext lc, final NodeVisitor<? extends LexicalContext> visitor) { if (visitor.enterSwitchNode(this)) { return visitor.leaveSwitchNode( setExpression(lc, (Expression)expression.accept(visitor)). setCases(lc, Node.accept(visitor, cases), defaultCaseIndex)); } return this; }
@Override public Node accept(final NodeVisitor<? extends LexicalContext> visitor) { if (visitor.enterIndexNode(this)) { return visitor.leaveIndexNode( setBase((Expression)base.accept(visitor)). setIndex((Expression)index.accept(visitor))); } return this; }
/** * Assist in IR navigation. * * @param visitor IR navigating visitor. * @return new or same node */ @Override public Node accept(final LexicalContext lc, final NodeVisitor<? extends LexicalContext> visitor) { if (visitor.enterBlock(this)) { return visitor.leaveBlock(setStatements(lc, Node.accept(visitor, statements))); } return this; }
@Override public Node accept(final NodeVisitor<? extends LexicalContext> visitor) { if(visitor.enterJoinPredecessorExpression(this)) { final Expression expr = getExpression(); return visitor.leaveJoinPredecessorExpression(expr == null ? this : setExpression((Expression)expr.accept(visitor))); } return this; }
/** * Assist in IR navigation. * @param visitor IR navigating visitor. */ @Override public Node accept(final NodeVisitor<? extends LexicalContext> visitor) { if (visitor.enterBinaryNode(this)) { return visitor.leaveBinaryNode(setLHS((Expression)lhs.accept(visitor)).setRHS((Expression)rhs.accept(visitor))); } return this; }
@Override public Node accept(final NodeVisitor<? extends LexicalContext> visitor) { if (visitor.enterRuntimeNode(this)) { return visitor.leaveRuntimeNode(setArgs(Node.accept(visitor, args))); } return this; }
@Override public Node accept(final LexicalContext lc, final NodeVisitor<? extends LexicalContext> visitor) { if (visitor.enterForNode(this)) { return visitor.leaveForNode( setInit(lc, init == null ? null : (Expression)init.accept(visitor)). setTest(lc, test == null ? null : (JoinPredecessorExpression)test.accept(visitor)). setModify(lc, modify == null ? null : (JoinPredecessorExpression)modify.accept(visitor)). setBody(lc, (Block)body.accept(visitor))); } return this; }
/** * Assist in IR navigation. * @param visitor IR navigating visitor. */ @Override public Node accept(final NodeVisitor<? extends LexicalContext> visitor) { if (visitor.enterAccessNode(this)) { return visitor.leaveAccessNode( setBase((Expression)base.accept(visitor))); } return this; }
@Override public Node accept(final LexicalContext lc, final NodeVisitor<? extends LexicalContext> visitor) { if (visitor.enterWhileNode(this)) { if (isDoWhile()) { return visitor.leaveWhileNode( setBody(lc, (Block)body.accept(visitor)). setTest(lc, (JoinPredecessorExpression)test.accept(visitor))); } return visitor.leaveWhileNode( setTest(lc, (JoinPredecessorExpression)test.accept(visitor)). setBody(lc, (Block)body.accept(visitor))); } return this; }
@Override public Node accept(final NodeVisitor<? extends LexicalContext> visitor) { if (visitor.enterEmptyNode(this)) { return visitor.leaveEmptyNode(this); } return this; }
/** * Assist in IR navigation. * @param visitor IR navigating visitor. */ @Override public Node accept(final NodeVisitor<? extends LexicalContext> visitor) { if (visitor.enterUnaryNode(this)) { return visitor.leaveUnaryNode(setExpression((Expression)expression.accept(visitor))); } return this; }
@Override public Node accept(final NodeVisitor<? extends LexicalContext> visitor) { if (visitor.enterBreakNode(this)) { return visitor.leaveBreakNode(this); } return this; }
@Override public Node accept(final LexicalContext lc, final NodeVisitor<? extends LexicalContext> visitor) { if (visitor.enterSplitNode(this)) { return visitor.leaveSplitNode(setBody(lc, (Block)body.accept(visitor))); } return this; }
@Override public Node accept(final NodeVisitor<? extends LexicalContext> visitor) { if (visitor.enterExpressionStatement(this)) { return visitor.leaveExpressionStatement(setExpression((Expression)expression.accept(visitor))); } return this; }
@Override public Node accept(final NodeVisitor<? extends LexicalContext> visitor) { if (visitor.enterContinueNode(this)) { return visitor.leaveContinueNode(this); } return this; }
@Override public Node accept(final NodeVisitor<? extends LexicalContext> visitor) { if (visitor.enterObjectNode(this)) { return visitor.leaveObjectNode(setElements(Node.accept(visitor, elements))); } return this; }
/** * Assist in IR navigation. * * @param visitor IR navigating visitor. */ @Override public Node accept(final NodeVisitor<? extends LexicalContext> visitor) { if (visitor.enterLiteralNode(this)) { return visitor.leaveLiteralNode(this); } return this; }
@Override public Node accept(final NodeVisitor<? extends LexicalContext> visitor) { if (visitor.enterLiteralNode(this)) { return visitor.leaveLiteralNode(this); } return this; }
@Override public Node accept(final LexicalContext lc, final NodeVisitor<? extends LexicalContext> visitor) { if (visitor.enterLiteralNode(this)) { final List<Expression> oldValue = Arrays.asList(value); final List<Expression> newValue = Node.accept(visitor, oldValue); return visitor.leaveLiteralNode(oldValue != newValue ? setValue(lc, newValue) : this); } return this; }
@Override public Node accept(final NodeVisitor<? extends LexicalContext> visitor) { if (visitor.enterTernaryNode(this)) { final Expression newTest = (Expression)getTest().accept(visitor); final JoinPredecessorExpression newTrueExpr = (JoinPredecessorExpression)trueExpr.accept(visitor); final JoinPredecessorExpression newFalseExpr = (JoinPredecessorExpression)falseExpr.accept(visitor); return visitor.leaveTernaryNode(setTest(newTest).setTrueExpression(newTrueExpr).setFalseExpression(newFalseExpr)); } return this; }
static <T extends Node> List<T> accept(final NodeVisitor<? extends LexicalContext> visitor, final List<T> list) { final int size = list.size(); if (size == 0) { return list; } List<T> newList = null; for (int i = 0; i < size; i++) { final T node = list.get(i); @SuppressWarnings("unchecked") final T newNode = node == null ? null : (T)node.accept(visitor); if (newNode != node) { if (newList == null) { newList = new ArrayList<>(size); for (int j = 0; j < i; j++) { newList.add(list.get(j)); } } newList.add(newNode); } else { if (newList != null) { newList.add(node); } } } return newList == null ? list : newList; }
/** * Assist in IR navigation. * @param visitor IR navigating visitor. */ @Override public Node accept(final NodeVisitor<? extends LexicalContext> visitor) { if (visitor.enterCatchNode(this)) { return visitor.leaveCatchNode( setException((IdentNode)exception.accept(visitor)). setExceptionCondition(exceptionCondition == null ? null : (Expression)exceptionCondition.accept(visitor)). setBody((Block)body.accept(visitor))); } return this; }
/** * Assist in IR navigation. * @param visitor IR navigating visitor. */ @Override public Node accept(final NodeVisitor<? extends LexicalContext> visitor) { if (visitor.enterThrowNode(this)) { return visitor.leaveThrowNode(setExpression((Expression)expression.accept(visitor))); } return this; }
@Override public Node accept(final NodeVisitor<? extends LexicalContext> visitor) { if (visitor.enterPropertyNode(this)) { return visitor.leavePropertyNode( setKey((PropertyKey)((Node)key).accept(visitor)). setValue(value == null ? null : (Expression)value.accept(visitor)). setGetter(getter == null ? null : (FunctionNode)getter.accept(visitor)). setSetter(setter == null ? null : (FunctionNode)setter.accept(visitor))); } return this; }
@Override public Node accept(final LexicalContext lc, final NodeVisitor<? extends LexicalContext> visitor) { if (visitor.enterLabelNode(this)) { return visitor.leaveLabelNode(setBody(lc, (Block)body.accept(visitor))); } return this; }
@Override public Node accept(final NodeVisitor<? extends LexicalContext> visitor) { if (visitor.enterIfNode(this)) { return visitor.leaveIfNode( setTest((Expression)test.accept(visitor)). setPass((Block)pass.accept(visitor)). setFail(fail == null ? null : (Block)fail.accept(visitor))); } return this; }
/** * Assist in IR navigation. * * @param visitor IR navigating visitor. */ @Override public Node accept(final NodeVisitor<? extends LexicalContext> visitor) { if (visitor.enterIdentNode(this)) { return visitor.leaveIdentNode(this); } return this; }
/** * Assist in IR navigation. * * @param visitor IR navigating visitor. */ @Override public Node accept(final LexicalContext lc, final NodeVisitor<? extends LexicalContext> visitor) { if (visitor.enterWithNode(this)) { return visitor.leaveWithNode( setExpression(lc, (Expression)expression.accept(visitor)). setBody(lc, (Block)body.accept(visitor))); } return this; }