Java Code Examples for com.google.javascript.rhino.Node#isReturn()

The following examples show how to use com.google.javascript.rhino.Node#isReturn() . 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: InlineSimpleMethods.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Return the node that represents the expression returned
 * by the method, given a FUNCTION node.
 */
private static Node returnedExpression(Node fn) {
  Node expectedBlock = getMethodBlock(fn);
  if (!expectedBlock.hasOneChild()) {
    return null;
  }

  Node expectedReturn = expectedBlock.getFirstChild();
  if (!expectedReturn.isReturn()) {
    return null;
  }

  if (!expectedReturn.hasOneChild()) {
    return null;
  }

  return expectedReturn.getLastChild();
}
 
Example 2
Source File: Cardumen_00151_t.java    From coming with MIT License 6 votes vote down vote up
private void addSimplifiedExpression(Node n, Node parent) {
  if (parent.isVar()) {
    Node value = n.getFirstChild();
    if (value != null) {
      addSimplifiedChildren(value);
    }
  } else if (n.isAssign() &&
      (parent.isExprResult() ||
       parent.isFor() ||
       parent.isReturn())) {
    for (Node child : n.children()) {
      addSimplifiedChildren(child);
    }
  } else if (n.isCall() &&
             parent.isExprResult()) {
    addSimplifiedChildren(n);
  } else {
    addAllChildren(n);
  }
}
 
Example 3
Source File: Cardumen_00151_s.java    From coming with MIT License 6 votes vote down vote up
private void addSimplifiedExpression(Node n, Node parent) {
  if (parent.isVar()) {
    Node value = n.getFirstChild();
    if (value != null) {
      addSimplifiedChildren(value);
    }
  } else if (n.isAssign() &&
      (parent.isExprResult() ||
       parent.isFor() ||
       parent.isReturn())) {
    for (Node child : n.children()) {
      addSimplifiedChildren(child);
    }
  } else if (n.isCall() &&
             parent.isExprResult()) {
    addSimplifiedChildren(n);
  } else {
    addAllChildren(n);
  }
}
 
Example 4
Source File: Closure_43_TypedScopeCreator_t.java    From coming with MIT License 6 votes vote down vote up
@Override public void visit(NodeTraversal t, Node n, Node parent) {
  if (t.inGlobalScope()) {
    return;
  }

  if (n.isReturn() && n.getFirstChild() != null) {
    data.get(t.getScopeRoot()).recordNonEmptyReturn();
  } else if (n.isName() && NodeUtil.isLValue(n)) {
    String name = n.getString();
    Scope scope = t.getScope();
    Var var = scope.getVar(name);
    if (var != null) {
      Scope ownerScope = var.getScope();
      if (scope != ownerScope && ownerScope.isLocal()) {
        data.get(ownerScope.getRootNode()).recordEscapedVarName(name);
      }
    }
  }
}
 
Example 5
Source File: Cardumen_0092_t.java    From coming with MIT License 6 votes vote down vote up
private void addSimplifiedExpression(Node n, Node parent) {
  if (parent.isVar()) {
    Node value = n.getFirstChild();
    if (value != null) {
      addSimplifiedChildren(value);
    }
  } else if (n.isAssign() &&
      (parent.isExprResult() ||
       parent.isFor() ||
       parent.isReturn())) {
    for (Node child : n.children()) {
      addSimplifiedChildren(child);
    }
  } else if (n.isCall() &&
             parent.isExprResult()) {
    addSimplifiedChildren(n);
  } else {
    addAllChildren(n);
  }
}
 
Example 6
Source File: Closure_40_NameAnalyzer_s.java    From coming with MIT License 6 votes vote down vote up
private void addSimplifiedExpression(Node n, Node parent) {
  if (parent.isVar()) {
    Node value = n.getFirstChild();
    if (value != null) {
      addSimplifiedChildren(value);
    }
  } else if (n.isAssign() &&
      (parent.isExprResult() ||
       parent.isFor() ||
       parent.isReturn())) {
    for (Node child : n.children()) {
      addSimplifiedChildren(child);
    }
  } else if (n.isCall() &&
             parent.isExprResult()) {
    addSimplifiedChildren(n);
  } else {
    addAllChildren(n);
  }
}
 
