/** * Like removeChildAfter, the firstChild is removed */ private static Node removeChildAfter(Node parent, @Nullable Node siblingBefore) { if (siblingBefore == null) { return parent.removeFirstChild(); } else { return parent.removeChildAfter(siblingBefore); } }
/** * Parse helper code needed by a reducer. * * @return Helper code root. If parse fails, return null. */ public Node parseHelperCode(Reducer reducer) { Node root = compiler.parseSyntheticCode(reducer.getClass() + ":helper", reducer.getHelperSource()); return (root != null) ? root.removeFirstChild() : null; }
protected static void fuseExpressionIntoFirstChild(Node exp, Node stmt) { Node val = stmt.removeFirstChild(); Node comma = fuseExpressionIntoExpression(exp, val); stmt.addChildToFront(comma); }
protected static void fuseExpressionIntoFirstChild(Node exp, Node stmt) { Node val = stmt.removeFirstChild(); Node comma = fuseExpressionIntoExpression(exp, val); stmt.addChildToFront(comma); }
private void visitLabel() { Node labelName = currentStatement.removeFirstChild(); Node child = currentStatement.removeFirstChild(); if (NodeUtil.isLoopStructure(child)) { currentStatement = child; visitLoop(labelName.getString()); } else { originalGeneratorBody.addChildToFront(child); } }
private Node createConditionalObjectDecl(String name, Node srcref) { String jscovDecl = " var " + name + " = window.top.__jscov || " + "(window.top.__jscov = {fileNames:[], instrumentedLines: [], executedLines: []});"; Node script = compiler.parseSyntheticCode(jscovDecl); Node var = script.removeFirstChild(); return var.useSourceInfoIfMissingFromForTree(srcref); } }
/** Transpiles a block. */ void transpileBlock(Node n) { while (n.hasChildren()) { transpileStatement(n.removeFirstChild()); } }
/** Adjust return statements. */ void visitReturn(Node n) { // return ...; => return $context.return(...); n.addChildToFront(returnExpression(n, n.removeFirstChild())); }
private void visitExponentiationOperator(Node operator) { Node callClone = mathPowCall.cloneTree(); callClone.addChildToBack(operator.removeFirstChild()); // Base argument. callClone.addChildToBack(operator.removeFirstChild()); // Exponent argument. callClone.useSourceInfoIfMissingFromForTree(operator); operator.replaceWith(callClone); compiler.reportChangeToEnclosingScope(callClone); }
private void visitContinue() { Preconditions.checkState(currentLoopContext.get(0).continueCase != -1); int continueCase; if (currentStatement.hasChildren()) { continueCase = getLoopContext(currentStatement.removeFirstChild().getString()).continueCase; } else { continueCase = currentLoopContext.get(0).continueCase; } enclosingBlock.addChildToBack(createStateUpdate(continueCase)); enclosingBlock.addChildToBack(createSafeBreak()); }
/** Transpiles marked "throw" statement. */ void transpileThrow(Node n) { n.addChildToFront(prepareNodeForWrite(maybeDecomposeExpression(n.removeFirstChild()))); context.writeGeneratedNode(n); context.currentCase.mayFallThrough = false; }
@Override public void visit(NodeTraversal t, Node n, Node parent) { if (!n.isReturn()) { return; } Node returnRhs = n.removeFirstChild(); Node call = newReportFunctionExitNode(n, returnRhs); n.addChildToFront(call); compiler.reportCodeChange(); }
@Override public void performRemove() { Node var = name.getParent(); Preconditions.checkState(var.getFirstChild() == var.getLastChild(), "AST should be normalized first"); Node parent = var.getParent(); Node rValue = name.removeFirstChild(); Preconditions.checkState(!parent.isFor()); parent.replaceChild(var, NodeUtil.newExpr(rValue)); }
@Override public void performRemove(AbstractCompiler compiler) { Node var = name.getParent(); checkState(var.getFirstChild() == var.getLastChild(), "AST should be normalized first"); Node parent = var.getParent(); Node rValue = name.removeFirstChild(); checkState(!NodeUtil.isLoopStructure(parent)); parent.replaceChild(var, NodeUtil.newExpr(rValue)); compiler.reportChangeToEnclosingScope(parent); }
private void visitExponentiationAssignmentOperator(Node operator) { Node lValue = operator.removeFirstChild(); Node callClone = mathPowCall.cloneTree(); callClone.addChildToBack(lValue.cloneTree()); // Base argument. callClone.addChildToBack(operator.removeFirstChild()); // Exponent argument. Node assignment = IR.assign(lValue, callClone).setJSType(numberType); assignment.useSourceInfoIfMissingFromForTree(operator); operator.replaceWith(assignment); compiler.reportChangeToEnclosingScope(assignment); }
/** Transpiles marked "return" statement. */ void transpileReturn(Node n) { n.addChildToFront( context.returnExpression( n, prepareNodeForWrite(maybeDecomposeExpression(n.removeFirstChild())))); context.writeGeneratedNode(n); context.currentCase.mayFallThrough = false; }
/** Adds a block of original code to the end of the current case. */ void transpileUnmarkedBlock(Node block) { if (block.hasChildren()) { NodeTraversal.traverse(compiler, block, new UnmarkedNodeTranspiler()); while (block.hasChildren()) { writeGeneratedNode(block.removeFirstChild()); } } }
/** Returns an assign replacing the equivalent var or let declaration. */ private static Node varToAssign(Node var) { Node assign = IR.assign(var.getFirstChild().cloneNode(), var.getFirstChild().removeFirstChild()); return IR.exprResult(assign).useSourceInfoIfMissingFromForTree(var); } }
/** * Translates {@code yield} to set the state so that execution resume at the next statement * when the function is next called and then returns an iterator result with * the desired value. */ private void visitYieldExprResult() { enclosingBlock.addChildToBack(createStateUpdate()); Node yield = currentStatement.getFirstChild(); Node value = yield.hasChildren() ? yield.removeFirstChild() : IR.name("undefined"); enclosingBlock.addChildToBack(IR.returnNode(createIteratorResult(value, false))); }
private void visitYieldExpr(Node n, Node parent) { Node enclosingStatement = NodeUtil.getEnclosingStatement(n); Node yieldStatement = IR.exprResult(n.hasChildren() ? IR.yield(n.removeFirstChild()) : IR.yield()); Node yieldResult = IR.name(GENERATOR_NEXT_ARG + generatorCounter.get()); Node yieldResultDecl = IR.var(yieldResult.cloneTree(), IR.name(GENERATOR_NEXT_ARG)); parent.replaceChild(n, yieldResult); enclosingStatement.getParent().addChildBefore(yieldStatement, enclosingStatement); enclosingStatement.getParent().addChildBefore(yieldResultDecl, enclosingStatement); visitYieldThrows(yieldStatement, yieldStatement.getParent()); compiler.reportCodeChange(); }