< prev index next >

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

Print this page




 136         typeEnter = TypeEnter.instance(context);
 137         make = TreeMaker.instance(context);
 138         enter = Enter.instance(context);
 139         infer = Infer.instance(context);
 140         analyzer = Analyzer.instance(context);
 141         deferredAttr = DeferredAttr.instance(context);
 142         cfolder = ConstFold.instance(context);
 143         target = Target.instance(context);
 144         types = Types.instance(context);
 145         diags = JCDiagnostic.Factory.instance(context);
 146         annotate = Annotate.instance(context);
 147         typeAnnotations = TypeAnnotations.instance(context);
 148         deferredLintHandler = DeferredLintHandler.instance(context);
 149         typeEnvs = TypeEnvs.instance(context);
 150         dependencies = Dependencies.instance(context);
 151         argumentAttr = ArgumentAttr.instance(context);
 152 
 153         Options options = Options.instance(context);
 154 
 155         Source source = Source.instance(context);
 156         allowStringsInSwitch = Feature.STRINGS_IN_SWITCH.allowedInSource(source);
 157         allowPoly = Feature.POLY.allowedInSource(source);
 158         allowTypeAnnos = Feature.TYPE_ANNOTATIONS.allowedInSource(source);
 159         allowLambda = Feature.LAMBDA.allowedInSource(source);
 160         allowDefaultMethods = Feature.DEFAULT_METHODS.allowedInSource(source);
 161         allowStaticInterfaceMethods = Feature.STATIC_INTERFACE_METHODS.allowedInSource(source);
 162         sourceName = source.name;
 163         useBeforeDeclarationWarning = options.isSet("useBeforeDeclarationWarning");
 164 
 165         statInfo = new ResultInfo(KindSelector.NIL, Type.noType);
 166         varAssignmentInfo = new ResultInfo(KindSelector.ASG, Type.noType);
 167         unknownExprInfo = new ResultInfo(KindSelector.VAL, Type.noType);
 168         methodAttrInfo = new MethodAttrInfo();
 169         unknownTypeInfo = new ResultInfo(KindSelector.TYP, Type.noType);
 170         unknownTypeExprInfo = new ResultInfo(KindSelector.VAL_TYP, Type.noType);
 171         recoveryInfo = new RecoveryInfo(deferredAttr.emptyDeferredAttrContext);
 172     }
 173 
 174     /** Switch: support target-typing inference
 175      */
 176     boolean allowPoly;


 181 
 182     /** Switch: support lambda expressions ?
 183      */
 184     boolean allowLambda;
 185 
 186     /** Switch: support default methods ?
 187      */
 188     boolean allowDefaultMethods;
 189 
 190     /** Switch: static interface methods enabled?
 191      */
 192     boolean allowStaticInterfaceMethods;
 193 
 194     /**
 195      * Switch: warn about use of variable before declaration?
 196      * RFE: 6425594
 197      */
 198     boolean useBeforeDeclarationWarning;
 199 
 200     /**
 201      * Switch: allow strings in switch?
 202      */
 203     boolean allowStringsInSwitch;
 204 
 205     /**
 206      * Switch: name of source level; used for error reporting.
 207      */
 208     String sourceName;
 209 
 210     /** Check kind and type of given tree against protokind and prototype.
 211      *  If check succeeds, store type in tree and return it.
 212      *  If check fails, store errType in tree and return it.
 213      *  No checks are performed if the prototype is a method type.
 214      *  It is not necessary in this case since we know that kind and type
 215      *  are correct.
 216      *
 217      *  @param tree     The tree whose kind and type is checked
 218      *  @param found    The computed type of the tree
 219      *  @param ownkind  The computed kind of the tree
 220      *  @param resultInfo  The expected result of the tree
 221      */
 222     Type check(final JCTree tree,
 223                final Type found,
 224                final KindSelector ownkind,
 225                final ResultInfo resultInfo) {


1386                           Errors.LabelAlreadyInUse(tree.label));
1387                 break;
1388             }
1389             env1 = env1.next;
1390         }
1391 
1392         attribStat(tree.body, env.dup(tree));
1393         result = null;
1394     }
1395 
1396     public void visitSwitch(JCSwitch tree) {
1397         Type seltype = attribExpr(tree.selector, env);
1398 
1399         Env<AttrContext> switchEnv =
1400             env.dup(tree, env.info.dup(env.info.scope.dup()));
1401 
1402         try {
1403 
1404             boolean enumSwitch = (seltype.tsym.flags() & Flags.ENUM) != 0;
1405             boolean stringSwitch = types.isSameType(seltype, syms.stringType);
1406             if (stringSwitch && !allowStringsInSwitch) {
1407                 log.error(DiagnosticFlag.SOURCE_LEVEL, tree.selector.pos(), Feature.STRINGS_IN_SWITCH.error(sourceName));
1408             }
1409             if (!enumSwitch && !stringSwitch)
1410                 seltype = chk.checkType(tree.selector.pos(), seltype, syms.intType);
1411 
1412             // Attribute all cases and
1413             // check that there are no duplicate case labels or default clauses.
1414             Set<Object> labels = new HashSet<>(); // The set of case labels.
1415             boolean hasDefault = false;      // Is there a default label?
1416             for (List<JCCase> l = tree.cases; l.nonEmpty(); l = l.tail) {
1417                 JCCase c = l.head;
1418                 if (c.pat != null) {
1419                     if (enumSwitch) {
1420                         Symbol sym = enumConstant(c.pat, seltype);
1421                         if (sym == null) {
1422                             log.error(c.pat.pos(), Errors.EnumLabelMustBeUnqualifiedEnum);
1423                         } else if (!labels.add(sym)) {
1424                             log.error(c.pos(), Errors.DuplicateCaseLabel);
1425                         }
1426                     } else {
1427                         Type pattype = attribExpr(c.pat, switchEnv, seltype);
1428                         if (!pattype.hasTag(ERROR)) {




 136         typeEnter = TypeEnter.instance(context);
 137         make = TreeMaker.instance(context);
 138         enter = Enter.instance(context);
 139         infer = Infer.instance(context);
 140         analyzer = Analyzer.instance(context);
 141         deferredAttr = DeferredAttr.instance(context);
 142         cfolder = ConstFold.instance(context);
 143         target = Target.instance(context);
 144         types = Types.instance(context);
 145         diags = JCDiagnostic.Factory.instance(context);
 146         annotate = Annotate.instance(context);
 147         typeAnnotations = TypeAnnotations.instance(context);
 148         deferredLintHandler = DeferredLintHandler.instance(context);
 149         typeEnvs = TypeEnvs.instance(context);
 150         dependencies = Dependencies.instance(context);
 151         argumentAttr = ArgumentAttr.instance(context);
 152 
 153         Options options = Options.instance(context);
 154 
 155         Source source = Source.instance(context);

 156         allowPoly = Feature.POLY.allowedInSource(source);
 157         allowTypeAnnos = Feature.TYPE_ANNOTATIONS.allowedInSource(source);
 158         allowLambda = Feature.LAMBDA.allowedInSource(source);
 159         allowDefaultMethods = Feature.DEFAULT_METHODS.allowedInSource(source);
 160         allowStaticInterfaceMethods = Feature.STATIC_INTERFACE_METHODS.allowedInSource(source);
 161         sourceName = source.name;
 162         useBeforeDeclarationWarning = options.isSet("useBeforeDeclarationWarning");
 163 
 164         statInfo = new ResultInfo(KindSelector.NIL, Type.noType);
 165         varAssignmentInfo = new ResultInfo(KindSelector.ASG, Type.noType);
 166         unknownExprInfo = new ResultInfo(KindSelector.VAL, Type.noType);
 167         methodAttrInfo = new MethodAttrInfo();
 168         unknownTypeInfo = new ResultInfo(KindSelector.TYP, Type.noType);
 169         unknownTypeExprInfo = new ResultInfo(KindSelector.VAL_TYP, Type.noType);
 170         recoveryInfo = new RecoveryInfo(deferredAttr.emptyDeferredAttrContext);
 171     }
 172 
 173     /** Switch: support target-typing inference
 174      */
 175     boolean allowPoly;


 180 
 181     /** Switch: support lambda expressions ?
 182      */
 183     boolean allowLambda;
 184 
 185     /** Switch: support default methods ?
 186      */
 187     boolean allowDefaultMethods;
 188 
 189     /** Switch: static interface methods enabled?
 190      */
 191     boolean allowStaticInterfaceMethods;
 192 
 193     /**
 194      * Switch: warn about use of variable before declaration?
 195      * RFE: 6425594
 196      */
 197     boolean useBeforeDeclarationWarning;
 198 
 199     /**





 200      * Switch: name of source level; used for error reporting.
 201      */
 202     String sourceName;
 203 
 204     /** Check kind and type of given tree against protokind and prototype.
 205      *  If check succeeds, store type in tree and return it.
 206      *  If check fails, store errType in tree and return it.
 207      *  No checks are performed if the prototype is a method type.
 208      *  It is not necessary in this case since we know that kind and type
 209      *  are correct.
 210      *
 211      *  @param tree     The tree whose kind and type is checked
 212      *  @param found    The computed type of the tree
 213      *  @param ownkind  The computed kind of the tree
 214      *  @param resultInfo  The expected result of the tree
 215      */
 216     Type check(final JCTree tree,
 217                final Type found,
 218                final KindSelector ownkind,
 219                final ResultInfo resultInfo) {


1380                           Errors.LabelAlreadyInUse(tree.label));
1381                 break;
1382             }
1383             env1 = env1.next;
1384         }
1385 
1386         attribStat(tree.body, env.dup(tree));
1387         result = null;
1388     }
1389 
1390     public void visitSwitch(JCSwitch tree) {
1391         Type seltype = attribExpr(tree.selector, env);
1392 
1393         Env<AttrContext> switchEnv =
1394             env.dup(tree, env.info.dup(env.info.scope.dup()));
1395 
1396         try {
1397 
1398             boolean enumSwitch = (seltype.tsym.flags() & Flags.ENUM) != 0;
1399             boolean stringSwitch = types.isSameType(seltype, syms.stringType);



1400             if (!enumSwitch && !stringSwitch)
1401                 seltype = chk.checkType(tree.selector.pos(), seltype, syms.intType);
1402 
1403             // Attribute all cases and
1404             // check that there are no duplicate case labels or default clauses.
1405             Set<Object> labels = new HashSet<>(); // The set of case labels.
1406             boolean hasDefault = false;      // Is there a default label?
1407             for (List<JCCase> l = tree.cases; l.nonEmpty(); l = l.tail) {
1408                 JCCase c = l.head;
1409                 if (c.pat != null) {
1410                     if (enumSwitch) {
1411                         Symbol sym = enumConstant(c.pat, seltype);
1412                         if (sym == null) {
1413                             log.error(c.pat.pos(), Errors.EnumLabelMustBeUnqualifiedEnum);
1414                         } else if (!labels.add(sym)) {
1415                             log.error(c.pos(), Errors.DuplicateCaseLabel);
1416                         }
1417                     } else {
1418                         Type pattype = attribExpr(c.pat, switchEnv, seltype);
1419                         if (!pattype.hasTag(ERROR)) {


< prev index next >