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

changeset 1850
6debfa63a4a1
parent 1843
be62183f938a
child 1851
e9ebff1840e5
equal deleted inserted replaced
1845:be10ac0081b2 1850:6debfa63a4a1
132 allowBoxing = source.allowBoxing(); 132 allowBoxing = source.allowBoxing();
133 allowCovariantReturns = source.allowCovariantReturns(); 133 allowCovariantReturns = source.allowCovariantReturns();
134 allowAnonOuterThis = source.allowAnonOuterThis(); 134 allowAnonOuterThis = source.allowAnonOuterThis();
135 allowStringsInSwitch = source.allowStringsInSwitch(); 135 allowStringsInSwitch = source.allowStringsInSwitch();
136 allowPoly = source.allowPoly(); 136 allowPoly = source.allowPoly();
137 allowTypeAnnos = source.allowTypeAnnotations();
137 allowLambda = source.allowLambda(); 138 allowLambda = source.allowLambda();
138 allowDefaultMethods = source.allowDefaultMethods(); 139 allowDefaultMethods = source.allowDefaultMethods();
139 sourceName = source.name; 140 sourceName = source.name;
140 relax = (options.isSet("-retrofit") || 141 relax = (options.isSet("-retrofit") ||
141 options.isSet("-relax")); 142 options.isSet("-relax"));
145 identifyLambdaCandidate = options.getBoolean("identifyLambdaCandidate", false); 146 identifyLambdaCandidate = options.getBoolean("identifyLambdaCandidate", false);
146 147
147 statInfo = new ResultInfo(NIL, Type.noType); 148 statInfo = new ResultInfo(NIL, Type.noType);
148 varInfo = new ResultInfo(VAR, Type.noType); 149 varInfo = new ResultInfo(VAR, Type.noType);
149 unknownExprInfo = new ResultInfo(VAL, Type.noType); 150 unknownExprInfo = new ResultInfo(VAL, Type.noType);
151 unknownAnyPolyInfo = new ResultInfo(VAL, Infer.anyPoly);
150 unknownTypeInfo = new ResultInfo(TYP, Type.noType); 152 unknownTypeInfo = new ResultInfo(TYP, Type.noType);
151 unknownTypeExprInfo = new ResultInfo(Kinds.TYP | Kinds.VAL, Type.noType); 153 unknownTypeExprInfo = new ResultInfo(Kinds.TYP | Kinds.VAL, Type.noType);
152 recoveryInfo = new RecoveryInfo(deferredAttr.emptyDeferredAttrContext); 154 recoveryInfo = new RecoveryInfo(deferredAttr.emptyDeferredAttrContext);
153 } 155 }
154 156
157 boolean relax; 159 boolean relax;
158 160
159 /** Switch: support target-typing inference 161 /** Switch: support target-typing inference
160 */ 162 */
161 boolean allowPoly; 163 boolean allowPoly;
164
165 /** Switch: support type annotations.
166 */
167 boolean allowTypeAnnos;
162 168
163 /** Switch: support generics? 169 /** Switch: support generics?
164 */ 170 */
165 boolean allowGenerics; 171 boolean allowGenerics;
166 172
238 */ 244 */
239 Type check(final JCTree tree, final Type found, final int ownkind, final ResultInfo resultInfo) { 245 Type check(final JCTree tree, final Type found, final int ownkind, final ResultInfo resultInfo) {
240 InferenceContext inferenceContext = resultInfo.checkContext.inferenceContext(); 246 InferenceContext inferenceContext = resultInfo.checkContext.inferenceContext();
241 Type owntype = found; 247 Type owntype = found;
242 if (!owntype.hasTag(ERROR) && !resultInfo.pt.hasTag(METHOD) && !resultInfo.pt.hasTag(FORALL)) { 248 if (!owntype.hasTag(ERROR) && !resultInfo.pt.hasTag(METHOD) && !resultInfo.pt.hasTag(FORALL)) {
243 if (inferenceContext.free(found)) { 249 if (allowPoly && inferenceContext.free(found)) {
244 inferenceContext.addFreeTypeListener(List.of(found, resultInfo.pt), new FreeTypeListener() { 250 inferenceContext.addFreeTypeListener(List.of(found, resultInfo.pt), new FreeTypeListener() {
245 @Override 251 @Override
246 public void typesInferred(InferenceContext inferenceContext) { 252 public void typesInferred(InferenceContext inferenceContext) {
247 ResultInfo pendingResult = 253 ResultInfo pendingResult =
248 resultInfo.dup(inferenceContext.asInstType(resultInfo.pt)); 254 resultInfo.dup(inferenceContext.asInstType(resultInfo.pt));
556 } 562 }
557 } 563 }
558 564
559 final ResultInfo statInfo; 565 final ResultInfo statInfo;
560 final ResultInfo varInfo; 566 final ResultInfo varInfo;
567 final ResultInfo unknownAnyPolyInfo;
561 final ResultInfo unknownExprInfo; 568 final ResultInfo unknownExprInfo;
562 final ResultInfo unknownTypeInfo; 569 final ResultInfo unknownTypeInfo;
563 final ResultInfo unknownTypeExprInfo; 570 final ResultInfo unknownTypeExprInfo;
564 final ResultInfo recoveryInfo; 571 final ResultInfo recoveryInfo;
565 572
662 <T extends JCTree> void attribStats(List<T> trees, Env<AttrContext> env) { 669 <T extends JCTree> void attribStats(List<T> trees, Env<AttrContext> env) {
663 for (List<T> l = trees; l.nonEmpty(); l = l.tail) 670 for (List<T> l = trees; l.nonEmpty(); l = l.tail)
664 attribStat(l.head, env); 671 attribStat(l.head, env);
665 } 672 }
666 673
667 /** Attribute the arguments in a method call, returning a list of types. 674 /** Attribute the arguments in a method call, returning the method kind.
668 */ 675 */
669 List<Type> attribArgs(List<JCExpression> trees, Env<AttrContext> env) { 676 int attribArgs(List<JCExpression> trees, Env<AttrContext> env, ListBuffer<Type> argtypes) {
670 ListBuffer<Type> argtypes = new ListBuffer<Type>(); 677 int kind = VAL;
671 for (JCExpression arg : trees) { 678 for (JCExpression arg : trees) {
672 Type argtype = allowPoly && deferredAttr.isDeferred(env, arg) ? 679 Type argtype;
673 deferredAttr.new DeferredType(arg, env) : 680 if (allowPoly && deferredAttr.isDeferred(env, arg)) {
674 chk.checkNonVoid(arg, attribExpr(arg, env, Infer.anyPoly)); 681 argtype = deferredAttr.new DeferredType(arg, env);
682 kind |= POLY;
683 } else {
684 argtype = chk.checkNonVoid(arg, attribTree(arg, env, unknownAnyPolyInfo));
685 }
675 argtypes.append(argtype); 686 argtypes.append(argtype);
676 } 687 }
677 return argtypes.toList(); 688 return kind;
678 } 689 }
679 690
680 /** Attribute a type argument list, returning a list of types. 691 /** Attribute a type argument list, returning a list of types.
681 * Caller is responsible for calling checkRefTypes. 692 * Caller is responsible for calling checkRefTypes.
682 */ 693 */
1737 Name methName = TreeInfo.name(tree.meth); 1748 Name methName = TreeInfo.name(tree.meth);
1738 1749
1739 boolean isConstructorCall = 1750 boolean isConstructorCall =
1740 methName == names._this || methName == names._super; 1751 methName == names._this || methName == names._super;
1741 1752
1753 ListBuffer<Type> argtypesBuf = ListBuffer.lb();
1742 if (isConstructorCall) { 1754 if (isConstructorCall) {
1743 // We are seeing a ...this(...) or ...super(...) call. 1755 // We are seeing a ...this(...) or ...super(...) call.
1744 // Check that this is the first statement in a constructor. 1756 // Check that this is the first statement in a constructor.
1745 if (checkFirstConstructorStat(tree, env)) { 1757 if (checkFirstConstructorStat(tree, env)) {
1746 1758
1747 // Record the fact 1759 // Record the fact
1748 // that this is a constructor call (using isSelfCall). 1760 // that this is a constructor call (using isSelfCall).
1749 localEnv.info.isSelfCall = true; 1761 localEnv.info.isSelfCall = true;
1750 1762
1751 // Attribute arguments, yielding list of argument types. 1763 // Attribute arguments, yielding list of argument types.
1752 argtypes = attribArgs(tree.args, localEnv); 1764 attribArgs(tree.args, localEnv, argtypesBuf);
1765 argtypes = argtypesBuf.toList();
1753 typeargtypes = attribTypes(tree.typeargs, localEnv); 1766 typeargtypes = attribTypes(tree.typeargs, localEnv);
1754 1767
1755 // Variable `site' points to the class in which the called 1768 // Variable `site' points to the class in which the called
1756 // constructor is defined. 1769 // constructor is defined.
1757 Type site = env.enclClass.sym.type; 1770 Type site = env.enclClass.sym.type;
1819 } 1832 }
1820 result = tree.type = syms.voidType; 1833 result = tree.type = syms.voidType;
1821 } else { 1834 } else {
1822 // Otherwise, we are seeing a regular method call. 1835 // Otherwise, we are seeing a regular method call.
1823 // Attribute the arguments, yielding list of argument types, ... 1836 // Attribute the arguments, yielding list of argument types, ...
1824 argtypes = attribArgs(tree.args, localEnv); 1837 int kind = attribArgs(tree.args, localEnv, argtypesBuf);
1838 argtypes = argtypesBuf.toList();
1825 typeargtypes = attribAnyTypes(tree.typeargs, localEnv); 1839 typeargtypes = attribAnyTypes(tree.typeargs, localEnv);
1826 1840
1827 // ... and attribute the method using as a prototype a methodtype 1841 // ... and attribute the method using as a prototype a methodtype
1828 // whose formal argument types is exactly the list of actual 1842 // whose formal argument types is exactly the list of actual
1829 // arguments (this will also set the method symbol). 1843 // arguments (this will also set the method symbol).
1830 Type mpt = newMethodTemplate(resultInfo.pt, argtypes, typeargtypes); 1844 Type mpt = newMethodTemplate(resultInfo.pt, argtypes, typeargtypes);
1831 localEnv.info.pendingResolutionPhase = null; 1845 localEnv.info.pendingResolutionPhase = null;
1832 Type mtype = attribTree(tree.meth, localEnv, new ResultInfo(VAL, mpt, resultInfo.checkContext)); 1846 Type mtype = attribTree(tree.meth, localEnv, new ResultInfo(kind, mpt, resultInfo.checkContext));
1833 1847
1834 // Compute the result type. 1848 // Compute the result type.
1835 Type restype = mtype.getReturnType(); 1849 Type restype = mtype.getReturnType();
1836 if (restype.hasTag(WILDCARD)) 1850 if (restype.hasTag(WILDCARD))
1837 throw new AssertionError(mtype); 1851 throw new AssertionError(mtype);
1997 // Check for the existence of an apropos outer instance 2011 // Check for the existence of an apropos outer instance
1998 rs.resolveImplicitThis(tree.pos(), env, clazztype); 2012 rs.resolveImplicitThis(tree.pos(), env, clazztype);
1999 } 2013 }
2000 2014
2001 // Attribute constructor arguments. 2015 // Attribute constructor arguments.
2002 List<Type> argtypes = attribArgs(tree.args, localEnv); 2016 ListBuffer<Type> argtypesBuf = ListBuffer.lb();
2017 int pkind = attribArgs(tree.args, localEnv, argtypesBuf);
2018 List<Type> argtypes = argtypesBuf.toList();
2003 List<Type> typeargtypes = attribTypes(tree.typeargs, localEnv); 2019 List<Type> typeargtypes = attribTypes(tree.typeargs, localEnv);
2004 2020
2005 // If we have made no mistakes in the class type... 2021 // If we have made no mistakes in the class type...
2006 if (clazztype.hasTag(CLASS)) { 2022 if (clazztype.hasTag(CLASS)) {
2007 // Enums may not be instantiated except implicitly 2023 // Enums may not be instantiated except implicitly
2084 if (cdef == null) { //do not check twice! 2100 if (cdef == null) { //do not check twice!
2085 tree.constructorType = checkId(tree, 2101 tree.constructorType = checkId(tree,
2086 clazztype, 2102 clazztype,
2087 tree.constructor, 2103 tree.constructor,
2088 rsEnv, 2104 rsEnv,
2089 new ResultInfo(MTH, newMethodTemplate(syms.voidType, argtypes, typeargtypes))); 2105 new ResultInfo(pkind, newMethodTemplate(syms.voidType, argtypes, typeargtypes)));
2090 if (rsEnv.info.lastResolveVarargs()) 2106 if (rsEnv.info.lastResolveVarargs())
2091 Assert.check(tree.constructorType.isErroneous() || tree.varargsElement != null); 2107 Assert.check(tree.constructorType.isErroneous() || tree.varargsElement != null);
2092 } 2108 }
2093 findDiamondIfNeeded(localEnv, tree, clazztype); 2109 if (cdef == null &&
2110 !clazztype.isErroneous() &&
2111 clazztype.getTypeArguments().nonEmpty() &&
2112 findDiamonds) {
2113 findDiamond(localEnv, tree, clazztype);
2114 }
2094 } 2115 }
2095 2116
2096 if (cdef != null) { 2117 if (cdef != null) {
2097 // We are seeing an anonymous class instance creation. 2118 // We are seeing an anonymous class instance creation.
2098 // In this case, the class instance creation 2119 // In this case, the class instance creation
2155 tree.constructor = sym; 2176 tree.constructor = sym;
2156 tree.constructorType = checkId(tree, 2177 tree.constructorType = checkId(tree,
2157 clazztype, 2178 clazztype,
2158 tree.constructor, 2179 tree.constructor,
2159 localEnv, 2180 localEnv,
2160 new ResultInfo(VAL, newMethodTemplate(syms.voidType, argtypes, typeargtypes))); 2181 new ResultInfo(pkind, newMethodTemplate(syms.voidType, argtypes, typeargtypes)));
2161 } else { 2182 } else {
2162 if (tree.clazz.hasTag(ANNOTATED_TYPE)) { 2183 if (tree.clazz.hasTag(ANNOTATED_TYPE)) {
2163 checkForDeclarationAnnotations(((JCAnnotatedType) tree.clazz).annotations, 2184 checkForDeclarationAnnotations(((JCAnnotatedType) tree.clazz).annotations,
2164 tree.clazz.type.tsym); 2185 tree.clazz.type.tsym);
2165 } 2186 }
2170 } 2191 }
2171 result = check(tree, owntype, VAL, resultInfo); 2192 result = check(tree, owntype, VAL, resultInfo);
2172 chk.validate(tree.typeargs, localEnv); 2193 chk.validate(tree.typeargs, localEnv);
2173 } 2194 }
2174 //where 2195 //where
2175 void findDiamondIfNeeded(Env<AttrContext> env, JCNewClass tree, Type clazztype) { 2196 void findDiamond(Env<AttrContext> env, JCNewClass tree, Type clazztype) {
2176 if (tree.def == null && 2197 JCTypeApply ta = (JCTypeApply)tree.clazz;
2177 !clazztype.isErroneous() && 2198 List<JCExpression> prevTypeargs = ta.arguments;
2178 clazztype.getTypeArguments().nonEmpty() && 2199 try {
2179 findDiamonds) { 2200 //create a 'fake' diamond AST node by removing type-argument trees
2180 JCTypeApply ta = (JCTypeApply)tree.clazz; 2201 ta.arguments = List.nil();
2181 List<JCExpression> prevTypeargs = ta.arguments; 2202 ResultInfo findDiamondResult = new ResultInfo(VAL,
2182 try { 2203 resultInfo.checkContext.inferenceContext().free(resultInfo.pt) ? Type.noType : pt());
2183 //create a 'fake' diamond AST node by removing type-argument trees 2204 Type inferred = deferredAttr.attribSpeculative(tree, env, findDiamondResult).type;
2184 ta.arguments = List.nil(); 2205 Type polyPt = allowPoly ?
2185 ResultInfo findDiamondResult = new ResultInfo(VAL, 2206 syms.objectType :
2186 resultInfo.checkContext.inferenceContext().free(resultInfo.pt) ? Type.noType : pt()); 2207 clazztype;
2187 Type inferred = deferredAttr.attribSpeculative(tree, env, findDiamondResult).type; 2208 if (!inferred.isErroneous() &&
2188 Type polyPt = allowPoly ? 2209 types.isAssignable(inferred, pt().hasTag(NONE) ? polyPt : pt(), types.noWarnings)) {
2189 syms.objectType : 2210 String key = types.isSameType(clazztype, inferred) ?
2190 clazztype; 2211 "diamond.redundant.args" :
2191 if (!inferred.isErroneous() && 2212 "diamond.redundant.args.1";
2192 types.isAssignable(inferred, pt().hasTag(NONE) ? polyPt : pt(), types.noWarnings)) { 2213 log.warning(tree.clazz.pos(), key, clazztype, inferred);
2193 String key = types.isSameType(clazztype, inferred) ? 2214 }
2194 "diamond.redundant.args" : 2215 } finally {
2195 "diamond.redundant.args.1"; 2216 ta.arguments = prevTypeargs;
2196 log.warning(tree.clazz.pos(), key, clazztype, inferred);
2197 }
2198 } finally {
2199 ta.arguments = prevTypeargs;
2200 }
2201 } 2217 }
2202 } 2218 }
2203 2219
2204 private void checkLambdaCandidate(JCNewClass tree, ClassSymbol csym, Type clazztype) { 2220 private void checkLambdaCandidate(JCNewClass tree, ClassSymbol csym, Type clazztype) {
2205 if (allowLambda && 2221 if (allowLambda &&
3049 //see Infer.instantiatePolymorphicSignatureInstance() 3065 //see Infer.instantiatePolymorphicSignatureInstance()
3050 Env<AttrContext> localEnv = env.dup(tree); 3066 Env<AttrContext> localEnv = env.dup(tree);
3051 //should we propagate the target type? 3067 //should we propagate the target type?
3052 final ResultInfo castInfo; 3068 final ResultInfo castInfo;
3053 JCExpression expr = TreeInfo.skipParens(tree.expr); 3069 JCExpression expr = TreeInfo.skipParens(tree.expr);
3054 boolean isPoly = expr.hasTag(LAMBDA) || expr.hasTag(REFERENCE); 3070 boolean isPoly = allowPoly && (expr.hasTag(LAMBDA) || expr.hasTag(REFERENCE));
3055 if (isPoly) { 3071 if (isPoly) {
3056 //expression is a poly - we need to propagate target type info 3072 //expression is a poly - we need to propagate target type info
3057 castInfo = new ResultInfo(VAL, clazztype, new Check.NestedCheckContext(resultInfo.checkContext) { 3073 castInfo = new ResultInfo(VAL, clazztype, new Check.NestedCheckContext(resultInfo.checkContext) {
3058 @Override 3074 @Override
3059 public boolean compatible(Type found, Type req, Warner warn) { 3075 public boolean compatible(Type found, Type req, Warner warn) {
3438 Type checkMethodIdInternal(JCTree tree, 3454 Type checkMethodIdInternal(JCTree tree,
3439 Type site, 3455 Type site,
3440 Symbol sym, 3456 Symbol sym,
3441 Env<AttrContext> env, 3457 Env<AttrContext> env,
3442 ResultInfo resultInfo) { 3458 ResultInfo resultInfo) {
3443 Type pt = resultInfo.pt.map(deferredAttr.new RecoveryDeferredTypeMap(AttrMode.SPECULATIVE, sym, env.info.pendingResolutionPhase)); 3459 if ((resultInfo.pkind & POLY) != 0) {
3444 Type owntype = checkIdInternal(tree, site, sym, pt, env, resultInfo); 3460 Type pt = resultInfo.pt.map(deferredAttr.new RecoveryDeferredTypeMap(AttrMode.SPECULATIVE, sym, env.info.pendingResolutionPhase));
3445 resultInfo.pt.map(deferredAttr.new RecoveryDeferredTypeMap(AttrMode.CHECK, sym, env.info.pendingResolutionPhase)); 3461 Type owntype = checkIdInternal(tree, site, sym, pt, env, resultInfo);
3446 return owntype; 3462 resultInfo.pt.map(deferredAttr.new RecoveryDeferredTypeMap(AttrMode.CHECK, sym, env.info.pendingResolutionPhase));
3463 return owntype;
3464 } else {
3465 return checkIdInternal(tree, site, sym, resultInfo.pt, env, resultInfo);
3466 }
3447 } 3467 }
3448 3468
3449 Type checkIdInternal(JCTree tree, 3469 Type checkIdInternal(JCTree tree,
3450 Type site, 3470 Type site,
3451 Symbol sym, 3471 Symbol sym,
3539 owntype = capture(owntype); // capture "names as expressions" 3559 owntype = capture(owntype); // capture "names as expressions"
3540 } 3560 }
3541 break; 3561 break;
3542 case MTH: { 3562 case MTH: {
3543 owntype = checkMethod(site, sym, 3563 owntype = checkMethod(site, sym,
3544 new ResultInfo(VAL, resultInfo.pt.getReturnType(), resultInfo.checkContext), 3564 new ResultInfo(resultInfo.pkind, resultInfo.pt.getReturnType(), resultInfo.checkContext),
3545 env, TreeInfo.args(env.tree), resultInfo.pt.getParameterTypes(), 3565 env, TreeInfo.args(env.tree), resultInfo.pt.getParameterTypes(),
3546 resultInfo.pt.getTypeArguments()); 3566 resultInfo.pt.getTypeArguments());
3547 break; 3567 break;
3548 } 3568 }
3549 case PCK: case ERR: 3569 case PCK: case ERR:
4287 isSerializable(c) && 4307 isSerializable(c) &&
4288 (c.flags() & Flags.ENUM) == 0 && 4308 (c.flags() & Flags.ENUM) == 0 &&
4289 (c.flags() & ABSTRACT) == 0) { 4309 (c.flags() & ABSTRACT) == 0) {
4290 checkSerialVersionUID(tree, c); 4310 checkSerialVersionUID(tree, c);
4291 } 4311 }
4292 4312 if (allowTypeAnnos) {
4293 // Correctly organize the postions of the type annotations 4313 // Correctly organize the postions of the type annotations
4294 TypeAnnotations.organizeTypeAnnotationsBodies(this.syms, this.names, this.log, tree); 4314 TypeAnnotations.organizeTypeAnnotationsBodies(this.syms, this.names, this.log, tree);
4295 4315
4296 // Check type annotations applicability rules 4316 // Check type annotations applicability rules
4297 validateTypeAnnotations(tree); 4317 validateTypeAnnotations(tree);
4318 }
4298 } 4319 }
4299 // where 4320 // where
4300 /** get a diagnostic position for an attribute of Type t, or null if attribute missing */ 4321 /** get a diagnostic position for an attribute of Type t, or null if attribute missing */
4301 private DiagnosticPosition getDiagnosticPosition(JCClassDecl tree, Type t) { 4322 private DiagnosticPosition getDiagnosticPosition(JCClassDecl tree, Type t) {
4302 for(List<JCAnnotation> al = tree.mods.annotations; !al.isEmpty(); al = al.tail) { 4323 for(List<JCAnnotation> al = tree.mods.annotations; !al.isEmpty(); al = al.tail) {

mercurial