langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java
changeset 25445 603f0c93d5c9
parent 25443 9187d77f2c64
child 25844 48eab270456c
equal deleted inserted replaced
25444:27045478cf23 25445:603f0c93d5c9
   125         typeEnvs = TypeEnvs.instance(context);
   125         typeEnvs = TypeEnvs.instance(context);
   126 
   126 
   127         Options options = Options.instance(context);
   127         Options options = Options.instance(context);
   128 
   128 
   129         Source source = Source.instance(context);
   129         Source source = Source.instance(context);
   130         allowGenerics = source.allowGenerics();
       
   131         allowVarargs = source.allowVarargs();
       
   132         allowEnums = source.allowEnums();
       
   133         allowBoxing = source.allowBoxing();
       
   134         allowCovariantReturns = source.allowCovariantReturns();
       
   135         allowAnonOuterThis = source.allowAnonOuterThis();
       
   136         allowStringsInSwitch = source.allowStringsInSwitch();
   130         allowStringsInSwitch = source.allowStringsInSwitch();
   137         allowPoly = source.allowPoly();
   131         allowPoly = source.allowPoly();
   138         allowTypeAnnos = source.allowTypeAnnotations();
   132         allowTypeAnnos = source.allowTypeAnnotations();
   139         allowLambda = source.allowLambda();
   133         allowLambda = source.allowLambda();
   140         allowDefaultMethods = source.allowDefaultMethods();
   134         allowDefaultMethods = source.allowDefaultMethods();
   166 
   160 
   167     /** Switch: support type annotations.
   161     /** Switch: support type annotations.
   168      */
   162      */
   169     boolean allowTypeAnnos;
   163     boolean allowTypeAnnos;
   170 
   164 
   171     /** Switch: support generics?
       
   172      */
       
   173     boolean allowGenerics;
       
   174 
       
   175     /** Switch: allow variable-arity methods.
       
   176      */
       
   177     boolean allowVarargs;
       
   178 
       
   179     /** Switch: support enums?
       
   180      */
       
   181     boolean allowEnums;
       
   182 
       
   183     /** Switch: support boxing and unboxing?
       
   184      */
       
   185     boolean allowBoxing;
       
   186 
       
   187     /** Switch: support covariant result types?
       
   188      */
       
   189     boolean allowCovariantReturns;
       
   190 
       
   191     /** Switch: support lambda expressions ?
   165     /** Switch: support lambda expressions ?
   192      */
   166      */
   193     boolean allowLambda;
   167     boolean allowLambda;
   194 
   168 
   195     /** Switch: support default methods ?
   169     /** Switch: support default methods ?
   197     boolean allowDefaultMethods;
   171     boolean allowDefaultMethods;
   198 
   172 
   199     /** Switch: static interface methods enabled?
   173     /** Switch: static interface methods enabled?
   200      */
   174      */
   201     boolean allowStaticInterfaceMethods;
   175     boolean allowStaticInterfaceMethods;
   202 
       
   203     /** Switch: allow references to surrounding object from anonymous
       
   204      * objects during constructor call?
       
   205      */
       
   206     boolean allowAnonOuterThis;
       
   207 
   176 
   208     /** Switch: generates a warning if diamond can be safely applied
   177     /** Switch: generates a warning if diamond can be safely applied
   209      *  to a given new expression
   178      *  to a given new expression
   210      */
   179      */
   211     boolean findDiamonds;
   180     boolean findDiamonds;
   819             if (t.getUpperBound() == null) {
   788             if (t.getUpperBound() == null) {
   820                 log.error(tree.pos(), "illegal.forward.ref");
   789                 log.error(tree.pos(), "illegal.forward.ref");
   821                 return types.createErrorType(t);
   790                 return types.createErrorType(t);
   822             }
   791             }
   823         } else {
   792         } else {
   824             t = chk.checkClassType(tree.pos(), t, checkExtensible|!allowGenerics);
   793             t = chk.checkClassType(tree.pos(), t, checkExtensible);
   825         }
   794         }
   826         if (interfaceExpected && (t.tsym.flags() & INTERFACE) == 0) {
   795         if (interfaceExpected && (t.tsym.flags() & INTERFACE) == 0) {
   827             log.error(tree.pos(), "intf.expected.here");
   796             log.error(tree.pos(), "intf.expected.here");
   828             // return errType is necessary since otherwise there might
   797             // return errType is necessary since otherwise there might
   829             // be undetected cycles which cause attribution to loop
   798             // be undetected cycles which cause attribution to loop
  1260         Env<AttrContext> switchEnv =
  1229         Env<AttrContext> switchEnv =
  1261             env.dup(tree, env.info.dup(env.info.scope.dup()));
  1230             env.dup(tree, env.info.dup(env.info.scope.dup()));
  1262 
  1231 
  1263         try {
  1232         try {
  1264 
  1233 
  1265             boolean enumSwitch =
  1234             boolean enumSwitch = (seltype.tsym.flags() & Flags.ENUM) != 0;
  1266                 allowEnums &&
       
  1267                 (seltype.tsym.flags() & Flags.ENUM) != 0;
       
  1268             boolean stringSwitch = false;
  1235             boolean stringSwitch = false;
  1269             if (types.isSameType(seltype, syms.stringType)) {
  1236             if (types.isSameType(seltype, syms.stringType)) {
  1270                 if (allowStringsInSwitch) {
  1237                 if (allowStringsInSwitch) {
  1271                     stringSwitch = true;
  1238                     stringSwitch = true;
  1272                 } else {
  1239                 } else {
  1552                                Type thentype, Type elsetype) {
  1519                                Type thentype, Type elsetype) {
  1553             // If same type, that is the result
  1520             // If same type, that is the result
  1554             if (types.isSameType(thentype, elsetype))
  1521             if (types.isSameType(thentype, elsetype))
  1555                 return thentype.baseType();
  1522                 return thentype.baseType();
  1556 
  1523 
  1557             Type thenUnboxed = (!allowBoxing || thentype.isPrimitive())
  1524             Type thenUnboxed = (thentype.isPrimitive())
  1558                 ? thentype : types.unboxedType(thentype);
  1525                 ? thentype : types.unboxedType(thentype);
  1559             Type elseUnboxed = (!allowBoxing || elsetype.isPrimitive())
  1526             Type elseUnboxed = (elsetype.isPrimitive())
  1560                 ? elsetype : types.unboxedType(elsetype);
  1527                 ? elsetype : types.unboxedType(elsetype);
  1561 
  1528 
  1562             // Otherwise, if both arms can be converted to a numeric
  1529             // Otherwise, if both arms can be converted to a numeric
  1563             // type, return the least numeric type that fits both arms
  1530             // type, return the least numeric type that fits both arms
  1564             // (i.e. return larger of the two, or return int if one
  1531             // (i.e. return larger of the two, or return int if one
  1586                     }
  1553                     }
  1587                 }
  1554                 }
  1588             }
  1555             }
  1589 
  1556 
  1590             // Those were all the cases that could result in a primitive
  1557             // Those were all the cases that could result in a primitive
  1591             if (allowBoxing) {
  1558             if (thentype.isPrimitive())
  1592                 if (thentype.isPrimitive())
  1559                 thentype = types.boxedClass(thentype).type;
  1593                     thentype = types.boxedClass(thentype).type;
  1560             if (elsetype.isPrimitive())
  1594                 if (elsetype.isPrimitive())
  1561                 elsetype = types.boxedClass(elsetype).type;
  1595                     elsetype = types.boxedClass(elsetype).type;
       
  1596             }
       
  1597 
  1562 
  1598             if (types.isSubtype(thentype, elsetype))
  1563             if (types.isSubtype(thentype, elsetype))
  1599                 return elsetype.baseType();
  1564                 return elsetype.baseType();
  1600             if (types.isSubtype(elsetype, thentype))
  1565             if (types.isSubtype(elsetype, thentype))
  1601                 return thentype.baseType();
  1566                 return thentype.baseType();
  1602 
  1567 
  1603             if (!allowBoxing || thentype.hasTag(VOID) || elsetype.hasTag(VOID)) {
  1568             if (thentype.hasTag(VOID) || elsetype.hasTag(VOID)) {
  1604                 log.error(pos, "neither.conditional.subtype",
  1569                 log.error(pos, "neither.conditional.subtype",
  1605                           thentype, elsetype);
  1570                           thentype, elsetype);
  1606                 return thentype.baseType();
  1571                 return thentype.baseType();
  1607             }
  1572             }
  1608 
  1573 
  1851                                   site.tsym);
  1816                                   site.tsym);
  1852                     }
  1817                     }
  1853 
  1818 
  1854                     // if we're calling a java.lang.Enum constructor,
  1819                     // if we're calling a java.lang.Enum constructor,
  1855                     // prefix the implicit String and int parameters
  1820                     // prefix the implicit String and int parameters
  1856                     if (site.tsym == syms.enumSym && allowEnums)
  1821                     if (site.tsym == syms.enumSym)
  1857                         argtypes = argtypes.prepend(syms.intType).prepend(syms.stringType);
  1822                         argtypes = argtypes.prepend(syms.intType).prepend(syms.stringType);
  1858 
  1823 
  1859                     // Resolve the called constructor under the assumption
  1824                     // Resolve the called constructor under the assumption
  1860                     // that we are referring to a superclass instance of the
  1825                     // that we are referring to a superclass instance of the
  1861                     // current instance (JLS ???).
  1826                     // current instance (JLS ???).
  1930 
  1895 
  1931         chk.validate(tree.typeargs, localEnv);
  1896         chk.validate(tree.typeargs, localEnv);
  1932     }
  1897     }
  1933     //where
  1898     //where
  1934         Type adjustMethodReturnType(Type qualifierType, Name methodName, List<Type> argtypes, Type restype) {
  1899         Type adjustMethodReturnType(Type qualifierType, Name methodName, List<Type> argtypes, Type restype) {
  1935             if (allowCovariantReturns &&
  1900             if (methodName == names.clone && types.isArray(qualifierType)) {
  1936                     methodName == names.clone &&
       
  1937                 types.isArray(qualifierType)) {
       
  1938                 // as a special case, array.clone() has a result that is
  1901                 // as a special case, array.clone() has a result that is
  1939                 // the same as static type of the array being cloned
  1902                 // the same as static type of the array being cloned
  1940                 return qualifierType;
  1903                 return qualifierType;
  1941             } else if (allowGenerics &&
  1904             } else if (methodName == names.getClass && argtypes.isEmpty()) {
  1942                     methodName == names.getClass &&
       
  1943                     argtypes.isEmpty()) {
       
  1944                 // as a special case, x.getClass() has type Class<? extends |X|>
  1905                 // as a special case, x.getClass() has type Class<? extends |X|>
  1945                 return new ClassType(restype.getEnclosingType(),
  1906                 return new ClassType(restype.getEnclosingType(),
  1946                               List.<Type>of(new WildcardType(types.erasure(qualifierType),
  1907                               List.<Type>of(new WildcardType(types.erasure(qualifierType),
  1947                                                                BoundKind.EXTENDS,
  1908                                                                BoundKind.EXTENDS,
  1948                                                              syms.boundClass,
  1909                                                              syms.boundClass,
  2131             chk.checkRefTypes(tree.typeargs, typeargtypesbuf.toList());
  2092             chk.checkRefTypes(tree.typeargs, typeargtypesbuf.toList());
  2132 
  2093 
  2133         // If we have made no mistakes in the class type...
  2094         // If we have made no mistakes in the class type...
  2134         if (clazztype.hasTag(CLASS)) {
  2095         if (clazztype.hasTag(CLASS)) {
  2135             // Enums may not be instantiated except implicitly
  2096             // Enums may not be instantiated except implicitly
  2136             if (allowEnums &&
  2097             if ((clazztype.tsym.flags_field & Flags.ENUM) != 0 &&
  2137                 (clazztype.tsym.flags_field&Flags.ENUM) != 0 &&
       
  2138                 (!env.tree.hasTag(VARDEF) ||
  2098                 (!env.tree.hasTag(VARDEF) ||
  2139                  (((JCVariableDecl) env.tree).mods.flags&Flags.ENUM) == 0 ||
  2099                  (((JCVariableDecl) env.tree).mods.flags & Flags.ENUM) == 0 ||
  2140                  ((JCVariableDecl) env.tree).init != tree))
  2100                  ((JCVariableDecl) env.tree).init != tree))
  2141                 log.error(tree.pos(), "enum.cant.be.instantiated");
  2101                 log.error(tree.pos(), "enum.cant.be.instantiated");
  2142             // Check that class is not abstract
  2102             // Check that class is not abstract
  2143             if (cdef == null &&
  2103             if (cdef == null &&
  2144                 (clazztype.tsym.flags() & (ABSTRACT | INTERFACE)) != 0) {
  2104                 (clazztype.tsym.flags() & (ABSTRACT | INTERFACE)) != 0) {
  3368 
  3328 
  3369             // Find environment in which identifier is defined.
  3329             // Find environment in which identifier is defined.
  3370             while (symEnv.outer != null &&
  3330             while (symEnv.outer != null &&
  3371                    !sym.isMemberOf(symEnv.enclClass.sym, types)) {
  3331                    !sym.isMemberOf(symEnv.enclClass.sym, types)) {
  3372                 if ((symEnv.enclClass.sym.flags() & NOOUTERTHIS) != 0)
  3332                 if ((symEnv.enclClass.sym.flags() & NOOUTERTHIS) != 0)
  3373                     noOuterThisPath = !allowAnonOuterThis;
  3333                     noOuterThisPath = false;
  3374                 symEnv = symEnv.outer;
  3334                 symEnv = symEnv.outer;
  3375             }
  3335             }
  3376         }
  3336         }
  3377 
  3337 
  3378         // If symbol is a variable, ...
  3338         // If symbol is a variable, ...
  3590                     return rs.resolveSelf(pos, env, site.tsym, name);
  3550                     return rs.resolveSelf(pos, env, site.tsym, name);
  3591                 } else if (name == names._class) {
  3551                 } else if (name == names._class) {
  3592                     // In this case, we have already made sure in
  3552                     // In this case, we have already made sure in
  3593                     // visitSelect that qualifier expression is a type.
  3553                     // visitSelect that qualifier expression is a type.
  3594                     Type t = syms.classType;
  3554                     Type t = syms.classType;
  3595                     List<Type> typeargs = allowGenerics
  3555                     List<Type> typeargs = List.of(types.erasure(site));
  3596                         ? List.of(types.erasure(site))
       
  3597                         : List.<Type>nil();
       
  3598                     t = new ClassType(t.getEnclosingType(), typeargs, t.tsym);
  3556                     t = new ClassType(t.getEnclosingType(), typeargs, t.tsym);
  3599                     return new VarSymbol(
  3557                     return new VarSymbol(
  3600                         STATIC | PUBLIC | FINAL, names._class, t, site.tsym);
  3558                         STATIC | PUBLIC | FINAL, names._class, t, site.tsym);
  3601                 } else {
  3559                 } else {
  3602                     // We are seeing a plain identifier as selector.
  3560                     // We are seeing a plain identifier as selector.
  3772             case VAR:
  3730             case VAR:
  3773                 VarSymbol v = (VarSymbol)sym;
  3731                 VarSymbol v = (VarSymbol)sym;
  3774                 // Test (4): if symbol is an instance field of a raw type,
  3732                 // Test (4): if symbol is an instance field of a raw type,
  3775                 // which is being assigned to, issue an unchecked warning if
  3733                 // which is being assigned to, issue an unchecked warning if
  3776                 // its type changes under erasure.
  3734                 // its type changes under erasure.
  3777                 if (allowGenerics &&
  3735                 if (resultInfo.pkind == VAR &&
  3778                     resultInfo.pkind == VAR &&
       
  3779                     v.owner.kind == TYP &&
  3736                     v.owner.kind == TYP &&
  3780                     (v.flags() & STATIC) == 0 &&
  3737                     (v.flags() & STATIC) == 0 &&
  3781                     (site.hasTag(CLASS) || site.hasTag(TYPEVAR))) {
  3738                     (site.hasTag(CLASS) || site.hasTag(TYPEVAR))) {
  3782                     Type s = types.asOuterSuper(site, v.owner);
  3739                     Type s = types.asOuterSuper(site, v.owner);
  3783                     if (s != null &&
  3740                     if (s != null &&
  3953                             final List<JCExpression> argtrees,
  3910                             final List<JCExpression> argtrees,
  3954                             List<Type> argtypes,
  3911                             List<Type> argtypes,
  3955                             List<Type> typeargtypes) {
  3912                             List<Type> typeargtypes) {
  3956         // Test (5): if symbol is an instance method of a raw type, issue
  3913         // Test (5): if symbol is an instance method of a raw type, issue
  3957         // an unchecked warning if its argument types change under erasure.
  3914         // an unchecked warning if its argument types change under erasure.
  3958         if (allowGenerics &&
  3915         if ((sym.flags() & STATIC) == 0 &&
  3959             (sym.flags() & STATIC) == 0 &&
       
  3960             (site.hasTag(CLASS) || site.hasTag(TYPEVAR))) {
  3916             (site.hasTag(CLASS) || site.hasTag(TYPEVAR))) {
  3961             Type s = types.asOuterSuper(site, sym.owner);
  3917             Type s = types.asOuterSuper(site, sym.owner);
  3962             if (s != null && s.isRaw() &&
  3918             if (s != null && s.isRaw() &&
  3963                 !types.isSameTypes(sym.type.getParameterTypes(),
  3919                 !types.isSameTypes(sym.type.getParameterTypes(),
  3964                                    sym.erasure(types).getParameterTypes())) {
  3920                                    sym.erasure(types).getParameterTypes())) {