Example 7
Source File: Closure_43_TypedScopeCreator_s.java    From coming with MIT License 6 votes vote down vote up
@Override public void visit(NodeTraversal t, Node n, Node parent) {
  if (t.inGlobalScope()) {
    return;
  }

  if (n.isReturn() && n.getFirstChild() != null) {
    data.get(t.getScopeRoot()).recordNonEmptyReturn();
  } else if (n.isName() && NodeUtil.isLValue(n)) {
    String name = n.getString();
    Scope scope = t.getScope();
    Var var = scope.getVar(name);
    if (var != null) {
      Scope ownerScope = var.getScope();
      if (scope != ownerScope && ownerScope.isLocal()) {
        data.get(ownerScope.getRootNode()).recordEscapedVarName(name);
      }
    }
  }
}
 
Example 8
Source File: ChainCalls.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void enterScope(NodeTraversal t) {
  ControlFlowGraph<Node> cfg = t.getControlFlowGraph();

  for (DiGraphEdge<Node, Branch> s : cfg.getImplicitReturn().getInEdges()) {
    Node exitNode = s.getSource().getValue();
    if (!exitNode.isReturn() ||
        exitNode.getFirstChild() == null ||
        !exitNode.getFirstChild().isThis()) {
      badFunctionNodes.add(t.getScopeRoot());
      return;
    }
  }

  goodFunctionNodes.add(t.getScopeRoot());
}
 
Example 9
Source File: Closure_40_NameAnalyzer_t.java    From coming with MIT License 6 votes vote down vote up
private void addSimplifiedExpression(Node n, Node parent) {
  if (parent.isVar()) {
    Node value = n.getFirstChild();
    if (value != null) {
      addSimplifiedChildren(value);
    }
  } else if (n.isAssign() &&
      (parent.isExprResult() ||
       parent.isFor() ||
       parent.isReturn())) {
    for (Node child : n.children()) {
      addSimplifiedChildren(child);
    }
  } else if (n.isCall() &&
             parent.isExprResult()) {
    addSimplifiedChildren(n);
  } else {
    addAllChildren(n);
  }
}
 
Example 10
Source File: Closure_20_PeepholeSubstituteAlternateSyntax_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * @return Whether the node is a block with a single statement that is
 *     an return.
 */
private boolean isReturnExpressBlock(Node n) {
  if (n.isBlock()) {
    if (n.hasOneChild()) {
      Node first = n.getFirstChild();
      if (first.isReturn()) {
        return first.hasOneChild();
      }
    }
  }

  return false;
}
 
Example 11
Source File: Closure_132_PeepholeSubstituteAlternateSyntax_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * @return Whether the node is a block with a single statement that is
 *     an return.
 */
private boolean isReturnExpressBlock(Node n) {
  if (n.isBlock()) {
    if (n.hasOneChild()) {
      Node first = n.getFirstChild();
      if (first.isReturn()) {
        return first.hasOneChild();
      }
    }
  }

  return false;
}
 
Example 12
Source File: Closure_20_PeepholeSubstituteAlternateSyntax_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * @return Whether the node is a single return statement.
 */
private boolean isReturnExpression(Node n) {
  if (n.isReturn()) {
    return n.hasOneChild();
  }
  return false;
}
 
Example 13
Source File: Closure_132_PeepholeSubstituteAlternateSyntax_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * @return Whether the node is a single return statement.
 */
private boolean isReturnExpression(Node n) {
  if (n.isReturn()) {
    return n.hasOneChild();
  }
  return false;
}
 
Example 14
Source File: Closure_132_PeepholeSubstituteAlternateSyntax_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * @return Whether the node is a block with a single statement that is
 *     an return.
 */
private boolean isReturnExpressBlock(Node n) {
  if (n.isBlock()) {
    if (n.hasOneChild()) {
      Node first = n.getFirstChild();
      if (first.isReturn()) {
        return first.hasOneChild();
      }
    }
  }

  return false;
}
 
Example 15
Source File: Closure_132_PeepholeSubstituteAlternateSyntax_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * @return Whether the node is a block with a single statement that is
 *     an return with or without an expression.
 */
private boolean isReturnBlock(Node n) {
  if (n.isBlock()) {
    if (n.hasOneChild()) {
      Node first = n.getFirstChild();
      return first.isReturn();
    }
  }

  return false;
}
 
Example 16
Source File: PeepholeSubstituteAlternateSyntax.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @return Whether the node is a single return statement.
 */
private boolean isReturnExpression(Node n) {
  if (n.isReturn()) {
    return n.hasOneChild();
  }
  return false;
}
 
