Java Code Examples for com.google.javascript.rhino.IR#block()

The following examples show how to use com.google.javascript.rhino.IR#block() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Cardumen_0017_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * TypeExpressionList := TopLevelTypeExpression
 *     | TopLevelTypeExpression ',' TypeExpressionList
 */
private Node parseTypeExpressionList(JsDocToken token) {
  Node typeExpr = parseTopLevelTypeExpression(token);
  if (typeExpr == null) {
    return null;
  }
  Node typeList = IR.block();
  typeList.addChildToBack(typeExpr);
  while (match(JsDocToken.COMMA)) {
    next();
    skipEOLs();
    typeExpr = parseTopLevelTypeExpression(next());
    if (typeExpr == null) {
      return null;
    }
    typeList.addChildToBack(typeExpr);
  }
  return typeList;
}
 
Example 2
Source File: Nopol2017_0025_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * TypeExpressionList := TopLevelTypeExpression
 *     | TopLevelTypeExpression ',' TypeExpressionList
 */
private Node parseTypeExpressionList(JsDocToken token) {
  Node typeExpr = parseTopLevelTypeExpression(token);
  if (typeExpr == null) {
    return null;
  }
  Node typeList = IR.block();
  typeList.addChildToBack(typeExpr);
  while (match(JsDocToken.COMMA)) {
    next();
    skipEOLs();
    typeExpr = parseTopLevelTypeExpression(next());
    if (typeExpr == null) {
      return null;
    }
    typeList.addChildToBack(typeExpr);
  }
  return typeList;
}
 
Example 3
Source File: JsDocInfoParser.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * TypeExpressionList := TopLevelTypeExpression
 *     | TopLevelTypeExpression ',' TypeExpressionList
 */
private Node parseTypeExpressionList(JsDocToken token) {
  Node typeExpr = parseTopLevelTypeExpression(token);
  if (typeExpr == null) {
    return null;
  }
  Node typeList = IR.block();
  typeList.addChildToBack(typeExpr);
  while (match(JsDocToken.COMMA)) {
    next();
    skipEOLs();
    typeExpr = parseTopLevelTypeExpression(next());
    if (typeExpr == null) {
      return null;
    }
    typeList.addChildToBack(typeExpr);
  }
  return typeList;
}
 
Example 4
Source File: Normalize.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Limit the number of special cases where LABELs need to be handled. Only
 * BLOCK and loops are allowed to be labeled.  Loop labels must remain in
 * place as the named continues are not allowed for labeled blocks.
 */
private void normalizeLabels(Node n) {
  Preconditions.checkArgument(n.isLabel());

  Node last = n.getLastChild();
  switch (last.getType()) {
    case Token.LABEL:
    case Token.BLOCK:
    case Token.FOR:
    case Token.WHILE:
    case Token.DO:
      return;
    default:
      Node block = IR.block();
      block.copyInformationFrom(last);
      n.replaceChild(last, block);
      block.addChildToFront(last);
      reportCodeChange("LABEL normalization");
      return;
  }
}
 
Example 5
Source File: Closure_32_JsDocInfoParser_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * TypeExpressionList := TopLevelTypeExpression
 *     | TopLevelTypeExpression ',' TypeExpressionList
 */
private Node parseTypeExpressionList(JsDocToken token) {
  Node typeExpr = parseTopLevelTypeExpression(token);
  if (typeExpr == null) {
    return null;
  }
  Node typeList = IR.block();
  typeList.addChildToBack(typeExpr);
  while (match(JsDocToken.COMMA)) {
    next();
    skipEOLs();
    typeExpr = parseTopLevelTypeExpression(next());
    if (typeExpr == null) {
      return null;
    }
    typeList.addChildToBack(typeExpr);
  }
  return typeList;
}
 
Example 6
Source File: Closure_32_JsDocInfoParser_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * TypeExpressionList := TopLevelTypeExpression
 *     | TopLevelTypeExpression ',' TypeExpressionList
 */
