changeset 56936:22e1747208b6 patterns-deconstruction

Merging patterns-stage-1 into patterns.
author jlahoda
date Mon, 15 Jul 2019 16:02:04 +0200
parents 08eadf898934 201fed33aa8f
children ded304317aee
files src/jdk.compiler/share/classes/com/sun/source/tree/AnyPatternTree.java src/jdk.compiler/share/classes/com/sun/source/tree/DeconstructionPatternTree.java 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
diffstat 10 files changed, 72 insertions(+), 75 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/source/tree/AnyPatternTree.java	Mon Jul 15 13:29:39 2019 +0200
+++ b/src/jdk.compiler/share/classes/com/sun/source/tree/AnyPatternTree.java	Mon Jul 15 16:02:04 2019 +0200
@@ -30,4 +30,3 @@
  */
 public interface AnyPatternTree extends PatternTree {
 }
-
--- a/src/jdk.compiler/share/classes/com/sun/source/tree/BindingPatternTree.java	Mon Jul 15 13:29:39 2019 +0200
+++ b/src/jdk.compiler/share/classes/com/sun/source/tree/BindingPatternTree.java	Mon Jul 15 16:02:04 2019 +0200
@@ -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/DeconstructionPatternTree.java	Mon Jul 15 13:29:39 2019 +0200
+++ b/src/jdk.compiler/share/classes/com/sun/source/tree/DeconstructionPatternTree.java	Mon Jul 15 16:02:04 2019 +0200
@@ -48,7 +48,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 13:29:39 2019 +0200
+++ b/src/jdk.compiler/share/classes/com/sun/source/tree/PatternTree.java	Mon Jul 15 16:02:04 2019 +0200
@@ -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 13:29:39 2019 +0200
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/code/TypeAnnotations.java	Mon Jul 15 16:02:04 2019 +0200
@@ -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 13:29:39 2019 +0200
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java	Mon Jul 15 16:02:04 2019 +0200
@@ -126,6 +126,7 @@
     final Annotate annotate;
     final ArgumentAttr argumentAttr;
     final ClassReader reader;
+    final MatchBindingsComputer matchBindingsComputer;
 
     public static Attr instance(Context context) {
         Attr instance = context.get(attrKey);
@@ -162,6 +163,7 @@
         dependencies = Dependencies.instance(context);
         argumentAttr = ArgumentAttr.instance(context);
         reader = ClassReader.instance(context);
+        matchBindingsComputer = MatchBindingsComputer.instance(context);
 
         Options options = Options.instance(context);
 
@@ -1338,7 +1340,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);
@@ -1349,14 +1351,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);
@@ -1378,7 +1380,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 {
@@ -1394,7 +1396,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);
@@ -1614,7 +1616,7 @@
                                 }
                                 break;
                         }
-                        matchBindings = getMatchBindings(types, log, pat, true, matchBindings);
+                        matchBindings = matchBindingsComputer.getMatchBindings(pat, true, matchBindings);
                     }
                 } else {
                     if (hasDefault) {
@@ -1622,7 +1624,7 @@
                     } else {
                         hasDefault = true;
                     }
-                    matchBindings = getMatchBindings(types, log, null, true, matchBindings);
+                    matchBindings = matchBindingsComputer.getMatchBindings(null, true, matchBindings);
                 }
 
                 Env<AttrContext> caseEnv = bindingEnv(switchEnv, matchBindings);
@@ -1802,7 +1804,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 {
@@ -1810,7 +1812,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 {
@@ -1981,7 +1983,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 {
@@ -1993,7 +1995,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);
@@ -3724,10 +3726,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();
@@ -5710,12 +5712,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 13:29:39 2019 +0200
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Flow.java	Mon Jul 15 16:02:04 2019 +0200
@@ -262,7 +262,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
@@ -279,7 +279,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 13:29:39 2019 +0200
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/MatchBindingsComputer.java	Mon Jul 15 16:02:04 2019 +0200
@@ -40,24 +40,45 @@
 import com.sun.tools.javac.tree.JCTree.JCDeconstructionPattern;
 import com.sun.tools.javac.tree.JCTree.JCPattern;
 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;
+    }
+
+    public List<BindingSymbol> getMatchBindings(JCTree expression, boolean whenTrue, List<BindingSymbol> intersectWith) {
+        List<BindingSymbol> bindings = getMatchBindings(expression, whenTrue);
+        if (intersectWith != null) {
+            bindings = intersection(expression, intersectWith, bindings);
+        }
+        return bindings;
     }
 
     @Override
@@ -198,14 +219,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 13:29:39 2019 +0200
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TransPatterns.java	Mon Jul 15 16:02:04 2019 +0200
@@ -102,16 +102,17 @@
         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 Resolve rs;
+    private final Target target;
+    private final MatchBindingsComputer matchBindingsComputer;
     private TreeMaker make;
     private Env<AttrContext> env;
-    private Types types;
-    private Operators operators;
-    private Log log;
-    private ConstFold constFold;
-    private Names names;
-    private Resolve rs;
-    private Target target;
 
     BindingContext bindingContext = new BindingContext() {
         @Override
@@ -163,6 +164,7 @@
         names = Names.instance(context);
         rs = Resolve.instance(context);
         target = Target.instance(context);
+        matchBindingsComputer = MatchBindingsComputer.instance(context);
         debugTransPatterns = Options.instance(context).isSet("debug.patterns");
     }
 
@@ -529,10 +531,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();
@@ -562,8 +564,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);
@@ -833,8 +835,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);
@@ -854,13 +856,7 @@
             this.parent = bindingContext;
             this.hoistedVarMap = matchBindings.stream()
                     .filter(v -> parent.getBindingFor(v) == null)
-<<<<<<< working copy
-                    .collect(Collectors.toMap(v -> v, v -> new VarSymbol(v.flags() & ~Flags.MATCH_BINDING, v.name.append(names.fromString("$binding")), v.type, v.owner)));
-||||||| base
-                    .collect(Collectors.toMap(v -> v, v -> new VarSymbol(v.flags(), v.name.append(names.fromString("$binding")), v.type, v.owner)));
-=======
-                    .collect(Collectors.toMap(v -> v, v -> new VarSymbol(v.flags(), v.name, v.type, v.owner)));
->>>>>>> merge rev
+                    .collect(Collectors.toMap(v -> v, v -> new VarSymbol(v.flags() & ~Flags.MATCH_BINDING, v.name, v.type, v.owner)));
         }
 
         @Override
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/Pretty.java	Mon Jul 15 13:29:39 2019 +0200
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/Pretty.java	Mon Jul 15 16:02:04 2019 +0200
@@ -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 {