Example 17
Source File: PeepholeSubstituteAlternateSyntax.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @return Whether the node is a block with a single statement that is
 *     an return with or without an expression.
 */
private boolean isReturnBlock(Node n) {
  if (n.isBlock()) {
    if (n.hasOneChild()) {
      Node first = n.getFirstChild();
      return first.isReturn();
    }
  }

  return false;
}
 
Example 18
Source File: Closure_116_FunctionInjector_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Determines whether a function can be inlined at a particular call site.
 * There are several criteria that the function and reference must hold in
 * order for the functions to be inlined:
 * 1) If a call's arguments have side effects,
 * the corresponding argument in the function must only be referenced once.
 * For instance, this will not be inlined:
 * <pre>
 *     function foo(a) { return a + a }
 *     x = foo(i++);
 * </pre>
 */
private CanInlineResult canInlineReferenceDirectly(
    Node callNode, Node fnNode) {
  if (!isDirectCallNodeReplacementPossible(fnNode)) {
    return CanInlineResult.NO;
  }

  Node block = fnNode.getLastChild();

  boolean hasSideEffects = false;  // empty function case
  if (block.hasChildren()) {
    Preconditions.checkState(block.hasOneChild());
    Node stmt = block.getFirstChild();
    if (stmt.isReturn()) {
      hasSideEffects = NodeUtil.mayHaveSideEffects(
          stmt.getFirstChild(), compiler);
    }
  }

  // CALL NODE: [ NAME, ARG1, ARG2, ... ]
  Node cArg = callNode.getFirstChild().getNext();

  // Functions called via 'call' and 'apply' have a this-object as
  // the first parameter, but this is not part of the called function's
  // parameter list.
  if (!callNode.getFirstChild().isName()) {
    if (NodeUtil.isFunctionObjectCall(callNode)) {
      // TODO(johnlenz): Support replace this with a value.
      if (cArg == null || !cArg.isThis()) {
        return CanInlineResult.NO;
      }
      cArg = cArg.getNext();
    } else {
      // ".apply" call should be filtered before this.
      Preconditions.checkState(!NodeUtil.isFunctionObjectApply(callNode));
    }
  }

  // FUNCTION NODE -> LP NODE: [ ARG1, ARG2, ... ]
  Node fnParam = NodeUtil.getFunctionParameters(fnNode).getFirstChild();
  while (cArg != null || fnParam != null) {
    // For each named parameter check if a mutable argument use more than one.
    if (fnParam != null) {
      if (cArg != null) {
        if (hasSideEffects && NodeUtil.canBeSideEffected(cArg)) {
          return CanInlineResult.NO;
        }

        // Check for arguments that are evaluated more than once.
        // Note: Unlike block inlining, there it is not possible that a
        // parameter reference will be in a loop.
        if (NodeUtil.mayEffectMutableState(cArg, compiler)
            && NodeUtil.getNameReferenceCount(
                block, fnParam.getString()) > 1) {
          return CanInlineResult.NO;
        }
      }

      // Move to the next name.
      fnParam = fnParam.getNext();
    }

    // For every call argument check for side-effects, even if there
    // isn't a named parameter to match.
    if (cArg != null) {
      if (NodeUtil.mayHaveSideEffects(cArg, compiler)) {
        return CanInlineResult.NO;
      }
      cArg = cArg.getNext();
    }
  }

  return CanInlineResult.YES;
}
 