private Node parseTypeExpressionList(JsDocToken token) {
  Node typeExpr = parseTopLevelTypeExpression(token);
  if (typeExpr == null) {
    return null;
  }
  Node typeList = IR.block();
  typeList.addChildToBack(typeExpr);
  while (match(JsDocToken.COMMA)) {
    next();
    skipEOLs();
    typeExpr = parseTopLevelTypeExpression(next());
    if (typeExpr == null) {
      return null;
    }
    typeList.addChildToBack(typeExpr);
  }
  return typeList;
}
 
Example 7
Source File: Closure_133_JsDocInfoParser_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * TypeExpressionList := TopLevelTypeExpression
 *     | TopLevelTypeExpression ',' TypeExpressionList
 */
private Node parseTypeExpressionList(JsDocToken token) {
  Node typeExpr = parseTopLevelTypeExpression(token);
  if (typeExpr == null) {
    return null;
  }
  Node typeList = IR.block();
  typeList.addChildToBack(typeExpr);
  while (match(JsDocToken.COMMA)) {
    next();
    skipEOLs();
    typeExpr = parseTopLevelTypeExpression(next());
    if (typeExpr == null) {
      return null;
    }
    typeList.addChildToBack(typeExpr);
  }
  return typeList;
}
 
Example 8
Source File: Closure_133_JsDocInfoParser_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * TypeExpressionList := TopLevelTypeExpression
 *     | TopLevelTypeExpression ',' TypeExpressionList
 */
private Node parseTypeExpressionList(JsDocToken token) {
  Node typeExpr = parseTopLevelTypeExpression(token);
  if (typeExpr == null) {
    return null;
  }
  Node typeList = IR.block();
  typeList.addChildToBack(typeExpr);
  while (match(JsDocToken.COMMA)) {
    next();
    skipEOLs();
    typeExpr = parseTopLevelTypeExpression(next());
    if (typeExpr == null) {
      return null;
    }
    typeList.addChildToBack(typeExpr);
  }
  return typeList;
}
 
Example 9
Source File: Closure_109_JsDocInfoParser_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * TypeExpressionList := TopLevelTypeExpression
 *     | TopLevelTypeExpression ',' TypeExpressionList
 */
private Node parseTypeExpressionList(JsDocToken token) {
  Node typeExpr = parseTopLevelTypeExpression(token);
  if (typeExpr == null) {
    return null;
  }
  Node typeList = IR.block();
  typeList.addChildToBack(typeExpr);
  while (match(JsDocToken.COMMA)) {
    next();
    skipEOLs();
    typeExpr = parseTopLevelTypeExpression(next());
    if (typeExpr == null) {
      return null;
    }
    typeList.addChildToBack(typeExpr);
  }
  return typeList;
}
 
Example 10
Source File: Closure_109_JsDocInfoParser_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * TypeExpressionList := TopLevelTypeExpression
 *     | TopLevelTypeExpression ',' TypeExpressionList
 */
private Node parseTypeExpressionList(JsDocToken token) {
  Node typeExpr = parseTopLevelTypeExpression(token);
  if (typeExpr == null) {
    return null;
  }
  Node typeList = IR.block();
  typeList.addChildToBack(typeExpr);
  while (match(JsDocToken.COMMA)) {
    next();
    skipEOLs();
    typeExpr = parseTopLevelTypeExpression(next());
    if (typeExpr == null) {
      return null;
    }
    typeList.addChildToBack(typeExpr);
  }
  return typeList;
}
 
Example 11
Source File: CreateSyntheticBlocks.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @param marker The marker to add synthetic blocks for.
 */
