Tue, 13 Oct 2009 15:26:30 -0700
6891079: Compiler allows invalid binary literals 0b and oBL
Reviewed-by: darcy
duke@1 | 1 | /* |
xdono@229 | 2 | * Copyright 1999-2009 Sun Microsystems, Inc. All Rights Reserved. |
duke@1 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@1 | 4 | * |
duke@1 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@1 | 6 | * under the terms of the GNU General Public License version 2 only, as |
duke@1 | 7 | * published by the Free Software Foundation. Sun designates this |
duke@1 | 8 | * particular file as subject to the "Classpath" exception as provided |
duke@1 | 9 | * by Sun in the LICENSE file that accompanied this code. |
duke@1 | 10 | * |
duke@1 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@1 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@1 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@1 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@1 | 15 | * accompanied this code). |
duke@1 | 16 | * |
duke@1 | 17 | * You should have received a copy of the GNU General Public License version |
duke@1 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@1 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@1 | 20 | * |
duke@1 | 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
duke@1 | 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
duke@1 | 23 | * have any questions. |
duke@1 | 24 | */ |
duke@1 | 25 | |
duke@1 | 26 | package com.sun.tools.javac.comp; |
duke@1 | 27 | |
duke@1 | 28 | import com.sun.tools.javac.util.*; |
duke@1 | 29 | import com.sun.tools.javac.util.List; |
duke@1 | 30 | import com.sun.tools.javac.code.*; |
duke@1 | 31 | import com.sun.tools.javac.code.Type.*; |
mcimadamore@396 | 32 | import com.sun.tools.javac.code.Type.ForAll.ConstraintKind; |
mcimadamore@299 | 33 | import com.sun.tools.javac.code.Symbol.*; |
mcimadamore@89 | 34 | import com.sun.tools.javac.util.JCDiagnostic; |
duke@1 | 35 | |
duke@1 | 36 | import static com.sun.tools.javac.code.TypeTags.*; |
duke@1 | 37 | |
duke@1 | 38 | /** Helper class for type parameter inference, used by the attribution phase. |
duke@1 | 39 | * |
duke@1 | 40 | * <p><b>This is NOT part of any API supported by Sun Microsystems. If |
duke@1 | 41 | * you write code that depends on this, you do so at your own risk. |
duke@1 | 42 | * This code and its internal interfaces are subject to change or |
duke@1 | 43 | * deletion without notice.</b> |
duke@1 | 44 | */ |
duke@1 | 45 | public class Infer { |
duke@1 | 46 | protected static final Context.Key<Infer> inferKey = |
duke@1 | 47 | new Context.Key<Infer>(); |
duke@1 | 48 | |
duke@1 | 49 | /** A value for prototypes that admit any type, including polymorphic ones. */ |
duke@1 | 50 | public static final Type anyPoly = new Type(NONE, null); |
duke@1 | 51 | |
duke@1 | 52 | Symtab syms; |
duke@1 | 53 | Types types; |
mcimadamore@396 | 54 | Check chk; |
mcimadamore@299 | 55 | Resolve rs; |
mcimadamore@89 | 56 | JCDiagnostic.Factory diags; |
duke@1 | 57 | |
duke@1 | 58 | public static Infer instance(Context context) { |
duke@1 | 59 | Infer instance = context.get(inferKey); |
duke@1 | 60 | if (instance == null) |
duke@1 | 61 | instance = new Infer(context); |
duke@1 | 62 | return instance; |
duke@1 | 63 | } |
duke@1 | 64 | |
duke@1 | 65 | protected Infer(Context context) { |
duke@1 | 66 | context.put(inferKey, this); |
duke@1 | 67 | syms = Symtab.instance(context); |
duke@1 | 68 | types = Types.instance(context); |
mcimadamore@299 | 69 | rs = Resolve.instance(context); |
mcimadamore@396 | 70 | chk = Check.instance(context); |
mcimadamore@89 | 71 | diags = JCDiagnostic.Factory.instance(context); |
mcimadamore@89 | 72 | ambiguousNoInstanceException = |
mcimadamore@89 | 73 | new NoInstanceException(true, diags); |
mcimadamore@89 | 74 | unambiguousNoInstanceException = |
mcimadamore@89 | 75 | new NoInstanceException(false, diags); |
mcimadamore@299 | 76 | invalidInstanceException = |
mcimadamore@299 | 77 | new InvalidInstanceException(diags); |
mcimadamore@299 | 78 | |
duke@1 | 79 | } |
duke@1 | 80 | |
mcimadamore@299 | 81 | public static class InferenceException extends RuntimeException { |
duke@1 | 82 | private static final long serialVersionUID = 0; |
duke@1 | 83 | |
duke@1 | 84 | JCDiagnostic diagnostic; |
mcimadamore@89 | 85 | JCDiagnostic.Factory diags; |
duke@1 | 86 | |
mcimadamore@299 | 87 | InferenceException(JCDiagnostic.Factory diags) { |
duke@1 | 88 | this.diagnostic = null; |
mcimadamore@89 | 89 | this.diags = diags; |
duke@1 | 90 | } |
mcimadamore@299 | 91 | |
mcimadamore@299 | 92 | InferenceException setMessage(String key, Object... args) { |
mcimadamore@299 | 93 | this.diagnostic = diags.fragment(key, args); |
duke@1 | 94 | return this; |
duke@1 | 95 | } |
mcimadamore@299 | 96 | |
duke@1 | 97 | public JCDiagnostic getDiagnostic() { |
mcimadamore@299 | 98 | return diagnostic; |
mcimadamore@299 | 99 | } |
mcimadamore@299 | 100 | } |
mcimadamore@299 | 101 | |
mcimadamore@299 | 102 | public static class NoInstanceException extends InferenceException { |
mcimadamore@299 | 103 | private static final long serialVersionUID = 1; |
mcimadamore@299 | 104 | |
mcimadamore@299 | 105 | boolean isAmbiguous; // exist several incomparable best instances? |
mcimadamore@299 | 106 | |
mcimadamore@299 | 107 | NoInstanceException(boolean isAmbiguous, JCDiagnostic.Factory diags) { |
mcimadamore@299 | 108 | super(diags); |
mcimadamore@299 | 109 | this.isAmbiguous = isAmbiguous; |
duke@1 | 110 | } |
duke@1 | 111 | } |
mcimadamore@299 | 112 | |
mcimadamore@299 | 113 | public static class InvalidInstanceException extends InferenceException { |
mcimadamore@299 | 114 | private static final long serialVersionUID = 2; |
mcimadamore@299 | 115 | |
mcimadamore@299 | 116 | InvalidInstanceException(JCDiagnostic.Factory diags) { |
mcimadamore@299 | 117 | super(diags); |
mcimadamore@299 | 118 | } |
mcimadamore@299 | 119 | } |
mcimadamore@299 | 120 | |
mcimadamore@89 | 121 | private final NoInstanceException ambiguousNoInstanceException; |
mcimadamore@89 | 122 | private final NoInstanceException unambiguousNoInstanceException; |
mcimadamore@299 | 123 | private final InvalidInstanceException invalidInstanceException; |
duke@1 | 124 | |
duke@1 | 125 | /*************************************************************************** |
duke@1 | 126 | * Auxiliary type values and classes |
duke@1 | 127 | ***************************************************************************/ |
duke@1 | 128 | |
duke@1 | 129 | /** A mapping that turns type variables into undetermined type variables. |
duke@1 | 130 | */ |
duke@1 | 131 | Mapping fromTypeVarFun = new Mapping("fromTypeVarFun") { |
duke@1 | 132 | public Type apply(Type t) { |
duke@1 | 133 | if (t.tag == TYPEVAR) return new UndetVar(t); |
duke@1 | 134 | else return t.map(this); |
duke@1 | 135 | } |
duke@1 | 136 | }; |
duke@1 | 137 | |
duke@1 | 138 | /** A mapping that returns its type argument with every UndetVar replaced |
duke@1 | 139 | * by its `inst' field. Throws a NoInstanceException |
duke@1 | 140 | * if this not possible because an `inst' field is null. |
duke@1 | 141 | */ |
duke@1 | 142 | Mapping getInstFun = new Mapping("getInstFun") { |
duke@1 | 143 | public Type apply(Type t) { |
duke@1 | 144 | switch (t.tag) { |
duke@1 | 145 | case UNKNOWN: |
duke@1 | 146 | throw ambiguousNoInstanceException |
duke@1 | 147 | .setMessage("undetermined.type"); |
duke@1 | 148 | case UNDETVAR: |
duke@1 | 149 | UndetVar that = (UndetVar) t; |
duke@1 | 150 | if (that.inst == null) |
duke@1 | 151 | throw ambiguousNoInstanceException |
duke@1 | 152 | .setMessage("type.variable.has.undetermined.type", |
duke@1 | 153 | that.qtype); |
duke@1 | 154 | return apply(that.inst); |
duke@1 | 155 | default: |
duke@1 | 156 | return t.map(this); |
duke@1 | 157 | } |
duke@1 | 158 | } |
duke@1 | 159 | }; |
duke@1 | 160 | |
duke@1 | 161 | /*************************************************************************** |
duke@1 | 162 | * Mini/Maximization of UndetVars |
duke@1 | 163 | ***************************************************************************/ |
duke@1 | 164 | |
duke@1 | 165 | /** Instantiate undetermined type variable to its minimal upper bound. |
duke@1 | 166 | * Throw a NoInstanceException if this not possible. |
duke@1 | 167 | */ |
duke@1 | 168 | void maximizeInst(UndetVar that, Warner warn) throws NoInstanceException { |
duke@1 | 169 | if (that.inst == null) { |
duke@1 | 170 | if (that.hibounds.isEmpty()) |
duke@1 | 171 | that.inst = syms.objectType; |
duke@1 | 172 | else if (that.hibounds.tail.isEmpty()) |
duke@1 | 173 | that.inst = that.hibounds.head; |
mcimadamore@210 | 174 | else |
mcimadamore@210 | 175 | that.inst = types.glb(that.hibounds); |
duke@1 | 176 | } |
mcimadamore@210 | 177 | if (that.inst == null || |
mcimadamore@298 | 178 | that.inst.isErroneous()) |
mcimadamore@210 | 179 | throw ambiguousNoInstanceException |
mcimadamore@210 | 180 | .setMessage("no.unique.maximal.instance.exists", |
mcimadamore@210 | 181 | that.qtype, that.hibounds); |
duke@1 | 182 | } |
duke@1 | 183 | //where |
duke@1 | 184 | private boolean isSubClass(Type t, final List<Type> ts) { |
duke@1 | 185 | t = t.baseType(); |
duke@1 | 186 | if (t.tag == TYPEVAR) { |
duke@1 | 187 | List<Type> bounds = types.getBounds((TypeVar)t); |
duke@1 | 188 | for (Type s : ts) { |
duke@1 | 189 | if (!types.isSameType(t, s.baseType())) { |
duke@1 | 190 | for (Type bound : bounds) { |
duke@1 | 191 | if (!isSubClass(bound, List.of(s.baseType()))) |
duke@1 | 192 | return false; |
duke@1 | 193 | } |
duke@1 | 194 | } |
duke@1 | 195 | } |
duke@1 | 196 | } else { |
duke@1 | 197 | for (Type s : ts) { |
duke@1 | 198 | if (!t.tsym.isSubClass(s.baseType().tsym, types)) |
duke@1 | 199 | return false; |
duke@1 | 200 | } |
duke@1 | 201 | } |
duke@1 | 202 | return true; |
duke@1 | 203 | } |
duke@1 | 204 | |
jjg@110 | 205 | /** Instantiate undetermined type variable to the lub of all its lower bounds. |
duke@1 | 206 | * Throw a NoInstanceException if this not possible. |
duke@1 | 207 | */ |
duke@1 | 208 | void minimizeInst(UndetVar that, Warner warn) throws NoInstanceException { |
duke@1 | 209 | if (that.inst == null) { |
duke@1 | 210 | if (that.lobounds.isEmpty()) |
duke@1 | 211 | that.inst = syms.botType; |
duke@1 | 212 | else if (that.lobounds.tail.isEmpty()) |
mcimadamore@5 | 213 | that.inst = that.lobounds.head.isPrimitive() ? syms.errType : that.lobounds.head; |
duke@1 | 214 | else { |
duke@1 | 215 | that.inst = types.lub(that.lobounds); |
mcimadamore@5 | 216 | } |
jjg@110 | 217 | if (that.inst == null || that.inst.tag == ERROR) |
duke@1 | 218 | throw ambiguousNoInstanceException |
duke@1 | 219 | .setMessage("no.unique.minimal.instance.exists", |
duke@1 | 220 | that.qtype, that.lobounds); |
duke@1 | 221 | // VGJ: sort of inlined maximizeInst() below. Adding |
duke@1 | 222 | // bounds can cause lobounds that are above hibounds. |
duke@1 | 223 | if (that.hibounds.isEmpty()) |
duke@1 | 224 | return; |
duke@1 | 225 | Type hb = null; |
duke@1 | 226 | if (that.hibounds.tail.isEmpty()) |
duke@1 | 227 | hb = that.hibounds.head; |
duke@1 | 228 | else for (List<Type> bs = that.hibounds; |
duke@1 | 229 | bs.nonEmpty() && hb == null; |
duke@1 | 230 | bs = bs.tail) { |
duke@1 | 231 | if (isSubClass(bs.head, that.hibounds)) |
duke@1 | 232 | hb = types.fromUnknownFun.apply(bs.head); |
duke@1 | 233 | } |
duke@1 | 234 | if (hb == null || |
duke@1 | 235 | !types.isSubtypeUnchecked(hb, that.hibounds, warn) || |
duke@1 | 236 | !types.isSubtypeUnchecked(that.inst, hb, warn)) |
duke@1 | 237 | throw ambiguousNoInstanceException; |
duke@1 | 238 | } |
duke@1 | 239 | } |
duke@1 | 240 | |
duke@1 | 241 | /*************************************************************************** |
duke@1 | 242 | * Exported Methods |
duke@1 | 243 | ***************************************************************************/ |
duke@1 | 244 | |
duke@1 | 245 | /** Try to instantiate expression type `that' to given type `to'. |
duke@1 | 246 | * If a maximal instantiation exists which makes this type |
duke@1 | 247 | * a subtype of type `to', return the instantiated type. |
duke@1 | 248 | * If no instantiation exists, or if several incomparable |
duke@1 | 249 | * best instantiations exist throw a NoInstanceException. |
duke@1 | 250 | */ |
duke@1 | 251 | public Type instantiateExpr(ForAll that, |
duke@1 | 252 | Type to, |
mcimadamore@299 | 253 | Warner warn) throws InferenceException { |
duke@1 | 254 | List<Type> undetvars = Type.map(that.tvars, fromTypeVarFun); |
duke@1 | 255 | for (List<Type> l = undetvars; l.nonEmpty(); l = l.tail) { |
mcimadamore@396 | 256 | UndetVar uv = (UndetVar) l.head; |
mcimadamore@396 | 257 | TypeVar tv = (TypeVar)uv.qtype; |
duke@1 | 258 | ListBuffer<Type> hibounds = new ListBuffer<Type>(); |
mcimadamore@396 | 259 | for (Type t : that.getConstraints(tv, ConstraintKind.EXTENDS).prependList(types.getBounds(tv))) { |
mcimadamore@396 | 260 | if (!t.containsSome(that.tvars) && t.tag != BOT) { |
mcimadamore@396 | 261 | hibounds.append(t); |
duke@1 | 262 | } |
duke@1 | 263 | } |
mcimadamore@396 | 264 | List<Type> inst = that.getConstraints(tv, ConstraintKind.EQUAL); |
mcimadamore@396 | 265 | if (inst.nonEmpty() && inst.head.tag != BOT) { |
mcimadamore@396 | 266 | uv.inst = inst.head; |
mcimadamore@396 | 267 | } |
mcimadamore@396 | 268 | uv.hibounds = hibounds.toList(); |
duke@1 | 269 | } |
duke@1 | 270 | Type qtype1 = types.subst(that.qtype, that.tvars, undetvars); |
duke@1 | 271 | if (!types.isSubtype(qtype1, to)) { |
duke@1 | 272 | throw unambiguousNoInstanceException |
duke@1 | 273 | .setMessage("no.conforming.instance.exists", |
duke@1 | 274 | that.tvars, that.qtype, to); |
duke@1 | 275 | } |
duke@1 | 276 | for (List<Type> l = undetvars; l.nonEmpty(); l = l.tail) |
duke@1 | 277 | maximizeInst((UndetVar) l.head, warn); |
duke@1 | 278 | // System.out.println(" = " + qtype1.map(getInstFun));//DEBUG |
duke@1 | 279 | |
duke@1 | 280 | // check bounds |
duke@1 | 281 | List<Type> targs = Type.map(undetvars, getInstFun); |
duke@1 | 282 | targs = types.subst(targs, that.tvars, targs); |
duke@1 | 283 | checkWithinBounds(that.tvars, targs, warn); |
mcimadamore@396 | 284 | return chk.checkType(warn.pos(), that.inst(targs, types), to); |
duke@1 | 285 | } |
duke@1 | 286 | |
duke@1 | 287 | /** Instantiate method type `mt' by finding instantiations of |
duke@1 | 288 | * `tvars' so that method can be applied to `argtypes'. |
duke@1 | 289 | */ |
duke@1 | 290 | public Type instantiateMethod(List<Type> tvars, |
duke@1 | 291 | MethodType mt, |
mcimadamore@299 | 292 | final List<Type> argtypes, |
mcimadamore@299 | 293 | final boolean allowBoxing, |
mcimadamore@299 | 294 | final boolean useVarargs, |
mcimadamore@299 | 295 | final Warner warn) throws InferenceException { |
duke@1 | 296 | //-System.err.println("instantiateMethod(" + tvars + ", " + mt + ", " + argtypes + ")"); //DEBUG |
duke@1 | 297 | List<Type> undetvars = Type.map(tvars, fromTypeVarFun); |
duke@1 | 298 | List<Type> formals = mt.argtypes; |
mcimadamore@299 | 299 | //need to capture exactly once - otherwise subsequent |
mcimadamore@299 | 300 | //applicability checks might fail |
mcimadamore@299 | 301 | final List<Type> capturedArgs = types.capture(argtypes); |
mcimadamore@299 | 302 | List<Type> actuals = capturedArgs; |
mcimadamore@299 | 303 | List<Type> actualsNoCapture = argtypes; |
duke@1 | 304 | // instantiate all polymorphic argument types and |
duke@1 | 305 | // set up lower bounds constraints for undetvars |
duke@1 | 306 | Type varargsFormal = useVarargs ? formals.last() : null; |
mcimadamore@299 | 307 | while (actuals.nonEmpty() && formals.head != varargsFormal) { |
mcimadamore@299 | 308 | Type formal = formals.head; |
mcimadamore@299 | 309 | Type actual = actuals.head.baseType(); |
mcimadamore@299 | 310 | Type actualNoCapture = actualsNoCapture.head.baseType(); |
mcimadamore@299 | 311 | if (actual.tag == FORALL) |
mcimadamore@299 | 312 | actual = instantiateArg((ForAll)actual, formal, tvars, warn); |
mcimadamore@299 | 313 | Type undetFormal = types.subst(formal, tvars, undetvars); |
duke@1 | 314 | boolean works = allowBoxing |
mcimadamore@299 | 315 | ? types.isConvertible(actual, undetFormal, warn) |
mcimadamore@299 | 316 | : types.isSubtypeUnchecked(actual, undetFormal, warn); |
duke@1 | 317 | if (!works) { |
duke@1 | 318 | throw unambiguousNoInstanceException |
duke@1 | 319 | .setMessage("no.conforming.assignment.exists", |
mcimadamore@299 | 320 | tvars, actualNoCapture, formal); |
duke@1 | 321 | } |
duke@1 | 322 | formals = formals.tail; |
mcimadamore@299 | 323 | actuals = actuals.tail; |
mcimadamore@299 | 324 | actualsNoCapture = actualsNoCapture.tail; |
duke@1 | 325 | } |
duke@1 | 326 | if (formals.head != varargsFormal || // not enough args |
mcimadamore@299 | 327 | !useVarargs && actuals.nonEmpty()) { // too many args |
duke@1 | 328 | // argument lists differ in length |
duke@1 | 329 | throw unambiguousNoInstanceException |
duke@1 | 330 | .setMessage("arg.length.mismatch"); |
duke@1 | 331 | } |
duke@1 | 332 | |
duke@1 | 333 | // for varargs arguments as well |
duke@1 | 334 | if (useVarargs) { |
mcimadamore@299 | 335 | Type elemType = types.elemtype(varargsFormal); |
mcimadamore@299 | 336 | Type elemUndet = types.subst(elemType, tvars, undetvars); |
mcimadamore@299 | 337 | while (actuals.nonEmpty()) { |
mcimadamore@299 | 338 | Type actual = actuals.head.baseType(); |
mcimadamore@299 | 339 | Type actualNoCapture = actualsNoCapture.head.baseType(); |
mcimadamore@299 | 340 | if (actual.tag == FORALL) |
mcimadamore@299 | 341 | actual = instantiateArg((ForAll)actual, elemType, tvars, warn); |
mcimadamore@299 | 342 | boolean works = types.isConvertible(actual, elemUndet, warn); |
duke@1 | 343 | if (!works) { |
duke@1 | 344 | throw unambiguousNoInstanceException |
duke@1 | 345 | .setMessage("no.conforming.assignment.exists", |
mcimadamore@299 | 346 | tvars, actualNoCapture, elemType); |
duke@1 | 347 | } |
mcimadamore@299 | 348 | actuals = actuals.tail; |
mcimadamore@299 | 349 | actualsNoCapture = actualsNoCapture.tail; |
duke@1 | 350 | } |
duke@1 | 351 | } |
duke@1 | 352 | |
duke@1 | 353 | // minimize as yet undetermined type variables |
duke@1 | 354 | for (Type t : undetvars) |
duke@1 | 355 | minimizeInst((UndetVar) t, warn); |
duke@1 | 356 | |
duke@1 | 357 | /** Type variables instantiated to bottom */ |
duke@1 | 358 | ListBuffer<Type> restvars = new ListBuffer<Type>(); |
duke@1 | 359 | |
mcimadamore@396 | 360 | /** Undet vars instantiated to bottom */ |
mcimadamore@396 | 361 | final ListBuffer<Type> restundet = new ListBuffer<Type>(); |
mcimadamore@396 | 362 | |
duke@1 | 363 | /** Instantiated types or TypeVars if under-constrained */ |
duke@1 | 364 | ListBuffer<Type> insttypes = new ListBuffer<Type>(); |
duke@1 | 365 | |
duke@1 | 366 | /** Instantiated types or UndetVars if under-constrained */ |
duke@1 | 367 | ListBuffer<Type> undettypes = new ListBuffer<Type>(); |
duke@1 | 368 | |
duke@1 | 369 | for (Type t : undetvars) { |
duke@1 | 370 | UndetVar uv = (UndetVar)t; |
duke@1 | 371 | if (uv.inst.tag == BOT) { |
duke@1 | 372 | restvars.append(uv.qtype); |
mcimadamore@396 | 373 | restundet.append(uv); |
duke@1 | 374 | insttypes.append(uv.qtype); |
duke@1 | 375 | undettypes.append(uv); |
duke@1 | 376 | uv.inst = null; |
duke@1 | 377 | } else { |
duke@1 | 378 | insttypes.append(uv.inst); |
duke@1 | 379 | undettypes.append(uv.inst); |
duke@1 | 380 | } |
duke@1 | 381 | } |
duke@1 | 382 | checkWithinBounds(tvars, undettypes.toList(), warn); |
duke@1 | 383 | |
mcimadamore@299 | 384 | mt = (MethodType)types.subst(mt, tvars, insttypes.toList()); |
mcimadamore@299 | 385 | |
duke@1 | 386 | if (!restvars.isEmpty()) { |
duke@1 | 387 | // if there are uninstantiated variables, |
duke@1 | 388 | // quantify result type with them |
mcimadamore@299 | 389 | final List<Type> inferredTypes = insttypes.toList(); |
mcimadamore@299 | 390 | final List<Type> all_tvars = tvars; //this is the wrong tvars |
mcimadamore@299 | 391 | final MethodType mt2 = new MethodType(mt.argtypes, null, mt.thrown, syms.methodClass); |
mcimadamore@299 | 392 | mt2.restype = new ForAll(restvars.toList(), mt.restype) { |
mcimadamore@299 | 393 | @Override |
mcimadamore@396 | 394 | public List<Type> getConstraints(TypeVar tv, ConstraintKind ck) { |
mcimadamore@396 | 395 | for (Type t : restundet.toList()) { |
mcimadamore@396 | 396 | UndetVar uv = (UndetVar)t; |
mcimadamore@396 | 397 | if (uv.qtype == tv) { |
mcimadamore@396 | 398 | switch (ck) { |
mcimadamore@396 | 399 | case EXTENDS: return uv.hibounds; |
mcimadamore@396 | 400 | case SUPER: return uv.lobounds; |
mcimadamore@396 | 401 | case EQUAL: return uv.inst != null ? List.of(uv.inst) : List.<Type>nil(); |
mcimadamore@396 | 402 | } |
mcimadamore@396 | 403 | } |
mcimadamore@396 | 404 | } |
mcimadamore@396 | 405 | return List.nil(); |
mcimadamore@396 | 406 | } |
mcimadamore@396 | 407 | |
mcimadamore@396 | 408 | @Override |
mcimadamore@299 | 409 | public Type inst(List<Type> inferred, Types types) throws NoInstanceException { |
mcimadamore@299 | 410 | List<Type> formals = types.subst(mt2.argtypes, tvars, inferred); |
mcimadamore@396 | 411 | if (!rs.argumentsAcceptable(capturedArgs, formals, |
mcimadamore@299 | 412 | allowBoxing, useVarargs, warn)) { |
mcimadamore@299 | 413 | // inferred method is not applicable |
mcimadamore@299 | 414 | throw invalidInstanceException.setMessage("inferred.do.not.conform.to.params", formals, argtypes); |
mcimadamore@396 | 415 | } |
mcimadamore@396 | 416 | // check that inferred bounds conform to their bounds |
mcimadamore@396 | 417 | checkWithinBounds(all_tvars, |
mcimadamore@299 | 418 | types.subst(inferredTypes, tvars, inferred), warn); |
mcimadamore@396 | 419 | return super.inst(inferred, types); |
mcimadamore@299 | 420 | }}; |
mcimadamore@299 | 421 | return mt2; |
duke@1 | 422 | } |
mcimadamore@299 | 423 | else if (!rs.argumentsAcceptable(capturedArgs, mt.getParameterTypes(), allowBoxing, useVarargs, warn)) { |
mcimadamore@299 | 424 | // inferred method is not applicable |
mcimadamore@299 | 425 | throw invalidInstanceException.setMessage("inferred.do.not.conform.to.params", mt.getParameterTypes(), argtypes); |
mcimadamore@299 | 426 | } |
mcimadamore@299 | 427 | else { |
mcimadamore@299 | 428 | // return instantiated version of method type |
mcimadamore@299 | 429 | return mt; |
mcimadamore@299 | 430 | } |
duke@1 | 431 | } |
duke@1 | 432 | //where |
duke@1 | 433 | |
duke@1 | 434 | /** Try to instantiate argument type `that' to given type `to'. |
duke@1 | 435 | * If this fails, try to insantiate `that' to `to' where |
duke@1 | 436 | * every occurrence of a type variable in `tvars' is replaced |
duke@1 | 437 | * by an unknown type. |
duke@1 | 438 | */ |
duke@1 | 439 | private Type instantiateArg(ForAll that, |
duke@1 | 440 | Type to, |
duke@1 | 441 | List<Type> tvars, |
mcimadamore@299 | 442 | Warner warn) throws InferenceException { |
duke@1 | 443 | List<Type> targs; |
duke@1 | 444 | try { |
duke@1 | 445 | return instantiateExpr(that, to, warn); |
duke@1 | 446 | } catch (NoInstanceException ex) { |
duke@1 | 447 | Type to1 = to; |
duke@1 | 448 | for (List<Type> l = tvars; l.nonEmpty(); l = l.tail) |
duke@1 | 449 | to1 = types.subst(to1, List.of(l.head), List.of(syms.unknownType)); |
duke@1 | 450 | return instantiateExpr(that, to1, warn); |
duke@1 | 451 | } |
duke@1 | 452 | } |
duke@1 | 453 | |
duke@1 | 454 | /** check that type parameters are within their bounds. |
duke@1 | 455 | */ |
duke@1 | 456 | private void checkWithinBounds(List<Type> tvars, |
duke@1 | 457 | List<Type> arguments, |
duke@1 | 458 | Warner warn) |
mcimadamore@299 | 459 | throws InvalidInstanceException { |
duke@1 | 460 | for (List<Type> tvs = tvars, args = arguments; |
duke@1 | 461 | tvs.nonEmpty(); |
duke@1 | 462 | tvs = tvs.tail, args = args.tail) { |
duke@1 | 463 | if (args.head instanceof UndetVar) continue; |
duke@1 | 464 | List<Type> bounds = types.subst(types.getBounds((TypeVar)tvs.head), tvars, arguments); |
duke@1 | 465 | if (!types.isSubtypeUnchecked(args.head, bounds, warn)) |
mcimadamore@299 | 466 | throw invalidInstanceException |
duke@1 | 467 | .setMessage("inferred.do.not.conform.to.bounds", |
mcimadamore@299 | 468 | args.head, bounds); |
duke@1 | 469 | } |
duke@1 | 470 | } |
duke@1 | 471 | } |