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

Print this page

        

@@ -47,12 +47,17 @@
 import com.sun.source.tree.MemberSelectTree;
 import com.sun.source.tree.TreeVisitor;
 import com.sun.source.util.SimpleTreeVisitor;
 
 import static com.sun.tools.javac.code.Flags.*;
+import static com.sun.tools.javac.code.Flags.BLOCK;
+import static com.sun.tools.javac.code.Flags.ANNOTATION;
 import static com.sun.tools.javac.code.Kinds.*;
+import static com.sun.tools.javac.code.Kinds.ERRONEOUS;
 import static com.sun.tools.javac.code.TypeTags.*;
+import static com.sun.tools.javac.code.TypeTags.WILDCARD;
+import static com.sun.tools.javac.tree.JCTree.Tag.*;
 
 /** This is the main context-dependent analysis phase in GJC. It
  *  encompasses name resolution, type checking and constant folding as
  *  subtasks. Some subtasks involve auxiliary classes.
  *  @see Check

@@ -243,11 +248,11 @@
     void checkAssignable(DiagnosticPosition pos, VarSymbol v, JCTree base, Env<AttrContext> env) {
         if ((v.flags() & FINAL) != 0 &&
             ((v.flags() & HASINIT) != 0
              ||
              !((base == null ||
-               (base.getTag() == JCTree.IDENT && TreeInfo.name(base) == names._this)) &&
+               (base.hasTag(IDENT) && TreeInfo.name(base) == names._this)) &&
                isAssignableAsBlankFinal(v, env)))) {
             if (v.isResourceVariable()) { //TWR resource
                 log.error(pos, "try.resource.may.not.be.assigned", v);
             } else {
                 log.error(pos, "cant.assign.val.to.final.var", v);

@@ -261,11 +266,11 @@
      *  It is assumed that tree is either a SELECT or an IDENT.
      *  We have to weed out selects from non-type names here.
      *  @param tree    The candidate tree.
      */
     boolean isStaticReference(JCTree tree) {
-        if (tree.getTag() == JCTree.SELECT) {
+        if (tree.hasTag(SELECT)) {
             Symbol lsym = TreeInfo.symbol(((JCFieldAccess) tree).selected);
             if (lsym == null || lsym.kind != TYP) {
                 return false;
             }
         }

@@ -691,11 +696,11 @@
             // in a superclass constructor call where
             // no explicit outer instance is given,
             // disable implicit outer instance from being passed.
             // (This would be an illegal access to "this before super").
             if (env.info.isSelfCall &&
-                env.tree.getTag() == JCTree.NEWCLASS &&
+                env.tree.hasTag(NEWCLASS) &&
                 ((JCNewClass) env.tree).encl == null)
             {
                 c.flags_field |= NOOUTERTHIS;
             }
             attribClass(tree.pos(), c);

@@ -861,11 +866,11 @@
 
         try {
             chk.checkDeprecatedAnnotation(tree.pos(), v);
 
             if (tree.init != null) {
-                if ((v.flags_field & FINAL) != 0 && tree.init.getTag() != JCTree.NEWCLASS) {
+                if ((v.flags_field & FINAL) != 0 && !tree.init.hasTag(NEWCLASS)) {
                     // In this case, `v' is final.  Ensure that it's initializer is
                     // evaluated.
                     v.getConstValue(); // ensure initializer is evaluated
                 } else {
                     // Attribute initializer in a new environment

@@ -969,12 +974,12 @@
     }
 
     public void visitLabelled(JCLabeledStatement tree) {
         // Check that label is not used in an enclosing statement
         Env<AttrContext> env1 = env;
-        while (env1 != null && env1.tree.getTag() != JCTree.CLASSDEF) {
-            if (env1.tree.getTag() == JCTree.LABELLED &&
+        while (env1 != null && !env1.tree.hasTag(CLASSDEF)) {
+            if (env1.tree.hasTag(LABELLED) &&
                 ((JCLabeledStatement) env1.tree).label == tree.label) {
                 log.error(tree.pos(), "label.already.in.use",
                           tree.label);
                 break;
             }

@@ -1050,18 +1055,18 @@
     // where
         /** Add any variables defined in stats to the switch scope. */
         private static void addVars(List<JCStatement> stats, Scope switchScope) {
             for (;stats.nonEmpty(); stats = stats.tail) {
                 JCTree stat = stats.head;
-                if (stat.getTag() == JCTree.VARDEF)
+                if (stat.hasTag(VARDEF))
                     switchScope.enter(((JCVariableDecl) stat).sym);
             }
         }
     // where
     /** Return the selected enumeration constant symbol, or null. */
     private Symbol enumConstant(JCTree tree, Type enumType) {
-        if (tree.getTag() != JCTree.IDENT) {
+        if (!tree.hasTag(IDENT)) {
             log.error(tree.pos(), "enum.label.must.be.unqualified.enum");
             return syms.errSymbol;
         }
         JCIdent ident = (JCIdent)tree;
         Name name = ident.name;

@@ -1092,11 +1097,11 @@
         Env<AttrContext> tryEnv = isTryWithResource ?
             env.dup(tree, localEnv.info.dup(localEnv.info.scope.dup())) :
             localEnv;
         // Attribute resource declarations
         for (JCTree resource : tree.resources) {
-            if (resource.getTag() == JCTree.VARDEF) {
+            if (resource.hasTag(VARDEF)) {
                 attribStat(resource, tryEnv);
                 chk.checkType(resource, resource.type, syms.autoCloseableType, "try.not.applicable.to.type");
 
                 //check that resource type cannot throw InterruptedException
                 checkAutoCloseable(resource.pos(), localEnv, resource.type);

@@ -1310,55 +1315,55 @@
          *  @param label   The label of the jump statement, or null if no
          *                 label is given.
          *  @param env     The environment current at the jump statement.
          */
         private JCTree findJumpTarget(DiagnosticPosition pos,
-                                    int tag,
+                                    JCTree.Tag tag,
                                     Name label,
                                     Env<AttrContext> env) {
             // Search environments outwards from the point of jump.
             Env<AttrContext> env1 = env;
             LOOP:
             while (env1 != null) {
                 switch (env1.tree.getTag()) {
-                case JCTree.LABELLED:
+                case LABELLED:
                     JCLabeledStatement labelled = (JCLabeledStatement)env1.tree;
                     if (label == labelled.label) {
                         // If jump is a continue, check that target is a loop.
-                        if (tag == JCTree.CONTINUE) {
-                            if (labelled.body.getTag() != JCTree.DOLOOP &&
-                                labelled.body.getTag() != JCTree.WHILELOOP &&
-                                labelled.body.getTag() != JCTree.FORLOOP &&
-                                labelled.body.getTag() != JCTree.FOREACHLOOP)
+                        if (tag == CONTINUE) {
+                            if (!labelled.body.hasTag(DOLOOP) &&
+                                !labelled.body.hasTag(WHILELOOP) &&
+                                !labelled.body.hasTag(FORLOOP) &&
+                                !labelled.body.hasTag(FOREACHLOOP))
                                 log.error(pos, "not.loop.label", label);
                             // Found labelled statement target, now go inwards
                             // to next non-labelled tree.
                             return TreeInfo.referencedStatement(labelled);
                         } else {
                             return labelled;
                         }
                     }
                     break;
-                case JCTree.DOLOOP:
-                case JCTree.WHILELOOP:
-                case JCTree.FORLOOP:
-                case JCTree.FOREACHLOOP:
+                case DOLOOP:
+                case WHILELOOP:
+                case FORLOOP:
+                case FOREACHLOOP:
                     if (label == null) return env1.tree;
                     break;
-                case JCTree.SWITCH:
-                    if (label == null && tag == JCTree.BREAK) return env1.tree;
+                case SWITCH:
+                    if (label == null && tag == BREAK) return env1.tree;
                     break;
-                case JCTree.METHODDEF:
-                case JCTree.CLASSDEF:
+                case METHODDEF:
+                case CLASSDEF:
                     break LOOP;
                 default:
                 }
                 env1 = env1.next;
             }
             if (label != null)
                 log.error(pos, "undef.label", label);
-            else if (tag == JCTree.CONTINUE)
+            else if (tag == CONTINUE)
                 log.error(pos, "cont.outside.loop");
             else
                 log.error(pos, "break.outside.switch.loop");
             return null;
         }

@@ -1450,11 +1455,11 @@
                     while (encl != null && encl.tag == TYPEVAR)
                         encl = encl.getUpperBound();
                     if (encl.tag == CLASS) {
                         // we are calling a nested class
 
-                        if (tree.meth.getTag() == JCTree.SELECT) {
+                        if (tree.meth.hasTag(SELECT)) {
                             JCTree qualifier = ((JCFieldAccess) tree.meth).selected;
 
                             // We are seeing a prefixed call, of the form
                             //     <expr>.super(...).
                             // Check that the prefix expression conforms

@@ -1466,11 +1471,11 @@
                             // qualifier omitted; check for existence
                             // of an appropriate implicit qualifier.
                             rs.resolveImplicitThis(tree.meth.pos(),
                                                    localEnv, site, true);
                         }
-                    } else if (tree.meth.getTag() == JCTree.SELECT) {
+                    } else if (tree.meth.hasTag(SELECT)) {
                         log.error(tree.meth.pos(), "illegal.qual.not.icls",
                                   site.tsym);
                     }
 
                     // if we're calling a java.lang.Enum constructor,

@@ -1520,20 +1525,20 @@
             if (restype.tag == WILDCARD)
                 throw new AssertionError(mtype);
 
             // as a special case, array.clone() has a result that is
             // the same as static type of the array being cloned
-            if (tree.meth.getTag() == JCTree.SELECT &&
+            if (tree.meth.hasTag(SELECT) &&
                 allowCovariantReturns &&
                 methName == names.clone &&
                 types.isArray(((JCFieldAccess) tree.meth).selected.type))
                 restype = ((JCFieldAccess) tree.meth).selected.type;
 
             // as a special case, x.getClass() has type Class<? extends |X|>
             if (allowGenerics &&
                 methName == names.getClass && tree.args.isEmpty()) {
-                Type qualifier = (tree.meth.getTag() == JCTree.SELECT)
+                Type qualifier = (tree.meth.hasTag(SELECT))
                     ? ((JCFieldAccess) tree.meth).selected.type
                     : env.enclClass.sym.type;
                 restype = new
                     ClassType(restype.getEnclosingType(),
                               List.<Type>of(new WildcardType(types.erasure(qualifier),

@@ -1558,11 +1563,11 @@
          */
         boolean checkFirstConstructorStat(JCMethodInvocation tree, Env<AttrContext> env) {
             JCMethodDecl enclMethod = env.enclMethod;
             if (enclMethod != null && enclMethod.name == names.init) {
                 JCBlock body = enclMethod.body;
-                if (body.stats.head.getTag() == JCTree.EXEC &&
+                if (body.stats.head.hasTag(EXEC) &&
                     ((JCExpressionStatement) body.stats.head).expr == tree)
                     return true;
             }
             log.error(tree.pos(),"call.must.be.first.stmt.in.ctor",
                       TreeInfo.name(tree.meth));

@@ -1589,11 +1594,11 @@
 
         // If enclosing class is given, attribute it, and
         // complete class name to be fully qualified
         JCExpression clazz = tree.clazz; // Class field following new
         JCExpression clazzid =          // Identifier in class field
-            (clazz.getTag() == JCTree.TYPEAPPLY)
+            (clazz.hasTag(TYPEAPPLY))
             ? ((JCTypeApply) clazz).clazz
             : clazz;
 
         JCExpression clazzid1 = clazzid; // The same in fully qualified form
 

@@ -1608,11 +1613,11 @@
             // yields a clazz T.C.
             Type encltype = chk.checkRefType(tree.encl.pos(),
                                              attribExpr(tree.encl, env));
             clazzid1 = make.at(clazz.pos).Select(make.Type(encltype),
                                                  ((JCIdent) clazzid).name);
-            if (clazz.getTag() == JCTree.TYPEAPPLY)
+            if (clazz.hasTag(TYPEAPPLY))
                 clazz = make.at(tree.pos).
                     TypeApply(clazzid1,
                               ((JCTypeApply) clazz).arguments);
             else
                 clazz = clazzid1;

@@ -1687,11 +1692,11 @@
         // If we have made no mistakes in the class type...
         if (clazztype.tag == CLASS) {
             // Enums may not be instantiated except implicitly
             if (allowEnums &&
                 (clazztype.tsym.flags_field&Flags.ENUM) != 0 &&
-                (env.tree.getTag() != JCTree.VARDEF ||
+                (!env.tree.hasTag(VARDEF) ||
                  (((JCVariableDecl) env.tree).mods.flags&Flags.ENUM) == 0 ||
                  ((JCVariableDecl) env.tree).init != tree))
                 log.error(tree.pos(), "enum.cant.be.instantiated");
             // Check that class is not abstract
             if (cdef == null &&

@@ -1928,11 +1933,11 @@
     public JCExpression makeNullCheck(JCExpression arg) {
         // optimization: X.this is never null; skip null check
         Name name = TreeInfo.name(arg);
         if (name == names._this || name == names._super) return arg;
 
-        int optag = JCTree.NULLCHK;
+        JCTree.Tag optag = NULLCHK;
         JCUnary tree = make.at(arg.pos).Unary(optag, arg);
         tree.operator = syms.nullcheck;
         tree.type = arg.type;
         return tree;
     }

@@ -1989,19 +1994,19 @@
         // Attribute arguments.
         Type owntype = attribTree(tree.lhs, env, VAR, Type.noType);
         Type operand = attribExpr(tree.rhs, env);
         // Find operator.
         Symbol operator = tree.operator = rs.resolveBinaryOperator(
-            tree.pos(), tree.getTag() - JCTree.ASGOffset, env,
+            tree.pos(), tree.getTag().noAssignOp(), env,
             owntype, operand);
 
         if (operator.kind == MTH &&
                 !owntype.isErroneous() &&
                 !operand.isErroneous()) {
             chk.checkOperator(tree.pos(),
                               (OperatorSymbol)operator,
-                              tree.getTag() - JCTree.ASGOffset,
+                              tree.getTag().noAssignOp(),
                               owntype,
                               operand);
             chk.checkDivZero(tree.rhs.pos(), operator, operand);
             chk.checkCastable(tree.rhs.pos(),
                               operator.type.getReturnType(),

@@ -2010,22 +2015,24 @@
         result = check(tree, owntype, VAL, pkind, pt);
     }
 
     public void visitUnary(JCUnary tree) {
         // Attribute arguments.
-        Type argtype = (JCTree.PREINC <= tree.getTag() && tree.getTag() <= JCTree.POSTDEC)
+        Type argtype = (PREINC.ordinal() <= tree.getTag().ordinal() && 
+                tree.getTag().ordinal() <= POSTDEC.ordinal())
             ? attribTree(tree.arg, env, VAR, Type.noType)
             : chk.checkNonVoid(tree.arg.pos(), attribExpr(tree.arg, env));
 
         // Find operator.
         Symbol operator = tree.operator =
             rs.resolveUnaryOperator(tree.pos(), tree.getTag(), env, argtype);
 
         Type owntype = types.createErrorType(tree.type);
         if (operator.kind == MTH &&
                 !argtype.isErroneous()) {
-            owntype = (JCTree.PREINC <= tree.getTag() && tree.getTag() <= JCTree.POSTDEC)
+            owntype = (PREINC.ordinal() <= tree.getTag().ordinal() && 
+                    tree.getTag().ordinal() <= POSTDEC.ordinal())
                 ? tree.arg.type
                 : operator.type.getReturnType();
             int opc = ((OperatorSymbol)operator).opcode;
 
             // If the argument is constant, fold it.

@@ -2619,11 +2626,11 @@
             if ((env.info.enclVar == v || v.pos > tree.pos) &&
                 v.owner.kind == TYP &&
                 canOwnInitializer(env.info.scope.owner) &&
                 v.owner == env.info.scope.owner.enclClass() &&
                 ((v.flags() & STATIC) != 0) == Resolve.isStatic(env) &&
-                (env.tree.getTag() != JCTree.ASSIGN ||
+                (!env.tree.hasTag(ASSIGN) ||
                  TreeInfo.skipParens(((JCAssign) env.tree).lhs) != tree)) {
                 String suffix = (env.info.enclVar == v) ?
                                 "self.ref" : "forward.ref";
                 if (!onlyWarning || isStaticEnumField(v)) {
                     log.error(tree.pos(), "illegal." + suffix);

@@ -2810,14 +2817,14 @@
                 if (owntype.getReturnType().tag != FORALL || warned) {
                     chk.checkVararg(env.tree.pos(), owntype.getParameterTypes(), sym);
                 }
                 Type elemtype = types.elemtype(argtype);
                 switch (tree.getTag()) {
-                case JCTree.APPLY:
+                case APPLY:
                     ((JCMethodInvocation) tree).varargsElement = elemtype;
                     break;
-                case JCTree.NEWCLASS:
+                case NEWCLASS:
                     ((JCNewClass) tree).varargsElement = elemtype;
                     break;
                 default:
                     throw new AssertionError(""+tree);
                 }

@@ -2894,13 +2901,13 @@
                 // Compute the proper generic outer
                 Type clazzOuter = clazztype.getEnclosingType();
                 if (clazzOuter.tag == CLASS) {
                     Type site;
                     JCExpression clazz = TreeInfo.typeIn(tree.clazz);
-                    if (clazz.getTag() == JCTree.IDENT) {
+                    if (clazz.hasTag(IDENT)) {
                         site = env.enclClass.sym.type;
-                    } else if (clazz.getTag() == JCTree.SELECT) {
+                    } else if (clazz.hasTag(SELECT)) {
                         site = ((JCFieldAccess) clazz).selected.type;
                     } else throw new AssertionError(""+tree);
                     if (clazzOuter.tag == CLASS && site != clazzOuter) {
                         if (site.tag == CLASS)
                             site = types.asOuterSuper(site, clazzOuter.tsym);

@@ -3066,11 +3073,11 @@
 
     /**
      * Attribute an env for either a top level tree or class declaration.
      */
     public void attrib(Env<AttrContext> env) {
-        if (env.tree.getTag() == JCTree.TOPLEVEL)
+        if (env.tree.hasTag(TOPLEVEL))
             attribTopLevel(env);
         else
             attribClass(env.tree.pos(), env.enclClass.sym);
     }
 

@@ -3243,11 +3250,11 @@
             // Make an exception for static constants.
             if (c.owner.kind != PCK &&
                 ((c.flags() & STATIC) == 0 || c.name == names.empty) &&
                 (TreeInfo.flags(l.head) & (STATIC | INTERFACE)) != 0) {
                 Symbol sym = null;
-                if (l.head.getTag() == JCTree.VARDEF) sym = ((JCVariableDecl) l.head).sym;
+                if (l.head.hasTag(VARDEF)) sym = ((JCVariableDecl) l.head).sym;
                 if (sym == null ||
                     sym.kind != VAR ||
                     ((VarSymbol) sym).getConstValue() == null)
                     log.error(l.head.pos(), "icls.cant.have.static.decl", c);
             }