private void addBlocks(Marker marker) {
  // Add block around the template section so that it looks like this:
  //  BLOCK (synthetic)
  //    START
  //      BLOCK (synthetic)
  //        BODY
  //    END
  // This prevents the start or end markers from mingling with the code
  // in the block body.


  Node originalParent = marker.endMarker.getParent();
  Node outerBlock = IR.block();
  outerBlock.setIsSyntheticBlock(true);
  originalParent.addChildBefore(outerBlock, marker.startMarker);

  Node innerBlock = IR.block();
  innerBlock.setIsSyntheticBlock(true);
  // Move everything after the start Node up to the end Node into the inner
  // block.
  moveSiblingExclusive(originalParent, innerBlock,
      marker.startMarker,
      marker.endMarker);

  // Add the start node.
  outerBlock.addChildToBack(originalParent.removeChildAfter(outerBlock));
  // Add the inner block
  outerBlock.addChildToBack(innerBlock);
  // and finally the end node.
  outerBlock.addChildToBack(originalParent.removeChildAfter(outerBlock));

  compiler.reportCodeChange();
}
 
Example 12
Source File: Reader.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private void transformTryStmt(JsonML element, Node parent)
    throws JsonMLException {
  Preconditions.checkState(insertExprResultState == true);
  Node node = createNode(Token.TRY, element);
  parent.addChildToBack(node);

  // the first child represents try body
  JsonML child = element.getChild(0);
  transformElement(child, node);

  // the second child represents catch
  Node block = IR.block();
  node.addChildToBack(block);
  child = element.getChild(1);

  if (child.getType() == TagType.CatchClause) {
    transformElement(child, block);
  } else {
    // catch clause is not present, but the element has to be counted
    nodeIndex++;
  }

  // if the third child is present, it represents finally
  if (element.childrenSize() == 3) {
    child = element.getChild(2);
    transformElement(child, node);
  }
}
 
Example 13
Source File: ExternExportsPass.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates an instance.
 */
ExternExportsPass(AbstractCompiler compiler) {
  this.exports = Lists.newArrayList();
  this.compiler = compiler;
  this.definitionMap = Maps.newHashMap();
  this.externsRoot = IR.block();
  this.externsRoot.setIsSyntheticBlock(true);
  this.alreadyExportedPaths = Sets.newHashSet();
  this.mappedPaths = Maps.newHashMap();

  initExportMethods();
}
 
Example 14
Source File: Closure_37_IRFactory_t.java    From coming with MIT License 4 votes vote down vote up
@Override
Node processFunctionNode(FunctionNode functionNode) {
  Name name = functionNode.getFunctionName();
  Boolean isUnnamedFunction = false;
  if (name == null) {
    int functionType = functionNode.getFunctionType();
    if (functionType != FunctionNode.FUNCTION_EXPRESSION) {
      errorReporter.error(
        "unnamed function statement",
        sourceName,
        functionNode.getLineno(), "", 0);

      // Return the bare minimum to put the AST in a valid state.
      return newNode(Token.EXPR_RESULT, Node.newNumber(0));
    }
    name = new Name();
    name.setIdentifier("");
    isUnnamedFunction = true;
  }
  Node node = newNode(Token.FUNCTION);
  Node newName = transform(name);
  if (isUnnamedFunction) {
    // Old Rhino tagged the empty name node with the line number of the
    // declaration.
    newName.setLineno(functionNode.getLineno());
    // TODO(bowdidge) Mark line number of paren correctly.
    // Same problem as below - the left paren might not be on the
    // same line as the function keyword.
    int lpColumn = functionNode.getAbsolutePosition() +
        functionNode.getLp();
    newName.setCharno(position2charno(lpColumn));
    maybeSetLengthFrom(newName, name);
  }

  node.addChildToBack(newName);
  Node lp = newNode(Token.PARAM_LIST);
  // The left paren's complicated because it's not represented by an
  // AstNode, so there's nothing that has the actual line number that it
  // appeared on.  We know the paren has to appear on the same line as the
  // function name (or else a semicolon will be inserted.)  If there's no
  // function name, assume the paren was on the same line as the function.
  // TODO(bowdidge): Mark line number of paren correctly.
  Name fnName = functionNode.getFunctionName();
  if (fnName != null) {
    lp.setLineno(fnName.getLineno());
  } else {
    lp.setLineno(functionNode.getLineno());
  }
  int lparenCharno = functionNode.getLp() +
      functionNode.getAbsolutePosition();

  lp.setCharno(position2charno(lparenCharno));
  for (AstNode param : functionNode.getParams()) {
    lp.addChildToBack(transform(param));
  }
  node.addChildToBack(lp);

  Node bodyNode = transform(functionNode.getBody());
  if (!bodyNode.isBlock()) {
    // When in ideMode Rhino tries to parse some constructs the compiler
    // doesn't support, repair it here. see Rhino's
    // Parser#parseFunctionBodyExpr.
    Preconditions.checkState(config.isIdeMode);
    bodyNode = IR.block();
  }
  parseDirectives(bodyNode);
  node.addChildToBack(bodyNode);
 return node;
}
 
