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

Sat, 18 Sep 2010 09:54:51 -0700

author
mcimadamore
date
Sat, 18 Sep 2010 09:54:51 -0700
changeset 688
50f9ac2f4730
parent 674
584365f256a7
child 689
77cc34d5e548
permissions
-rw-r--r--

6980862: too aggressive compiler optimization causes stale results of Types.implementation()
Summary: use a scope counter in order to determine when/if the implementation cache entries are stale
Reviewed-by: jjg

duke@1 1 /*
ohair@554 2 * Copyright (c) 1999, 2009, Oracle and/or its affiliates. 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
ohair@554 7 * published by the Free Software Foundation. Oracle designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
ohair@554 9 * by Oracle 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 *
ohair@554 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 22 * or visit www.oracle.com if you need additional information or have any
ohair@554 23 * questions.
duke@1 24 */
duke@1 25
duke@1 26 package com.sun.tools.javac.comp;
duke@1 27
mcimadamore@674 28 import com.sun.tools.javac.tree.JCTree;
mcimadamore@674 29 import com.sun.tools.javac.tree.JCTree.JCTypeCast;
duke@1 30 import com.sun.tools.javac.util.*;
duke@1 31 import com.sun.tools.javac.util.List;
duke@1 32 import com.sun.tools.javac.code.*;
duke@1 33 import com.sun.tools.javac.code.Type.*;
mcimadamore@396 34 import com.sun.tools.javac.code.Type.ForAll.ConstraintKind;
mcimadamore@299 35 import com.sun.tools.javac.code.Symbol.*;
mcimadamore@89 36 import com.sun.tools.javac.util.JCDiagnostic;
duke@1 37
duke@1 38 import static com.sun.tools.javac.code.TypeTags.*;
duke@1 39
duke@1 40 /** Helper class for type parameter inference, used by the attribution phase.
duke@1 41 *
jjg@581 42 * <p><b>This is NOT part of any supported API.
jjg@581 43 * If you write code that depends on this, you do so at your own risk.
duke@1 44 * This code and its internal interfaces are subject to change or
duke@1 45 * deletion without notice.</b>
duke@1 46 */
duke@1 47 public class Infer {
duke@1 48 protected static final Context.Key<Infer> inferKey =
duke@1 49 new Context.Key<Infer>();
duke@1 50
duke@1 51 /** A value for prototypes that admit any type, including polymorphic ones. */
duke@1 52 public static final Type anyPoly = new Type(NONE, null);
duke@1 53
duke@1 54 Symtab syms;
duke@1 55 Types types;
mcimadamore@396 56 Check chk;
mcimadamore@299 57 Resolve rs;
mcimadamore@89 58 JCDiagnostic.Factory diags;
duke@1 59
duke@1 60 public static Infer instance(Context context) {
duke@1 61 Infer instance = context.get(inferKey);
duke@1 62 if (instance == null)
duke@1 63 instance = new Infer(context);
duke@1 64 return instance;
duke@1 65 }
duke@1 66
duke@1 67 protected Infer(Context context) {
duke@1 68 context.put(inferKey, this);
duke@1 69 syms = Symtab.instance(context);
duke@1 70 types = Types.instance(context);
mcimadamore@299 71 rs = Resolve.instance(context);
mcimadamore@396 72 chk = Check.instance(context);
mcimadamore@89 73 diags = JCDiagnostic.Factory.instance(context);
mcimadamore@89 74 ambiguousNoInstanceException =
mcimadamore@89 75 new NoInstanceException(true, diags);
mcimadamore@89 76 unambiguousNoInstanceException =
mcimadamore@89 77 new NoInstanceException(false, diags);
mcimadamore@299 78 invalidInstanceException =
mcimadamore@299 79 new InvalidInstanceException(diags);
mcimadamore@299 80
duke@1 81 }
duke@1 82
mcimadamore@299 83 public static class InferenceException extends RuntimeException {
duke@1 84 private static final long serialVersionUID = 0;
duke@1 85
duke@1 86 JCDiagnostic diagnostic;
mcimadamore@89 87 JCDiagnostic.Factory diags;
duke@1 88
mcimadamore@299 89 InferenceException(JCDiagnostic.Factory diags) {
duke@1 90 this.diagnostic = null;
mcimadamore@89 91 this.diags = diags;
duke@1 92 }
mcimadamore@299 93
mcimadamore@299 94 InferenceException setMessage(String key, Object... args) {
mcimadamore@299 95 this.diagnostic = diags.fragment(key, args);
duke@1 96 return this;
duke@1 97 }
mcimadamore@299 98
duke@1 99 public JCDiagnostic getDiagnostic() {
mcimadamore@299 100 return diagnostic;
mcimadamore@299 101 }
mcimadamore@299 102 }
mcimadamore@299 103
mcimadamore@299 104 public static class NoInstanceException extends InferenceException {
mcimadamore@299 105 private static final long serialVersionUID = 1;
mcimadamore@299 106
mcimadamore@299 107 boolean isAmbiguous; // exist several incomparable best instances?
mcimadamore@299 108
mcimadamore@299 109 NoInstanceException(boolean isAmbiguous, JCDiagnostic.Factory diags) {
mcimadamore@299 110 super(diags);
mcimadamore@299 111 this.isAmbiguous = isAmbiguous;
duke@1 112 }
duke@1 113 }
mcimadamore@299 114
mcimadamore@299 115 public static class InvalidInstanceException extends InferenceException {
mcimadamore@299 116 private static final long serialVersionUID = 2;
mcimadamore@299 117
mcimadamore@299 118 InvalidInstanceException(JCDiagnostic.Factory diags) {
mcimadamore@299 119 super(diags);
mcimadamore@299 120 }
mcimadamore@299 121 }
mcimadamore@299 122
mcimadamore@89 123 private final NoInstanceException ambiguousNoInstanceException;
mcimadamore@89 124 private final NoInstanceException unambiguousNoInstanceException;
mcimadamore@299 125 private final InvalidInstanceException invalidInstanceException;
duke@1 126
duke@1 127 /***************************************************************************
duke@1 128 * Auxiliary type values and classes
duke@1 129 ***************************************************************************/
duke@1 130
duke@1 131 /** A mapping that turns type variables into undetermined type variables.
duke@1 132 */
duke@1 133 Mapping fromTypeVarFun = new Mapping("fromTypeVarFun") {
duke@1 134 public Type apply(Type t) {
duke@1 135 if (t.tag == TYPEVAR) return new UndetVar(t);
duke@1 136 else return t.map(this);
duke@1 137 }
duke@1 138 };
duke@1 139
duke@1 140 /** A mapping that returns its type argument with every UndetVar replaced
duke@1 141 * by its `inst' field. Throws a NoInstanceException
duke@1 142 * if this not possible because an `inst' field is null.
mcimadamore@635 143 * Note: mutually referring undertvars will be left uninstantiated
mcimadamore@635 144 * (that is, they will be replaced by the underlying type-variable).
duke@1 145 */
mcimadamore@635 146
duke@1 147 Mapping getInstFun = new Mapping("getInstFun") {
duke@1 148 public Type apply(Type t) {
duke@1 149 switch (t.tag) {
mcimadamore@635 150 case UNKNOWN:
duke@1 151 throw ambiguousNoInstanceException
mcimadamore@635 152 .setMessage("undetermined.type");
mcimadamore@635 153 case UNDETVAR:
mcimadamore@635 154 UndetVar that = (UndetVar) t;
mcimadamore@635 155 if (that.inst == null)
mcimadamore@635 156 throw ambiguousNoInstanceException
mcimadamore@635 157 .setMessage("type.variable.has.undetermined.type",
mcimadamore@635 158 that.qtype);
mcimadamore@635 159 return isConstraintCyclic(that) ?
mcimadamore@635 160 that.qtype :
mcimadamore@635 161 apply(that.inst);
mcimadamore@635 162 default:
mcimadamore@635 163 return t.map(this);
duke@1 164 }
duke@1 165 }
mcimadamore@635 166
mcimadamore@635 167 private boolean isConstraintCyclic(UndetVar uv) {
mcimadamore@635 168 Types.UnaryVisitor<Boolean> constraintScanner =
mcimadamore@635 169 new Types.UnaryVisitor<Boolean>() {
mcimadamore@635 170
mcimadamore@635 171 List<Type> seen = List.nil();
mcimadamore@635 172
mcimadamore@635 173 Boolean visit(List<Type> ts) {
mcimadamore@635 174 for (Type t : ts) {
mcimadamore@635 175 if (visit(t)) return true;
mcimadamore@635 176 }
mcimadamore@635 177 return false;
mcimadamore@635 178 }
mcimadamore@635 179
mcimadamore@635 180 public Boolean visitType(Type t, Void ignored) {
mcimadamore@635 181 return false;
mcimadamore@635 182 }
mcimadamore@635 183
mcimadamore@635 184 @Override
mcimadamore@635 185 public Boolean visitClassType(ClassType t, Void ignored) {
mcimadamore@635 186 if (t.isCompound()) {
mcimadamore@635 187 return visit(types.supertype(t)) ||
mcimadamore@635 188 visit(types.interfaces(t));
mcimadamore@635 189 } else {
mcimadamore@635 190 return visit(t.getTypeArguments());
mcimadamore@635 191 }
mcimadamore@635 192 }
mcimadamore@635 193 @Override
mcimadamore@635 194 public Boolean visitWildcardType(WildcardType t, Void ignored) {
mcimadamore@635 195 return visit(t.type);
mcimadamore@635 196 }
mcimadamore@635 197
mcimadamore@635 198 @Override
mcimadamore@635 199 public Boolean visitUndetVar(UndetVar t, Void ignored) {
mcimadamore@635 200 if (seen.contains(t)) {
mcimadamore@635 201 return true;
mcimadamore@635 202 } else {
mcimadamore@635 203 seen = seen.prepend(t);
mcimadamore@635 204 return visit(t.inst);
mcimadamore@635 205 }
mcimadamore@635 206 }
mcimadamore@635 207 };
mcimadamore@635 208 return constraintScanner.visit(uv);
mcimadamore@635 209 }
duke@1 210 };
duke@1 211
duke@1 212 /***************************************************************************
duke@1 213 * Mini/Maximization of UndetVars
duke@1 214 ***************************************************************************/
duke@1 215
duke@1 216 /** Instantiate undetermined type variable to its minimal upper bound.
duke@1 217 * Throw a NoInstanceException if this not possible.
duke@1 218 */
duke@1 219 void maximizeInst(UndetVar that, Warner warn) throws NoInstanceException {
duke@1 220 if (that.inst == null) {
duke@1 221 if (that.hibounds.isEmpty())
duke@1 222 that.inst = syms.objectType;
duke@1 223 else if (that.hibounds.tail.isEmpty())
duke@1 224 that.inst = that.hibounds.head;
mcimadamore@210 225 else
mcimadamore@210 226 that.inst = types.glb(that.hibounds);
duke@1 227 }
mcimadamore@210 228 if (that.inst == null ||
mcimadamore@298 229 that.inst.isErroneous())
mcimadamore@210 230 throw ambiguousNoInstanceException
mcimadamore@210 231 .setMessage("no.unique.maximal.instance.exists",
mcimadamore@210 232 that.qtype, that.hibounds);
duke@1 233 }
duke@1 234 //where
duke@1 235 private boolean isSubClass(Type t, final List<Type> ts) {
duke@1 236 t = t.baseType();
duke@1 237 if (t.tag == TYPEVAR) {
duke@1 238 List<Type> bounds = types.getBounds((TypeVar)t);
duke@1 239 for (Type s : ts) {
duke@1 240 if (!types.isSameType(t, s.baseType())) {
duke@1 241 for (Type bound : bounds) {
duke@1 242 if (!isSubClass(bound, List.of(s.baseType())))
duke@1 243 return false;
duke@1 244 }
duke@1 245 }
duke@1 246 }
duke@1 247 } else {
duke@1 248 for (Type s : ts) {
duke@1 249 if (!t.tsym.isSubClass(s.baseType().tsym, types))
duke@1 250 return false;
duke@1 251 }
duke@1 252 }
duke@1 253 return true;
duke@1 254 }
duke@1 255
jjg@110 256 /** Instantiate undetermined type variable to the lub of all its lower bounds.
duke@1 257 * Throw a NoInstanceException if this not possible.
duke@1 258 */
duke@1 259 void minimizeInst(UndetVar that, Warner warn) throws NoInstanceException {
duke@1 260 if (that.inst == null) {
duke@1 261 if (that.lobounds.isEmpty())
duke@1 262 that.inst = syms.botType;
duke@1 263 else if (that.lobounds.tail.isEmpty())
mcimadamore@5 264 that.inst = that.lobounds.head.isPrimitive() ? syms.errType : that.lobounds.head;
duke@1 265 else {
duke@1 266 that.inst = types.lub(that.lobounds);
mcimadamore@5 267 }
jjg@110 268 if (that.inst == null || that.inst.tag == ERROR)
duke@1 269 throw ambiguousNoInstanceException
duke@1 270 .setMessage("no.unique.minimal.instance.exists",
duke@1 271 that.qtype, that.lobounds);
duke@1 272 // VGJ: sort of inlined maximizeInst() below. Adding
duke@1 273 // bounds can cause lobounds that are above hibounds.
duke@1 274 if (that.hibounds.isEmpty())
duke@1 275 return;
duke@1 276 Type hb = null;
duke@1 277 if (that.hibounds.tail.isEmpty())
duke@1 278 hb = that.hibounds.head;
duke@1 279 else for (List<Type> bs = that.hibounds;
duke@1 280 bs.nonEmpty() && hb == null;
duke@1 281 bs = bs.tail) {
duke@1 282 if (isSubClass(bs.head, that.hibounds))
duke@1 283 hb = types.fromUnknownFun.apply(bs.head);
duke@1 284 }
duke@1 285 if (hb == null ||
duke@1 286 !types.isSubtypeUnchecked(hb, that.hibounds, warn) ||
duke@1 287 !types.isSubtypeUnchecked(that.inst, hb, warn))
duke@1 288 throw ambiguousNoInstanceException;
duke@1 289 }
duke@1 290 }
duke@1 291
duke@1 292 /***************************************************************************
duke@1 293 * Exported Methods
duke@1 294 ***************************************************************************/
duke@1 295
duke@1 296 /** Try to instantiate expression type `that' to given type `to'.
duke@1 297 * If a maximal instantiation exists which makes this type
duke@1 298 * a subtype of type `to', return the instantiated type.
duke@1 299 * If no instantiation exists, or if several incomparable
duke@1 300 * best instantiations exist throw a NoInstanceException.
duke@1 301 */
duke@1 302 public Type instantiateExpr(ForAll that,
duke@1 303 Type to,
mcimadamore@299 304 Warner warn) throws InferenceException {
duke@1 305 List<Type> undetvars = Type.map(that.tvars, fromTypeVarFun);
duke@1 306 for (List<Type> l = undetvars; l.nonEmpty(); l = l.tail) {
mcimadamore@396 307 UndetVar uv = (UndetVar) l.head;
mcimadamore@396 308 TypeVar tv = (TypeVar)uv.qtype;
duke@1 309 ListBuffer<Type> hibounds = new ListBuffer<Type>();
mcimadamore@615 310 for (Type t : that.getConstraints(tv, ConstraintKind.EXTENDS)) {
mcimadamore@635 311 hibounds.append(types.subst(t, that.tvars, undetvars));
duke@1 312 }
mcimadamore@635 313
mcimadamore@396 314 List<Type> inst = that.getConstraints(tv, ConstraintKind.EQUAL);
mcimadamore@396 315 if (inst.nonEmpty() && inst.head.tag != BOT) {
mcimadamore@396 316 uv.inst = inst.head;
mcimadamore@396 317 }
mcimadamore@396 318 uv.hibounds = hibounds.toList();
duke@1 319 }
duke@1 320 Type qtype1 = types.subst(that.qtype, that.tvars, undetvars);
duke@1 321 if (!types.isSubtype(qtype1, to)) {
duke@1 322 throw unambiguousNoInstanceException
duke@1 323 .setMessage("no.conforming.instance.exists",
duke@1 324 that.tvars, that.qtype, to);
duke@1 325 }
duke@1 326 for (List<Type> l = undetvars; l.nonEmpty(); l = l.tail)
duke@1 327 maximizeInst((UndetVar) l.head, warn);
duke@1 328 // System.out.println(" = " + qtype1.map(getInstFun));//DEBUG
duke@1 329
duke@1 330 // check bounds
duke@1 331 List<Type> targs = Type.map(undetvars, getInstFun);
mcimadamore@635 332 if (Type.containsAny(targs, that.tvars)) {
mcimadamore@635 333 //replace uninferred type-vars
mcimadamore@635 334 targs = types.subst(targs,
mcimadamore@635 335 that.tvars,
mcimadamore@635 336 instaniateAsUninferredVars(undetvars, that.tvars));
mcimadamore@635 337 }
mcimadamore@396 338 return chk.checkType(warn.pos(), that.inst(targs, types), to);
duke@1 339 }
mcimadamore@635 340 //where
mcimadamore@635 341 private List<Type> instaniateAsUninferredVars(List<Type> undetvars, List<Type> tvars) {
mcimadamore@635 342 ListBuffer<Type> new_targs = ListBuffer.lb();
mcimadamore@635 343 //step 1 - create syntethic captured vars
mcimadamore@635 344 for (Type t : undetvars) {
mcimadamore@635 345 UndetVar uv = (UndetVar)t;
mcimadamore@635 346 Type newArg = new CapturedType(t.tsym.name, t.tsym, uv.inst, syms.botType, null);
mcimadamore@635 347 new_targs = new_targs.append(newArg);
mcimadamore@635 348 }
mcimadamore@635 349 //step 2 - replace synthetic vars in their bounds
mcimadamore@635 350 for (Type t : new_targs.toList()) {
mcimadamore@635 351 CapturedType ct = (CapturedType)t;
mcimadamore@635 352 ct.bound = types.subst(ct.bound, tvars, new_targs.toList());
mcimadamore@635 353 WildcardType wt = new WildcardType(ct.bound, BoundKind.EXTENDS, syms.boundClass);
mcimadamore@635 354 ct.wildcard = wt;
mcimadamore@635 355 }
mcimadamore@635 356 return new_targs.toList();
mcimadamore@635 357 }
duke@1 358
duke@1 359 /** Instantiate method type `mt' by finding instantiations of
duke@1 360 * `tvars' so that method can be applied to `argtypes'.
duke@1 361 */
mcimadamore@547 362 public Type instantiateMethod(final Env<AttrContext> env,
mcimadamore@547 363 List<Type> tvars,
duke@1 364 MethodType mt,
mcimadamore@580 365 final Symbol msym,
mcimadamore@299 366 final List<Type> argtypes,
mcimadamore@299 367 final boolean allowBoxing,
mcimadamore@299 368 final boolean useVarargs,
mcimadamore@299 369 final Warner warn) throws InferenceException {
duke@1 370 //-System.err.println("instantiateMethod(" + tvars + ", " + mt + ", " + argtypes + ")"); //DEBUG
duke@1 371 List<Type> undetvars = Type.map(tvars, fromTypeVarFun);
duke@1 372 List<Type> formals = mt.argtypes;
mcimadamore@299 373 //need to capture exactly once - otherwise subsequent
mcimadamore@299 374 //applicability checks might fail
mcimadamore@299 375 final List<Type> capturedArgs = types.capture(argtypes);
mcimadamore@299 376 List<Type> actuals = capturedArgs;
mcimadamore@299 377 List<Type> actualsNoCapture = argtypes;
duke@1 378 // instantiate all polymorphic argument types and
duke@1 379 // set up lower bounds constraints for undetvars
duke@1 380 Type varargsFormal = useVarargs ? formals.last() : null;
mcimadamore@299 381 while (actuals.nonEmpty() && formals.head != varargsFormal) {
mcimadamore@299 382 Type formal = formals.head;
mcimadamore@299 383 Type actual = actuals.head.baseType();
mcimadamore@299 384 Type actualNoCapture = actualsNoCapture.head.baseType();
mcimadamore@299 385 if (actual.tag == FORALL)
mcimadamore@299 386 actual = instantiateArg((ForAll)actual, formal, tvars, warn);
mcimadamore@299 387 Type undetFormal = types.subst(formal, tvars, undetvars);
duke@1 388 boolean works = allowBoxing
mcimadamore@299 389 ? types.isConvertible(actual, undetFormal, warn)
mcimadamore@299 390 : types.isSubtypeUnchecked(actual, undetFormal, warn);
duke@1 391 if (!works) {
duke@1 392 throw unambiguousNoInstanceException
duke@1 393 .setMessage("no.conforming.assignment.exists",
mcimadamore@299 394 tvars, actualNoCapture, formal);
duke@1 395 }
duke@1 396 formals = formals.tail;
mcimadamore@299 397 actuals = actuals.tail;
mcimadamore@299 398 actualsNoCapture = actualsNoCapture.tail;
duke@1 399 }
duke@1 400 if (formals.head != varargsFormal || // not enough args
mcimadamore@299 401 !useVarargs && actuals.nonEmpty()) { // too many args
duke@1 402 // argument lists differ in length
duke@1 403 throw unambiguousNoInstanceException
duke@1 404 .setMessage("arg.length.mismatch");
duke@1 405 }
duke@1 406
duke@1 407 // for varargs arguments as well
duke@1 408 if (useVarargs) {
mcimadamore@299 409 Type elemType = types.elemtype(varargsFormal);
mcimadamore@299 410 Type elemUndet = types.subst(elemType, tvars, undetvars);
mcimadamore@299 411 while (actuals.nonEmpty()) {
mcimadamore@299 412 Type actual = actuals.head.baseType();
mcimadamore@299 413 Type actualNoCapture = actualsNoCapture.head.baseType();
mcimadamore@299 414 if (actual.tag == FORALL)
mcimadamore@299 415 actual = instantiateArg((ForAll)actual, elemType, tvars, warn);
mcimadamore@299 416 boolean works = types.isConvertible(actual, elemUndet, warn);
duke@1 417 if (!works) {
duke@1 418 throw unambiguousNoInstanceException
duke@1 419 .setMessage("no.conforming.assignment.exists",
mcimadamore@299 420 tvars, actualNoCapture, elemType);
duke@1 421 }
mcimadamore@299 422 actuals = actuals.tail;
mcimadamore@299 423 actualsNoCapture = actualsNoCapture.tail;
duke@1 424 }
duke@1 425 }
duke@1 426
duke@1 427 // minimize as yet undetermined type variables
duke@1 428 for (Type t : undetvars)
duke@1 429 minimizeInst((UndetVar) t, warn);
duke@1 430
duke@1 431 /** Type variables instantiated to bottom */
duke@1 432 ListBuffer<Type> restvars = new ListBuffer<Type>();
duke@1 433
mcimadamore@396 434 /** Undet vars instantiated to bottom */
mcimadamore@396 435 final ListBuffer<Type> restundet = new ListBuffer<Type>();
mcimadamore@396 436
duke@1 437 /** Instantiated types or TypeVars if under-constrained */
duke@1 438 ListBuffer<Type> insttypes = new ListBuffer<Type>();
duke@1 439
duke@1 440 /** Instantiated types or UndetVars if under-constrained */
duke@1 441 ListBuffer<Type> undettypes = new ListBuffer<Type>();
duke@1 442
duke@1 443 for (Type t : undetvars) {
duke@1 444 UndetVar uv = (UndetVar)t;
duke@1 445 if (uv.inst.tag == BOT) {
duke@1 446 restvars.append(uv.qtype);
mcimadamore@396 447 restundet.append(uv);
duke@1 448 insttypes.append(uv.qtype);
duke@1 449 undettypes.append(uv);
duke@1 450 uv.inst = null;
duke@1 451 } else {
duke@1 452 insttypes.append(uv.inst);
duke@1 453 undettypes.append(uv.inst);
duke@1 454 }
duke@1 455 }
duke@1 456 checkWithinBounds(tvars, undettypes.toList(), warn);
duke@1 457
mcimadamore@299 458 mt = (MethodType)types.subst(mt, tvars, insttypes.toList());
mcimadamore@299 459
duke@1 460 if (!restvars.isEmpty()) {
duke@1 461 // if there are uninstantiated variables,
duke@1 462 // quantify result type with them
mcimadamore@299 463 final List<Type> inferredTypes = insttypes.toList();
mcimadamore@299 464 final List<Type> all_tvars = tvars; //this is the wrong tvars
mcimadamore@299 465 final MethodType mt2 = new MethodType(mt.argtypes, null, mt.thrown, syms.methodClass);
mcimadamore@299 466 mt2.restype = new ForAll(restvars.toList(), mt.restype) {
mcimadamore@299 467 @Override
mcimadamore@396 468 public List<Type> getConstraints(TypeVar tv, ConstraintKind ck) {
mcimadamore@396 469 for (Type t : restundet.toList()) {
mcimadamore@396 470 UndetVar uv = (UndetVar)t;
mcimadamore@396 471 if (uv.qtype == tv) {
mcimadamore@396 472 switch (ck) {
mcimadamore@615 473 case EXTENDS: return uv.hibounds.appendList(types.subst(types.getBounds(tv), all_tvars, inferredTypes));
mcimadamore@396 474 case SUPER: return uv.lobounds;
mcimadamore@396 475 case EQUAL: return uv.inst != null ? List.of(uv.inst) : List.<Type>nil();
mcimadamore@396 476 }
mcimadamore@396 477 }
mcimadamore@396 478 }
mcimadamore@396 479 return List.nil();
mcimadamore@396 480 }
mcimadamore@396 481
mcimadamore@396 482 @Override
mcimadamore@299 483 public Type inst(List<Type> inferred, Types types) throws NoInstanceException {
mcimadamore@299 484 List<Type> formals = types.subst(mt2.argtypes, tvars, inferred);
mcimadamore@396 485 if (!rs.argumentsAcceptable(capturedArgs, formals,
mcimadamore@299 486 allowBoxing, useVarargs, warn)) {
mcimadamore@299 487 // inferred method is not applicable
mcimadamore@299 488 throw invalidInstanceException.setMessage("inferred.do.not.conform.to.params", formals, argtypes);
mcimadamore@396 489 }
mcimadamore@396 490 // check that inferred bounds conform to their bounds
mcimadamore@396 491 checkWithinBounds(all_tvars,
mcimadamore@299 492 types.subst(inferredTypes, tvars, inferred), warn);
mcimadamore@547 493 if (useVarargs) {
mcimadamore@580 494 chk.checkVararg(env.tree.pos(), formals, msym, env);
mcimadamore@547 495 }
mcimadamore@396 496 return super.inst(inferred, types);
mcimadamore@299 497 }};
mcimadamore@299 498 return mt2;
duke@1 499 }
mcimadamore@299 500 else if (!rs.argumentsAcceptable(capturedArgs, mt.getParameterTypes(), allowBoxing, useVarargs, warn)) {
mcimadamore@299 501 // inferred method is not applicable
mcimadamore@299 502 throw invalidInstanceException.setMessage("inferred.do.not.conform.to.params", mt.getParameterTypes(), argtypes);
mcimadamore@299 503 }
mcimadamore@299 504 else {
mcimadamore@299 505 // return instantiated version of method type
mcimadamore@299 506 return mt;
mcimadamore@299 507 }
duke@1 508 }
duke@1 509 //where
duke@1 510
duke@1 511 /** Try to instantiate argument type `that' to given type `to'.
duke@1 512 * If this fails, try to insantiate `that' to `to' where
duke@1 513 * every occurrence of a type variable in `tvars' is replaced
duke@1 514 * by an unknown type.
duke@1 515 */
duke@1 516 private Type instantiateArg(ForAll that,
duke@1 517 Type to,
duke@1 518 List<Type> tvars,
mcimadamore@299 519 Warner warn) throws InferenceException {
duke@1 520 List<Type> targs;
duke@1 521 try {
duke@1 522 return instantiateExpr(that, to, warn);
duke@1 523 } catch (NoInstanceException ex) {
duke@1 524 Type to1 = to;
duke@1 525 for (List<Type> l = tvars; l.nonEmpty(); l = l.tail)
duke@1 526 to1 = types.subst(to1, List.of(l.head), List.of(syms.unknownType));
duke@1 527 return instantiateExpr(that, to1, warn);
duke@1 528 }
duke@1 529 }
duke@1 530
duke@1 531 /** check that type parameters are within their bounds.
duke@1 532 */
mcimadamore@615 533 void checkWithinBounds(List<Type> tvars,
duke@1 534 List<Type> arguments,
duke@1 535 Warner warn)
mcimadamore@299 536 throws InvalidInstanceException {
duke@1 537 for (List<Type> tvs = tvars, args = arguments;
duke@1 538 tvs.nonEmpty();
duke@1 539 tvs = tvs.tail, args = args.tail) {
duke@1 540 if (args.head instanceof UndetVar) continue;
duke@1 541 List<Type> bounds = types.subst(types.getBounds((TypeVar)tvs.head), tvars, arguments);
duke@1 542 if (!types.isSubtypeUnchecked(args.head, bounds, warn))
mcimadamore@299 543 throw invalidInstanceException
duke@1 544 .setMessage("inferred.do.not.conform.to.bounds",
mcimadamore@299 545 args.head, bounds);
duke@1 546 }
duke@1 547 }
mcimadamore@674 548
mcimadamore@674 549 /**
mcimadamore@674 550 * Compute a synthetic method type corresponding to the requested polymorphic
mcimadamore@674 551 * method signature. If no explicit return type is supplied, a provisional
mcimadamore@674 552 * return type is computed (just Object in case of non-transitional 292)
mcimadamore@674 553 */
mcimadamore@674 554 Type instantiatePolymorphicSignatureInstance(Env<AttrContext> env, Type site,
mcimadamore@674 555 Name name,
mcimadamore@674 556 MethodSymbol spMethod, // sig. poly. method or null if none
mcimadamore@674 557 List<Type> argtypes,
mcimadamore@674 558 List<Type> typeargtypes) {
mcimadamore@674 559 final Type restype;
mcimadamore@674 560 if (rs.allowTransitionalJSR292 && typeargtypes.nonEmpty()) {
mcimadamore@674 561 restype = typeargtypes.head;
mcimadamore@674 562 } else {
mcimadamore@674 563 //The return type for a polymorphic signature call is computed from
mcimadamore@674 564 //the enclosing tree E, as follows: if E is a cast, then use the
mcimadamore@674 565 //target type of the cast expression as a return type; if E is an
mcimadamore@674 566 //expression statement, the return type is 'void' - otherwise the
mcimadamore@674 567 //return type is simply 'Object'.
mcimadamore@674 568 switch (env.outer.tree.getTag()) {
mcimadamore@674 569 case JCTree.TYPECAST:
mcimadamore@674 570 restype = ((JCTypeCast)env.outer.tree).clazz.type; break;
mcimadamore@674 571 case JCTree.EXEC:
mcimadamore@674 572 restype = syms.voidType; break;
mcimadamore@674 573 default:
mcimadamore@674 574 restype = syms.objectType;
mcimadamore@674 575 }
mcimadamore@674 576 }
mcimadamore@674 577
mcimadamore@674 578 List<Type> paramtypes = Type.map(argtypes, implicitArgType);
mcimadamore@674 579 List<Type> exType = spMethod != null ?
mcimadamore@674 580 spMethod.getThrownTypes() :
mcimadamore@674 581 List.of(syms.throwableType); // make it throw all exceptions
mcimadamore@674 582
mcimadamore@674 583 MethodType mtype = new MethodType(paramtypes,
mcimadamore@674 584 restype,
mcimadamore@674 585 exType,
mcimadamore@674 586 syms.methodClass);
mcimadamore@674 587 return mtype;
mcimadamore@674 588 }
mcimadamore@674 589 //where
mcimadamore@674 590 Mapping implicitArgType = new Mapping ("implicitArgType") {
mcimadamore@674 591 public Type apply(Type t) {
mcimadamore@674 592 t = types.erasure(t);
mcimadamore@674 593 if (t.tag == BOT)
mcimadamore@674 594 // nulls type as the marker type Null (which has no instances)
mcimadamore@674 595 // infer as java.lang.Void for now
mcimadamore@674 596 t = types.boxedClass(syms.voidType).type;
mcimadamore@674 597 return t;
mcimadamore@674 598 }
mcimadamore@674 599 };
duke@1 600 }

mercurial