本文整理汇总了Java中com.google.javascript.rhino.Token.RETURN属性的典型用法代码示例。如果您正苦于以下问题:Java Token.RETURN属性的具体用法?Java Token.RETURN怎么用?Java Token.RETURN使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类com.google.javascript.rhino.Token
的用法示例。
在下文中一共展示了Token.RETURN属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: returnedExpression
/**
* 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.getType() != Token.RETURN) {
return null;
}
if (!expectedReturn.hasOneChild()) {
return null;
}
return expectedReturn.getLastChild();
}
示例2: isDirectCallNodeReplacementPossible
/**
* Checks if the given function matches the criteria for an inlinable
* function, and if so, adds it to our set of inlinable functions.
*/
boolean isDirectCallNodeReplacementPossible(Node fnNode) {
// Only inline single-statement functions
Node block = NodeUtil.getFunctionBody(fnNode);
// Check if this function is suitable for direct replacement of a CALL node:
// a function that consists of single return that returns an expression.
if (!block.hasChildren()) {
// special case empty functions.
return true;
} else if (block.hasOneChild()) {
// Only inline functions that return something.
if (block.getFirstChild().getType() == Token.RETURN
&& block.getFirstChild().getFirstChild() != null) {
return true;
}
}
return false;
}
示例3: valueConsumedByParent
/**
* Determine if the parent reads the value of a child expression
* directly. This is true children used in predicates, RETURN
* statements and, rhs of variable declarations and assignments.
*
* In the case of:
* if (a) b else c
*
* This method returns true for "a", and false for "b" and "c": the
* IF expression does something special based on "a"'s value. "b"
* and "c" are effectivelly outputs. Same logic applies to FOR,
* WHILE and DO loop predicates. AND/OR/HOOK expressions are
* syntactic sugar for IF statements; therefore this method returns
* true for the predicate and false otherwise.
*/
private boolean valueConsumedByParent(Node n, Node parent) {
if (NodeUtil.isAssignmentOp(parent)) {
return parent.getLastChild() == n;
}
switch (parent.getType()) {
case Token.NAME:
case Token.RETURN:
return true;
case Token.AND:
case Token.OR:
case Token.HOOK:
return parent.getFirstChild() == n;
case Token.FOR:
return parent.getFirstChild().getNext() == n;
case Token.IF:
case Token.WHILE:
return parent.getFirstChild() == n;
case Token.DO:
return parent.getLastChild() == n;
default:
return false;
}
}
示例4: allPathsReturn
/**
* @returns true if all paths from block must exit with an explicit return.
*/
private boolean allPathsReturn(Node block) {
// Computes the control flow graph.
ControlFlowAnalysis cfa = new ControlFlowAnalysis(compiler, false);
cfa.process(null, block);
ControlFlowGraph<Node> cfg = cfa.getCfg();
Node returnPathsParent = cfg.getImplicitReturn().getValue();
for (DiGraphNode<Node, Branch> pred :
cfg.getDirectedPredNodes(returnPathsParent)) {
Node n = pred.getValue();
if (n.getType() != Token.RETURN) {
return false;
}
}
return true;
}
示例5: matchingExitNode
/**
* Determines if n matches the type and name for the following types of
* "exits":
* - return without values
* - continues and breaks with or without names.
* @param n The node to inspect.
* @param type The Token type to look for.
* @param labelName The name that must be associated with the exit type.
* @nullable labelName non-null only for breaks associated with labels.
* @return Whether the node matches the specified block-exit type.
*/
static private boolean matchingExitNode(Node n, int type, String labelName) {
if (n.getType() == type) {
if (type == Token.RETURN) {
// only returns without expressions.
return !n.hasChildren();
} else {
if (labelName == null) {
return !n.hasChildren();
} else {
return n.hasChildren()
&& labelName.equals(n.getFirstChild().getString());
}
}
}
return false;
}
示例6: processReturnStatement
@Override
Node processReturnStatement(ReturnStatement statementNode) {
Node node = new Node(Token.RETURN);
if (statementNode.getReturnValue() != null) {
node.addChildToBack(transform(statementNode.getReturnValue()));
}
return node;
}
示例7: maybeGetSingleReturnRValue
/**
* @return function return value node if function body contains a
* single return statement. Otherwise, null.
*/
protected final Node maybeGetSingleReturnRValue(Node functionNode) {
Node body = functionNode.getLastChild();
if (!body.hasOneChild()) {
return null;
}
Node statement = body.getFirstChild();
if (statement.getType() == Token.RETURN) {
return statement.getFirstChild();
}
return null;
}
示例8: isInThrowExpression
/**
* Is the {@link Node} currently within a 'throw' expression?
*/
private static boolean isInThrowExpression(Node n) {
// Look up the traversal stack to find a THROW node
for (Node ancestor : n.getAncestors()) {
switch (ancestor.getType()) {
case Token.THROW:
return true;
case Token.IF:
case Token.WHILE:
case Token.DO:
case Token.FOR:
case Token.SWITCH:
case Token.CASE:
case Token.DEFAULT:
case Token.BLOCK:
case Token.SCRIPT:
case Token.FUNCTION:
case Token.TRY:
case Token.CATCH:
case Token.RETURN:
case Token.EXPR_RESULT:
// early exit - these nodes types can't be within a THROW
return false;
}
}
return false;
}
示例9: findExpressionRoot
/**
* @return The statement containing the expression. null if subExpression
* is not contain by in by a Node where inlining is known to be possible.
* For example, a WHILE node condition expression.
*/
static Node findExpressionRoot(Node subExpression) {
Node child = subExpression;
for (Node parent : child.getAncestors()) {
int parentType = parent.getType();
switch (parentType) {
// Supported expression roots:
// SWITCH and IF can have multiple children, but the CASE, DEFAULT,
// or BLOCK will be encountered first for any of the children other
// than the condition.
case Token.EXPR_RESULT:
case Token.IF:
case Token.SWITCH:
case Token.RETURN:
case Token.VAR:
Preconditions.checkState(child == parent.getFirstChild());
return parent;
// Any of these indicate an unsupported expression:
case Token.SCRIPT:
case Token.BLOCK:
case Token.LABEL:
case Token.CASE:
case Token.DEFAULT:
return null;
}
child = parent;
}
throw new IllegalStateException("Unexpected AST structure.");
}
示例10: visit
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
if (NodeUtil.isFunction(n)) {
visitFunction(t, n);
} else if (n.getType() == Token.RETURN) {
visitReturn(t, n);
}
}
示例11: isReturnExpressBlock
/**
* @return Whether the node is a block with a single statement that is
* an return.
*/
private boolean isReturnExpressBlock(Node n) {
if (n.getType() == Token.BLOCK) {
if (n.hasOneChild()) {
Node first = n.getFirstChild();
if (first.getType() == Token.RETURN) {
return first.hasOneChild();
}
}
}
return false;
}
示例12: tryRemoveDeadAssignments
/**
* Try to remove useless assignments from a control flow graph that has been
* annotated with liveness information.
*
* @param t The node traversal.
* @param cfg The control flow graph of the program annotated with liveness
* information.
*/
private void tryRemoveDeadAssignments(NodeTraversal t,
ControlFlowGraph<Node> cfg) {
List<DiGraphNode<Node, Branch>> nodes = cfg.getDirectedGraphNodes();
for (DiGraphNode<Node, Branch> cfgNode : nodes) {
FlowState<LiveVariableLattice> state =
cfgNode.getAnnotation();
Node n = cfgNode.getValue();
if (n == null) {
continue;
}
switch (n.getType()) {
case Token.IF:
case Token.WHILE:
case Token.DO:
tryRemoveAssignment(t, NodeUtil.getConditionExpression(n), state);
continue;
case Token.FOR:
if (!NodeUtil.isForIn(n)) {
tryRemoveAssignment(
t, NodeUtil.getConditionExpression(n), state);
}
continue;
case Token.SWITCH:
case Token.CASE:
case Token.RETURN:
if (n.hasChildren()) {
tryRemoveAssignment(t, n.getFirstChild(), state);
}
continue;
// TODO(user): case Token.VAR: Remove var a=1;a=2;.....
}
tryRemoveAssignment(t, n, state);
}
}
示例13: replaceReturnWithBreak
/**
* Replace the 'return' statement with its child expression.
* "return foo()" becomes "{foo(); break;}" or
* "{resultName = foo(); break;}"
* "return" becomes {break;} or "{resultName = void 0;break;}".
*/
private static Node replaceReturnWithBreak(Node current, Node parent,
String resultName, String labelName) {
if (current.getType() == Token.FUNCTION
|| current.getType() == Token.EXPR_RESULT) {
// Don't recurse into functions definitions, and expressions can't
// contain RETURN nodes.
return current;
}
if (current.getType() == Token.RETURN) {
Preconditions.checkState(NodeUtil.isStatementBlock(parent));
Node resultNode = getReplacementReturnStatement(current, resultName);
Node name = Node.newString(Token.NAME, labelName);
Node breakNode = new Node(Token.BREAK, name);
// Replace the node in parent, and reset current to the first new child.
parent.replaceChild(current, breakNode);
if (resultNode != null) {
parent.addChildBefore(resultNode, breakNode);
}
current = breakNode;
} else {
for (Node c = current.getFirstChild(); c != null; c = c.getNext()) {
// c may be replaced.
c = replaceReturnWithBreak(c, current, resultName, labelName);
}
}
return current;
}
示例14: visit
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
if (n.getType() != Token.RETURN) {
return;
}
Node call = newReportFunctionExitNode();
Node returnRhs = n.removeFirstChild();
if (returnRhs != null) {
call.addChildToBack(returnRhs);
}
n.addChildToFront(call);
compiler.reportCodeChange();
}
示例15: fastAllPathsReturnCheck
/**
* Fast check to see if all execution paths contain a return statement.
* May spuriously report that a return statement is missing.
*
* @return true if all paths return, converse not necessarily true
*/
private static boolean fastAllPathsReturnCheck(ControlFlowGraph<Node> cfg) {
for (DiGraphEdge<Node, Branch> s : cfg.getImplicitReturn().getInEdges()) {
if (s.getSource().getValue().getType() != Token.RETURN) {
return false;
}
}
return true;
}