changeset 49312:677168216389 condy-folding

consistency refactoring: updating Pool.DynamicVariable in the lines of Pool.DynamicMethod, additional renaming
author vromero
date Sat, 03 Mar 2018 16:54:58 -0500
parents 1903c35f59dc
children b9d0b29a9e10
files src/jdk.compiler/share/classes/com/sun/tools/javac/code/Symbol.java src/jdk.compiler/share/classes/com/sun/tools/javac/comp/ConstablesVisitor.java src/jdk.compiler/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassWriter.java src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/Code.java src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/Gen.java src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/Items.java src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/Pool.java src/jdk.compiler/share/classes/com/sun/tools/javac/util/Constables.java
diffstat 9 files changed, 103 insertions(+), 81 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Symbol.java	Thu Mar 01 14:25:00 2018 -0500
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Symbol.java	Sat Mar 03 16:54:58 2018 -0500
@@ -1533,6 +1533,10 @@
             return name.toString();
         }
 
+        public boolean isDynamic() {
+            return false;
+        }
+
         public Symbol asMemberOf(Type site, Types types) {
             return new VarSymbol(flags_field, name, types.memberType(site, this), owner);
         }
@@ -2008,27 +2012,22 @@
 
     /** A class for condy.
      */
-    public static class DynamicFieldSymbol extends Symbol {
-
+    public static class DynamicVarSymbol extends VarSymbol {
         public Object[] staticArgs;
         public MethodSymbol bsm;
         public int bsmKind;
 
-        public DynamicFieldSymbol(Name name, Symbol owner, int bsmKind, MethodSymbol bsm, Type type, Object[] staticArgs) {
-            super(Kind.VAR, 0, name, type, owner);
+        public DynamicVarSymbol(Name name, Symbol owner, int bsmKind, MethodSymbol bsm, Type type, Object[] staticArgs) {
+            super(0, name, type, owner);
             this.bsm = bsm;
             this.bsmKind = bsmKind;
             this.staticArgs = staticArgs;
         }
 
+        @Override
         public boolean isDynamic() {
             return true;
         }
-
-        @Override
-        public <R, P> R accept(ElementVisitor<R, P> v, P p) {
-            return v.visit(this, p);
-        }
     }
 
     /** A class for predefined operators.
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/ConstablesVisitor.java	Thu Mar 01 14:25:00 2018 -0500
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/ConstablesVisitor.java	Sat Mar 03 16:54:58 2018 -0500
@@ -32,7 +32,7 @@
 import java.util.Set;
 
 import com.sun.tools.javac.code.Symbol;
-import com.sun.tools.javac.code.Symbol.DynamicFieldSymbol;
+import com.sun.tools.javac.code.Symbol.DynamicVarSymbol;
 import com.sun.tools.javac.code.Symbol.DynamicMethodSymbol;
 import com.sun.tools.javac.code.Symbol.IntrinsicsLDCMethodSymbol;
 import com.sun.tools.javac.code.Symbol.MethodSymbol;
@@ -299,7 +299,7 @@
                     if (constables.dynamicConstantClass.isInstance(constant)) {
                         constant = constables.convertConstant(tree, attrEnv,
                                 constant, attrEnv.enclClass.sym.packge().modle);
-                        newType = ((Pool.ConstantDynamic)constant).type;
+                        newType = ((Pool.DynamicVariable)constant).type;
                     } else {
                         newType = tree.meth.type.asMethodType().restype;
                         Type unboxed = types.unboxedType(newType);
@@ -408,9 +408,9 @@
             if (tree != null &&
                     treesToCheck.contains(tree.getTag()) &&
                     constant != null) {
-                Optional<DynamicFieldSymbol> opDynSym = constables.getDynamicFieldSymbol(tree, constant, attrEnv);
+                Optional<DynamicVarSymbol> opDynSym = constables.getDynamicFieldSymbol(tree, constant, attrEnv);
                 if (opDynSym.isPresent()) {
-                    DynamicFieldSymbol dynSym = opDynSym.get();
+                    DynamicVarSymbol dynSym = opDynSym.get();
                     JCTree ident = make.at(tree.pos()).Ident(dynSym);
                     ident.type = dynSym.type.constType(constant);
                     return (T)ident;
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java	Thu Mar 01 14:25:00 2018 -0500
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java	Sat Mar 03 16:54:58 2018 -0500
@@ -72,7 +72,7 @@
 import javax.lang.model.type.TypeKind;
 
 import com.sun.tools.javac.code.Source;
-import com.sun.tools.javac.code.Symbol.DynamicFieldSymbol;
+import com.sun.tools.javac.code.Symbol.DynamicVarSymbol;
 
 /**
  * This pass desugars lambda expressions into static methods
@@ -1142,7 +1142,7 @@
             Symbol bsm = rs.resolveInternalMethod(pos, attrEnv, site,
                     bsmName, bsm_staticArgs, List.nil());
 
-            DynamicFieldSymbol dynSym = new DynamicFieldSymbol(methName,
+            DynamicVarSymbol dynSym = new DynamicVarSymbol(methName,
                     syms.noSymbol,
                     bsm.isStatic() ?
                         ClassFile.REF_invokeStatic :
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassWriter.java	Thu Mar 01 14:25:00 2018 -0500
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassWriter.java	Sat Mar 03 16:54:58 2018 -0500
@@ -406,26 +406,28 @@
                     poolbuf.appendChar(val.index);
                     poolbuf.appendChar(pool.put(nameType(dynSym)));
                 }
-            } else if (value instanceof Pool.ConstantDynamic) {
-                Pool.ConstantDynamic cd = (Pool.ConstantDynamic)value;
-                MethodHandle handle = cd.bsm;
-                DynamicMethodSymbol dynSym = new DynamicMethodSymbol(
-                        handle.refSym.name,
-                        syms.noSymbol,
-                        handle.refKind,
-                        (MethodSymbol)handle.refSym,
-                        handle.refSym.type,
-                        cd.args);
-                DynamicMethod.BootstrapMethodsValue val = writeDynSymbol(dynSym, handle);
-                poolbuf.appendByte(CONSTANT_Dynamic);
-                poolbuf.appendChar(val.index);
-                NameAndType nt = new NameAndType(cd.name, cd.type, cd.types);
-                poolbuf.appendChar(pool.put(nt));
             } else if (value instanceof VarSymbol) {
                 VarSymbol v = (VarSymbol)value;
-                poolbuf.appendByte(CONSTANT_Fieldref);
-                poolbuf.appendChar(pool.put(v.owner));
-                poolbuf.appendChar(pool.put(nameType(v)));
+                if (!v.isDynamic()) {
+                    poolbuf.appendByte(CONSTANT_Fieldref);
+                    poolbuf.appendChar(pool.put(v.owner));
+                    poolbuf.appendChar(pool.put(nameType(v)));
+                } else {
+                    DynamicVarSymbol dynVarSym = (DynamicVarSymbol)v;
+                    MethodHandle handle = new MethodHandle(dynVarSym.bsmKind, dynVarSym.bsm, types);
+                    DynamicMethodSymbol dynSym = new DynamicMethodSymbol(
+                            handle.refSym.name,
+                            syms.noSymbol,
+                            handle.refKind,
+                            (MethodSymbol)handle.refSym,
+                            handle.refSym.type,
+                            dynVarSym.staticArgs);
+                    DynamicMethod.BootstrapMethodsValue val = writeDynSymbol(dynSym, handle);
+                    poolbuf.appendByte(CONSTANT_Dynamic);
+                    poolbuf.appendChar(val.index);
+                    NameAndType nt = new NameAndType(dynVarSym.name, dynVarSym.type, types);
+                    poolbuf.appendChar(pool.put(nt));
+                }
             } else if (value instanceof Name) {
                 poolbuf.appendByte(CONSTANT_Utf8);
                 byte[] bs = ((Name)value).toUtf();
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/Code.java	Thu Mar 01 14:25:00 2018 -0500
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/Code.java	Sat Mar 03 16:54:58 2018 -0500
@@ -29,7 +29,7 @@
 import com.sun.tools.javac.code.Symbol.*;
 import com.sun.tools.javac.code.Types.UniqueType;
 import com.sun.tools.javac.resources.CompilerProperties.Errors;
-import com.sun.tools.javac.jvm.Pool.ConstantDynamic;
+import com.sun.tools.javac.jvm.Pool.DynamicVariable;
 import com.sun.tools.javac.tree.JCTree;
 import com.sun.tools.javac.util.*;
 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
@@ -921,7 +921,7 @@
         if (o instanceof ClassSymbol) return syms.classType;
         if (o instanceof Pool.MethodHandle) return syms.methodHandleType;
         if (o instanceof UniqueType) return typeForPool(((UniqueType)o).type);
-        if (o instanceof Pool.ConstantDynamic) return ((Pool.ConstantDynamic)o).type;
+        if (o instanceof Pool.DynamicVariable) return ((Pool.DynamicVariable)o).type;
         if (o instanceof Type) {
             Type ty = (Type) o;
 
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/Gen.java	Thu Mar 01 14:25:00 2018 -0500
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/Gen.java	Sat Mar 03 16:54:58 2018 -0500
@@ -819,7 +819,7 @@
                 tree.accept(classReferenceVisitor);
                 checkStringConstant(tree.pos(), tree.type.constValue());
                 Symbol sym = TreeInfo.symbol(tree);
-                if (sym != null && isLambdaCondy(sym)) {
+                if (sym != null && isConstantDynamic(sym)) {
                     result = items.makeDynamicItem(sym);
                 } else {
                     result = items.makeImmediateItem(tree.type, tree.type.constValue());
@@ -1662,8 +1662,8 @@
             // primitives special case
             if (constant instanceof VarSymbol && ((VarSymbol)constant).name == names.TYPE) {
                 m = items.makeStaticItem((Symbol)constant);
-            } else if (constant instanceof Pool.ConstantDynamic) {
-                m = items.makeCondyItem((Pool.ConstantDynamic)constant);
+            } else if (constant instanceof Pool.DynamicVariable) {
+                m = items.makeCondyItem((Pool.DynamicVariable)constant);
             } else {
                 m = items.makeImmediateItem(pt, constant);
             }
@@ -2070,7 +2070,7 @@
                 res = items.makeMemberItem(sym, true);
             }
             result = res;
-        } else if (isInvokeDynamic(sym) || isLambdaCondy(sym)) {
+        } else if (isInvokeDynamic(sym) || isConstantDynamic(sym)) {
             result = items.makeDynamicItem(sym);
         } else if (sym.kind == VAR && sym.owner.kind == MTH) {
             result = items.makeLocalItem((VarSymbol)sym);
@@ -2085,10 +2085,10 @@
         }
     }
 
-    public boolean isLambdaCondy(Symbol sym) {
+    public boolean isConstantDynamic(Symbol sym) {
         return sym.kind == VAR &&
-                sym instanceof DynamicFieldSymbol &&
-                ((DynamicFieldSymbol)sym).isDynamic();
+                sym instanceof DynamicVarSymbol &&
+                ((DynamicVarSymbol)sym).isDynamic();
     }
 
     public void visitSelect(JCFieldAccess tree) {
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/Items.java	Thu Mar 01 14:25:00 2018 -0500
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/Items.java	Sat Mar 03 16:54:58 2018 -0500
@@ -30,7 +30,7 @@
 import com.sun.tools.javac.code.Symbol.*;
 import com.sun.tools.javac.code.Type.*;
 import com.sun.tools.javac.jvm.Code.*;
-import com.sun.tools.javac.jvm.Pool.ConstantDynamic;
+import com.sun.tools.javac.jvm.Pool.DynamicVariable;
 import com.sun.tools.javac.tree.JCTree;
 import com.sun.tools.javac.util.Assert;
 
@@ -168,7 +168,7 @@
     /** Make an item representing a condy.
      *  @param value    The condy value.
      */
-    Item makeCondyItem(ConstantDynamic value) {
+    Item makeCondyItem(DynamicVariable value) {
         return new CondyItem(value);
     }
 
@@ -476,9 +476,9 @@
     /** An item representing a condy
      */
     class CondyItem extends Item {
-        ConstantDynamic value;
+        DynamicVariable value;
 
-        CondyItem(ConstantDynamic value) {
+        CondyItem(DynamicVariable value) {
             super(Code.typecode(value.type));
             this.value = value;
         }
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/Pool.java	Thu Mar 01 14:25:00 2018 -0500
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/Pool.java	Sat Mar 03 16:54:58 2018 -0500
@@ -39,6 +39,7 @@
 
 import java.util.*;
 
+import com.sun.tools.javac.code.Symtab;
 import com.sun.tools.javac.util.DefinedBy;
 import com.sun.tools.javac.util.DefinedBy.Api;
 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
@@ -132,8 +133,8 @@
     Object makePoolValue(Object o) {
         if (o instanceof DynamicMethodSymbol) {
             return new DynamicMethod((DynamicMethodSymbol)o, types);
-        } else if (o instanceof DynamicFieldSymbol) {
-            return new Pool.ConstantDynamic((DynamicFieldSymbol)o, types);
+        } else if (o instanceof DynamicVarSymbol) {
+            return new Pool.DynamicVariable((DynamicVarSymbol)o, types);
         } else if (o instanceof MethodSymbol) {
             return new Method((MethodSymbol)o, types);
         } else if (o instanceof VarSymbol) {
@@ -300,54 +301,74 @@
     /**
      * Pool entry associated with dynamic constants.
      */
-    public static class ConstantDynamic {
+    public static class DynamicVariable extends Variable {
         public MethodHandle bsm;
-        public Name name;
-        public Type type;
-        public Object[] args;
+        private Object[] uniqueStaticArgs;
         Types types;
 
-        public ConstantDynamic(Name name, MethodHandle bsm, Object[] args, Types types) {
-            this(name, bsm, bsm.refSym.type.asMethodType().restype, args, types);
+        public DynamicVariable(Name name, MethodHandle bsm, Object[] args, Types types, Symtab syms) {
+            this(name, bsm, bsm.refSym.type.asMethodType().restype, args, types, syms);
         }
 
-        public ConstantDynamic(Name name, MethodHandle bsm, Type type, Object[] args, Types types) {
-            Assert.checkNonNull(args);
-            this.bsm = bsm;
-            this.name = name;
-            this.type = type;
-            this.args = args;
-            this.types = types;
+        public DynamicVariable(Name name, MethodHandle bsm, Type type, Object[] args, Types types, Symtab syms) {
+            this(new DynamicVarSymbol(name,
+                            syms.noSymbol,
+                            bsm.refKind,
+                            (MethodSymbol)bsm.refSym,
+                            type,
+                            args), types, bsm);
         }
 
-        public ConstantDynamic(DynamicFieldSymbol dynField, Types types) {
-            this.bsm = new MethodHandle(dynField.bsmKind, dynField.bsm, types);
-            this.name = dynField.name;
-            this.type = dynField.type;
-            this.args = dynField.staticArgs;
+        public DynamicVariable(DynamicVarSymbol dynField, Types types) {
+            this(dynField, types, null);
+        }
+
+        private DynamicVariable(DynamicVarSymbol dynField, Types types, MethodHandle bsm) {
+            super(dynField, types);
+            this.bsm = bsm != null ?
+                    bsm :
+                    new MethodHandle(dynField.bsmKind, dynField.bsm, types);
             this.types = types;
+            uniqueStaticArgs = getUniqueTypeArray(staticArgs(), types);
+        }
+
+        private Object[] getUniqueTypeArray(Object[] objects, Types types) {
+            Object[] result = new Object[objects.length];
+            for (int i = 0; i < objects.length; i++) {
+                if (objects[i] instanceof Type) {
+                    result[i] = new UniqueType((Type)objects[i], types);
+                } else {
+                    result[i] = objects[i];
+                }
+            }
+            return result;
         }
 
         @Override
         public int hashCode() {
-            return bsm.hashCode() * 67 + name.hashCode() + type.hashCode() * 13;
+            int hash = bsm.hashCode() * 67 + other.name.hashCode() + type.hashCode() * 13 + uniqueType.hashCode();
+            for (Object uniqueStaticArg : uniqueStaticArgs) {
+                hash += (uniqueStaticArg.hashCode() * 23);
+            }
+            return hash;
         }
 
         @Override
         public boolean equals(Object obj) {
-            if (obj instanceof ConstantDynamic) {
-                ConstantDynamic that = (ConstantDynamic)obj;
+            if (obj instanceof DynamicVariable) {
+                DynamicVariable that = (DynamicVariable)obj;
                 return that.bsm.equals(bsm) &&
                         types.isSameType(that.type, type) &&
-                        that.name.equals(name) &&
-                        Arrays.equals(that.args, args);
+                        that.other.name.equals(other.name) &&
+                        Arrays.equals(uniqueStaticArgs, that.uniqueStaticArgs) &&
+                        that.uniqueType.equals(uniqueType);
             } else {
                 return false;
             }
         }
 
-        public void updateType(Type type) {
-            this.type = type;
+        public Object[] staticArgs() {
+            return ((DynamicVarSymbol)other).staticArgs;
         }
     }
 
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Constables.java	Thu Mar 01 14:25:00 2018 -0500
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Constables.java	Sat Mar 03 16:54:58 2018 -0500
@@ -39,7 +39,7 @@
 import com.sun.tools.javac.code.Symbol;
 import com.sun.tools.javac.code.Symbol.ClassSymbol;
 import com.sun.tools.javac.code.Symbol.CompletionFailure;
-import com.sun.tools.javac.code.Symbol.DynamicFieldSymbol;
+import com.sun.tools.javac.code.Symbol.DynamicVarSymbol;
 import com.sun.tools.javac.code.Symbol.MethodSymbol;
 import com.sun.tools.javac.code.Symbol.ModuleSymbol;
 import com.sun.tools.javac.code.Symbol.VarSymbol;
@@ -272,7 +272,7 @@
         return head.tail;
     }
 
-    public Optional<DynamicFieldSymbol> getDynamicFieldSymbol(JCTree tree, Object constant, Env<AttrContext> attrEnv) {
+    public Optional<DynamicVarSymbol> getDynamicFieldSymbol(JCTree tree, Object constant, Env<AttrContext> attrEnv) {
         if (constant != null) {
             if (!canMakeItToConstantValue(tree.type) &&
                 constantRefClass.isInstance(constant)) {
@@ -280,14 +280,14 @@
                 // now this should be a condy that the compiler can understand
                 // a Pool.ConstantDynamic
                 Object condyOb = convertConstant(tree, attrEnv, constant, attrEnv.enclClass.sym.packge().modle);
-                if (condyOb instanceof Pool.ConstantDynamic) {
-                    Pool.ConstantDynamic condy = (Pool.ConstantDynamic)condyOb;
-                    DynamicFieldSymbol dynSym = new DynamicFieldSymbol(condy.name,
+                if (condyOb instanceof Pool.DynamicVariable) {
+                    Pool.DynamicVariable condy = (Pool.DynamicVariable)condyOb;
+                    DynamicVarSymbol dynSym = new DynamicVarSymbol(condy.name,
                             syms.noSymbol,
                             condy.bsm.refKind,
                             (MethodSymbol)condy.bsm.refSym,
                             condy.type,
-                            condy.args);
+                            condy.staticArgs());
                     return Optional.of(dynSym);
                 }
             }
@@ -334,7 +334,7 @@
                 Object BSM_PRIMITIVE_CLASS = getFieldValueReflectively(symRefs, null, "BSM_PRIMITIVE_CLASS");
                 Pool.MethodHandle methodHandle = (Pool.MethodHandle)convertConstant(tree, attrEnv,
                         BSM_PRIMITIVE_CLASS, currentModule);
-                return new Pool.ConstantDynamic(names.fromString(descriptor), methodHandle, new Object[0], types);
+                return new Pool.DynamicVariable(names.fromString(descriptor), methodHandle, new Object[0], types, syms);
             }
             Type type = descriptorToType(descriptor, currentModule, false);
             Symbol symToLoad;
@@ -363,7 +363,7 @@
             Pool.MethodHandle methodHandle = (Pool.MethodHandle)convertConstant(tree, attrEnv, mh, currentModule);
             Object[] args = (Object[])invokeMethodReflectively(dynamicConstantClass, constant, "bootstrapArgs");
             Object[] convertedArgs = convertConstants(tree, attrEnv, args, currentModule, true);
-            return new Pool.ConstantDynamic(names.fromString(name), methodHandle, type, convertedArgs, types);
+            return new Pool.DynamicVariable(names.fromString(name), methodHandle, type, convertedArgs, types, syms);
         }
         return constant;
     }