Example 15
Source File: Closure_122_IRFactory_s.java    From coming with MIT License 4 votes vote down vote up
@Override
Node processFunctionNode(FunctionNode functionNode) {
  Name name = functionNode.getFunctionName();
  Boolean isUnnamedFunction = false;
  if (name == null) {
    int functionType = functionNode.getFunctionType();
    if (functionType != FunctionNode.FUNCTION_EXPRESSION) {
      errorReporter.error(
        "unnamed function statement",
        sourceName,
        functionNode.getLineno(), "", 0);

      // Return the bare minimum to put the AST in a valid state.
      return newNode(Token.EXPR_RESULT, Node.newNumber(0));
    }
    name = new Name();
    name.setIdentifier("");
    isUnnamedFunction = true;
  }
  Node node = newNode(Token.FUNCTION);
  Node newName = transform(name);
  if (isUnnamedFunction) {
    // Old Rhino tagged the empty name node with the line number of the
    // declaration.
    newName.setLineno(functionNode.getLineno());
    // TODO(bowdidge) Mark line number of paren correctly.
    // Same problem as below - the left paren might not be on the
    // same line as the function keyword.
    int lpColumn = functionNode.getAbsolutePosition() +
        functionNode.getLp();
    newName.setCharno(position2charno(lpColumn));
    maybeSetLengthFrom(newName, name);
  }

  node.addChildToBack(newName);
  Node lp = newNode(Token.PARAM_LIST);
  // The left paren's complicated because it's not represented by an
  // AstNode, so there's nothing that has the actual line number that it
  // appeared on.  We know the paren has to appear on the same line as the
  // function name (or else a semicolon will be inserted.)  If there's no
  // function name, assume the paren was on the same line as the function.
  // TODO(bowdidge): Mark line number of paren correctly.
  Name fnName = functionNode.getFunctionName();
  if (fnName != null) {
    lp.setLineno(fnName.getLineno());
  } else {
    lp.setLineno(functionNode.getLineno());
  }
  int lparenCharno = functionNode.getLp() +
      functionNode.getAbsolutePosition();

  lp.setCharno(position2charno(lparenCharno));
  for (AstNode param : functionNode.getParams()) {
    Node paramNode = transformParameter(param);
    // When in ideMode Rhino can generate a param list with only a single
    // ErrorNode. This is transformed into an EMPTY node. Drop this node in
    // ideMode to keep the AST in a valid state.
    if (paramNode.isName()) {
      lp.addChildToBack(paramNode);
    } else {
      // We expect this in ideMode or when there is an error handling
      // destructuring parameter assignments which aren't supported
      // (an error has already been reported).
      Preconditions.checkState(
          config.isIdeMode
          || paramNode.isObjectLit()
          || paramNode.isArrayLit());
    }
  }
  node.addChildToBack(lp);

  Node bodyNode = transform(functionNode.getBody());
  if (!bodyNode.isBlock()) {
    // When in ideMode Rhino tries to parse some constructs the compiler
    // doesn't support, repair it here. see Rhino's
    // Parser#parseFunctionBodyExpr.
    Preconditions.checkState(config.isIdeMode);
    bodyNode = IR.block();
  }
  parseDirectives(bodyNode);
  node.addChildToBack(bodyNode);
 return node;
}
 
