7115046: Add AST node for lambda expressions

Thu, 24 Nov 2011 13:36:20 +0000

author
mcimadamore
date
Thu, 24 Nov 2011 13:36:20 +0000
changeset 1142
c896d95e7469
parent 1140
07599bd780ca
child 1143
ec59a2ce9114

7115046: Add AST node for lambda expressions
Summary: Add tree nodes for representing lambda expressions and update relevant visitors interfaces
Reviewed-by: jjg

src/share/classes/com/sun/source/tree/LambdaExpressionTree.java file | annotate | diff | comparison | revisions
src/share/classes/com/sun/source/tree/Tree.java file | annotate | diff | comparison | revisions
src/share/classes/com/sun/source/tree/TreeVisitor.java file | annotate | diff | comparison | revisions
src/share/classes/com/sun/source/util/SimpleTreeVisitor.java file | annotate | diff | comparison | revisions
src/share/classes/com/sun/source/util/TreeScanner.java file | annotate | diff | comparison | revisions
src/share/classes/com/sun/tools/javac/tree/JCTree.java file | annotate | diff | comparison | revisions
src/share/classes/com/sun/tools/javac/tree/Pretty.java file | annotate | diff | comparison | revisions
src/share/classes/com/sun/tools/javac/tree/TreeCopier.java file | annotate | diff | comparison | revisions
src/share/classes/com/sun/tools/javac/tree/TreeMaker.java file | annotate | diff | comparison | revisions
src/share/classes/com/sun/tools/javac/tree/TreeScanner.java file | annotate | diff | comparison | revisions
src/share/classes/com/sun/tools/javac/tree/TreeTranslator.java file | annotate | diff | comparison | revisions
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/com/sun/source/tree/LambdaExpressionTree.java	Thu Nov 24 13:36:20 2011 +0000
     1.3 @@ -0,0 +1,56 @@
     1.4 +/*
     1.5 + * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.source.tree;
    1.30 +
    1.31 +import java.util.List;
    1.32 +
    1.33 +/**
    1.34 + * A tree node for a lambda expression.
    1.35 + *
    1.36 + * For example:
    1.37 + * <pre>
    1.38 + *   ()->{}
    1.39 + *   (List<String> ls)->ls.size()
    1.40 + *   (x,y)-> { return x + y; }
    1.41 + * </pre>
    1.42 + */
    1.43 +public interface LambdaExpressionTree extends ExpressionTree {
    1.44 +
    1.45 +    /**
    1.46 +     * Lambda expressions come in two forms: (i) expression lambdas, whose body
    1.47 +     * is an expression, and (ii) statement lambdas, whose body is a block
    1.48 +     */
    1.49 +    public enum BodyKind {
    1.50 +        /** enum constant for expression lambdas */
    1.51 +        EXPRESSION,
    1.52 +        /** enum constant for statement lambdas */
    1.53 +        STATEMENT;
    1.54 +    }
    1.55 +
    1.56 +    List<? extends VariableTree> getParameters();
    1.57 +    Tree getBody();
    1.58 +    BodyKind getBodyKind();
    1.59 +}
     2.1 --- a/src/share/classes/com/sun/source/tree/Tree.java	Sat Nov 19 15:54:04 2011 -0800
     2.2 +++ b/src/share/classes/com/sun/source/tree/Tree.java	Thu Nov 24 13:36:20 2011 +0000
     2.3 @@ -187,6 +187,11 @@
     2.4          NEW_CLASS(NewClassTree.class),
     2.5  
     2.6          /**
     2.7 +         * Used for instances of {@link LambdaExpressionTree}.
     2.8 +         */
     2.9 +        LAMBDA_EXPRESSION(LambdaExpressionTree.class),
    2.10 +
    2.11 +        /**
    2.12           * Used for instances of {@link ParenthesizedTree}.
    2.13           */
    2.14          PARENTHESIZED(ParenthesizedTree.class),
     3.1 --- a/src/share/classes/com/sun/source/tree/TreeVisitor.java	Sat Nov 19 15:54:04 2011 -0800
     3.2 +++ b/src/share/classes/com/sun/source/tree/TreeVisitor.java	Thu Nov 24 13:36:20 2011 +0000
     3.3 @@ -85,6 +85,7 @@
     3.4      R visitModifiers(ModifiersTree node, P p);
     3.5      R visitNewArray(NewArrayTree node, P p);
     3.6      R visitNewClass(NewClassTree node, P p);
     3.7 +    R visitLambdaExpression(LambdaExpressionTree node, P p);
     3.8      R visitParenthesized(ParenthesizedTree node, P p);
     3.9      R visitReturn(ReturnTree node, P p);
    3.10      R visitMemberSelect(MemberSelectTree node, P p);
     4.1 --- a/src/share/classes/com/sun/source/util/SimpleTreeVisitor.java	Sat Nov 19 15:54:04 2011 -0800
     4.2 +++ b/src/share/classes/com/sun/source/util/SimpleTreeVisitor.java	Thu Nov 24 13:36:20 2011 +0000
     4.3 @@ -172,6 +172,10 @@
     4.4          return defaultAction(node, p);
     4.5      }
     4.6  
     4.7 +    public R visitLambdaExpression(LambdaExpressionTree node, P p) {
     4.8 +        return defaultAction(node, p);
     4.9 +    }
    4.10 +
    4.11      public R visitParenthesized(ParenthesizedTree node, P p) {
    4.12          return defaultAction(node, p);
    4.13      }
     5.1 --- a/src/share/classes/com/sun/source/util/TreeScanner.java	Sat Nov 19 15:54:04 2011 -0800
     5.2 +++ b/src/share/classes/com/sun/source/util/TreeScanner.java	Thu Nov 24 13:36:20 2011 +0000
     5.3 @@ -285,6 +285,12 @@
     5.4          return r;
     5.5      }
     5.6  
     5.7 +    public R visitLambdaExpression(LambdaExpressionTree node, P p) {
     5.8 +        R r = scan(node.getParameters(), p);
     5.9 +        r = scanAndReduce(node.getBody(), p, r);
    5.10 +        return r;
    5.11 +    }
    5.12 +
    5.13      public R visitParenthesized(ParenthesizedTree node, P p) {
    5.14          return scan(node.getExpression(), p);
    5.15      }
     6.1 --- a/src/share/classes/com/sun/tools/javac/tree/JCTree.java	Sat Nov 19 15:54:04 2011 -0800
     6.2 +++ b/src/share/classes/com/sun/tools/javac/tree/JCTree.java	Thu Nov 24 13:36:20 2011 +0000
     6.3 @@ -41,6 +41,7 @@
     6.4  import com.sun.tools.javac.code.Symbol.*;
     6.5  import com.sun.tools.javac.parser.EndPosTable;
     6.6  import com.sun.source.tree.*;
     6.7 +import com.sun.source.tree.LambdaExpressionTree.BodyKind;
     6.8  
     6.9  import static com.sun.tools.javac.code.BoundKind.*;
    6.10  import static com.sun.tools.javac.tree.JCTree.Tag.*;
    6.11 @@ -198,6 +199,10 @@
    6.12           */
    6.13          NEWARRAY,
    6.14  
    6.15 +        /** Lambda expression, of type Lambda.
    6.16 +         */
    6.17 +        LAMBDA,
    6.18 +
    6.19          /** Parenthesized subexpressions, of type Parens.
    6.20           */
    6.21          PARENS,
    6.22 @@ -1487,6 +1492,56 @@
    6.23      }
    6.24  
    6.25      /**
    6.26 +     * A lambda expression.
    6.27 +     */
    6.28 +    public static class JCLambda extends JCExpression implements LambdaExpressionTree {
    6.29 +
    6.30 +        public List<JCVariableDecl> params;
    6.31 +        public JCTree body;
    6.32 +        public Type targetType;
    6.33 +        public boolean canCompleteNormally = true;
    6.34 +        public List<Type> inferredThrownTypes;
    6.35 +
    6.36 +        public JCLambda(List<JCVariableDecl> params,
    6.37 +                        JCTree body) {
    6.38 +            this.params = params;
    6.39 +            this.body = body;
    6.40 +        }
    6.41 +        @Override
    6.42 +        public Tag getTag() {
    6.43 +            return LAMBDA;
    6.44 +        }
    6.45 +        @Override
    6.46 +        public void accept(Visitor v) {
    6.47 +            v.visitLambda(this);
    6.48 +        }
    6.49 +        @Override
    6.50 +        public <R, D> R accept(TreeVisitor<R, D> v, D d) {
    6.51 +            return v.visitLambdaExpression(this, d);
    6.52 +        }
    6.53 +        public Kind getKind() {
    6.54 +            return Kind.LAMBDA_EXPRESSION;
    6.55 +        }
    6.56 +        public JCTree getBody() {
    6.57 +            return body;
    6.58 +        }
    6.59 +        public java.util.List<? extends VariableTree> getParameters() {
    6.60 +            return params;
    6.61 +        }
    6.62 +        @Override
    6.63 +        public JCLambda setType(Type type) {
    6.64 +            super.setType(type);
    6.65 +            return this;
    6.66 +        }
    6.67 +        @Override
    6.68 +        public BodyKind getBodyKind() {
    6.69 +            return body.hasTag(BLOCK) ?
    6.70 +                    BodyKind.STATEMENT :
    6.71 +                    BodyKind.EXPRESSION;
    6.72 +        }
    6.73 +    }
    6.74 +
    6.75 +    /**
    6.76       * A parenthesized subexpression ( ... )
    6.77       */
    6.78      public static class JCParens extends JCExpression implements ParenthesizedTree {
    6.79 @@ -2271,6 +2326,7 @@
    6.80          public void visitApply(JCMethodInvocation that)      { visitTree(that); }
    6.81          public void visitNewClass(JCNewClass that)           { visitTree(that); }
    6.82          public void visitNewArray(JCNewArray that)           { visitTree(that); }
    6.83 +        public void visitLambda(JCLambda that)               { visitTree(that); }
    6.84          public void visitParens(JCParens that)               { visitTree(that); }
    6.85          public void visitAssign(JCAssign that)               { visitTree(that); }
    6.86          public void visitAssignop(JCAssignOp that)           { visitTree(that); }
     7.1 --- a/src/share/classes/com/sun/tools/javac/tree/Pretty.java	Sat Nov 19 15:54:04 2011 -0800
     7.2 +++ b/src/share/classes/com/sun/tools/javac/tree/Pretty.java	Thu Nov 24 13:36:20 2011 +0000
     7.3 @@ -907,6 +907,17 @@
     7.4          }
     7.5      }
     7.6  
     7.7 +    public void visitLambda(JCLambda tree) {
     7.8 +        try {
     7.9 +            print("(");
    7.10 +            printExprs(tree.params);
    7.11 +            print(")->");
    7.12 +            printExpr(tree.body);
    7.13 +        } catch (IOException e) {
    7.14 +            throw new UncheckedIOException(e);
    7.15 +        }
    7.16 +    }
    7.17 +
    7.18      public void visitParens(JCParens tree) {
    7.19          try {
    7.20              print("(");
     8.1 --- a/src/share/classes/com/sun/tools/javac/tree/TreeCopier.java	Sat Nov 19 15:54:04 2011 -0800
     8.2 +++ b/src/share/classes/com/sun/tools/javac/tree/TreeCopier.java	Thu Nov 24 13:36:20 2011 +0000
     8.3 @@ -271,6 +271,13 @@
     8.4          return M.at(t.pos).NewClass(encl, typeargs, clazz, args, def);
     8.5      }
     8.6  
     8.7 +    public JCTree visitLambdaExpression(LambdaExpressionTree node, P p) {
     8.8 +        JCLambda t = (JCLambda) node;
     8.9 +        List<JCVariableDecl> params = copy(t.params, p);
    8.10 +        JCTree body = copy(t.body, p);
    8.11 +        return M.at(t.pos).Lambda(params, body);
    8.12 +    }
    8.13 +
    8.14      public JCTree visitParenthesized(ParenthesizedTree node, P p) {
    8.15          JCParens t = (JCParens) node;
    8.16          JCExpression expr = copy(t.expr, p);
     9.1 --- a/src/share/classes/com/sun/tools/javac/tree/TreeMaker.java	Sat Nov 19 15:54:04 2011 -0800
     9.2 +++ b/src/share/classes/com/sun/tools/javac/tree/TreeMaker.java	Thu Nov 24 13:36:20 2011 +0000
     9.3 @@ -351,6 +351,14 @@
     9.4          return tree;
     9.5      }
     9.6  
     9.7 +    public JCLambda Lambda(List<JCVariableDecl> params,
     9.8 +                           JCTree body)
     9.9 +    {
    9.10 +        JCLambda tree = new JCLambda(params, body);
    9.11 +        tree.pos = pos;
    9.12 +        return tree;
    9.13 +    }
    9.14 +
    9.15      public JCParens Parens(JCExpression expr) {
    9.16          JCParens tree = new JCParens(expr);
    9.17          tree.pos = pos;
    10.1 --- a/src/share/classes/com/sun/tools/javac/tree/TreeScanner.java	Sat Nov 19 15:54:04 2011 -0800
    10.2 +++ b/src/share/classes/com/sun/tools/javac/tree/TreeScanner.java	Thu Nov 24 13:36:20 2011 +0000
    10.3 @@ -212,6 +212,11 @@
    10.4          scan(tree.elems);
    10.5      }
    10.6  
    10.7 +    public void visitLambda(JCLambda tree) {
    10.8 +        scan(tree.body);
    10.9 +        scan(tree.params);
   10.10 +    }
   10.11 +
   10.12      public void visitParens(JCParens tree) {
   10.13          scan(tree.expr);
   10.14      }
    11.1 --- a/src/share/classes/com/sun/tools/javac/tree/TreeTranslator.java	Sat Nov 19 15:54:04 2011 -0800
    11.2 +++ b/src/share/classes/com/sun/tools/javac/tree/TreeTranslator.java	Thu Nov 24 13:36:20 2011 +0000
    11.3 @@ -282,6 +282,12 @@
    11.4          result = tree;
    11.5      }
    11.6  
    11.7 +    public void visitLambda(JCLambda tree) {
    11.8 +        tree.params = translate(tree.params);
    11.9 +        tree.body = translate(tree.body);
   11.10 +        result = tree;
   11.11 +    }
   11.12 +
   11.13      public void visitNewArray(JCNewArray tree) {
   11.14          tree.elemtype = translate(tree.elemtype);
   11.15          tree.dims = translate(tree.dims);

mercurial