8162874: AST nodes representing operators should have a common superclass
authorvromero
Mon, 08 Aug 2016 11:48:02 -0700
changeset 40236 48d9e26a8fa4
parent 40235 fe6c00618434
child 40237 dfa339918e9e
8162874: AST nodes representing operators should have a common superclass Reviewed-by: mcimadamore
langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java
langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/JCTree.java
langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/TreeInfo.java
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java	Mon Aug 08 17:22:44 2016 +0200
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java	Mon Aug 08 11:48:02 2016 -0700
@@ -52,6 +52,7 @@
 import static com.sun.tools.javac.code.Kinds.Kind.*;
 import static com.sun.tools.javac.code.Symbol.OperatorSymbol.AccessCode.DEREF;
 import static com.sun.tools.javac.jvm.ByteCodes.*;
+import static com.sun.tools.javac.tree.JCTree.JCOperatorExpression.OperandPos.LEFT;
 import static com.sun.tools.javac.tree.JCTree.Tag.*;
 
 /** This pass translates away some syntactic sugar: inner classes,
@@ -850,12 +851,9 @@
         else if (enclOp.hasTag(ASSIGN) &&
                  tree == TreeInfo.skipParens(((JCAssign) enclOp).lhs))
             return AccessCode.ASSIGN.code;
-        else if (enclOp.getTag().isIncOrDecUnaryOp() &&
-                 tree == TreeInfo.skipParens(((JCUnary) enclOp).arg))
-            return (((JCUnary) enclOp).operator).getAccessCode(enclOp.getTag());
-        else if (enclOp.getTag().isAssignop() &&
-                 tree == TreeInfo.skipParens(((JCAssignOp) enclOp).lhs))
-            return (((JCAssignOp) enclOp).operator).getAccessCode(enclOp.getTag());
+        else if ((enclOp.getTag().isIncOrDecUnaryOp() || enclOp.getTag().isAssignop()) &&
+                tree == TreeInfo.skipParens(((JCOperatorExpression) enclOp).getOperand(LEFT)))
+            return (((JCOperatorExpression) enclOp).operator).getAccessCode(enclOp.getTag());
         else
             return AccessCode.DEREF.code;
     }
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/JCTree.java	Mon Aug 08 17:22:44 2016 +0200
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/JCTree.java	Mon Aug 08 11:48:02 2016 -0700
@@ -1869,14 +1869,33 @@
         }
     }
 
+    public static abstract class JCOperatorExpression extends JCExpression {
+        public enum OperandPos {
+            LEFT,
+            RIGHT
+        }
+
+        protected Tag opcode;
+        public OperatorSymbol operator;
+
+        public OperatorSymbol getOperator() {
+            return operator;
+        }
+
+        @Override
+        public Tag getTag() {
+            return opcode;
+        }
+
+        public abstract JCExpression getOperand(OperandPos pos);
+    }
+
     /**
      * An assignment with "+=", "|=" ...
      */