Example 16
Source File: IgnoreCajaProperties.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
  // Look for a for..in loop.
  if (n.isFor() && n.getChildCount() == 3) {
    Node body = n.getLastChild();
    n.removeChild(body);
    Node key = n.getFirstChild();
    n.removeChild(key);
    Node tmp = IR.name(
        "JSCompiler_IgnoreCajaProperties_" + counter++);
    n.addChildToFront(IR.var(tmp));
    Node assignment;
    Node ifBody;

    // Construct the body of the if statement.
    if (key.isVar()) {
      // for (var key in x) { body; }
      // =>
      // for (var tmp in x) {
      //   if (!tmp.match(/___$/)) {
      //     var key;
      //     key = tmp;
      //     body;
      //   }
      // }
      ifBody = IR.block(
          key,
          IR.exprResult(
              IR.assign(
                  key.getFirstChild().cloneNode(),
                  tmp.cloneTree())),
          body);
    } else {
      // for (key in x) { body; }
      // =>
      // for (var tmp in x) {
      //   if (!tmp.match(/___$/)) {
      //     key = tmp;
      //     body;
      //   }
      // }
      ifBody = IR.block(
          IR.exprResult(
              IR.assign(
                  key,
                  tmp.cloneTree())),
          body);
    }

    // Construct the new body of the for loop.
    Node newBody = IR.block(
        IR.ifNode(
            IR.not(
                IR.call(
                    IR.getprop(
                        tmp.cloneTree(),
                        IR.string("match")),
                    IR.regexp(
                        IR.string("___$")))),
            ifBody));
    n.addChildToBack(newBody);
    compiler.reportCodeChange();
  }
}
 
Example 17
Source File: Closure_122_IRFactory_t.java    From coming with MIT License 4 votes vote down vote up
@Override
Node processFunctionNode(FunctionNode functionNode) {
  Name name = functionNode.getFunctionName();
  Boolean isUnnamedFunction = false;
  if (name == null) {
    int functionType = functionNode.getFunctionType();
    if (functionType != FunctionNode.FUNCTION_EXPRESSION) {
      errorReporter.error(
        "unnamed function statement",
        sourceName,
        functionNode.getLineno(), "", 0);

      // Return the bare minimum to put the AST in a valid state.
      return newNode(Token.EXPR_RESULT, Node.newNumber(0));
    }
    name = new Name();
    name.setIdentifier("");
    isUnnamedFunction = true;
  }
  Node node = newNode(Token.FUNCTION);
  Node newName = transform(name);
  if (isUnnamedFunction) {
    // Old Rhino tagged the empty name node with the line number of the
    // declaration.
    newName.setLineno(functionNode.getLineno());
    // TODO(bowdidge) Mark line number of paren correctly.
    // Same problem as below - the left paren might not be on the
    // same line as the function keyword.
    int lpColumn = functionNode.getAbsolutePosition() +
        functionNode.getLp();
    newName.setCharno(position2charno(lpColumn));
    maybeSetLengthFrom(newName, name);
  }

  node.addChildToBack(newName);
  Node lp = newNode(Token.PARAM_LIST);
  // The left paren's complicated because it's not represented by an
  // AstNode, so there's nothing that has the actual line number that it
  // appeared on.  We know the paren has to appear on the same line as the
  // function name (or else a semicolon will be inserted.)  If there's no
  // function name, assume the paren was on the same line as the function.
  // TODO(bowdidge): Mark line number of paren correctly.
  Name fnName = functionNode.getFunctionName();
  if (fnName != null) {
    lp.setLineno(fnName.getLineno());
  } else {
    lp.setLineno(functionNode.getLineno());
  }
  int lparenCharno = functionNode.getLp() +
      functionNode.getAbsolutePosition();

  lp.setCharno(position2charno(lparenCharno));
  for (AstNode param : functionNode.getParams()) {
    Node paramNode = transformParameter(param);
    // When in ideMode Rhino can generate a param list with only a single
    // ErrorNode. This is transformed into an EMPTY node. Drop this node in
    // ideMode to keep the AST in a valid state.
    if (paramNode.isName()) {
      lp.addChildToBack(paramNode);
    } else {
      // We expect this in ideMode or when there is an error handling
      // destructuring parameter assignments which aren't supported
      // (an error has already been reported).
      Preconditions.checkState(
          config.isIdeMode
          || paramNode.isObjectLit()
          || paramNode.isArrayLit());
    }
  }
  node.addChildToBack(lp);

  Node bodyNode = transform(functionNode.getBody());
  if (!bodyNode.isBlock()) {
    // When in ideMode Rhino tries to parse some constructs the compiler
    // doesn't support, repair it here. see Rhino's
    // Parser#parseFunctionBodyExpr.
    Preconditions.checkState(config.isIdeMode);
    bodyNode = IR.block();
  }
  parseDirectives(bodyNode);
  node.addChildToBack(bodyNode);
 return node;
}
 
