changeset 56934:f3e3e9e47ade patterns

Automatic merge with patterns-stage-1
author mcimadamore
date Mon, 15 Jul 2019 12:50:29 +0000
parents c487001dbbbc cba20f435610
children 201fed33aa8f
files src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Flow.java src/jdk.compiler/share/classes/com/sun/tools/javac/comp/MatchBindingsComputer.java src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TransPatterns.java src/jdk.compiler/share/classes/com/sun/tools/javac/tree/Pretty.java
diffstat 8 files changed, 59 insertions(+), 63 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/source/tree/BindingPatternTree.java	Mon Jul 15 12:52:33 2019 +0200
+++ b/src/jdk.compiler/share/classes/com/sun/source/tree/BindingPatternTree.java	Mon Jul 15 12:50:29 2019 +0000
@@ -40,7 +40,7 @@
 
     /**
      * A binding variable name.
-     * @return something
+     * @return the name of the binding variable
      */
     Name getBinding();
 
--- a/src/jdk.compiler/share/classes/com/sun/source/tree/PatternTree.java	Mon Jul 15 12:52:33 2019 +0200
+++ b/src/jdk.compiler/share/classes/com/sun/source/tree/PatternTree.java	Mon Jul 15 12:50:29 2019 +0000
@@ -25,11 +25,8 @@
 
 package com.sun.source.tree;
 
-import javax.lang.model.element.Name;
-
 /**
- * A super-type for all the patterns.
+ * A tree node used as the base class for the different kinds of
+ * statements.
  */
-public interface PatternTree extends Tree {
-
-}
+public interface PatternTree extends Tree {}
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/code/TypeAnnotations.java	Mon Jul 15 12:52:33 2019 +0200
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/code/TypeAnnotations.java	Mon Jul 15 12:50:29 2019 +0000
@@ -1067,6 +1067,13 @@
                                         outer_type_index, location);
                 }
 
