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

Tue, 08 Nov 2011 11:51:05 -0800

author
jjg
date
Tue, 08 Nov 2011 11:51:05 -0800
changeset 1127
ca49d50318dc
parent 1114
05814303a056
child 1178
133744729455
permissions
-rw-r--r--

6921494: provide way to print javac tree tag values
Reviewed-by: jjg, mcimadamore
Contributed-by: vicenterz@yahoo.es

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

mercurial