Example 18
Source File: IRFactory.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Override
Node processFunctionNode(FunctionNode functionNode) {
  Name name = functionNode.getFunctionName();
  Boolean isUnnamedFunction = false;
  if (name == null) {
    int functionType = functionNode.getFunctionType();
    if (functionType != FunctionNode.FUNCTION_EXPRESSION) {
      errorReporter.error(
        "unnamed function statement",
        sourceName,
        functionNode.getLineno(), "", 0);

      // Return the bare minimum to put the AST in a valid state.
      return newNode(Token.EXPR_RESULT, Node.newNumber(0));
    }
    name = new Name();
    name.setIdentifier("");
    isUnnamedFunction = true;
  }
  Node node = newNode(Token.FUNCTION);
  Node newName = transform(name);
  if (isUnnamedFunction) {
    // Old Rhino tagged the empty name node with the line number of the
    // declaration.
    newName.setLineno(functionNode.getLineno());
    // TODO(bowdidge) Mark line number of paren correctly.
    // Same problem as below - the left paren might not be on the
    // same line as the function keyword.
    int lpColumn = functionNode.getAbsolutePosition() +
        functionNode.getLp();
    newName.setCharno(position2charno(lpColumn));
    maybeSetLengthFrom(newName, name);
  }

  node.addChildToBack(newName);
  Node lp = newNode(Token.PARAM_LIST);
  // The left paren's complicated because it's not represented by an
  // AstNode, so there's nothing that has the actual line number that it
  // appeared on.  We know the paren has to appear on the same line as the
  // function name (or else a semicolon will be inserted.)  If there's no
  // function name, assume the paren was on the same line as the function.
  // TODO(bowdidge): Mark line number of paren correctly.
  Name fnName = functionNode.getFunctionName();
  if (fnName != null) {
    lp.setLineno(fnName.getLineno());
  } else {
    lp.setLineno(functionNode.getLineno());
  }
  int lparenCharno = functionNode.getLp() +
      functionNode.getAbsolutePosition();

  lp.setCharno(position2charno(lparenCharno));
  for (AstNode param : functionNode.getParams()) {
    Node paramNode = transform(param);
    // When in ideMode Rhino can generate a param list with only a single
    // ErrorNode. This is transformed into an EMPTY node. Drop this node in
    // ideMode to keep the AST in a valid state.
    if (paramNode.isName()) {
      lp.addChildToBack(paramNode);
    } else {
      // We expect this in ideMode or when there is an error handling
      // destructuring parameter assignments which aren't supported
      // (an error has already been reported).
      Preconditions.checkState(
          config.isIdeMode
          || paramNode.isObjectLit()
          || paramNode.isArrayLit());
    }
  }
  node.addChildToBack(lp);

  Node bodyNode = transform(functionNode.getBody());
  if (!bodyNode.isBlock()) {
    // When in ideMode Rhino tries to parse some constructs the compiler
    // doesn't support, repair it here. see Rhino's
    // Parser#parseFunctionBodyExpr.
    Preconditions.checkState(config.isIdeMode);
    bodyNode = IR.block();
  }
  parseDirectives(bodyNode);
  node.addChildToBack(bodyNode);
 return node;
}
 
