duke@1: /* mcimadamore@1178: * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved. duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@1: * duke@1: * This code is free software; you can redistribute it and/or modify it duke@1: * under the terms of the GNU General Public License version 2 only, as ohair@554: * published by the Free Software Foundation. Oracle designates this duke@1: * particular file as subject to the "Classpath" exception as provided ohair@554: * by Oracle in the LICENSE file that accompanied this code. duke@1: * duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@1: * version 2 for more details (a copy is included in the LICENSE file that duke@1: * accompanied this code). duke@1: * duke@1: * You should have received a copy of the GNU General Public License version duke@1: * 2 along with this work; if not, write to the Free Software Foundation, duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@1: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. duke@1: */ duke@1: duke@1: package com.sun.tools.javac.comp; duke@1: mcimadamore@674: import com.sun.tools.javac.tree.JCTree; mcimadamore@674: import com.sun.tools.javac.tree.JCTree.JCTypeCast; mcimadamore@820: import com.sun.tools.javac.tree.TreeInfo; duke@1: import com.sun.tools.javac.util.*; duke@1: import com.sun.tools.javac.util.List; duke@1: import com.sun.tools.javac.code.*; duke@1: import com.sun.tools.javac.code.Type.*; mcimadamore@299: import com.sun.tools.javac.code.Symbol.*; mcimadamore@1186: import com.sun.tools.javac.comp.Resolve.InapplicableMethodException; mcimadamore@1114: import com.sun.tools.javac.comp.Resolve.VerboseResolutionMode; mcimadamore@1114: import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; duke@1: duke@1: import static com.sun.tools.javac.code.TypeTags.*; duke@1: duke@1: /** Helper class for type parameter inference, used by the attribution phase. duke@1: * jjg@581: *

This is NOT part of any supported API. jjg@581: * If you write code that depends on this, you do so at your own risk. duke@1: * This code and its internal interfaces are subject to change or duke@1: * deletion without notice. duke@1: */ duke@1: public class Infer { duke@1: protected static final Context.Key inferKey = duke@1: new Context.Key(); duke@1: duke@1: /** A value for prototypes that admit any type, including polymorphic ones. */ duke@1: public static final Type anyPoly = new Type(NONE, null); duke@1: duke@1: Symtab syms; duke@1: Types types; mcimadamore@396: Check chk; mcimadamore@299: Resolve rs; mcimadamore@1114: Log log; mcimadamore@89: JCDiagnostic.Factory diags; duke@1: duke@1: public static Infer instance(Context context) { duke@1: Infer instance = context.get(inferKey); duke@1: if (instance == null) duke@1: instance = new Infer(context); duke@1: return instance; duke@1: } duke@1: duke@1: protected Infer(Context context) { duke@1: context.put(inferKey, this); duke@1: syms = Symtab.instance(context); duke@1: types = Types.instance(context); mcimadamore@299: rs = Resolve.instance(context); mcimadamore@1114: log = Log.instance(context); mcimadamore@396: chk = Check.instance(context); mcimadamore@89: diags = JCDiagnostic.Factory.instance(context); mcimadamore@89: ambiguousNoInstanceException = mcimadamore@89: new NoInstanceException(true, diags); mcimadamore@89: unambiguousNoInstanceException = mcimadamore@89: new NoInstanceException(false, diags); mcimadamore@299: invalidInstanceException = mcimadamore@299: new InvalidInstanceException(diags); mcimadamore@299: duke@1: } duke@1: mcimadamore@1186: public static class InferenceException extends InapplicableMethodException { duke@1: private static final long serialVersionUID = 0; duke@1: mcimadamore@299: InferenceException(JCDiagnostic.Factory diags) { mcimadamore@689: super(diags); duke@1: } mcimadamore@299: } mcimadamore@299: mcimadamore@299: public static class NoInstanceException extends InferenceException { mcimadamore@299: private static final long serialVersionUID = 1; mcimadamore@299: mcimadamore@299: boolean isAmbiguous; // exist several incomparable best instances? mcimadamore@299: mcimadamore@299: NoInstanceException(boolean isAmbiguous, JCDiagnostic.Factory diags) { mcimadamore@299: super(diags); mcimadamore@299: this.isAmbiguous = isAmbiguous; duke@1: } duke@1: } mcimadamore@299: mcimadamore@299: public static class InvalidInstanceException extends InferenceException { mcimadamore@299: private static final long serialVersionUID = 2; mcimadamore@299: mcimadamore@299: InvalidInstanceException(JCDiagnostic.Factory diags) { mcimadamore@299: super(diags); mcimadamore@299: } mcimadamore@299: } mcimadamore@299: mcimadamore@89: private final NoInstanceException ambiguousNoInstanceException; mcimadamore@89: private final NoInstanceException unambiguousNoInstanceException; mcimadamore@299: private final InvalidInstanceException invalidInstanceException; duke@1: duke@1: /*************************************************************************** duke@1: * Auxiliary type values and classes duke@1: ***************************************************************************/ duke@1: duke@1: /** A mapping that turns type variables into undetermined type variables. duke@1: */ mcimadamore@1251: List makeUndetvars(List tvars) { mcimadamore@1251: List undetvars = Type.map(tvars, fromTypeVarFun); mcimadamore@1251: for (Type t : undetvars) { mcimadamore@1251: UndetVar uv = (UndetVar)t; mcimadamore@1251: uv.hibounds = types.getBounds((TypeVar)uv.qtype); mcimadamore@1251: } mcimadamore@1251: return undetvars; mcimadamore@1251: } mcimadamore@1251: //where mcimadamore@1251: Mapping fromTypeVarFun = new Mapping("fromTypeVarFun") { mcimadamore@1251: public Type apply(Type t) { mcimadamore@1251: if (t.tag == TYPEVAR) return new UndetVar(t); mcimadamore@1251: else return t.map(this); duke@1: } mcimadamore@1251: }; duke@1: duke@1: /*************************************************************************** duke@1: * Mini/Maximization of UndetVars duke@1: ***************************************************************************/ duke@1: duke@1: /** Instantiate undetermined type variable to its minimal upper bound. duke@1: * Throw a NoInstanceException if this not possible. duke@1: */ duke@1: void maximizeInst(UndetVar that, Warner warn) throws NoInstanceException { mcimadamore@828: List hibounds = Type.filter(that.hibounds, errorFilter); mcimadamore@1251: if (that.eq.isEmpty()) { mcimadamore@828: if (hibounds.isEmpty()) duke@1: that.inst = syms.objectType; mcimadamore@828: else if (hibounds.tail.isEmpty()) mcimadamore@828: that.inst = hibounds.head; mcimadamore@210: else mcimadamore@828: that.inst = types.glb(hibounds); mcimadamore@1251: } else { mcimadamore@1251: that.inst = that.eq.head; duke@1: } mcimadamore@210: if (that.inst == null || mcimadamore@298: that.inst.isErroneous()) mcimadamore@210: throw ambiguousNoInstanceException mcimadamore@210: .setMessage("no.unique.maximal.instance.exists", mcimadamore@828: that.qtype, hibounds); duke@1: } duke@1: mcimadamore@828: private Filter errorFilter = new Filter() { mcimadamore@828: @Override mcimadamore@828: public boolean accepts(Type t) { mcimadamore@828: return !t.isErroneous(); mcimadamore@828: } mcimadamore@828: }; mcimadamore@828: jjg@110: /** Instantiate undetermined type variable to the lub of all its lower bounds. duke@1: * Throw a NoInstanceException if this not possible. duke@1: */ duke@1: void minimizeInst(UndetVar that, Warner warn) throws NoInstanceException { mcimadamore@828: List lobounds = Type.filter(that.lobounds, errorFilter); mcimadamore@1251: if (that.eq.isEmpty()) { mcimadamore@828: if (lobounds.isEmpty()) duke@1: that.inst = syms.botType; mcimadamore@828: else if (lobounds.tail.isEmpty()) mcimadamore@828: that.inst = lobounds.head.isPrimitive() ? syms.errType : lobounds.head; duke@1: else { mcimadamore@828: that.inst = types.lub(lobounds); mcimadamore@5: } jjg@110: if (that.inst == null || that.inst.tag == ERROR) duke@1: throw ambiguousNoInstanceException duke@1: .setMessage("no.unique.minimal.instance.exists", mcimadamore@828: that.qtype, lobounds); mcimadamore@1251: } else { mcimadamore@1251: that.inst = that.eq.head; duke@1: } duke@1: } duke@1: mcimadamore@1186: Type asUndetType(Type t, List undetvars) { mcimadamore@1186: return types.subst(t, inferenceVars(undetvars), undetvars); mcimadamore@1186: } mcimadamore@1186: mcimadamore@1186: List inferenceVars(List undetvars) { mcimadamore@1186: ListBuffer tvars = ListBuffer.lb(); mcimadamore@1186: for (Type uv : undetvars) { mcimadamore@1186: tvars.append(((UndetVar)uv).qtype); mcimadamore@1186: } mcimadamore@1186: return tvars.toList(); mcimadamore@1186: } mcimadamore@1186: duke@1: /*************************************************************************** duke@1: * Exported Methods duke@1: ***************************************************************************/ duke@1: duke@1: /** Try to instantiate expression type `that' to given type `to'. duke@1: * If a maximal instantiation exists which makes this type duke@1: * a subtype of type `to', return the instantiated type. duke@1: * If no instantiation exists, or if several incomparable duke@1: * best instantiations exist throw a NoInstanceException. duke@1: */ mcimadamore@1268: public List instantiateUninferred(DiagnosticPosition pos, mcimadamore@1268: List undetvars, mcimadamore@1268: List tvars, mcimadamore@1268: MethodType mtype, mcimadamore@1268: Attr.ResultInfo resultInfo, mcimadamore@299: Warner warn) throws InferenceException { mcimadamore@1268: Type to = resultInfo.pt; mcimadamore@1268: if (to.tag == NONE) { mcimadamore@1268: to = mtype.getReturnType().tag <= VOID ? mcimadamore@1268: mtype.getReturnType() : syms.objectType; mcimadamore@1268: } mcimadamore@1268: Type qtype1 = types.subst(mtype.getReturnType(), tvars, undetvars); mcimadamore@753: if (!types.isSubtype(qtype1, mcimadamore@753: qtype1.tag == UNDETVAR ? types.boxedTypeOrType(to) : to)) { duke@1: throw unambiguousNoInstanceException mcimadamore@689: .setMessage("infer.no.conforming.instance.exists", mcimadamore@1268: tvars, mtype.getReturnType(), to); duke@1: } duke@1: mcimadamore@1251: List insttypes; mcimadamore@1251: while (true) { mcimadamore@1251: boolean stuck = true; mcimadamore@1251: insttypes = List.nil(); mcimadamore@1251: for (Type t : undetvars) { mcimadamore@1251: UndetVar uv = (UndetVar)t; mcimadamore@1268: if (uv.inst == null && (uv.eq.nonEmpty() || !Type.containsAny(uv.hibounds, tvars))) { mcimadamore@1251: maximizeInst((UndetVar)t, warn); mcimadamore@1251: stuck = false; mcimadamore@1251: } mcimadamore@1251: insttypes = insttypes.append(uv.inst == null ? uv.qtype : uv.inst); mcimadamore@1251: } mcimadamore@1268: if (!Type.containsAny(insttypes, tvars)) { mcimadamore@1251: //all variables have been instantiated - exit mcimadamore@1251: break; mcimadamore@1251: } else if (stuck) { mcimadamore@1251: //some variables could not be instantiated because of cycles in mcimadamore@1251: //upper bounds - provide a (possibly recursive) default instantiation mcimadamore@1251: insttypes = types.subst(insttypes, mcimadamore@1268: tvars, mcimadamore@1268: instantiateAsUninferredVars(undetvars, tvars)); mcimadamore@1251: break; mcimadamore@1251: } else { mcimadamore@1251: //some variables have been instantiated - replace newly instantiated mcimadamore@1251: //variables in remaining upper bounds and continue mcimadamore@1251: for (Type t : undetvars) { mcimadamore@1251: UndetVar uv = (UndetVar)t; mcimadamore@1268: uv.hibounds = types.subst(uv.hibounds, tvars, insttypes); mcimadamore@1251: } mcimadamore@1251: } mcimadamore@635: } mcimadamore@1268: return insttypes; duke@1: } mcimadamore@1251: mcimadamore@1251: /** mcimadamore@1251: * Infer cyclic inference variables as described in 15.12.2.8. mcimadamore@1251: */ mcimadamore@1178: private List instantiateAsUninferredVars(List undetvars, List tvars) { mcimadamore@1178: Assert.check(undetvars.length() == tvars.length()); mcimadamore@1251: ListBuffer insttypes = ListBuffer.lb(); mcimadamore@1251: ListBuffer todo = ListBuffer.lb(); mcimadamore@1251: //step 1 - create fresh tvars mcimadamore@635: for (Type t : undetvars) { mcimadamore@635: UndetVar uv = (UndetVar)t; mcimadamore@1251: if (uv.inst == null) { mcimadamore@1251: TypeSymbol fresh_tvar = new TypeSymbol(Flags.SYNTHETIC, uv.qtype.tsym.name, null, uv.qtype.tsym.owner); mcimadamore@1251: fresh_tvar.type = new TypeVar(fresh_tvar, types.makeCompoundType(uv.hibounds), null); mcimadamore@1251: todo.append(uv); mcimadamore@1251: uv.inst = fresh_tvar.type; mcimadamore@1251: } mcimadamore@1251: insttypes.append(uv.inst); mcimadamore@635: } mcimadamore@1251: //step 2 - replace fresh tvars in their bounds mcimadamore@1178: List formals = tvars; mcimadamore@1251: for (Type t : todo) { mcimadamore@1251: UndetVar uv = (UndetVar)t; mcimadamore@1251: TypeVar ct = (TypeVar)uv.inst; mcimadamore@1251: ct.bound = types.glb(types.subst(types.getBounds(ct), tvars, insttypes.toList())); mcimadamore@1251: if (ct.bound.isErroneous()) { mcimadamore@1251: //report inference error if glb fails mcimadamore@1251: reportBoundError(uv, BoundErrorKind.BAD_UPPER); mcimadamore@1251: } mcimadamore@1178: formals = formals.tail; mcimadamore@635: } mcimadamore@1251: return insttypes.toList(); mcimadamore@635: } duke@1: duke@1: /** Instantiate method type `mt' by finding instantiations of duke@1: * `tvars' so that method can be applied to `argtypes'. duke@1: */ mcimadamore@1268: public Type instantiateMethod(Env env, mcimadamore@547: List tvars, duke@1: MethodType mt, mcimadamore@1268: Attr.ResultInfo resultInfo, mcimadamore@1268: Symbol msym, mcimadamore@1268: List argtypes, mcimadamore@1268: boolean allowBoxing, mcimadamore@1268: boolean useVarargs, mcimadamore@1268: Warner warn) throws InferenceException { duke@1: //-System.err.println("instantiateMethod(" + tvars + ", " + mt + ", " + argtypes + ")"); //DEBUG mcimadamore@1268: List undetvars = makeUndetvars(tvars); mcimadamore@689: mcimadamore@1268: List capturedArgs = mcimadamore@1186: rs.checkRawArgumentsAcceptable(env, undetvars, argtypes, mt.getParameterTypes(), mcimadamore@1186: allowBoxing, useVarargs, warn, new InferenceCheckHandler(undetvars)); duke@1: duke@1: // minimize as yet undetermined type variables duke@1: for (Type t : undetvars) duke@1: minimizeInst((UndetVar) t, warn); duke@1: duke@1: /** Type variables instantiated to bottom */ duke@1: ListBuffer restvars = new ListBuffer(); duke@1: mcimadamore@396: /** Undet vars instantiated to bottom */ mcimadamore@396: final ListBuffer restundet = new ListBuffer(); mcimadamore@396: duke@1: /** Instantiated types or TypeVars if under-constrained */ duke@1: ListBuffer insttypes = new ListBuffer(); duke@1: duke@1: /** Instantiated types or UndetVars if under-constrained */ duke@1: ListBuffer undettypes = new ListBuffer(); duke@1: duke@1: for (Type t : undetvars) { duke@1: UndetVar uv = (UndetVar)t; duke@1: if (uv.inst.tag == BOT) { duke@1: restvars.append(uv.qtype); mcimadamore@396: restundet.append(uv); duke@1: insttypes.append(uv.qtype); duke@1: undettypes.append(uv); duke@1: uv.inst = null; duke@1: } else { duke@1: insttypes.append(uv.inst); duke@1: undettypes.append(uv.inst); duke@1: } duke@1: } mcimadamore@1251: checkWithinBounds(tvars, undetvars, insttypes.toList(), warn); duke@1: mcimadamore@299: mt = (MethodType)types.subst(mt, tvars, insttypes.toList()); mcimadamore@299: mcimadamore@1268: if (!restvars.isEmpty() && resultInfo != null) { mcimadamore@1268: List restInferred = mcimadamore@1268: instantiateUninferred(env.tree.pos(), restundet.toList(), restvars.toList(), mt, resultInfo, warn); mcimadamore@1268: checkWithinBounds(tvars, undetvars, mcimadamore@1268: types.subst(insttypes.toList(), restvars.toList(), restInferred), warn); mcimadamore@1268: mt = (MethodType)types.subst(mt, restvars.toList(), restInferred); mcimadamore@1268: if (rs.verboseResolutionMode.contains(VerboseResolutionMode.DEFERRED_INST)) { mcimadamore@1268: log.note(env.tree.pos, "deferred.method.inst", msym, mt, resultInfo.pt); mcimadamore@1268: } duke@1: } mcimadamore@1268: mcimadamore@1268: if (restvars.isEmpty() || resultInfo != null) { mcimadamore@845: // check that actuals conform to inferred formals mcimadamore@845: checkArgumentsAcceptable(env, capturedArgs, mt.getParameterTypes(), allowBoxing, useVarargs, warn); mcimadamore@299: } mcimadamore@1268: // return instantiated version of method type mcimadamore@1268: return mt; duke@1: } duke@1: //where duke@1: mcimadamore@1186: /** inference check handler **/ mcimadamore@1186: class InferenceCheckHandler implements Resolve.MethodCheckHandler { mcimadamore@1186: mcimadamore@1186: List undetvars; mcimadamore@1186: mcimadamore@1186: public InferenceCheckHandler(List undetvars) { mcimadamore@1186: this.undetvars = undetvars; mcimadamore@1186: } mcimadamore@1186: mcimadamore@1186: public InapplicableMethodException arityMismatch() { mcimadamore@1186: return unambiguousNoInstanceException.setMessage("infer.arg.length.mismatch"); mcimadamore@1186: } mcimadamore@1186: public InapplicableMethodException argumentMismatch(boolean varargs, Type found, Type expected) { mcimadamore@1186: String key = varargs ? mcimadamore@1186: "infer.varargs.argument.mismatch" : mcimadamore@1186: "infer.no.conforming.assignment.exists"; mcimadamore@1186: return unambiguousNoInstanceException.setMessage(key, mcimadamore@1186: inferenceVars(undetvars), found, expected); mcimadamore@1186: } mcimadamore@1186: public InapplicableMethodException inaccessibleVarargs(Symbol location, Type expected) { mcimadamore@1186: return unambiguousNoInstanceException.setMessage("inaccessible.varargs.type", mcimadamore@1186: expected, Kinds.kindName(location), location); mcimadamore@1186: } mcimadamore@1186: } mcimadamore@1186: mcimadamore@845: private void checkArgumentsAcceptable(Env env, List actuals, List formals, mcimadamore@845: boolean allowBoxing, boolean useVarargs, Warner warn) { mcimadamore@845: try { mcimadamore@845: rs.checkRawArgumentsAcceptable(env, actuals, formals, mcimadamore@845: allowBoxing, useVarargs, warn); mcimadamore@845: } mcimadamore@1186: catch (InapplicableMethodException ex) { mcimadamore@845: // inferred method is not applicable mcimadamore@845: throw invalidInstanceException.setMessage(ex.getDiagnostic()); mcimadamore@845: } mcimadamore@845: } mcimadamore@845: mcimadamore@1251: /** check that type parameters are within their bounds. mcimadamore@895: */ mcimadamore@1251: void checkWithinBounds(List tvars, mcimadamore@1251: List undetvars, mcimadamore@1251: List arguments, mcimadamore@1251: Warner warn) mcimadamore@1251: throws InvalidInstanceException { mcimadamore@1251: List args = arguments; mcimadamore@1251: for (Type t : undetvars) { mcimadamore@1251: UndetVar uv = (UndetVar)t; mcimadamore@1251: uv.hibounds = types.subst(uv.hibounds, tvars, arguments); mcimadamore@1251: uv.lobounds = types.subst(uv.lobounds, tvars, arguments); mcimadamore@1251: uv.eq = types.subst(uv.eq, tvars, arguments); mcimadamore@1251: checkCompatibleUpperBounds(uv, tvars); mcimadamore@1251: if (args.head.tag != TYPEVAR || !args.head.containsAny(tvars)) { mcimadamore@1251: Type inst = args.head; mcimadamore@1251: for (Type u : uv.hibounds) { mcimadamore@1251: if (!types.isSubtypeUnchecked(inst, types.subst(u, tvars, undetvars), warn)) { mcimadamore@1251: reportBoundError(uv, BoundErrorKind.UPPER); mcimadamore@1251: } mcimadamore@1251: } mcimadamore@1251: for (Type l : uv.lobounds) { mcimadamore@1251: if (!types.isSubtypeUnchecked(types.subst(l, tvars, undetvars), inst, warn)) { mcimadamore@1251: reportBoundError(uv, BoundErrorKind.LOWER); mcimadamore@1251: } mcimadamore@1251: } mcimadamore@1251: for (Type e : uv.eq) { mcimadamore@1251: if (!types.isSameType(inst, types.subst(e, tvars, undetvars))) { mcimadamore@1251: reportBoundError(uv, BoundErrorKind.EQ); mcimadamore@1251: } mcimadamore@1251: } mcimadamore@1251: } mcimadamore@1251: args = args.tail; duke@1: } mcimadamore@895: } duke@1: mcimadamore@1251: void checkCompatibleUpperBounds(UndetVar uv, List tvars) { mcimadamore@1251: // VGJ: sort of inlined maximizeInst() below. Adding mcimadamore@1251: // bounds can cause lobounds that are above hibounds. mcimadamore@1251: ListBuffer hiboundsNoVars = ListBuffer.lb(); mcimadamore@1251: for (Type t : Type.filter(uv.hibounds, errorFilter)) { mcimadamore@1251: if (!t.containsAny(tvars)) { mcimadamore@1251: hiboundsNoVars.append(t); mcimadamore@1251: } duke@1: } mcimadamore@1251: List hibounds = hiboundsNoVars.toList(); mcimadamore@1251: Type hb = null; mcimadamore@1251: if (hibounds.isEmpty()) mcimadamore@1251: hb = syms.objectType; mcimadamore@1251: else if (hibounds.tail.isEmpty()) mcimadamore@1251: hb = hibounds.head; mcimadamore@1251: else mcimadamore@1251: hb = types.glb(hibounds); mcimadamore@1251: if (hb == null || hb.isErroneous()) mcimadamore@1251: reportBoundError(uv, BoundErrorKind.BAD_UPPER); mcimadamore@1251: } mcimadamore@1251: mcimadamore@1251: enum BoundErrorKind { mcimadamore@1251: BAD_UPPER() { mcimadamore@1251: @Override mcimadamore@1251: InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) { mcimadamore@1251: return ex.setMessage("incompatible.upper.bounds", uv.qtype, uv.hibounds); mcimadamore@1251: } mcimadamore@1251: }, mcimadamore@1251: UPPER() { mcimadamore@1251: @Override mcimadamore@1251: InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) { mcimadamore@1251: return ex.setMessage("inferred.do.not.conform.to.upper.bounds", uv.inst, uv.hibounds); mcimadamore@1251: } mcimadamore@1251: }, mcimadamore@1251: LOWER() { mcimadamore@1251: @Override mcimadamore@1251: InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) { mcimadamore@1251: return ex.setMessage("inferred.do.not.conform.to.lower.bounds", uv.inst, uv.lobounds); mcimadamore@1251: } mcimadamore@1251: }, mcimadamore@1251: EQ() { mcimadamore@1251: @Override mcimadamore@1251: InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) { mcimadamore@1251: return ex.setMessage("inferred.do.not.conform.to.eq.bounds", uv.inst, uv.eq); mcimadamore@1251: } mcimadamore@1251: }; mcimadamore@1251: mcimadamore@1251: abstract InapplicableMethodException setMessage(InferenceException ex, UndetVar uv); mcimadamore@1251: } mcimadamore@1251: //where mcimadamore@1251: void reportBoundError(UndetVar uv, BoundErrorKind bk) { mcimadamore@1251: throw bk.setMessage(uv.inst == null ? ambiguousNoInstanceException : invalidInstanceException, uv); duke@1: } mcimadamore@674: mcimadamore@674: /** mcimadamore@674: * Compute a synthetic method type corresponding to the requested polymorphic mcimadamore@820: * method signature. The target return type is computed from the immediately mcimadamore@820: * enclosing scope surrounding the polymorphic-signature call. mcimadamore@674: */ mcimadamore@1239: Type instantiatePolymorphicSignatureInstance(Env env, mcimadamore@674: MethodSymbol spMethod, // sig. poly. method or null if none mcimadamore@820: List argtypes) { mcimadamore@674: final Type restype; mcimadamore@716: mcimadamore@820: //The return type for a polymorphic signature call is computed from mcimadamore@820: //the enclosing tree E, as follows: if E is a cast, then use the mcimadamore@820: //target type of the cast expression as a return type; if E is an mcimadamore@820: //expression statement, the return type is 'void' - otherwise the mcimadamore@820: //return type is simply 'Object'. A correctness check ensures that mcimadamore@820: //env.next refers to the lexically enclosing environment in which mcimadamore@820: //the polymorphic signature call environment is nested. mcimadamore@820: mcimadamore@820: switch (env.next.tree.getTag()) { jjg@1127: case TYPECAST: mcimadamore@820: JCTypeCast castTree = (JCTypeCast)env.next.tree; mcimadamore@820: restype = (TreeInfo.skipParens(castTree.expr) == env.tree) ? mcimadamore@820: castTree.clazz.type : mcimadamore@820: syms.objectType; mcimadamore@820: break; jjg@1127: case EXEC: mcimadamore@820: JCTree.JCExpressionStatement execTree = mcimadamore@820: (JCTree.JCExpressionStatement)env.next.tree; mcimadamore@820: restype = (TreeInfo.skipParens(execTree.expr) == env.tree) ? mcimadamore@820: syms.voidType : mcimadamore@820: syms.objectType; mcimadamore@820: break; mcimadamore@820: default: mcimadamore@820: restype = syms.objectType; mcimadamore@674: } mcimadamore@674: mcimadamore@674: List paramtypes = Type.map(argtypes, implicitArgType); mcimadamore@674: List exType = spMethod != null ? mcimadamore@674: spMethod.getThrownTypes() : mcimadamore@674: List.of(syms.throwableType); // make it throw all exceptions mcimadamore@674: mcimadamore@674: MethodType mtype = new MethodType(paramtypes, mcimadamore@674: restype, mcimadamore@674: exType, mcimadamore@674: syms.methodClass); mcimadamore@674: return mtype; mcimadamore@674: } mcimadamore@674: //where mcimadamore@674: Mapping implicitArgType = new Mapping ("implicitArgType") { mcimadamore@674: public Type apply(Type t) { mcimadamore@674: t = types.erasure(t); mcimadamore@674: if (t.tag == BOT) mcimadamore@674: // nulls type as the marker type Null (which has no instances) mcimadamore@674: // infer as java.lang.Void for now mcimadamore@674: t = types.boxedClass(syms.voidType).type; mcimadamore@674: return t; mcimadamore@674: } mcimadamore@674: }; mcimadamore@895: }