7115046: Add AST node for lambda expressions
Summary: Add tree nodes for representing lambda expressions and update relevant visitors interfaces
Reviewed-by: jjg
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/src/share/classes/com/sun/source/tree/LambdaExpressionTree.java Thu Nov 24 13:36:20 2011 +0000
@@ -0,0 +1,56 @@
+/*
+ * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation. Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package com.sun.source.tree;
+
+import java.util.List;
+
+/**
+ * A tree node for a lambda expression.
+ *
+ * For example:
+ * <pre>
+ * ()->{}
+ * (List<String> ls)->ls.size()
+ * (x,y)-> { return x + y; }
+ * </pre>
+ */
+public interface LambdaExpressionTree extends ExpressionTree {
+
+ /**
+ * Lambda expressions come in two forms: (i) expression lambdas, whose body
+ * is an expression, and (ii) statement lambdas, whose body is a block
+ */
+ public enum BodyKind {
+ /** enum constant for expression lambdas */
+ EXPRESSION,
+ /** enum constant for statement lambdas */
+ STATEMENT;
+ }
+
+ List<? extends VariableTree> getParameters();
+ Tree getBody();
+ BodyKind getBodyKind();
+}
--- a/langtools/src/share/classes/com/sun/source/tree/Tree.java Wed Jul 05 17:56:18 2017 +0200
+++ b/langtools/src/share/classes/com/sun/source/tree/Tree.java Thu Nov 24 13:36:20 2011 +0000
@@ -187,6 +187,11 @@
NEW_CLASS(NewClassTree.class),
/**
+ * Used for instances of {@link LambdaExpressionTree}.
+ */
+ LAMBDA_EXPRESSION(LambdaExpressionTree.class),
+
+ /**
* Used for instances of {@link ParenthesizedTree}.
*/
PARENTHESIZED(ParenthesizedTree.class),
--- a/langtools/src/share/classes/com/sun/source/tree/TreeVisitor.java Wed Jul 05 17:56:18 2017 +0200
+++ b/langtools/src/share/classes/com/sun/source/tree/TreeVisitor.java Thu Nov 24 13:36:20 2011 +0000
@@ -85,6 +85,7 @@
R visitModifiers(ModifiersTree node, P p);
R visitNewArray(NewArrayTree node, P p);
R visitNewClass(NewClassTree node, P p);
+ R visitLambdaExpression(LambdaExpressionTree node, P p);
R visitParenthesized(ParenthesizedTree node, P p);
R visitReturn(ReturnTree node, P p);
R visitMemberSelect(MemberSelectTree node, P p);
--- a/langtools/src/share/classes/com/sun/source/util/SimpleTreeVisitor.java Wed Jul 05 17:56:18 2017 +0200
+++ b/langtools/src/share/classes/com/sun/source/util/SimpleTreeVisitor.java Thu Nov 24 13:36:20 2011 +0000
@@ -172,6 +172,10 @@
return defaultAction(node, p);
}
+ public R visitLambdaExpression(LambdaExpressionTree node, P p) {
+ return defaultAction(node, p);
+ }
+
public R visitParenthesized(ParenthesizedTree node, P p) {
return defaultAction(node, p);
}
--- a/langtools/src/share/classes/com/sun/source/util/TreeScanner.java Wed Jul 05 17:56:18 2017 +0200
+++ b/langtools/src/share/classes/com/sun/source/util/TreeScanner.java Thu Nov 24 13:36:20 2011 +0000
@@ -285,6 +285,12 @@
return r;
}
+ public R visitLambdaExpression(LambdaExpressionTree node, P p) {
+ R r = scan(node.getParameters(), p);
+ r = scanAndReduce(node.getBody(), p, r);
+ return r;
+ }
+
public R visitParenthesized(ParenthesizedTree node, P p) {
return scan(node.getExpression(), p);
}
--- a/langtools/src/share/classes/com/sun/tools/javac/tree/JCTree.java Wed Jul 05 17:56:18 2017 +0200
+++ b/langtools/src/share/classes/com/sun/tools/javac/tree/JCTree.java Thu Nov 24 13:36:20 2011 +0000
@@ -41,6 +41,7 @@
import com.sun.tools.javac.code.Symbol.*;
import com.sun.tools.javac.parser.EndPosTable;
import com.sun.source.tree.*;
+import com.sun.source.tree.LambdaExpressionTree.BodyKind;
import static com.sun.tools.javac.code.BoundKind.*;
import static com.sun.tools.javac.tree.JCTree.Tag.*;
@@ -198,6 +199,10 @@
*/
NEWARRAY,
+ /** Lambda expression, of type Lambda.
+ */
+ LAMBDA,
+
/** Parenthesized subexpressions, of type Parens.
*/
PARENS,
@@ -1487,6 +1492,56 @@
}
/**
+ * A lambda expression.
+ */
+ public static class JCLambda extends JCExpression implements LambdaExpressionTree {
+
+ public List<JCVariableDecl> params;
+ public JCTree body;
+ public Type targetType;
+ public boolean canCompleteNormally = true;
+ public List<Type> inferredThrownTypes;
+
+ public JCLambda(List<JCVariableDecl> params,
+ JCTree body) {
+ this.params = params;
+ this.body = body;
+ }
+ @Override
+ public Tag getTag() {
+ return LAMBDA;
+ }
+ @Override
+ public void accept(Visitor v) {
+ v.visitLambda(this);
+ }
+ @Override
+ public <R, D> R accept(TreeVisitor<R, D> v, D d) {
+ return v.visitLambdaExpression(this, d);
+ }
+ public Kind getKind() {
+ return Kind.LAMBDA_EXPRESSION;
+ }
+ public JCTree getBody() {
+ return body;
+ }
+ public java.util.List<? extends VariableTree> getParameters() {
+ return params;
+ }
+ @Override
+ public JCLambda setType(Type type) {
+ super.setType(type);
+ return this;
+ }
+ @Override
+ public BodyKind getBodyKind() {
+ return body.hasTag(BLOCK) ?
+ BodyKind.STATEMENT :
+ BodyKind.EXPRESSION;
+ }
+ }
+
+ /**
* A parenthesized subexpression ( ... )
*/
public static class JCParens extends JCExpression implements ParenthesizedTree {
@@ -2271,6 +2326,7 @@
public void visitApply(JCMethodInvocation that) { visitTree(that); }
public void visitNewClass(JCNewClass that) { visitTree(that); }
public void visitNewArray(JCNewArray that) { visitTree(that); }
+ public void visitLambda(JCLambda that) { visitTree(that); }
public void visitParens(JCParens that) { visitTree(that); }
public void visitAssign(JCAssign that) { visitTree(that); }
public void visitAssignop(JCAssignOp that) { visitTree(that); }
--- a/langtools/src/share/classes/com/sun/tools/javac/tree/Pretty.java Wed Jul 05 17:56:18 2017 +0200
+++ b/langtools/src/share/classes/com/sun/tools/javac/tree/Pretty.java Thu Nov 24 13:36:20 2011 +0000
@@ -907,6 +907,17 @@
}
}
+ public void visitLambda(JCLambda tree) {
+ try {
+ print("(");
+ printExprs(tree.params);
+ print(")->");
+ printExpr(tree.body);
+ } catch (IOException e) {
+ throw new UncheckedIOException(e);
+ }
+ }
+
public void visitParens(JCParens tree) {
try {
print("(");
--- a/langtools/src/share/classes/com/sun/tools/javac/tree/TreeCopier.java Wed Jul 05 17:56:18 2017 +0200
+++ b/langtools/src/share/classes/com/sun/tools/javac/tree/TreeCopier.java Thu Nov 24 13:36:20 2011 +0000
@@ -271,6 +271,13 @@
return M.at(t.pos).NewClass(encl, typeargs, clazz, args, def);
}
+ public JCTree visitLambdaExpression(LambdaExpressionTree node, P p) {
+ JCLambda t = (JCLambda) node;
+ List<JCVariableDecl> params = copy(t.params, p);
+ JCTree body = copy(t.body, p);
+ return M.at(t.pos).Lambda(params, body);
+ }
+
public JCTree visitParenthesized(ParenthesizedTree node, P p) {
JCParens t = (JCParens) node;
JCExpression expr = copy(t.expr, p);
--- a/langtools/src/share/classes/com/sun/tools/javac/tree/TreeMaker.java Wed Jul 05 17:56:18 2017 +0200
+++ b/langtools/src/share/classes/com/sun/tools/javac/tree/TreeMaker.java Thu Nov 24 13:36:20 2011 +0000
@@ -351,6 +351,14 @@
return tree;
}
+ public JCLambda Lambda(List<JCVariableDecl> params,
+ JCTree body)
+ {
+ JCLambda tree = new JCLambda(params, body);
+ tree.pos = pos;
+ return tree;
+ }
+
public JCParens Parens(JCExpression expr) {
JCParens tree = new JCParens(expr);
tree.pos = pos;
--- a/langtools/src/share/classes/com/sun/tools/javac/tree/TreeScanner.java Wed Jul 05 17:56:18 2017 +0200
+++ b/langtools/src/share/classes/com/sun/tools/javac/tree/TreeScanner.java Thu Nov 24 13:36:20 2011 +0000
@@ -212,6 +212,11 @@
scan(tree.elems);
}
+ public void visitLambda(JCLambda tree) {
+ scan(tree.body);
+ scan(tree.params);
+ }
+
public void visitParens(JCParens tree) {
scan(tree.expr);
}
--- a/langtools/src/share/classes/com/sun/tools/javac/tree/TreeTranslator.java Wed Jul 05 17:56:18 2017 +0200
+++ b/langtools/src/share/classes/com/sun/tools/javac/tree/TreeTranslator.java Thu Nov 24 13:36:20 2011 +0000
@@ -282,6 +282,12 @@
result = tree;
}
+ public void visitLambda(JCLambda tree) {
+ tree.params = translate(tree.params);
+ tree.body = translate(tree.body);
+ result = tree;
+ }
+
public void visitNewArray(JCNewArray tree) {
tree.elemtype = translate(tree.elemtype);
tree.dims = translate(tree.dims);