changeset 2997:9a9deb54d415

Fix: tweak heuristics to detect cross-specialization boundaries * generalize SpecializeTypes translator so that all expression unerased types are retained * add more tests
author mcimadamore
date Sat, 06 Jun 2015 02:25:12 +0100
parents 156d2dfb901f
children a0d7ac464e8b
files src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java src/jdk.compiler/share/classes/com/sun/tools/javac/comp/SpecializeTypes.java test/tools/javac/valhalla/typespec/SpecializedAccessors.java test/tools/javac/valhalla/typespec/SpecializedAccessors2.java test/tools/javac/valhalla/typespec/SpecializedAccessors2Aux.java test/tools/javac/valhalla/typespec/SpecializedAccessors3.java test/tools/javac/valhalla/typespec/SpecializedAccessors4.java
diffstat 7 files changed, 176 insertions(+), 41 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java	Fri Jun 05 15:03:30 2015 +0100
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java	Sat Jun 06 02:25:12 2015 +0100
@@ -1088,9 +1088,9 @@
 
     /** Do we need an access method to reference private symbol?
      */
-    boolean needsPrivateAccess(Symbol sym) {
+    boolean needsPrivateAccess(Symbol sym, JCTree tree) {
         if ((sym.flags() & PRIVATE) == 0 ||
-                (sym.owner == currentClass && !crossSpecializationBoundaries(sym))) {
+                (sym.owner == currentClass && !crossSpecializationBoundaries(sym, tree))) {
             return false;
         } else if (sym.name == names.init && sym.owner.isLocal()) {
             // private constructor in local class: relax protection
@@ -1105,7 +1105,7 @@
      */
     boolean needsProtectedAccess(Symbol sym, JCTree tree) {
         if ((sym.flags() & PROTECTED) == 0 ||
-            (sym.owner.owner == currentClass.owner && !crossSpecializationBoundaries(sym)) || // fast special case
+            (sym.owner.owner == currentClass.owner && !crossSpecializationBoundaries(sym, tree)) || // fast special case
             sym.packge() == currentClass.packge())
             return false;
         if (!currentClass.isSubClass(sym.owner, types))
@@ -1117,10 +1117,15 @@
         return !((JCFieldAccess) tree).selected.type.tsym.isSubClass(currentClass, types);
     }
 
-    boolean crossSpecializationBoundaries(Symbol sym) {
+    boolean crossSpecializationBoundaries(Symbol sym, JCTree base) {
+        Type rec = base != null && base.hasTag(SELECT) ?
+                    ((JCFieldAccess)base).selected.unerasedType :
+                    null;
         return sym.isStatic() ?
                 types.needsMangling(currentClass.type) :
-                !types.isSameType(currentClass.type, sym.specialized().owner.type);
+                (types.needsMangling(sym.owner.type) &&
+                        !types.isSameType(currentClass.type,
+                        rec == null ? sym.owner.type : types.asSuper(rec, sym.owner)));
     }
 
     /** The class in which an access method for given symbol goes.
@@ -1217,9 +1222,9 @@
                 //  - protected members in a superclass of an
                 //    enclosing class contained in another package.
                 //  - all non-private members accessed via a qualified super.
-                boolean protAccess = refSuper && !needsPrivateAccess(sym)
+                boolean protAccess = refSuper && !needsPrivateAccess(sym, tree)
                     || needsProtectedAccess(sym, tree);
-                boolean accReq = protAccess || needsPrivateAccess(sym);
+                boolean accReq = protAccess || needsPrivateAccess(sym, tree);
 
                 // A base has to be supplied for
                 //  - simple identifiers accessing variables in outer classes.
@@ -1265,7 +1270,11 @@
                             base != null ? base : make.QualIdent(access.owner),
                             access);
 
-                        return make.App(receiver, args).setType(access.erasure(types).getReturnType());
+                        JCMethodInvocation app = make.App(receiver, args).setType(access.erasure(types).getReturnType());
+                        app.unerasedType = tree.unerasedType != null && tree.unerasedType.hasTag(METHOD) ?
+                                tree.unerasedType.getReturnType() :
+                                tree.unerasedType;
+                        return app;
 
                     // Other accesses to members of outer classes get a
                     // qualifier.
@@ -1301,7 +1310,7 @@
      *  @param constr    The private constructor.
      */
     Symbol accessConstructor(DiagnosticPosition pos, Symbol constr) {
-        if (needsPrivateAccess(constr)) {
+        if (needsPrivateAccess(constr, null)) {
             ClassSymbol accOwner = constr.owner.enclClass();
             MethodSymbol aconstr = accessConstrs.get(constr);
             if (aconstr == null) {
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/SpecializeTypes.java	Fri Jun 05 15:03:30 2015 +0100
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/SpecializeTypes.java	Sat Jun 06 02:25:12 2015 +0100
@@ -47,14 +47,10 @@
 import com.sun.tools.javac.tree.JCTree.JCArrayTypeTree;
 import com.sun.tools.javac.tree.JCTree.JCBlock;
 import com.sun.tools.javac.tree.JCTree.JCClassDecl;
-import com.sun.tools.javac.tree.JCTree.JCEnhancedForLoop;
 import com.sun.tools.javac.tree.JCTree.JCExpression;
 import com.sun.tools.javac.tree.JCTree.JCFieldAccess;
 import com.sun.tools.javac.tree.JCTree.JCIdent;
 import com.sun.tools.javac.tree.JCTree.JCInstanceOf;
-import com.sun.tools.javac.tree.JCTree.JCLambda;
-import com.sun.tools.javac.tree.JCTree.JCLiteral;
-import com.sun.tools.javac.tree.JCTree.JCMemberReference;
 import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
 import com.sun.tools.javac.tree.JCTree.JCMethodInvocation;
 import com.sun.tools.javac.tree.JCTree.JCNewArray;
@@ -276,6 +272,14 @@
         return tree;
     }
 
+    @Override
+    public <T extends JCTree> T translate(T tree) {
+        if (tree instanceof JCExpression) {
+            ((JCExpression)tree).unerasedType = tree.type;
+        }
+        return super.translate(tree);
+    }
+
     /**
      * ****************************************************************
      * Visitor methods
@@ -289,7 +293,6 @@
         if (tree.def != null) {
             tree.def = translate(tree.def);
         }
-        tree.unerasedType = tree.type;
         Symbol specializedConstructor = asSpecializedSymbol(tree.type, tree.constructor);
         if (specializedConstructor != null) {
             //fixup constructor symbol
@@ -299,22 +302,9 @@
     }
 
     @Override
-    public void visitLambda(JCLambda tree) {
-        tree.unerasedType = tree.type;
-        super.visitLambda(tree);
-    }
-
-    @Override
-    public void visitReference(JCMemberReference tree) {
-        tree.unerasedType = tree.type;
-        super.visitReference(tree);
-    }
-
-    @Override
     public void visitNewArray(JCNewArray tree) {
         tree.elemtype = translate(tree.elemtype);
         tree.elems = translate(tree.elems);
-        tree.unerasedType = tree.type;
         result = tree;
     }
 
@@ -461,14 +451,6 @@
     }
 
     @Override
-    public void visitLiteral(JCLiteral tree) {
-        if (tree.isDefault()) {
-            tree.unerasedType = tree.type;
-        }
-        super.visitLiteral(tree);
-    }
-
-    @Override
     public void visitVarDef(JCTree.JCVariableDecl tree) {
         tree.vartype = translate(tree.vartype);
         if (tree.init != null) {
@@ -629,12 +611,6 @@
     }
 
     @Override
-    public void visitForeachLoop(JCEnhancedForLoop tree) {
-        super.visitForeachLoop(tree);
-        tree.getExpression().unerasedType = tree.getExpression().type;
-    }
-
-    @Override
     public void visitPackageDef(JCPackageDecl tree) {
         //do nothing
     }
--- a/test/tools/javac/valhalla/typespec/SpecializedAccessors.java	Fri Jun 05 15:03:30 2015 +0100
+++ b/test/tools/javac/valhalla/typespec/SpecializedAccessors.java	Sat Jun 06 02:25:12 2015 +0100
@@ -1,3 +1,28 @@
+/*
+ * Copyright (c) 2015, 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.
+ */
+
 /**
  * @test
  * @clean .*
--- a/test/tools/javac/valhalla/typespec/SpecializedAccessors2.java	Fri Jun 05 15:03:30 2015 +0100
+++ b/test/tools/javac/valhalla/typespec/SpecializedAccessors2.java	Sat Jun 06 02:25:12 2015 +0100
@@ -1,3 +1,28 @@
+/*
+ * Copyright (c) 2015, 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.
+ */
+
 /**
  * @test
  * @clean .*
--- a/test/tools/javac/valhalla/typespec/SpecializedAccessors2Aux.java	Fri Jun 05 15:03:30 2015 +0100
+++ b/test/tools/javac/valhalla/typespec/SpecializedAccessors2Aux.java	Sat Jun 06 02:25:12 2015 +0100
@@ -1,3 +1,28 @@
+/*
+ * Copyright (c) 2015, 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.
+ */
+
 public class SpecializedAccessors2Aux {
     SpecializedAccessors2 a;
 }
--- a/test/tools/javac/valhalla/typespec/SpecializedAccessors3.java	Fri Jun 05 15:03:30 2015 +0100
+++ b/test/tools/javac/valhalla/typespec/SpecializedAccessors3.java	Sat Jun 06 02:25:12 2015 +0100
@@ -1,3 +1,28 @@
+/*
+ * Copyright (c) 2015, 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.
+ */
+
 /**
  * @test
  * @clean .*
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/valhalla/typespec/SpecializedAccessors4.java	Sat Jun 06 02:25:12 2015 +0100
@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2015, 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.
+ */
+
+/**
+ * @test
+ * @clean .*
+ * @compile SpecializedAccessors4.java
+ * @run main SpecializedAccessors4
+ */
+public class SpecializedAccessors4<any X, any Y, any Z> {
+	private SpecializedAccessors4<X, Y, Z> f2;
+
+	SpecializedAccessors4() {
+		f2 = this;
+    }
+
+	private SpecializedAccessors4<X, Y, X> m() { return new SpecializedAccessors4<X, Y, X>(); }
+
+	public void g() {
+		SpecializedAccessors4<X,Object,Z> f = new SpecializedAccessors4<>();
+		f.f2.m().m();
+	}
+
+
+	public static void main(String[] args) {
+		new SpecializedAccessors4<long, int, double>().g();
+    }
+}