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

changeset 1268
af6a4c24f4e3
parent 1238
e28a06a3c5d9
child 1269
37dc15c68760
equal deleted inserted replaced
1267:f43aded513e7 1268:af6a4c24f4e3
1527 // Set method symbol to resolved constructor... 1527 // Set method symbol to resolved constructor...
1528 TreeInfo.setSymbol(tree.meth, sym); 1528 TreeInfo.setSymbol(tree.meth, sym);
1529 1529
1530 // ...and check that it is legal in the current context. 1530 // ...and check that it is legal in the current context.
1531 // (this will also set the tree's type) 1531 // (this will also set the tree's type)
1532 Type mpt = newMethTemplate(argtypes, typeargtypes); 1532 Type mpt = newMethodTemplate(resultInfo.pt, argtypes, typeargtypes);
1533 checkId(tree.meth, site, sym, localEnv, new ResultInfo(MTH, mpt), 1533 checkId(tree.meth, site, sym, localEnv, new ResultInfo(MTH, mpt),
1534 tree.varargsElement != null); 1534 tree.varargsElement != null);
1535 } 1535 }
1536 // Otherwise, `site' is an error type and we do nothing 1536 // Otherwise, `site' is an error type and we do nothing
1537 } 1537 }
1543 typeargtypes = attribAnyTypes(tree.typeargs, localEnv); 1543 typeargtypes = attribAnyTypes(tree.typeargs, localEnv);
1544 1544
1545 // ... and attribute the method using as a prototype a methodtype 1545 // ... and attribute the method using as a prototype a methodtype
1546 // whose formal argument types is exactly the list of actual 1546 // whose formal argument types is exactly the list of actual
1547 // arguments (this will also set the method symbol). 1547 // arguments (this will also set the method symbol).
1548 Type mpt = newMethTemplate(argtypes, typeargtypes); 1548 Type mpt = newMethodTemplate(resultInfo.pt, argtypes, typeargtypes);
1549 localEnv.info.varArgs = false; 1549 localEnv.info.varArgs = false;
1550 Type mtype = attribExpr(tree.meth, localEnv, mpt); 1550 Type mtype = attribExpr(tree.meth, localEnv, mpt);
1551 1551
1552 // Compute the result type. 1552 // Compute the result type.
1553 Type restype = mtype.getReturnType(); 1553 Type restype = mtype.getReturnType();
1606 return false; 1606 return false;
1607 } 1607 }
1608 1608
1609 /** Obtain a method type with given argument types. 1609 /** Obtain a method type with given argument types.
1610 */ 1610 */
1611 Type newMethTemplate(List<Type> argtypes, List<Type> typeargtypes) { 1611 Type newMethodTemplate(Type restype, List<Type> argtypes, List<Type> typeargtypes) {
1612 MethodType mt = new MethodType(argtypes, null, null, syms.methodClass); 1612 MethodType mt = new MethodType(argtypes, restype, null, syms.methodClass);
1613 return (typeargtypes == null) ? mt : (Type)new ForAll(typeargtypes, mt); 1613 return (typeargtypes == null) ? mt : (Type)new ForAll(typeargtypes, mt);
1614 } 1614 }
1615 1615
1616 public void visitNewClass(JCNewClass tree) { 1616 public void visitNewClass(JCNewClass tree) {
1617 Type owntype = types.createErrorType(tree.type); 1617 Type owntype = types.createErrorType(tree.type);
1881 site, 1881 site,
1882 argtypes, 1882 argtypes,
1883 typeargtypes); 1883 typeargtypes);
1884 1884
1885 if (constructor.kind == MTH) { 1885 if (constructor.kind == MTH) {
1886 clazztype = checkMethod(site,
1887 constructor,
1888 localEnv,
1889 tree.args,
1890 argtypes,
1891 typeargtypes,
1892 localEnv.info.varArgs).getReturnType();
1893 } else {
1894 clazztype = syms.errType;
1895 }
1896
1897 if (clazztype.tag == FORALL && !resultInfo.pt.isErroneous()) {
1898 try { 1886 try {
1899 clazztype = resultInfo.checkContext.rawInstantiatePoly((ForAll)clazztype, pt(), Warner.noWarnings); 1887 clazztype = rawCheckMethod(site,
1900 } catch (Infer.InferenceException ex) { 1888 constructor,
1889 resultInfo,
1890 localEnv,
1891 tree.args,
1892 argtypes,
1893 typeargtypes,
1894 localEnv.info.varArgs).getReturnType();
1895 } catch (Resolve.InapplicableMethodException ex) {
1901 //an error occurred while inferring uninstantiated type-variables 1896 //an error occurred while inferring uninstantiated type-variables
1902 resultInfo.checkContext.report(tree.clazz.pos(), clazztype, resultInfo.pt, 1897 resultInfo.checkContext.report(tree.clazz.pos(), clazztype, resultInfo.pt,
1903 diags.fragment("cant.apply.diamond.1", diags.fragment("diamond", clazztype.tsym), ex.diagnostic)); 1898 diags.fragment("cant.apply.diamond.1", diags.fragment("diamond", clazztype.tsym), ex.diagnostic));
1904 } 1899 clazztype = syms.errType;
1900 }
1901 } else {
1902 clazztype = syms.errType;
1905 } 1903 }
1906 1904
1907 return chk.checkClassType(tree.clazz.pos(), clazztype, true); 1905 return chk.checkClassType(tree.clazz.pos(), clazztype, true);
1908 } 1906 }
1909 1907
2253 boolean selectSuperPrev = env.info.selectSuper; 2251 boolean selectSuperPrev = env.info.selectSuper;
2254 env.info.selectSuper = 2252 env.info.selectSuper =
2255 sitesym != null && 2253 sitesym != null &&
2256 sitesym.name == names._super; 2254 sitesym.name == names._super;
2257 2255
2258 // If selected expression is polymorphic, strip
2259 // type parameters and remember in env.info.tvars, so that
2260 // they can be added later (in Attr.checkId and Infer.instantiateMethod).
2261 if (tree.selected.type.tag == FORALL) {
2262 ForAll pstype = (ForAll)tree.selected.type;
2263 env.info.tvars = pstype.tvars;
2264 site = tree.selected.type = pstype.qtype;
2265 }
2266
2267 // Determine the symbol represented by the selection. 2256 // Determine the symbol represented by the selection.
2268 env.info.varArgs = false; 2257 env.info.varArgs = false;
2269 Symbol sym = selectSym(tree, sitesym, site, env, resultInfo); 2258 Symbol sym = selectSym(tree, sitesym, site, env, resultInfo);
2270 if (sym.exists() && !isType(sym) && (pkind() & (PCK | TYP)) != 0) { 2259 if (sym.exists() && !isType(sym) && (pkind() & (PCK | TYP)) != 0) {
2271 site = capture(site); 2260 site = capture(site);
2345 } 2334 }
2346 } 2335 }
2347 2336
2348 env.info.selectSuper = selectSuperPrev; 2337 env.info.selectSuper = selectSuperPrev;
2349 result = checkId(tree, site, sym, env, resultInfo, varArgs); 2338 result = checkId(tree, site, sym, env, resultInfo, varArgs);
2350 env.info.tvars = List.nil();
2351 } 2339 }
2352 //where 2340 //where
2353 /** Determine symbol referenced by a Select expression, 2341 /** Determine symbol referenced by a Select expression,
2354 * 2342 *
2355 * @param tree The select tree. 2343 * @param tree The select tree.
2528 owntype = (sym.owner.kind == TYP && 2516 owntype = (sym.owner.kind == TYP &&
2529 sym.name != names._this && sym.name != names._super) 2517 sym.name != names._this && sym.name != names._super)
2530 ? types.memberType(site, sym) 2518 ? types.memberType(site, sym)
2531 : sym.type; 2519 : sym.type;
2532 2520
2533 if (env.info.tvars.nonEmpty()) {
2534 Type owntype1 = new ForAll(env.info.tvars, owntype);
2535 for (List<Type> l = env.info.tvars; l.nonEmpty(); l = l.tail)
2536 if (!owntype.contains(l.head)) {
2537 log.error(tree.pos(), "undetermined.type", owntype1);
2538 owntype1 = types.createErrorType(owntype1);
2539 }
2540 owntype = owntype1;
2541 }
2542
2543 // If the variable is a constant, record constant value in 2521 // If the variable is a constant, record constant value in
2544 // computed type. 2522 // computed type.
2545 if (v.getConstValue() != null && isStaticReference(tree)) 2523 if (v.getConstValue() != null && isStaticReference(tree))
2546 owntype = owntype.constType(v.getConstValue()); 2524 owntype = owntype.constType(v.getConstValue());
2547 2525
2549 owntype = capture(owntype); // capture "names as expressions" 2527 owntype = capture(owntype); // capture "names as expressions"
2550 } 2528 }
2551 break; 2529 break;
2552 case MTH: { 2530 case MTH: {
2553 JCMethodInvocation app = (JCMethodInvocation)env.tree; 2531 JCMethodInvocation app = (JCMethodInvocation)env.tree;
2554 owntype = checkMethod(site, sym, env, app.args, 2532 owntype = checkMethod(site, sym,
2555 resultInfo.pt.getParameterTypes(), resultInfo.pt.getTypeArguments(), 2533 new ResultInfo(VAL, resultInfo.pt.getReturnType(), resultInfo.checkContext),
2556 env.info.varArgs); 2534 env, app.args, resultInfo.pt.getParameterTypes(),
2535 resultInfo.pt.getTypeArguments(), env.info.varArgs);
2557 break; 2536 break;
2558 } 2537 }
2559 case PCK: case ERR: 2538 case PCK: case ERR:
2560 owntype = sym.type; 2539 owntype = sym.type;
2561 break; 2540 break;
2690 /** 2669 /**
2691 * Check that method arguments conform to its instantiation. 2670 * Check that method arguments conform to its instantiation.
2692 **/ 2671 **/
2693 public Type checkMethod(Type site, 2672 public Type checkMethod(Type site,
2694 Symbol sym, 2673 Symbol sym,
2674 ResultInfo resultInfo,
2675 Env<AttrContext> env,
2676 final List<JCExpression> argtrees,
2677 List<Type> argtypes,
2678 List<Type> typeargtypes,
2679 boolean useVarargs) {
2680 try {
2681 return rawCheckMethod(site, sym, resultInfo, env, argtrees, argtypes, typeargtypes, useVarargs);
2682 } catch (Resolve.InapplicableMethodException ex) {
2683 String key = ex.getDiagnostic() == null ?
2684 "cant.apply.symbol" :
2685 "cant.apply.symbol.1";
2686 log.error(env.tree.pos, key,
2687 Kinds.kindName(sym),
2688 sym.name == names.init ? sym.owner.name : sym.name,
2689 rs.methodArguments(sym.type.getParameterTypes()),
2690 rs.methodArguments(argtypes),
2691 Kinds.kindName(sym.owner),
2692 sym.owner.type,
2693 ex.getDiagnostic());
2694 return types.createErrorType(site);
2695 }
2696 }
2697
2698 private Type rawCheckMethod(Type site,
2699 Symbol sym,
2700 ResultInfo resultInfo,
2695 Env<AttrContext> env, 2701 Env<AttrContext> env,
2696 final List<JCExpression> argtrees, 2702 final List<JCExpression> argtrees,
2697 List<Type> argtypes, 2703 List<Type> argtypes,
2698 List<Type> typeargtypes, 2704 List<Type> typeargtypes,
2699 boolean useVarargs) { 2705 boolean useVarargs) {
2715 // Compute the identifier's instantiated type. 2721 // Compute the identifier's instantiated type.
2716 // For methods, we need to compute the instance type by 2722 // For methods, we need to compute the instance type by
2717 // Resolve.instantiate from the symbol's type as well as 2723 // Resolve.instantiate from the symbol's type as well as
2718 // any type arguments and value arguments. 2724 // any type arguments and value arguments.
2719 noteWarner.clear(); 2725 noteWarner.clear();
2720 Type owntype = rs.instantiate(env, 2726 Type owntype = rs.rawInstantiate(env,
2721 site, 2727 site,
2722 sym, 2728 sym,
2723 argtypes, 2729 resultInfo,
2724 typeargtypes, 2730 argtypes,
2725 true, 2731 typeargtypes,
2726 useVarargs, 2732 true,
2727 noteWarner); 2733 useVarargs,
2734 noteWarner);
2728 2735
2729 boolean unchecked = noteWarner.hasNonSilentLint(LintCategory.UNCHECKED); 2736 boolean unchecked = noteWarner.hasNonSilentLint(LintCategory.UNCHECKED);
2730 2737
2731 // If this fails, something went wrong; we should not have 2738 return chk.checkMethod(owntype, sym, env, argtrees, argtypes, useVarargs, unchecked);
2732 // found the identifier in the first place.
2733 if (owntype == null) {
2734 if (!pt().isErroneous())
2735 log.error(env.tree.pos(),
2736 "internal.error.cant.instantiate",
2737 sym, site,
2738 Type.toString(pt().getParameterTypes()));
2739 owntype = types.createErrorType(site);
2740 return types.createErrorType(site);
2741 } else if (owntype.getReturnType().tag == FORALL && !unchecked) {
2742 return owntype;
2743 } else {
2744 return chk.checkMethod(owntype, sym, env, argtrees, argtypes, useVarargs, unchecked);
2745 }
2746 } 2739 }
2747 2740
2748 /** 2741 /**
2749 * Check that constructor arguments conform to its instantiation. 2742 * Check that constructor arguments conform to its instantiation.
2750 **/ 2743 **/
2753 Env<AttrContext> env, 2746 Env<AttrContext> env,
2754 final List<JCExpression> argtrees, 2747 final List<JCExpression> argtrees,
2755 List<Type> argtypes, 2748 List<Type> argtypes,
2756 List<Type> typeargtypes, 2749 List<Type> typeargtypes,
2757 boolean useVarargs) { 2750 boolean useVarargs) {
2758 Type owntype = checkMethod(site, sym, env, argtrees, argtypes, typeargtypes, useVarargs); 2751 Type owntype = checkMethod(site, sym, new ResultInfo(VAL, syms.voidType), env, argtrees, argtypes, typeargtypes, useVarargs);
2759 chk.checkType(env.tree.pos(), owntype.getReturnType(), syms.voidType); 2752 chk.checkType(env.tree.pos(), owntype.getReturnType(), syms.voidType);
2760 return owntype; 2753 return owntype;
2761 } 2754 }
2762 2755
2763 public void visitLiteral(JCLiteral tree) { 2756 public void visitLiteral(JCLiteral tree) {

mercurial