+                case BINDING_PATTERN: {
+                    final List<JCTree> newPath = path.tail;
+                    return resolveFrame(newPath.head, newPath.tail.head,
+                                        newPath, currentLambda,
+                                        outer_type_index, location);
+                }
+
                 default:
                     throw new AssertionError("Unresolved frame: " + frame +
                                              " of kind: " + frame.getKind() +
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java	Mon Jul 15 12:52:33 2019 +0200
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java	Mon Jul 15 12:50:29 2019 +0000
@@ -123,6 +123,7 @@
     final Dependencies dependencies;
     final Annotate annotate;
     final ArgumentAttr argumentAttr;
+    final MatchBindingsComputer matchBindingsComputer;
 
     public static Attr instance(Context context) {
         Attr instance = context.get(attrKey);
@@ -158,6 +159,7 @@
         typeEnvs = TypeEnvs.instance(context);
         dependencies = Dependencies.instance(context);
         argumentAttr = ArgumentAttr.instance(context);
+        matchBindingsComputer = MatchBindingsComputer.instance(context);
 
         Options options = Options.instance(context);
 
@@ -1325,7 +1327,7 @@
         attribStat(tree.body, env.dup(tree));
         attribExpr(tree.cond, env, syms.booleanType);
         if (!breaksOutOf(tree, tree.body)) {
-            List<BindingSymbol> bindings = getMatchBindings(types, log, tree.cond, false);
+            List<BindingSymbol> bindings = matchBindingsComputer.getMatchBindings(tree.cond, false);
 
             bindings.forEach(env.info.scope::enter);
             bindings.forEach(BindingSymbol::preserveBinding);
@@ -1336,14 +1338,14 @@
     public void visitWhileLoop(JCWhileLoop tree) {
         attribExpr(tree.cond, env, syms.booleanType);
         // include x.T in while's body
-        Env<AttrContext> whileEnv = bindingEnv(env, getMatchBindings(types, log, tree.cond, true));
+        Env<AttrContext> whileEnv = bindingEnv(env, matchBindingsComputer.getMatchBindings(tree.cond, true));
         try {
             attribStat(tree.body, whileEnv.dup(tree));
         } finally {
             whileEnv.info.scope.leave();
         }
         if (!breaksOutOf(tree, tree.body)) {
-            List<BindingSymbol> bindings = getMatchBindings(types, log, tree.cond, false);
+            List<BindingSymbol> bindings = matchBindingsComputer.getMatchBindings(tree.cond, false);
 
             bindings.forEach(env.info.scope::enter);
             bindings.forEach(BindingSymbol::preserveBinding);
@@ -1365,7 +1367,7 @@
             if (tree.cond != null) {
                 attribExpr(tree.cond, loopEnv, syms.booleanType);
                 // include x.T in the evaluation scopes of body & step.
-                matchBindings = getMatchBindings(types, log, tree.cond, true);
+                matchBindings = matchBindingsComputer.getMatchBindings(tree.cond, true);
             }
             Env<AttrContext> bodyEnv = bindingEnv(loopEnv, matchBindings);
             try {
@@ -1381,7 +1383,7 @@
             loopEnv.info.scope.leave(); // all injected match bindings vanish here.
         }
         if (!breaksOutOf(tree, tree.body)) {
-            List<BindingSymbol> bindings = getMatchBindings(types, log, tree.cond, false);
+            List<BindingSymbol> bindings = matchBindingsComputer.getMatchBindings(tree.cond, false);
 
             bindings.forEach(env.info.scope::enter);
             bindings.forEach(BindingSymbol::preserveBinding);
@@ -1789,7 +1791,7 @@
         */
 
         Type truetype;
-        Env<AttrContext> trueEnv = bindingEnv(env, getMatchBindings(types, log, tree.cond, true));
+        Env<AttrContext> trueEnv = bindingEnv(env, matchBindingsComputer.getMatchBindings(tree.cond, true));
         try {
             truetype = attribTree(tree.truepart, trueEnv, condInfo);
         } finally {
@@ -1797,7 +1799,7 @@
         }
 
         Type falsetype;
-        Env<AttrContext> falseEnv = bindingEnv(env, getMatchBindings(types, log, tree.cond, false));
+        Env<AttrContext> falseEnv = bindingEnv(env, matchBindingsComputer.getMatchBindings(tree.cond, false));
         try {
             falsetype = attribTree(tree.falsepart, falseEnv, condInfo);
         } finally {
@@ -1968,7 +1970,7 @@
 
         // if (x) { y } [ else z ] include x.T in y; include x.F in z
 
-        List<BindingSymbol> thenBindings = getMatchBindings(types, log, tree.cond, true);
+        List<BindingSymbol> thenBindings = matchBindingsComputer.getMatchBindings(tree.cond, true);
         Env<AttrContext> thenEnv = bindingEnv(env, thenBindings);
 
         try {
@@ -1980,7 +1982,7 @@
         preFlow(tree.thenpart);
         boolean aliveAfterThen = flow.aliveAfter(env, tree.thenpart, make);
         boolean aliveAfterElse;
-        List<BindingSymbol> elseBindings = getMatchBindings(types, log, tree.cond, false);
+        List<BindingSymbol> elseBindings = matchBindingsComputer.getMatchBindings(tree.cond, false);
 
         if (tree.elsepart != null) {
             Env<AttrContext> elseEnv = bindingEnv(env, elseBindings);
@@ -3711,10 +3713,10 @@
         List<BindingSymbol> matchBindings;
         switch (tree.getTag()) {
             case AND:
-                matchBindings = getMatchBindings(types, log, tree.lhs, true);
+                matchBindings = matchBindingsComputer.getMatchBindings(tree.lhs, true);
                 break;
             case OR:
-                matchBindings = getMatchBindings(types, log, tree.lhs, false);
+                matchBindings = matchBindingsComputer.getMatchBindings(tree.lhs, false);
                 break;
             default:
                 matchBindings = List.nil();
@@ -5598,12 +5600,4 @@
         }.scan(pid);
     }
 
-
-    public static List<BindingSymbol> getMatchBindings(Types types, Log log, JCTree expression, boolean whenTrue) {
-        return getMatchBindings(types, log, expression, whenTrue, null);
-    }
-
-    public static List<BindingSymbol> getMatchBindings(Types types, Log log, JCTree expression, boolean whenTrue, List<BindingSymbol> intersectWith) {
-        return new MatchBindingsComputer(types, log, expression, whenTrue).getBindings(intersectWith);
-    }
 }
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Flow.java	Mon Jul 15 12:52:33 2019 +0200
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Flow.java	Mon Jul 15 12:50:29 2019 +0000
@@ -258,7 +258,7 @@
 
     public boolean aliveAfter(Env<AttrContext> env, JCTree that, TreeMaker make) {
         //we need to disable diagnostics temporarily; the problem is that if
-        //a lambda expression contains e.g. an unreachable statement, an error
+        //"that" contains e.g. an unreachable statement, an error
         //message will be reported and will cause compilation to skip the flow analyis
         //step - if we suppress diagnostics, we won't stop at Attr for flow-analysis
         //related errors, which will allow for more errors to be detected
@@ -275,7 +275,7 @@
 
     public boolean breaksOutOf(Env<AttrContext> env, JCTree loop, JCTree body, TreeMaker make) {
         //we need to disable diagnostics temporarily; the problem is that if
-        //a lambda expression contains e.g. an unreachable statement, an error
+        //"that" contains e.g. an unreachable statement, an error
         //message will be reported and will cause compilation to skip the flow analyis
         //step - if we suppress diagnostics, we won't stop at Attr for flow-analysis
         //related errors, which will allow for more errors to be detected
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/MatchBindingsComputer.java	Mon Jul 15 12:52:33 2019 +0200
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/MatchBindingsComputer.java	Mon Jul 15 12:50:29 2019 +0000
@@ -38,24 +38,37 @@
 import com.sun.tools.javac.tree.JCTree.JCUnary;
 import com.sun.tools.javac.tree.JCTree.JCBindingPattern;
 import com.sun.tools.javac.tree.TreeScanner;
+import com.sun.tools.javac.util.Context;
 import com.sun.tools.javac.util.List;
 import com.sun.tools.javac.util.Log;
 import com.sun.tools.javac.util.Name;
 
 
 public class MatchBindingsComputer extends TreeScanner {
+    protected static final Context.Key<MatchBindingsComputer> matchBindingsComputerKey = new Context.Key<>();
 
-    private final JCTree tree;
     private final Log log;
     private final Types types;
     boolean whenTrue;
     List<BindingSymbol> bindings;
 
-    public MatchBindingsComputer(Types types, Log log, JCTree tree, boolean whenTrue) {
-        this.tree = tree;
+    public static MatchBindingsComputer instance(Context context) {
+        MatchBindingsComputer instance = context.get(matchBindingsComputerKey);
+        if (instance == null)
+            instance = new MatchBindingsComputer(context);
+        return instance;
+    }
+
+    protected MatchBindingsComputer(Context context) {
+        this.log = Log.instance(context);
+        this.types = Types.instance(context);
+    }
+
+    public List<BindingSymbol> getMatchBindings(JCTree expression, boolean whenTrue) {
         this.whenTrue = whenTrue;
-        this.log = log;
-        this.types = types;
+        this.bindings = List.nil();
+        scan(expression);
+        return bindings;
     }
 
     @Override
@@ -186,14 +199,6 @@
         super.scan(tree);
     }
 
-    public List<BindingSymbol> getBindings(List<BindingSymbol> intersectWith) {
-        scan(tree);
-        if (intersectWith != null) {
-            bindings = intersection(tree, intersectWith, bindings);
-        }
-        return bindings;
-    }
-
     public static class BindingSymbol extends VarSymbol {
 
         public BindingSymbol(Name name, Type type, Symbol owner) {
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TransPatterns.java	Mon Jul 15 12:52:33 2019 +0200
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TransPatterns.java	Mon Jul 15 12:50:29 2019 +0000
@@ -93,14 +93,15 @@
         return instance;
     }
 
-    private Symtab syms;
+    private final Symtab syms;
+    private final Types types;
+    private final Operators operators;
+    private final Log log;
+    private final ConstFold constFold;
+    private final Names names;
+    private final Target target;
+    private final MatchBindingsComputer matchBindingsComputer;
     private TreeMaker make;
-    private Types types;
-    private Operators operators;
-    private Log log;
-    private ConstFold constFold;
-    private Names names;
-    private Target target;
 
     BindingContext bindingContext = new BindingContext() {
         @Override
@@ -146,6 +147,7 @@
         constFold = ConstFold.instance(context);
         names = Names.instance(context);
         target = Target.instance(context);
+        matchBindingsComputer = MatchBindingsComputer.instance(context);
         debugTransPatterns = Options.instance(context).isSet("debug.patterns");
     }
 
@@ -206,10 +208,10 @@
         List<BindingSymbol> matchBindings;
         switch (tree.getTag()) {
             case AND:
-                matchBindings = Attr.getMatchBindings(types, log, tree.lhs, true);
+                matchBindings = matchBindingsComputer.getMatchBindings(tree.lhs, true);
                 break;
             case OR:
-                matchBindings = Attr.getMatchBindings(types, log, tree.lhs, false);
+                matchBindings = matchBindingsComputer.getMatchBindings(tree.lhs, false);
                 break;
             default:
                 matchBindings = List.nil();
@@ -239,8 +241,8 @@
     @Override
     public void visitConditional(JCConditional tree) {
         bindingContext = new BasicBindingContext(
-                Attr.getMatchBindings(types, log, tree.cond, true)
-                        .appendList(Attr.getMatchBindings(types, log, tree.cond, false)));
+                matchBindingsComputer.getMatchBindings(tree.cond, true)
+                        .appendList(matchBindingsComputer.getMatchBindings(tree.cond, false)));
         try {
             super.visitConditional(tree);
             result = bindingContext.decorateExpression(tree);
@@ -464,8 +466,8 @@
     }
 
     private List<BindingSymbol> getMatchBindings(JCExpression cond) {
-        return Attr.getMatchBindings(types, log, cond, true)
-                        .appendList(Attr.getMatchBindings(types, log, cond, false));
+        return matchBindingsComputer.getMatchBindings(cond, true)
+                        .appendList(matchBindingsComputer.getMatchBindings(cond, false));
     }
     abstract class BindingContext {
         abstract VarSymbol getBindingFor(BindingSymbol varSymbol);
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/Pretty.java	Mon Jul 15 12:52:33 2019 +0200
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/Pretty.java	Mon Jul 15 12:50:29 2019 +0000
@@ -243,15 +243,6 @@
         printExpr(tree);
     }
 
-    public <T extends JCTree> void printPatterns(List<T> trees) throws IOException {
-        if (trees.nonEmpty()) {
-            printPattern(trees.head);
-            for (List<T> l = trees.tail; l.nonEmpty(); l = l.tail) {
-                print(", ");
-                printPattern(l.head);
-            }
-        }
-    }
     /** Derived visitor method: print list of statements, each on a separate line.
      */
     public void printStats(List<? extends JCTree> trees) throws IOException {