Commits

Artem Melentyev  committed a4aac68

index operator overloading uses translateMap. move translation to TransType

  • Participants
  • Parent commits 1c50610

Comments (0)

Files changed (3)

File src/share/classes/com/sun/tools/javac/comp/Attr.java

             if ((ownkind & ~pkind) == 0) {
                 JCExpression t = tryImplicitConversion(tree, owntype, pt);
                 if (t != null) {
-                    addTranslate(tree, t);
+                    putTranslate(tree, t);
                     return tree.type = owntype;
                 }
                 owntype = chk.checkType(tree.pos(), owntype, pt, errKey);
     }
     /** WeakHashMap to allow GC collect entries. Because we don't need them then they are gone */
     private Map<JCTree, JCExpression> translateMap = new WeakHashMap<>();
-    public void addTranslate(JCTree from, JCExpression to) {
+    public void putTranslate(JCTree from, JCExpression to) {
         translateMap.put(from, to);
     }
     public JCExpression removeTranslate(JCTree from) {
         } else if (atype.tag != ERROR) {
             attribExpr(tree.index, env);
             boolean ok = false;
-            if (env.tree.getKind() == Tree.Kind.ASSIGNMENT) {
+            if (env.tree.getKind() == Tree.Kind.ASSIGNMENT && ((JCAssign)env.tree).lhs == tree) {
                 JCAssign ass = (JCAssign) env.tree;
-                if (ass.lhs == tree) {
-                    Type rhstype = attribExpr(ass.rhs, env);
-                    List<Type> argtypes = List.of(tree.index.type, rhstype);
-                    Symbol m = rs.findMethod(env, atype, names.fromString("set"), argtypes, null, true, false, false);
-                    if (m.kind != Kinds.MTH)
-                        m = rs.findMethod(env, atype, names.fromString("put"), argtypes, null, true, false, false); // Map#put
-                    if (m.kind == Kinds.MTH) {
-                        JCMethodInvocation mi = make.Apply(null, make.Select(tree.indexed, m), List.of(tree.index, ass.rhs));
-                        mi.type = attribExpr(mi, env);
-                        tree.indexed = mi;
-                        owntype = rhstype;
-                        ok = true;
-                    }
+                Type rhstype = attribExpr(ass.rhs, env);
+                List<Type> argtypes = List.of(tree.index.type, rhstype);
+                Symbol m = rs.findMethod(env, atype, names.fromString("set"), argtypes, null, true, false, false);
+                if (m.kind != Kinds.MTH)
+                    m = rs.findMethod(env, atype, names.fromString("put"), argtypes, null, true, false, false); // Map#put
+                if (m.kind == Kinds.MTH) {
+                    JCMethodInvocation mi = make.Apply(null, make.Select(tree.indexed, m), List.of(tree.index, ass.rhs));
+                    mi.type = attribExpr(mi, env);
+                    putTranslate(ass, mi);
+                    owntype = rhstype;
+                    ok = true;
                 }
-            }
-            if (!ok) {
+            } else {
                 List<Type> argtypes = List.of(tree.index.type);
                 Symbol m = rs.findMethod(env, atype, names.fromString("get"), argtypes, null, true, false, false);
                 if (m.kind == Kinds.MTH) {
                     //owntype = rs.instantiate(env, atype, m, argtypes, null, true, false, noteWarner).getReturnType();
                     JCMethodInvocation mi = make.Apply(null, make.Select(tree.indexed, m), List.of(tree.index));
                     attribExpr(mi, env);
-                    tree.indexed = mi;
+                    putTranslate(tree, mi);
                     owntype = mi.type;
                     ok = true;
                 }

File src/share/classes/com/sun/tools/javac/comp/Lower.java

 
 import java.util.*;
 
-import com.sun.source.tree.Tree;
 import com.sun.tools.javac.code.*;
 import com.sun.tools.javac.jvm.*;
 import com.sun.tools.javac.main.RecognizedOptions.PkgInfo;
                 Integer endPos = endPositions.remove(tree);
                 if (endPos != null) endPositions.put(result, endPos);
             }
-            JCExpression t = attr.removeTranslate(tree);
-            if (t!=null)
-                return (T) translate(t);
             return result;
         }
     }
     /** Visitor method: Translate a single node, boxing or unboxing if needed.
      */
     public <T extends JCTree> T translate(T tree, Type type) {
-        JCExpression t = attr.removeTranslate(tree);
-        if (t!=null)
-            return (T) translate(t);
         return (tree == null) ? null : boxIfNeeded(translate(tree), type);
     }
 
     }
 
     public void visitIndexed(JCArrayAccess tree) {
-    	if (types.isArray(tree.indexed.type)) {
-    		tree.indexed = translate(tree.indexed);
-        	tree.index = translate(tree.index, syms.intType);
-        	result = tree;
-    	} else {
-    		result = translate(tree.indexed);
-    	}
+        tree.indexed = translate(tree.indexed);
+        tree.index = translate(tree.index, syms.intType);
+        result = tree;
     }
 
     public void visitAssign(JCAssign tree) {
-        JCTree waslhs = tree.lhs;
         tree.lhs = translate(tree.lhs, tree);
         tree.rhs = translate(tree.rhs, tree.lhs.type);
 
             JCMethodInvocation app = (JCMethodInvocation)tree.lhs;
             app.args = List.of(tree.rhs).prependList(app.args);
             result = app;
-        } else if (waslhs.getKind() == Tree.Kind.ARRAY_ACCESS && !types.isArray(((JCArrayAccess)waslhs).indexed.type)) {
-            result = tree.lhs;
         } else {
             result = tree;
         }

File src/share/classes/com/sun/tools/javac/comp/TransTypes.java

     private boolean allowEnums;
     private Types types;
     private final Resolve resolve;
+    private Attr attr;
 
     /**
      * Flag to indicate whether or not to generate bridge methods.
         types = Types.instance(context);
         make = TreeMaker.instance(context);
         resolve = Resolve.instance(context);
+        attr = Attr.instance(context);
     }
 
     /** A hashtable mapping bridge methods to the methods they override after
      */
     private Type pt;
 
+    @Override
+    public <T extends JCTree> T translate(T tree) {
+        JCExpression t = attr.removeTranslate(tree);
+        if (t!=null)
+            return (T) translate(t);
+        return super.translate(tree);
+    }
+
     /** Visitor method: perform a type translation on tree.
      */
     public <T extends JCTree> T translate(T tree, Type pt) {
         tree.index = translate(tree.index, syms.intType);
 
         // Insert casts of indexed expressions as needed.
-        Type elemtype = types.elemtype(tree.indexed.type);
-        if (elemtype != null)
-        	result = retype(tree, elemtype, pt);
-        else
-        	result = tree;
+        result = retype(tree, types.elemtype(tree.indexed.type), pt);
     }
 
     // There ought to be nothing to rewrite here;