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

Fri, 12 Nov 2010 12:33:52 +0000

author
mcimadamore
date
Fri, 12 Nov 2010 12:33:52 +0000
changeset 742
fdc67f5170e9
parent 716
493ecc8111ba
child 753
2536dedd897e
permissions
-rw-r--r--

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

mercurial