-    public static class JCAssignOp extends JCExpression implements CompoundAssignmentTree {
-        private Tag opcode;
+    public static class JCAssignOp extends JCOperatorExpression implements CompoundAssignmentTree {
         public JCExpression lhs;
         public JCExpression rhs;
-        public OperatorSymbol operator;
         protected JCAssignOp(Tag opcode, JCTree lhs, JCTree rhs, OperatorSymbol operator) {
             this.opcode = opcode;
             this.lhs = (JCExpression)lhs;
@@ -1892,26 +1911,21 @@
         public JCExpression getVariable() { return lhs; }
         @DefinedBy(Api.COMPILER_TREE)
         public JCExpression getExpression() { return rhs; }
-        public OperatorSymbol getOperator() {
-            return operator;
-        }
         @Override @DefinedBy(Api.COMPILER_TREE)
         public <R,D> R accept(TreeVisitor<R,D> v, D d) {
             return v.visitCompoundAssignment(this, d);
         }
         @Override
-        public Tag getTag() {
-            return opcode;
+        public JCExpression getOperand(OperandPos pos) {
+            return pos == OperandPos.LEFT ? lhs : rhs;
         }
     }
 
     /**
      * A unary operation.
      */
-    public static class JCUnary extends JCExpression implements UnaryTree {
-        private Tag opcode;
+    public static class JCUnary extends JCOperatorExpression implements UnaryTree {
         public JCExpression arg;
-        public OperatorSymbol operator;
         protected JCUnary(Tag opcode, JCExpression arg) {
             this.opcode = opcode;
             this.arg = arg;
@@ -1923,31 +1937,25 @@
         public Kind getKind() { return TreeInfo.tagToKind(getTag()); }
         @DefinedBy(Api.COMPILER_TREE)
         public JCExpression getExpression() { return arg; }
-        public OperatorSymbol getOperator() {
-            return operator;
-        }
         @Override @DefinedBy(Api.COMPILER_TREE)
         public <R,D> R accept(TreeVisitor<R,D> v, D d) {
             return v.visitUnary(this, d);
         }
-        @Override
-        public Tag getTag() {
-            return opcode;
-        }
-
         public void setTag(Tag tag) {
             opcode = tag;
         }
+        @Override
+        public JCExpression getOperand(OperandPos pos) {
+            return arg;
+        }
     }
 
     /**
      * A binary operation.
      */
-    public static class JCBinary extends JCExpression implements BinaryTree {
-        private Tag opcode;
+    public static class JCBinary extends JCOperatorExpression implements BinaryTree {
         public JCExpression lhs;
         public JCExpression rhs;
-        public OperatorSymbol operator;
         protected JCBinary(Tag opcode,
                          JCExpression lhs,
                          JCExpression rhs,
@@ -1966,16 +1974,13 @@
         public JCExpression getLeftOperand() { return lhs; }
         @DefinedBy(Api.COMPILER_TREE)
         public JCExpression getRightOperand() { return rhs; }
-        public OperatorSymbol getOperator() {
-            return operator;
-        }
         @Override @DefinedBy(Api.COMPILER_TREE)
         public <R,D> R accept(TreeVisitor<R,D> v, D d) {
             return v.visitBinary(this, d);
         }
         @Override
-        public Tag getTag() {
-            return opcode;
+        public JCExpression getOperand(OperandPos pos) {
+            return pos == OperandPos.LEFT ? lhs : rhs;
         }
     }
 
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/TreeInfo.java	Mon Aug 08 17:22:44 2016 +0200
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/TreeInfo.java	Mon Aug 08 11:48:02 2016 -0700
@@ -36,14 +36,19 @@
 import com.sun.tools.javac.tree.JCTree.JCPolyExpression.*;
 import com.sun.tools.javac.util.*;
 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
+
 import static com.sun.tools.javac.code.Flags.*;
 import static com.sun.tools.javac.code.Kinds.Kind.*;
 import static com.sun.tools.javac.code.TypeTag.BOT;
 import static com.sun.tools.javac.tree.JCTree.Tag.*;
 import static com.sun.tools.javac.tree.JCTree.Tag.BLOCK;
 import static com.sun.tools.javac.tree.JCTree.Tag.SYNCHRONIZED;
+
 import javax.tools.JavaFileObject;
 
+import static com.sun.tools.javac.tree.JCTree.JCOperatorExpression.OperandPos.LEFT;
+import static com.sun.tools.javac.tree.JCTree.JCOperatorExpression.OperandPos.RIGHT;
+
 /** Utility class containing inspector methods for trees.
  *
  *  <p><b>This is NOT part of any supported API.
@@ -386,7 +391,6 @@
             case SL_ASG: case SR_ASG: case USR_ASG:
             case PLUS_ASG: case MINUS_ASG: case MUL_ASG:
             case DIV_ASG: case MOD_ASG:
-                return getStartPos(((JCAssignOp) tree).lhs);
             case OR: case AND: case BITOR:
             case BITXOR: case BITAND: case EQ:
             case NE: case LT: case GT:
@@ -394,7 +398,9 @@
             case SR: case USR: case PLUS:
             case MINUS: case MUL: case DIV:
             case MOD:
-                return getStartPos(((JCBinary) tree).lhs);
+            case POSTINC:
+            case POSTDEC:
+                return getStartPos(((JCOperatorExpression) tree).getOperand(LEFT));
             case CLASSDEF: {
                 JCClassDecl node = (JCClassDecl)tree;
                 if (node.mods.pos != Position.NOPOS)
@@ -423,9 +429,6 @@
                 return getStartPos(((JCArrayTypeTree) tree).elemtype);
             case TYPETEST:
                 return getStartPos(((JCInstanceOf) tree).expr);
-            case POSTINC:
-            case POSTDEC:
-                return getStartPos(((JCUnary) tree).arg);
             case ANNOTATED_TYPE: {
                 JCAnnotatedType node = (JCAnnotatedType) tree;
                 if (node.annotations.nonEmpty()) {
@@ -486,7 +489,6 @@
             case SL_ASG: case SR_ASG: case USR_ASG:
             case PLUS_ASG: case MINUS_ASG: case MUL_ASG:
             case DIV_ASG: case MOD_ASG:
-                return getEndPos(((JCAssignOp) tree).rhs, endPosTable);
             case OR: case AND: case BITOR:
             case BITXOR: case BITAND: case EQ:
             case NE: case LT: case GT:
@@ -494,7 +496,13 @@
             case SR: case USR: case PLUS:
             case MINUS: case MUL: case DIV:
             case MOD:
-                return getEndPos(((JCBinary) tree).rhs, endPosTable);
+            case POS:
+            case NEG:
+            case NOT:
+            case COMPL:
+            case PREINC:
+            case PREDEC:
+                return getEndPos(((JCOperatorExpression) tree).getOperand(RIGHT), endPosTable);
             case CASE:
                 return getEndPos(((JCCase) tree).stats.last(), endPosTable);
             case CATCH:
@@ -537,13 +545,6 @@
                 return getEndPos(((JCTypeCast) tree).expr, endPosTable);
             case TYPETEST:
                 return getEndPos(((JCInstanceOf) tree).clazz, endPosTable);
-            case POS:
-            case NEG:
-            case NOT:
-            case COMPL:
-            case PREINC:
-            case PREDEC:
-                return getEndPos(((JCUnary) tree).arg, endPosTable);
             case WHILELOOP:
                 return getEndPos(((JCWhileLoop) tree).body, endPosTable);
             case ANNOTATED_TYPE: