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

Print this page




  98         rs = Resolve.instance(context);
  99         chk = Check.instance(context);
 100         memberEnter = MemberEnter.instance(context);
 101         make = TreeMaker.instance(context);
 102         enter = Enter.instance(context);
 103         cfolder = ConstFold.instance(context);
 104         target = Target.instance(context);
 105         types = Types.instance(context);
 106         diags = JCDiagnostic.Factory.instance(context);
 107         annotate = Annotate.instance(context);
 108 
 109         Options options = Options.instance(context);
 110 
 111         Source source = Source.instance(context);
 112         allowGenerics = source.allowGenerics();
 113         allowVarargs = source.allowVarargs();
 114         allowEnums = source.allowEnums();
 115         allowBoxing = source.allowBoxing();
 116         allowCovariantReturns = source.allowCovariantReturns();
 117         allowAnonOuterThis = source.allowAnonOuterThis();


 118         relax = (options.get("-retrofit") != null ||
 119                  options.get("-relax") != null);
 120         useBeforeDeclarationWarning = options.get("useBeforeDeclarationWarning") != null;
 121         allowInvokedynamic = options.get("invokedynamic") != null;
 122         enableSunApiLintControl = options.get("enableSunApiLintControl") != null;
 123     }
 124 
 125     /** Switch: relax some constraints for retrofit mode.
 126      */
 127     boolean relax;
 128 
 129     /** Switch: support generics?
 130      */
 131     boolean allowGenerics;
 132 
 133     /** Switch: allow variable-arity methods.
 134      */
 135     boolean allowVarargs;
 136 
 137     /** Switch: support enums?


 150      * objects during constructor call?
 151      */
 152     boolean allowAnonOuterThis;
 153 
 154     /** Switch: allow invokedynamic syntax
 155      */
 156     boolean allowInvokedynamic;
 157 
 158     /**
 159      * Switch: warn about use of variable before declaration?
 160      * RFE: 6425594
 161      */
 162     boolean useBeforeDeclarationWarning;
 163 
 164     /**
 165      * Switch: allow lint infrastructure to control Sun proprietary
 166      * API warnings.
 167      */
 168     boolean enableSunApiLintControl;
 169 










 170     /** Check kind and type of given tree against protokind and prototype.
 171      *  If check succeeds, store type in tree and return it.
 172      *  If check fails, store errType in tree and return it.
 173      *  No checks are performed if the prototype is a method type.
 174      *  It is not necessary in this case since we know that kind and type
 175      *  are correct.
 176      *
 177      *  @param tree     The tree whose kind and type is checked
 178      *  @param owntype  The computed type of the tree
 179      *  @param ownkind  The computed kind of the tree
 180      *  @param pkind    The expected kind (or: protokind) of the tree
 181      *  @param pt       The expected type (or: prototype) of the tree
 182      */
 183     Type check(JCTree tree, Type owntype, int ownkind, int pkind, Type pt) {
 184         if (owntype.tag != ERROR && pt.tag != METHOD && pt.tag != FORALL) {
 185             if ((ownkind & ~pkind) == 0) {
 186                 owntype = chk.checkType(tree.pos(), owntype, pt);
 187             } else {
 188                 log.error(tree.pos(), "unexpected.type",
 189                           kindNames(pkind),


 869                 log.error(tree.pos(), "label.already.in.use",
 870                           tree.label);
 871                 break;
 872             }
 873             env1 = env1.next;
 874         }
 875 
 876         attribStat(tree.body, env.dup(tree));
 877         result = null;
 878     }
 879 
 880     public void visitSwitch(JCSwitch tree) {
 881         Type seltype = attribExpr(tree.selector, env);
 882 
 883         Env<AttrContext> switchEnv =
 884             env.dup(tree, env.info.dup(env.info.scope.dup()));
 885 
 886         boolean enumSwitch =
 887             allowEnums &&
 888             (seltype.tsym.flags() & Flags.ENUM) != 0;
 889         if (!enumSwitch)








 890             seltype = chk.checkType(tree.selector.pos(), seltype, syms.intType);
 891 

 892         // Attribute all cases and
 893         // check that there are no duplicate case labels or default clauses.
 894         Set<Object> labels = new HashSet<Object>(); // The set of case labels.
 895         boolean hasDefault = false;      // Is there a default label?
 896         for (List<JCCase> l = tree.cases; l.nonEmpty(); l = l.tail) {
 897             JCCase c = l.head;
 898             Env<AttrContext> caseEnv =
 899                 switchEnv.dup(c, env.info.dup(switchEnv.info.scope.dup()));
 900             if (c.pat != null) {
 901                 if (enumSwitch) {
 902                     Symbol sym = enumConstant(c.pat, seltype);
 903                     if (sym == null) {
 904                         log.error(c.pat.pos(), "enum.const.req");
 905                     } else if (!labels.add(sym)) {
 906                         log.error(c.pos(), "duplicate.case.label");
 907                     }
 908                 } else {
 909                     Type pattype = attribExpr(c.pat, switchEnv, seltype);
 910                     if (pattype.tag != ERROR) {
 911                         if (pattype.constValue() == null) {
 912                             log.error(c.pat.pos(), "const.expr.req");

 913                         } else if (labels.contains(pattype.constValue())) {
 914                             log.error(c.pos(), "duplicate.case.label");
 915                         } else {
 916                             labels.add(pattype.constValue());
 917                         }
 918                     }
 919                 }
 920             } else if (hasDefault) {
 921                 log.error(c.pos(), "duplicate.default.label");
 922             } else {
 923                 hasDefault = true;
 924             }
 925             attribStats(c.stats, caseEnv);
 926             caseEnv.info.scope.leave();
 927             addVars(c.stats, switchEnv.info.scope);
 928         }
 929 
 930         switchEnv.info.scope.leave();
 931         result = null;
 932     }




  98         rs = Resolve.instance(context);
  99         chk = Check.instance(context);
 100         memberEnter = MemberEnter.instance(context);
 101         make = TreeMaker.instance(context);
 102         enter = Enter.instance(context);
 103         cfolder = ConstFold.instance(context);
 104         target = Target.instance(context);
 105         types = Types.instance(context);
 106         diags = JCDiagnostic.Factory.instance(context);
 107         annotate = Annotate.instance(context);
 108 
 109         Options options = Options.instance(context);
 110 
 111         Source source = Source.instance(context);
 112         allowGenerics = source.allowGenerics();
 113         allowVarargs = source.allowVarargs();
 114         allowEnums = source.allowEnums();
 115         allowBoxing = source.allowBoxing();
 116         allowCovariantReturns = source.allowCovariantReturns();
 117         allowAnonOuterThis = source.allowAnonOuterThis();
 118         allowStringsInSwitch = source.allowStringsInSwitch();
 119         sourceName = source.name;
 120         relax = (options.get("-retrofit") != null ||
 121                  options.get("-relax") != null);
 122         useBeforeDeclarationWarning = options.get("useBeforeDeclarationWarning") != null;
 123         allowInvokedynamic = options.get("invokedynamic") != null;
 124         enableSunApiLintControl = options.get("enableSunApiLintControl") != null;
 125     }
 126 
 127     /** Switch: relax some constraints for retrofit mode.
 128      */
 129     boolean relax;
 130 
 131     /** Switch: support generics?
 132      */
 133     boolean allowGenerics;
 134 
 135     /** Switch: allow variable-arity methods.
 136      */
 137     boolean allowVarargs;
 138 
 139     /** Switch: support enums?


 152      * objects during constructor call?
 153      */
 154     boolean allowAnonOuterThis;
 155 
 156     /** Switch: allow invokedynamic syntax
 157      */
 158     boolean allowInvokedynamic;
 159 
 160     /**
 161      * Switch: warn about use of variable before declaration?
 162      * RFE: 6425594
 163      */
 164     boolean useBeforeDeclarationWarning;
 165 
 166     /**
 167      * Switch: allow lint infrastructure to control Sun proprietary
 168      * API warnings.
 169      */
 170     boolean enableSunApiLintControl;
 171 
 172     /**
 173      * Switch: allow strings in switch?
 174      */
 175     boolean allowStringsInSwitch;
 176 
 177     /**
 178      * Switch: name of source level; used for error reporting.
 179      */
 180     String sourceName;
 181 
 182     /** Check kind and type of given tree against protokind and prototype.
 183      *  If check succeeds, store type in tree and return it.
 184      *  If check fails, store errType in tree and return it.
 185      *  No checks are performed if the prototype is a method type.
 186      *  It is not necessary in this case since we know that kind and type
 187      *  are correct.
 188      *
 189      *  @param tree     The tree whose kind and type is checked
 190      *  @param owntype  The computed type of the tree
 191      *  @param ownkind  The computed kind of the tree
 192      *  @param pkind    The expected kind (or: protokind) of the tree
 193      *  @param pt       The expected type (or: prototype) of the tree
 194      */
 195     Type check(JCTree tree, Type owntype, int ownkind, int pkind, Type pt) {
 196         if (owntype.tag != ERROR && pt.tag != METHOD && pt.tag != FORALL) {
 197             if ((ownkind & ~pkind) == 0) {
 198                 owntype = chk.checkType(tree.pos(), owntype, pt);
 199             } else {
 200                 log.error(tree.pos(), "unexpected.type",
 201                           kindNames(pkind),


 881                 log.error(tree.pos(), "label.already.in.use",
 882                           tree.label);
 883                 break;
 884             }
 885             env1 = env1.next;
 886         }
 887 
 888         attribStat(tree.body, env.dup(tree));
 889         result = null;
 890     }
 891 
 892     public void visitSwitch(JCSwitch tree) {
 893         Type seltype = attribExpr(tree.selector, env);
 894 
 895         Env<AttrContext> switchEnv =
 896             env.dup(tree, env.info.dup(env.info.scope.dup()));
 897 
 898         boolean enumSwitch =
 899             allowEnums &&
 900             (seltype.tsym.flags() & Flags.ENUM) != 0;
 901         boolean stringSwitch = false;
 902         if (types.isSameType(seltype, syms.stringType)) {
 903             if (allowStringsInSwitch) {
 904                 stringSwitch = true;
 905             } else {
 906                 log.error(tree.selector.pos(), "string.switch.not.supported.in.source", sourceName);
 907             }
 908         }
 909         if (!enumSwitch && !stringSwitch)
 910             seltype = chk.checkType(tree.selector.pos(), seltype, syms.intType);
 911 
 912 
 913         // Attribute all cases and
 914         // check that there are no duplicate case labels or default clauses.
 915         Set<Object> labels = new HashSet<Object>(); // The set of case labels.
 916         boolean hasDefault = false;      // Is there a default label?
 917         for (List<JCCase> l = tree.cases; l.nonEmpty(); l = l.tail) {
 918             JCCase c = l.head;
 919             Env<AttrContext> caseEnv =
 920                 switchEnv.dup(c, env.info.dup(switchEnv.info.scope.dup()));
 921             if (c.pat != null) {
 922                 if (enumSwitch) {
 923                     Symbol sym = enumConstant(c.pat, seltype);
 924                     if (sym == null) {
 925                         log.error(c.pat.pos(), "enum.const.req");
 926                     } else if (!labels.add(sym)) {
 927                         log.error(c.pos(), "duplicate.case.label");
 928                     }
 929                 } else {
 930                     Type pattype = attribExpr(c.pat, switchEnv, seltype);
 931                     if (pattype.tag != ERROR) {
 932                         if (pattype.constValue() == null) {
 933                             log.error(c.pat.pos(),
 934                                       (stringSwitch ? "string.const.req" : "const.expr.req"));
 935                         } else if (labels.contains(pattype.constValue())) {
 936                             log.error(c.pos(), "duplicate.case.label");
 937                         } else {
 938                             labels.add(pattype.constValue());
 939                         }
 940                     }
 941                 }
 942             } else if (hasDefault) {
 943                 log.error(c.pos(), "duplicate.default.label");
 944             } else {
 945                 hasDefault = true;
 946             }
 947             attribStats(c.stats, caseEnv);
 948             caseEnv.info.scope.leave();
 949             addVars(c.stats, switchEnv.info.scope);
 950         }
 951 
 952         switchEnv.info.scope.leave();
 953         result = null;
 954     }