Example 19
Source File: ReplaceMessages.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Updates the descendants of a FUNCTION node to represent a message's value.
 * <p>
 * The tree looks something like:
 * <pre>
 * function
 *  |-- name
 *  |-- lp
 *  |   |-- name <arg1>
 *  |    -- name <arg2>
 *   -- block
 *      |
 *       --return
 *           |
 *            --add
 *               |-- string foo
 *                -- name <arg1>
 * </pre>
 *
 * @param message  a message
 * @param functionNode  the message's original FUNCTION value node
 *
 * @throws MalformedException if the passed node's subtree structure is
 *         not as expected
 */
private void updateFunctionNode(JsMessage message, Node functionNode)
    throws MalformedException {
  checkNode(functionNode, Token.FUNCTION);
  Node nameNode = functionNode.getFirstChild();
  checkNode(nameNode, Token.NAME);
  Node argListNode = nameNode.getNext();
  checkNode(argListNode, Token.PARAM_LIST);
  Node oldBlockNode = argListNode.getNext();
  checkNode(oldBlockNode, Token.BLOCK);

  Iterator<CharSequence> iterator = message.parts().iterator();
  Node valueNode = iterator.hasNext()
      ? constructAddOrStringNode(iterator, argListNode)
      : IR.string("");
  Node newBlockNode = IR.block(IR.returnNode(valueNode));

  // TODO(user): checkTreeEqual is overkill. I am in process of rewriting
  // these functions.
  if (newBlockNode.checkTreeEquals(oldBlockNode) != null) {
    newBlockNode.copyInformationFromForTree(oldBlockNode);
    functionNode.replaceChild(oldBlockNode, newBlockNode);
    compiler.reportCodeChange();
  }
}
 
Example 20
Source File: SpecializeModule.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Creates an AST that consists solely of copies of the input roots for the
 * passed in module.
 *
 * Also records a map in {@link #functionInfoBySpecializedFunctionNode}
 * of information about the original function keyed on the copies of the
 * functions to specialized.
 */
private Node copyModuleInputs(JSModule module) {

  specializedInputRootsByOriginal = Maps.newLinkedHashMap();

  functionInfoBySpecializedFunctionNode = Maps.newLinkedHashMap();

  Node syntheticModuleJsRoot = IR.block();
  syntheticModuleJsRoot.setIsSyntheticBlock(true);

  for (CompilerInput input : module.getInputs()) {
    Node originalInputRoot = input.getAstRoot(compiler);

    Node copiedInputRoot = originalInputRoot.cloneTree();
    copiedInputRoot.copyInformationFromForTree(originalInputRoot);

    specializedInputRootsByOriginal.put(originalInputRoot,
        copiedInputRoot);

    matchTopLevelFunctions(originalInputRoot, copiedInputRoot);

    syntheticModuleJsRoot.addChildToBack(copiedInputRoot);
  }

  // The jsRoot needs a parent (in a normal compilation this would be the
  // node that contains jsRoot and the externs).
  Node syntheticExternsAndJsRoot = IR.block();
  syntheticExternsAndJsRoot.addChildToBack(syntheticModuleJsRoot);

  return syntheticModuleJsRoot;
}