Example 19
Source File: PureFunctionIdentifier.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void visit(NodeTraversal traversal, Node node, Node parent) {

  if (inExterns) {
    return;
  }

  if (!NodeUtil.nodeTypeMayHaveSideEffects(node)
      && !node.isReturn()) {
    return;
  }

  if (node.isCall() || node.isNew()) {
    allFunctionCalls.add(node);
  }

  Node enclosingFunction = traversal.getEnclosingFunction();
  if (enclosingFunction != null) {
    FunctionInformation sideEffectInfo =
        functionSideEffectMap.get(enclosingFunction);
    Preconditions.checkNotNull(sideEffectInfo);

    if (NodeUtil.isAssignmentOp(node)) {
      visitAssignmentOrUnaryOperator(
          sideEffectInfo, traversal.getScope(),
          node, node.getFirstChild(), node.getLastChild());
    } else {
      switch(node.getType()) {
        case Token.CALL:
        case Token.NEW:
          visitCall(sideEffectInfo, node);
          break;
        case Token.DELPROP:
        case Token.DEC:
        case Token.INC:
          visitAssignmentOrUnaryOperator(
              sideEffectInfo, traversal.getScope(),
              node, node.getFirstChild(), null);
          break;
        case Token.NAME:
          // Variable definition are not side effects.
          // Just check that the name appears in the context of a
          // variable declaration.
          Preconditions.checkArgument(
              NodeUtil.isVarDeclaration(node));
          Node value = node.getFirstChild();
          // Assignment to local, if the value isn't a safe local value,
          // new object creation or literal or known primitive result
          // value, add it to the local blacklist.
          if (value != null && !NodeUtil.evaluatesToLocalValue(value)) {
            Scope scope = traversal.getScope();
            Var var = scope.getVar(node.getString());
            sideEffectInfo.blacklistLocal(var);
          }
          break;
        case Token.THROW:
          visitThrow(sideEffectInfo);
          break;
        case Token.RETURN:
          if (node.hasChildren()
              && !NodeUtil.evaluatesToLocalValue(node.getFirstChild())) {
            sideEffectInfo.setTaintsReturn();
          }
          break;
        default:
          throw new IllegalArgumentException(
              "Unhandled side effect node type " +
              Token.name(node.getType()));
      }
    }
  }
}
 
Example 20
Source File: 1_FunctionInjector.java    From SimFix with GNU General Public License v2.0 4 votes vote down vote up
/**
   * Determines whether a function can be inlined at a particular call site.
   * There are several criteria that the function and reference must hold in
   * order for the functions to be inlined:
   * 1) If a call's arguments have side effects,
   * the corresponding argument in the function must only be referenced once.
   * For instance, this will not be inlined:
   * <pre>
   *     function foo(a) { return a + a }
   *     x = foo(i++);
   * </pre>
   */
  private CanInlineResult canInlineReferenceDirectly(
      Node callNode, Node fnNode) {
    if (!isDirectCallNodeReplacementPossible(fnNode)) {
      return CanInlineResult.NO;
    }

    Node block = fnNode.getLastChild();

    boolean hasSideEffects = false;
    if (block.hasChildren()) {
      Preconditions.checkState(block.hasOneChild());
      Node stmt = block.getFirstChild();
      if (stmt.isReturn()) {
        hasSideEffects = NodeUtil.mayHaveSideEffects(stmt.getFirstChild(), compiler);
      }
    }
    // CALL NODE: [ NAME, ARG1, ARG2, ... ]
    Node cArg = callNode.getFirstChild().getNext();

    // Functions called via 'call' and 'apply' have a this-object as
    // the first parameter, but this is not part of the called function's
    // parameter list.
    if (!callNode.getFirstChild().isName()) {
      if (NodeUtil.isFunctionObjectCall(callNode)) {
        // TODO(johnlenz): Support replace this with a value.
        if (cArg == null || !cArg.isThis()) {
          return CanInlineResult.NO;
        }
        cArg = cArg.getNext();
      } else {
        // ".apply" call should be filtered before this.
        Preconditions.checkState(!NodeUtil.isFunctionObjectApply(callNode));
      }
    }

    // FUNCTION NODE -> LP NODE: [ ARG1, ARG2, ... ]
    Node fnParam = NodeUtil.getFunctionParameters(fnNode).getFirstChild();
    while (cArg != null || fnParam != null) {
      // For each named parameter check if a mutable argument use more than one.
      if (fnParam != null) {
        if (cArg != null) {
// start of generated patch
if(cArg==null&&NodeUtil.canBeSideEffected(cArg)){
return CanInlineResult.NO;
}
// end of generated patch
/* start of original code
          if (hasSideEffects && NodeUtil.canBeSideEffected(cArg)) {
            return CanInlineResult.NO;
          }
 end of original code*/
          // Check for arguments that are evaluated more than once.
          // Note: Unlike block inlining, there it is not possible that a
          // parameter reference will be in a loop.
          if (NodeUtil.mayEffectMutableState(cArg, compiler)
              && NodeUtil.getNameReferenceCount(
                  block, fnParam.getString()) > 1) {
            return CanInlineResult.NO;
          }
        }

        // Move to the next name.
        fnParam = fnParam.getNext();
      }

      // For every call argument check for side-effects, even if there
      // isn't a named parameter to match.
      if (cArg != null) {
        if (NodeUtil.mayHaveSideEffects(cArg, compiler)) {
          return CanInlineResult.NO;
        }
        cArg = cArg.getNext();
      }
    }

    return CanInlineResult.YES;
  }