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

Wed, 17 Jul 2013 14:19:25 +0100

author
mcimadamore
date
Wed, 17 Jul 2013 14:19:25 +0100
changeset 1904
b577222ef7b3
parent 1903
155809b1b969
child 1905
f65a807714ba
permissions
-rw-r--r--

8019340: varargs-related warnings are meaningless on signature-polymorphic methods such as MethodHandle.invokeExact
Summary: Disable certain varargs warnings when compiling polymorphic signature calls
Reviewed-by: jjg

duke@1 1 /*
mcimadamore@1562 2 * Copyright (c) 1999, 2013, 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.*;
mcimadamore@1562 32 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
duke@1 33 import com.sun.tools.javac.util.List;
mcimadamore@1562 34 import com.sun.tools.javac.code.*;
mcimadamore@1562 35 import com.sun.tools.javac.code.Type.*;
mcimadamore@1562 36 import com.sun.tools.javac.code.Type.UndetVar.InferenceBound;
mcimadamore@1562 37 import com.sun.tools.javac.code.Symbol.*;
mcimadamore@1562 38 import com.sun.tools.javac.comp.DeferredAttr.AttrMode;
mcimadamore@1562 39 import com.sun.tools.javac.comp.Infer.GraphSolver.InferenceGraph;
mcimadamore@1562 40 import com.sun.tools.javac.comp.Infer.GraphSolver.InferenceGraph.Node;
mcimadamore@1562 41 import com.sun.tools.javac.comp.Resolve.InapplicableMethodException;
mcimadamore@1562 42 import com.sun.tools.javac.comp.Resolve.VerboseResolutionMode;
duke@1 43
mcimadamore@1903 44 import java.util.Comparator;
mcimadamore@1337 45 import java.util.HashMap;
mcimadamore@1337 46 import java.util.Map;
mcimadamore@1562 47 import java.util.Set;
mcimadamore@1903 48 import java.util.TreeSet;
mcimadamore@1562 49
mcimadamore@1562 50 import java.util.ArrayList;
mcimadamore@1562 51 import java.util.Collections;
mcimadamore@1562 52 import java.util.EnumSet;
mcimadamore@1562 53 import java.util.HashSet;
mcimadamore@1337 54
jjg@1374 55 import static com.sun.tools.javac.code.TypeTag.*;
duke@1 56
duke@1 57 /** Helper class for type parameter inference, used by the attribution phase.
duke@1 58 *
jjg@581 59 * <p><b>This is NOT part of any supported API.
jjg@581 60 * If you write code that depends on this, you do so at your own risk.
duke@1 61 * This code and its internal interfaces are subject to change or
duke@1 62 * deletion without notice.</b>
duke@1 63 */
duke@1 64 public class Infer {
duke@1 65 protected static final Context.Key<Infer> inferKey =
duke@1 66 new Context.Key<Infer>();
duke@1 67
mcimadamore@1562 68 Resolve rs;
mcimadamore@1562 69 Check chk;
duke@1 70 Symtab syms;
duke@1 71 Types types;
mcimadamore@1562 72 JCDiagnostic.Factory diags;
mcimadamore@1114 73 Log log;
duke@1 74
mcimadamore@1562 75 /** should the graph solver be used? */
mcimadamore@1562 76 boolean allowGraphInference;
mcimadamore@1510 77
duke@1 78 public static Infer instance(Context context) {
duke@1 79 Infer instance = context.get(inferKey);
duke@1 80 if (instance == null)
duke@1 81 instance = new Infer(context);
duke@1 82 return instance;
duke@1 83 }
duke@1 84
duke@1 85 protected Infer(Context context) {
duke@1 86 context.put(inferKey, this);
mcimadamore@1562 87
mcimadamore@1562 88 rs = Resolve.instance(context);
mcimadamore@1562 89 chk = Check.instance(context);
duke@1 90 syms = Symtab.instance(context);
duke@1 91 types = Types.instance(context);
mcimadamore@1562 92 diags = JCDiagnostic.Factory.instance(context);
mcimadamore@1114 93 log = Log.instance(context);
mcimadamore@1298 94 inferenceException = new InferenceException(diags);
mcimadamore@1562 95 Options options = Options.instance(context);
mcimadamore@1562 96 allowGraphInference = Source.instance(context).allowGraphInference()
mcimadamore@1562 97 && options.isUnset("useLegacyInference");
duke@1 98 }
duke@1 99
mcimadamore@1562 100 /** A value for prototypes that admit any type, including polymorphic ones. */
vromero@1853 101 public static final Type anyPoly = new JCNoType();
mcimadamore@1562 102
mcimadamore@1337 103 /**
mcimadamore@1337 104 * This exception class is design to store a list of diagnostics corresponding
mcimadamore@1337 105 * to inference errors that can arise during a method applicability check.
mcimadamore@1337 106 */
mcimadamore@1186 107 public static class InferenceException extends InapplicableMethodException {
duke@1 108 private static final long serialVersionUID = 0;
duke@1 109
mcimadamore@1337 110 List<JCDiagnostic> messages = List.nil();
mcimadamore@1337 111
mcimadamore@299 112 InferenceException(JCDiagnostic.Factory diags) {
mcimadamore@689 113 super(diags);
duke@1 114 }
mcimadamore@1337 115
mcimadamore@1337 116 @Override
mcimadamore@1337 117 InapplicableMethodException setMessage(JCDiagnostic diag) {
mcimadamore@1337 118 messages = messages.append(diag);
mcimadamore@1337 119 return this;
mcimadamore@1337 120 }
mcimadamore@1337 121
mcimadamore@1337 122 @Override
mcimadamore@1337 123 public JCDiagnostic getDiagnostic() {
mcimadamore@1337 124 return messages.head;
mcimadamore@1337 125 }
mcimadamore@1337 126
mcimadamore@1337 127 void clear() {
mcimadamore@1337 128 messages = List.nil();
mcimadamore@1337 129 }
mcimadamore@299 130 }
mcimadamore@299 131
mcimadamore@1562 132 protected final InferenceException inferenceException;
duke@1 133
mcimadamore@1562 134 // <editor-fold defaultstate="collapsed" desc="Inference routines">
mcimadamore@1337 135 /**
mcimadamore@1562 136 * Main inference entry point - instantiate a generic method type
mcimadamore@1562 137 * using given argument types and (possibly) an expected target-type.
duke@1 138 */
mcimadamore@1268 139 public Type instantiateMethod(Env<AttrContext> env,
mcimadamore@547 140 List<Type> tvars,
duke@1 141 MethodType mt,
mcimadamore@1268 142 Attr.ResultInfo resultInfo,
mcimadamore@1268 143 Symbol msym,
mcimadamore@1268 144 List<Type> argtypes,
mcimadamore@1268 145 boolean allowBoxing,
mcimadamore@1268 146 boolean useVarargs,
mcimadamore@1347 147 Resolve.MethodResolutionContext resolveContext,
mcimadamore@1268 148 Warner warn) throws InferenceException {
duke@1 149 //-System.err.println("instantiateMethod(" + tvars + ", " + mt + ", " + argtypes + ")"); //DEBUG
mcimadamore@1550 150 final InferenceContext inferenceContext = new InferenceContext(tvars);
mcimadamore@1337 151 inferenceException.clear();
mcimadamore@1562 152 try {
mcimadamore@1562 153 DeferredAttr.DeferredAttrContext deferredAttrContext =
mcimadamore@1897 154 resolveContext.deferredAttrContext(msym, inferenceContext, resultInfo, warn);
mcimadamore@689 155
mcimadamore@1674 156 resolveContext.methodCheck.argumentsAcceptable(env, deferredAttrContext,
mcimadamore@1562 157 argtypes, mt.getParameterTypes(), warn);
mcimadamore@1479 158
mcimadamore@1562 159 if (allowGraphInference &&
mcimadamore@1562 160 resultInfo != null &&
mcimadamore@1510 161 !warn.hasNonSilentLint(Lint.LintCategory.UNCHECKED)) {
mcimadamore@1562 162 //inject return constraints earlier
mcimadamore@1562 163 checkWithinBounds(inferenceContext, warn); //propagation
mcimadamore@1898 164 Type newRestype = generateReturnConstraints(resultInfo, mt, inferenceContext);
mcimadamore@1898 165 mt = (MethodType)types.createMethodTypeWithReturn(mt, newRestype);
mcimadamore@1562 166 //propagate outwards if needed
mcimadamore@1562 167 if (resultInfo.checkContext.inferenceContext().free(resultInfo.pt)) {
mcimadamore@1562 168 //propagate inference context outwards and exit
mcimadamore@1562 169 inferenceContext.dupTo(resultInfo.checkContext.inferenceContext());
mcimadamore@1562 170 deferredAttrContext.complete();
mcimadamore@1562 171 return mt;
mcimadamore@1562 172 }
mcimadamore@1510 173 }
mcimadamore@1510 174
mcimadamore@1479 175 deferredAttrContext.complete();
duke@1 176
mcimadamore@1337 177 // minimize as yet undetermined type variables
mcimadamore@1562 178 if (allowGraphInference) {
mcimadamore@1562 179 inferenceContext.solve(warn);
mcimadamore@1562 180 } else {
mcimadamore@1562 181 inferenceContext.solveLegacy(true, warn, LegacyInferenceSteps.EQ_LOWER.steps); //minimizeInst
mcimadamore@1337 182 }
duke@1 183
mcimadamore@1550 184 mt = (MethodType)inferenceContext.asInstType(mt);
mcimadamore@396 185
mcimadamore@1562 186 if (!allowGraphInference &&
mcimadamore@1562 187 inferenceContext.restvars().nonEmpty() &&
mcimadamore@1562 188 resultInfo != null &&
mcimadamore@1562 189 !warn.hasNonSilentLint(Lint.LintCategory.UNCHECKED)) {
mcimadamore@1562 190 generateReturnConstraints(resultInfo, mt, inferenceContext);
mcimadamore@1562 191 inferenceContext.solveLegacy(false, warn, LegacyInferenceSteps.EQ_UPPER.steps); //maximizeInst
mcimadamore@1562 192 mt = (MethodType)inferenceContext.asInstType(mt);
mcimadamore@1562 193 }
duke@1 194
mcimadamore@1562 195 if (resultInfo != null && rs.verboseResolutionMode.contains(VerboseResolutionMode.DEFERRED_INST)) {
mcimadamore@1562 196 log.note(env.tree.pos, "deferred.method.inst", msym, mt, resultInfo.pt);
mcimadamore@1337 197 }
duke@1 198
mcimadamore@1337 199 // return instantiated version of method type
mcimadamore@1337 200 return mt;
mcimadamore@1337 201 } finally {
mcimadamore@1562 202 if (resultInfo != null || !allowGraphInference) {
mcimadamore@1562 203 inferenceContext.notifyChange();
mcimadamore@1562 204 } else {
mcimadamore@1562 205 inferenceContext.notifyChange(inferenceContext.boundedVars());
mcimadamore@1338 206 }
duke@1 207 }
mcimadamore@895 208 }
duke@1 209
mcimadamore@1562 210 /**
mcimadamore@1562 211 * Generate constraints from the generic method's return type. If the method
mcimadamore@1562 212 * call occurs in a context where a type T is expected, use the expected
mcimadamore@1562 213 * type to derive more constraints on the generic method inference variables.
mcimadamore@1562 214 */
mcimadamore@1898 215 Type generateReturnConstraints(Attr.ResultInfo resultInfo,
mcimadamore@1562 216 MethodType mt, InferenceContext inferenceContext) {
mcimadamore@1898 217 Type from = mt.getReturnType();
mcimadamore@1898 218 if (mt.getReturnType().containsAny(inferenceContext.inferencevars) &&
mcimadamore@1898 219 resultInfo.checkContext.inferenceContext() != emptyContext) {
mcimadamore@1898 220 from = types.capture(from);
mcimadamore@1898 221 //add synthetic captured ivars
mcimadamore@1898 222 for (Type t : from.getTypeArguments()) {
mcimadamore@1898 223 if (t.hasTag(TYPEVAR) && ((TypeVar)t).isCaptured()) {
mcimadamore@1898 224 inferenceContext.addVar((TypeVar)t);
mcimadamore@1898 225 }
mcimadamore@1898 226 }
mcimadamore@1898 227 }
mcimadamore@1898 228 Type qtype1 = inferenceContext.asFree(from);
mcimadamore@1562 229 Type to = returnConstraintTarget(qtype1, resultInfo.pt);
mcimadamore@1562 230 Assert.check(allowGraphInference || !resultInfo.checkContext.inferenceContext().free(to),
mcimadamore@1562 231 "legacy inference engine cannot handle constraints on both sides of a subtyping assertion");
mcimadamore@1562 232 //we need to skip capture?
mcimadamore@1562 233 Warner retWarn = new Warner();
mcimadamore@1562 234 if (!resultInfo.checkContext.compatible(qtype1, resultInfo.checkContext.inferenceContext().asFree(to), retWarn) ||
mcimadamore@1800 235 //unchecked conversion is not allowed in source 7 mode
mcimadamore@1800 236 (!allowGraphInference && retWarn.hasLint(Lint.LintCategory.UNCHECKED))) {
mcimadamore@1562 237 throw inferenceException
mcimadamore@1562 238 .setMessage("infer.no.conforming.instance.exists",
mcimadamore@1562 239 inferenceContext.restvars(), mt.getReturnType(), to);
mcimadamore@1562 240 }
mcimadamore@1898 241 return from;
mcimadamore@1562 242 }
mcimadamore@1897 243
mcimadamore@1897 244 Type returnConstraintTarget(Type from, Type to) {
mcimadamore@1897 245 if (from.hasTag(VOID)) {
mcimadamore@1897 246 return syms.voidType;
mcimadamore@1897 247 } else if (to.hasTag(NONE)) {
mcimadamore@1897 248 return from.isPrimitive() ? from : syms.objectType;
mcimadamore@1897 249 } else if (from.hasTag(UNDETVAR) && to.isPrimitive()) {
mcimadamore@1897 250 if (!allowGraphInference) {
mcimadamore@1897 251 //if legacy, just return boxed type
mcimadamore@1897 252 return types.boxedClass(to).type;
mcimadamore@1897 253 }
mcimadamore@1897 254 //if graph inference we need to skip conflicting boxed bounds...
mcimadamore@1897 255 UndetVar uv = (UndetVar)from;
mcimadamore@1897 256 for (Type t : uv.getBounds(InferenceBound.EQ, InferenceBound.LOWER)) {
mcimadamore@1897 257 Type boundAsPrimitive = types.unboxedType(t);
mcimadamore@1897 258 if (boundAsPrimitive == null) continue;
mcimadamore@1897 259 if (types.isConvertible(boundAsPrimitive, to)) {
mcimadamore@1897 260 //effectively skip return-type constraint generation (compatibility)
mcimadamore@1897 261 return syms.objectType;
mcimadamore@1562 262 }
mcimadamore@1251 263 }
mcimadamore@1897 264 return types.boxedClass(to).type;
mcimadamore@1897 265 } else {
mcimadamore@1897 266 return to;
duke@1 267 }
mcimadamore@1897 268 }
mcimadamore@1251 269
mcimadamore@1562 270 /**
mcimadamore@1562 271 * Infer cyclic inference variables as described in 15.12.2.8.
mcimadamore@1562 272 */
mcimadamore@1562 273 private void instantiateAsUninferredVars(List<Type> vars, InferenceContext inferenceContext) {
mcimadamore@1562 274 ListBuffer<Type> todo = ListBuffer.lb();
mcimadamore@1562 275 //step 1 - create fresh tvars
mcimadamore@1562 276 for (Type t : vars) {
mcimadamore@1562 277 UndetVar uv = (UndetVar)inferenceContext.asFree(t);
mcimadamore@1562 278 List<Type> upperBounds = uv.getBounds(InferenceBound.UPPER);
mcimadamore@1562 279 if (Type.containsAny(upperBounds, vars)) {
jfranck@1689 280 TypeSymbol fresh_tvar = new TypeVariableSymbol(Flags.SYNTHETIC, uv.qtype.tsym.name, null, uv.qtype.tsym.owner);
mcimadamore@1562 281 fresh_tvar.type = new TypeVar(fresh_tvar, types.makeCompoundType(uv.getBounds(InferenceBound.UPPER)), null);
mcimadamore@1562 282 todo.append(uv);
mcimadamore@1562 283 uv.inst = fresh_tvar.type;
mcimadamore@1562 284 } else if (upperBounds.nonEmpty()) {
mcimadamore@1562 285 uv.inst = types.glb(upperBounds);
mcimadamore@1562 286 } else {
mcimadamore@1562 287 uv.inst = syms.objectType;
mcimadamore@1338 288 }
mcimadamore@1562 289 }
mcimadamore@1562 290 //step 2 - replace fresh tvars in their bounds
mcimadamore@1562 291 List<Type> formals = vars;
mcimadamore@1562 292 for (Type t : todo) {
mcimadamore@1562 293 UndetVar uv = (UndetVar)t;
mcimadamore@1562 294 TypeVar ct = (TypeVar)uv.inst;
mcimadamore@1562 295 ct.bound = types.glb(inferenceContext.asInstTypes(types.getBounds(ct)));
mcimadamore@1562 296 if (ct.bound.isErroneous()) {
mcimadamore@1562 297 //report inference error if glb fails
mcimadamore@1562 298 reportBoundError(uv, BoundErrorKind.BAD_UPPER);
mcimadamore@1338 299 }
mcimadamore@1562 300 formals = formals.tail;
mcimadamore@1348 301 }
mcimadamore@1348 302 }
mcimadamore@1348 303
mcimadamore@674 304 /**
mcimadamore@674 305 * Compute a synthetic method type corresponding to the requested polymorphic
mcimadamore@820 306 * method signature. The target return type is computed from the immediately
mcimadamore@820 307 * enclosing scope surrounding the polymorphic-signature call.
mcimadamore@674 308 */
mcimadamore@1239 309 Type instantiatePolymorphicSignatureInstance(Env<AttrContext> env,
mcimadamore@674 310 MethodSymbol spMethod, // sig. poly. method or null if none
mcimadamore@1347 311 Resolve.MethodResolutionContext resolveContext,
mcimadamore@820 312 List<Type> argtypes) {
mcimadamore@674 313 final Type restype;
mcimadamore@716 314
mcimadamore@820 315 //The return type for a polymorphic signature call is computed from
mcimadamore@820 316 //the enclosing tree E, as follows: if E is a cast, then use the
mcimadamore@820 317 //target type of the cast expression as a return type; if E is an
mcimadamore@820 318 //expression statement, the return type is 'void' - otherwise the
mcimadamore@820 319 //return type is simply 'Object'. A correctness check ensures that
mcimadamore@820 320 //env.next refers to the lexically enclosing environment in which
mcimadamore@820 321 //the polymorphic signature call environment is nested.
mcimadamore@820 322
mcimadamore@820 323 switch (env.next.tree.getTag()) {
jjg@1127 324 case TYPECAST:
mcimadamore@820 325 JCTypeCast castTree = (JCTypeCast)env.next.tree;
mcimadamore@820 326 restype = (TreeInfo.skipParens(castTree.expr) == env.tree) ?
mcimadamore@820 327 castTree.clazz.type :
mcimadamore@820 328 syms.objectType;
mcimadamore@820 329 break;
jjg@1127 330 case EXEC:
mcimadamore@820 331 JCTree.JCExpressionStatement execTree =
mcimadamore@820 332 (JCTree.JCExpressionStatement)env.next.tree;
mcimadamore@820 333 restype = (TreeInfo.skipParens(execTree.expr) == env.tree) ?
mcimadamore@820 334 syms.voidType :
mcimadamore@820 335 syms.objectType;
mcimadamore@820 336 break;
mcimadamore@820 337 default:
mcimadamore@820 338 restype = syms.objectType;
mcimadamore@674 339 }
mcimadamore@674 340
mcimadamore@1347 341 List<Type> paramtypes = Type.map(argtypes, new ImplicitArgType(spMethod, resolveContext.step));
mcimadamore@674 342 List<Type> exType = spMethod != null ?
mcimadamore@674 343 spMethod.getThrownTypes() :
mcimadamore@674 344 List.of(syms.throwableType); // make it throw all exceptions
mcimadamore@674 345
mcimadamore@674 346 MethodType mtype = new MethodType(paramtypes,
mcimadamore@674 347 restype,
mcimadamore@674 348 exType,
mcimadamore@674 349 syms.methodClass);
mcimadamore@674 350 return mtype;
mcimadamore@674 351 }
mcimadamore@674 352 //where
mcimadamore@1347 353 class ImplicitArgType extends DeferredAttr.DeferredTypeMap {
mcimadamore@1347 354
mcimadamore@1347 355 public ImplicitArgType(Symbol msym, Resolve.MethodResolutionPhase phase) {
mcimadamore@1562 356 rs.deferredAttr.super(AttrMode.SPECULATIVE, msym, phase);
mcimadamore@1347 357 }
mcimadamore@1347 358
mcimadamore@1347 359 public Type apply(Type t) {
mcimadamore@1347 360 t = types.erasure(super.apply(t));
jjg@1374 361 if (t.hasTag(BOT))
mcimadamore@1347 362 // nulls type as the marker type Null (which has no instances)
mcimadamore@1347 363 // infer as java.lang.Void for now
mcimadamore@1347 364 t = types.boxedClass(syms.voidType).type;
mcimadamore@1347 365 return t;
mcimadamore@1347 366 }
mcimadamore@1347 367 }
mcimadamore@1337 368
mcimadamore@1337 369 /**
mcimadamore@1562 370 * This method is used to infer a suitable target SAM in case the original
mcimadamore@1562 371 * SAM type contains one or more wildcards. An inference process is applied
mcimadamore@1562 372 * so that wildcard bounds, as well as explicit lambda/method ref parameters
mcimadamore@1562 373 * (where applicable) are used to constraint the solution.
mcimadamore@1562 374 */
mcimadamore@1562 375 public Type instantiateFunctionalInterface(DiagnosticPosition pos, Type funcInterface,
mcimadamore@1562 376 List<Type> paramTypes, Check.CheckContext checkContext) {
mcimadamore@1562 377 if (types.capture(funcInterface) == funcInterface) {
mcimadamore@1562 378 //if capture doesn't change the type then return the target unchanged
mcimadamore@1562 379 //(this means the target contains no wildcards!)
mcimadamore@1562 380 return funcInterface;
mcimadamore@1562 381 } else {
mcimadamore@1562 382 Type formalInterface = funcInterface.tsym.type;
mcimadamore@1562 383 InferenceContext funcInterfaceContext =
mcimadamore@1562 384 new InferenceContext(funcInterface.tsym.type.getTypeArguments());
mcimadamore@1562 385
mcimadamore@1562 386 Assert.check(paramTypes != null);
mcimadamore@1562 387 //get constraints from explicit params (this is done by
mcimadamore@1562 388 //checking that explicit param types are equal to the ones
mcimadamore@1562 389 //in the functional interface descriptors)
mcimadamore@1562 390 List<Type> descParameterTypes = types.findDescriptorType(formalInterface).getParameterTypes();
mcimadamore@1562 391 if (descParameterTypes.size() != paramTypes.size()) {
mcimadamore@1562 392 checkContext.report(pos, diags.fragment("incompatible.arg.types.in.lambda"));
mcimadamore@1562 393 return types.createErrorType(funcInterface);
mcimadamore@1562 394 }
mcimadamore@1562 395 for (Type p : descParameterTypes) {
mcimadamore@1562 396 if (!types.isSameType(funcInterfaceContext.asFree(p), paramTypes.head)) {
mcimadamore@1562 397 checkContext.report(pos, diags.fragment("no.suitable.functional.intf.inst", funcInterface));
mcimadamore@1562 398 return types.createErrorType(funcInterface);
mcimadamore@1562 399 }
mcimadamore@1562 400 paramTypes = paramTypes.tail;
mcimadamore@1562 401 }
mcimadamore@1562 402
mcimadamore@1562 403 try {
mcimadamore@1562 404 funcInterfaceContext.solve(funcInterfaceContext.boundedVars(), types.noWarnings);
mcimadamore@1562 405 } catch (InferenceException ex) {
mcimadamore@1562 406 checkContext.report(pos, diags.fragment("no.suitable.functional.intf.inst", funcInterface));
mcimadamore@1562 407 }
mcimadamore@1562 408
mcimadamore@1562 409 List<Type> actualTypeargs = funcInterface.getTypeArguments();
mcimadamore@1562 410 for (Type t : funcInterfaceContext.undetvars) {
mcimadamore@1562 411 UndetVar uv = (UndetVar)t;
mcimadamore@1562 412 if (uv.inst == null) {
mcimadamore@1562 413 uv.inst = actualTypeargs.head;
mcimadamore@1562 414 }
mcimadamore@1562 415 actualTypeargs = actualTypeargs.tail;
mcimadamore@1562 416 }
mcimadamore@1562 417
mcimadamore@1562 418 Type owntype = funcInterfaceContext.asInstType(formalInterface);
mcimadamore@1562 419 if (!chk.checkValidGenericType(owntype)) {
mcimadamore@1562 420 //if the inferred functional interface type is not well-formed,
mcimadamore@1562 421 //or if it's not a subtype of the original target, issue an error
mcimadamore@1562 422 checkContext.report(pos, diags.fragment("no.suitable.functional.intf.inst", funcInterface));
mcimadamore@1562 423 }
mcimadamore@1562 424 return owntype;
mcimadamore@1562 425 }
mcimadamore@1562 426 }
mcimadamore@1562 427 // </editor-fold>
mcimadamore@1562 428
mcimadamore@1562 429 // <editor-fold defaultstate="collapsed" desc="Bound checking">
mcimadamore@1562 430 /**
mcimadamore@1562 431 * Check bounds and perform incorporation
mcimadamore@1562 432 */
mcimadamore@1562 433 void checkWithinBounds(InferenceContext inferenceContext,
mcimadamore@1562 434 Warner warn) throws InferenceException {
mcimadamore@1562 435 MultiUndetVarListener mlistener = new MultiUndetVarListener(inferenceContext.undetvars);
mcimadamore@1891 436 List<Type> saved_undet = inferenceContext.save();
mcimadamore@1562 437 try {
mcimadamore@1562 438 while (true) {
mcimadamore@1562 439 mlistener.reset();
mcimadamore@1562 440 if (!allowGraphInference) {
mcimadamore@1562 441 //in legacy mode we lack of transitivity, so bound check
mcimadamore@1562 442 //cannot be run in parallel with other incoprporation rounds
mcimadamore@1562 443 for (Type t : inferenceContext.undetvars) {
mcimadamore@1562 444 UndetVar uv = (UndetVar)t;
mcimadamore@1562 445 IncorporationStep.CHECK_BOUNDS.apply(uv, inferenceContext, warn);
mcimadamore@1562 446 }
mcimadamore@1562 447 }
mcimadamore@1562 448 for (Type t : inferenceContext.undetvars) {
mcimadamore@1562 449 UndetVar uv = (UndetVar)t;
mcimadamore@1562 450 //bound incorporation
mcimadamore@1562 451 EnumSet<IncorporationStep> incorporationSteps = allowGraphInference ?
mcimadamore@1562 452 incorporationStepsGraph : incorporationStepsLegacy;
mcimadamore@1562 453 for (IncorporationStep is : incorporationSteps) {
mcimadamore@1898 454 if (is.accepts(uv, inferenceContext)) {
mcimadamore@1898 455 is.apply(uv, inferenceContext, warn);
mcimadamore@1898 456 }
mcimadamore@1562 457 }
mcimadamore@1562 458 }
mcimadamore@1562 459 if (!mlistener.changed || !allowGraphInference) break;
mcimadamore@1562 460 }
mcimadamore@1562 461 }
mcimadamore@1562 462 finally {
mcimadamore@1562 463 mlistener.detach();
mcimadamore@1891 464 if (mlistener.rounds == MAX_INCORPORATION_STEPS) {
mcimadamore@1891 465 inferenceContext.rollback(saved_undet);
mcimadamore@1891 466 }
mcimadamore@1562 467 }
mcimadamore@1562 468 }
mcimadamore@1562 469 //where
mcimadamore@1562 470 /**
mcimadamore@1562 471 * This listener keeps track of changes on a group of inference variable
mcimadamore@1562 472 * bounds. Note: the listener must be detached (calling corresponding
mcimadamore@1562 473 * method) to make sure that the underlying inference variable is
mcimadamore@1562 474 * left in a clean state.
mcimadamore@1562 475 */
mcimadamore@1562 476 class MultiUndetVarListener implements UndetVar.UndetVarListener {
mcimadamore@1562 477
mcimadamore@1562 478 int rounds;
mcimadamore@1562 479 boolean changed;
mcimadamore@1562 480 List<Type> undetvars;
mcimadamore@1562 481
mcimadamore@1562 482 public MultiUndetVarListener(List<Type> undetvars) {
mcimadamore@1562 483 this.undetvars = undetvars;
mcimadamore@1562 484 for (Type t : undetvars) {
mcimadamore@1562 485 UndetVar uv = (UndetVar)t;
mcimadamore@1562 486 uv.listener = this;
mcimadamore@1562 487 }
mcimadamore@1562 488 }
mcimadamore@1562 489
mcimadamore@1562 490 public void varChanged(UndetVar uv, Set<InferenceBound> ibs) {
mcimadamore@1562 491 //avoid non-termination
mcimadamore@1562 492 if (rounds < MAX_INCORPORATION_STEPS) {
mcimadamore@1562 493 changed = true;
mcimadamore@1562 494 }
mcimadamore@1562 495 }
mcimadamore@1562 496
mcimadamore@1562 497 void reset() {
mcimadamore@1562 498 rounds++;
mcimadamore@1562 499 changed = false;
mcimadamore@1562 500 }
mcimadamore@1562 501
mcimadamore@1562 502 void detach() {
mcimadamore@1562 503 for (Type t : undetvars) {
mcimadamore@1562 504 UndetVar uv = (UndetVar)t;
mcimadamore@1562 505 uv.listener = null;
mcimadamore@1562 506 }
mcimadamore@1562 507 }
mcimadamore@1562 508 };
mcimadamore@1562 509
mcimadamore@1562 510 /** max number of incorporation rounds */
mcimadamore@1562 511 static final int MAX_INCORPORATION_STEPS = 100;
mcimadamore@1562 512
mcimadamore@1562 513 /**
mcimadamore@1562 514 * This enumeration defines an entry point for doing inference variable
mcimadamore@1562 515 * bound incorporation - it can be used to inject custom incorporation
mcimadamore@1562 516 * logic into the basic bound checking routine
mcimadamore@1562 517 */
mcimadamore@1562 518 enum IncorporationStep {
mcimadamore@1562 519 /**
mcimadamore@1562 520 * Performs basic bound checking - i.e. is the instantiated type for a given
mcimadamore@1562 521 * inference variable compatible with its bounds?
mcimadamore@1562 522 */
mcimadamore@1562 523 CHECK_BOUNDS() {
mcimadamore@1562 524 public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) {
mcimadamore@1562 525 Infer infer = inferenceContext.infer();
mcimadamore@1562 526 uv.substBounds(inferenceContext.inferenceVars(), inferenceContext.instTypes(), infer.types);
mcimadamore@1562 527 infer.checkCompatibleUpperBounds(uv, inferenceContext);
mcimadamore@1562 528 if (uv.inst != null) {
mcimadamore@1562 529 Type inst = uv.inst;
mcimadamore@1562 530 for (Type u : uv.getBounds(InferenceBound.UPPER)) {
mcimadamore@1562 531 if (!infer.types.isSubtypeUnchecked(inst, inferenceContext.asFree(u), warn)) {
mcimadamore@1562 532 infer.reportBoundError(uv, BoundErrorKind.UPPER);
mcimadamore@1562 533 }
mcimadamore@1562 534 }
mcimadamore@1562 535 for (Type l : uv.getBounds(InferenceBound.LOWER)) {
mcimadamore@1562 536 if (!infer.types.isSubtypeUnchecked(inferenceContext.asFree(l), inst, warn)) {
mcimadamore@1562 537 infer.reportBoundError(uv, BoundErrorKind.LOWER);
mcimadamore@1562 538 }
mcimadamore@1562 539 }
mcimadamore@1562 540 for (Type e : uv.getBounds(InferenceBound.EQ)) {
mcimadamore@1562 541 if (!infer.types.isSameType(inst, inferenceContext.asFree(e))) {
mcimadamore@1562 542 infer.reportBoundError(uv, BoundErrorKind.EQ);
mcimadamore@1562 543 }
mcimadamore@1562 544 }
mcimadamore@1562 545 }
mcimadamore@1562 546 }
mcimadamore@1898 547 @Override
mcimadamore@1898 548 boolean accepts(UndetVar uv, InferenceContext inferenceContext) {
mcimadamore@1898 549 //applies to all undetvars
mcimadamore@1898 550 return true;
mcimadamore@1898 551 }
mcimadamore@1562 552 },
mcimadamore@1562 553 /**
mcimadamore@1562 554 * Check consistency of equality constraints. This is a slightly more aggressive
mcimadamore@1562 555 * inference routine that is designed as to maximize compatibility with JDK 7.
mcimadamore@1562 556 * Note: this is not used in graph mode.
mcimadamore@1562 557 */
mcimadamore@1562 558 EQ_CHECK_LEGACY() {
mcimadamore@1562 559 public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) {
mcimadamore@1562 560 Infer infer = inferenceContext.infer();
mcimadamore@1562 561 Type eq = null;
mcimadamore@1562 562 for (Type e : uv.getBounds(InferenceBound.EQ)) {
mcimadamore@1562 563 Assert.check(!inferenceContext.free(e));
mcimadamore@1562 564 if (eq != null && !infer.types.isSameType(e, eq)) {
mcimadamore@1562 565 infer.reportBoundError(uv, BoundErrorKind.EQ);
mcimadamore@1562 566 }
mcimadamore@1562 567 eq = e;
mcimadamore@1562 568 for (Type l : uv.getBounds(InferenceBound.LOWER)) {
mcimadamore@1562 569 Assert.check(!inferenceContext.free(l));
mcimadamore@1562 570 if (!infer.types.isSubtypeUnchecked(l, e, warn)) {
mcimadamore@1562 571 infer.reportBoundError(uv, BoundErrorKind.BAD_EQ_LOWER);
mcimadamore@1562 572 }
mcimadamore@1562 573 }
mcimadamore@1562 574 for (Type u : uv.getBounds(InferenceBound.UPPER)) {
mcimadamore@1562 575 if (inferenceContext.free(u)) continue;
mcimadamore@1562 576 if (!infer.types.isSubtypeUnchecked(e, u, warn)) {
mcimadamore@1562 577 infer.reportBoundError(uv, BoundErrorKind.BAD_EQ_UPPER);
mcimadamore@1562 578 }
mcimadamore@1562 579 }
mcimadamore@1562 580 }
mcimadamore@1562 581 }
mcimadamore@1562 582 },
mcimadamore@1562 583 /**
mcimadamore@1562 584 * Check consistency of equality constraints.
mcimadamore@1562 585 */
mcimadamore@1562 586 EQ_CHECK() {
mcimadamore@1562 587 public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) {
mcimadamore@1562 588 Infer infer = inferenceContext.infer();
mcimadamore@1562 589 for (Type e : uv.getBounds(InferenceBound.EQ)) {
mcimadamore@1562 590 if (e.containsAny(inferenceContext.inferenceVars())) continue;
mcimadamore@1562 591 for (Type u : uv.getBounds(InferenceBound.UPPER)) {
mcimadamore@1562 592 if (!infer.types.isSubtypeUnchecked(e, inferenceContext.asFree(u), warn)) {
mcimadamore@1562 593 infer.reportBoundError(uv, BoundErrorKind.BAD_EQ_UPPER);
mcimadamore@1562 594 }
mcimadamore@1562 595 }
mcimadamore@1562 596 for (Type l : uv.getBounds(InferenceBound.LOWER)) {
mcimadamore@1562 597 if (!infer.types.isSubtypeUnchecked(inferenceContext.asFree(l), e, warn)) {
mcimadamore@1562 598 infer.reportBoundError(uv, BoundErrorKind.BAD_EQ_LOWER);
mcimadamore@1562 599 }
mcimadamore@1562 600 }
mcimadamore@1562 601 }
mcimadamore@1562 602 }
mcimadamore@1562 603 },
mcimadamore@1562 604 /**
mcimadamore@1562 605 * Given a bound set containing {@code alpha <: T} and {@code alpha :> S}
mcimadamore@1562 606 * perform {@code S <: T} (which could lead to new bounds).
mcimadamore@1562 607 */
mcimadamore@1562 608 CROSS_UPPER_LOWER() {
mcimadamore@1562 609 public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) {
mcimadamore@1562 610 Infer infer = inferenceContext.infer();
mcimadamore@1562 611 for (Type b1 : uv.getBounds(InferenceBound.UPPER)) {
mcimadamore@1562 612 for (Type b2 : uv.getBounds(InferenceBound.LOWER)) {
mcimadamore@1655 613 infer.types.isSubtypeUnchecked(inferenceContext.asFree(b2), inferenceContext.asFree(b1));
mcimadamore@1562 614 }
mcimadamore@1562 615 }
mcimadamore@1562 616 }
mcimadamore@1562 617 },
mcimadamore@1562 618 /**
mcimadamore@1562 619 * Given a bound set containing {@code alpha <: T} and {@code alpha == S}
mcimadamore@1562 620 * perform {@code S <: T} (which could lead to new bounds).
mcimadamore@1562 621 */
mcimadamore@1562 622 CROSS_UPPER_EQ() {
mcimadamore@1562 623 public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) {
mcimadamore@1562 624 Infer infer = inferenceContext.infer();
mcimadamore@1562 625 for (Type b1 : uv.getBounds(InferenceBound.UPPER)) {
mcimadamore@1562 626 for (Type b2 : uv.getBounds(InferenceBound.EQ)) {
mcimadamore@1655 627 infer.types.isSubtypeUnchecked(inferenceContext.asFree(b2), inferenceContext.asFree(b1));
mcimadamore@1562 628 }
mcimadamore@1562 629 }
mcimadamore@1562 630 }
mcimadamore@1562 631 },
mcimadamore@1562 632 /**
mcimadamore@1562 633 * Given a bound set containing {@code alpha :> S} and {@code alpha == T}
mcimadamore@1562 634 * perform {@code S <: T} (which could lead to new bounds).
mcimadamore@1562 635 */
mcimadamore@1562 636 CROSS_EQ_LOWER() {
mcimadamore@1562 637 public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) {
mcimadamore@1562 638 Infer infer = inferenceContext.infer();
mcimadamore@1562 639 for (Type b1 : uv.getBounds(InferenceBound.EQ)) {
mcimadamore@1562 640 for (Type b2 : uv.getBounds(InferenceBound.LOWER)) {
mcimadamore@1655 641 infer.types.isSubtypeUnchecked(inferenceContext.asFree(b2), inferenceContext.asFree(b1));
mcimadamore@1655 642 }
mcimadamore@1655 643 }
mcimadamore@1655 644 }
mcimadamore@1655 645 },
mcimadamore@1655 646 /**
mcimadamore@1655 647 * Given a bound set containing {@code alpha == S} and {@code alpha == T}
mcimadamore@1655 648 * perform {@code S == T} (which could lead to new bounds).
mcimadamore@1655 649 */
mcimadamore@1655 650 CROSS_EQ_EQ() {
mcimadamore@1655 651 public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) {
mcimadamore@1655 652 Infer infer = inferenceContext.infer();
mcimadamore@1655 653 for (Type b1 : uv.getBounds(InferenceBound.EQ)) {
mcimadamore@1655 654 for (Type b2 : uv.getBounds(InferenceBound.EQ)) {
mcimadamore@1655 655 if (b1 != b2) {
mcimadamore@1655 656 infer.types.isSameType(inferenceContext.asFree(b2), inferenceContext.asFree(b1));
mcimadamore@1562 657 }
mcimadamore@1562 658 }
mcimadamore@1562 659 }
mcimadamore@1562 660 }
mcimadamore@1562 661 },
mcimadamore@1562 662 /**
mcimadamore@1562 663 * Given a bound set containing {@code alpha <: beta} propagate lower bounds
mcimadamore@1562 664 * from alpha to beta; also propagate upper bounds from beta to alpha.
mcimadamore@1562 665 */
mcimadamore@1562 666 PROP_UPPER() {
mcimadamore@1562 667 public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) {
mcimadamore@1562 668 Infer infer = inferenceContext.infer();
mcimadamore@1562 669 for (Type b : uv.getBounds(InferenceBound.UPPER)) {
mcimadamore@1562 670 if (inferenceContext.inferenceVars().contains(b)) {
mcimadamore@1562 671 UndetVar uv2 = (UndetVar)inferenceContext.asFree(b);
mcimadamore@1898 672 if (uv2.isCaptured()) continue;
mcimadamore@1562 673 //alpha <: beta
mcimadamore@1628 674 //0. set beta :> alpha
mcimadamore@1891 675 uv2.addBound(InferenceBound.LOWER, uv, infer.types);
mcimadamore@1562 676 //1. copy alpha's lower to beta's
mcimadamore@1562 677 for (Type l : uv.getBounds(InferenceBound.LOWER)) {
mcimadamore@1562 678 uv2.addBound(InferenceBound.LOWER, inferenceContext.asInstType(l), infer.types);
mcimadamore@1562 679 }
mcimadamore@1562 680 //2. copy beta's upper to alpha's
mcimadamore@1562 681 for (Type u : uv2.getBounds(InferenceBound.UPPER)) {
mcimadamore@1562 682 uv.addBound(InferenceBound.UPPER, inferenceContext.asInstType(u), infer.types);
mcimadamore@1562 683 }
mcimadamore@1562 684 }
mcimadamore@1562 685 }
mcimadamore@1562 686 }
mcimadamore@1562 687 },
mcimadamore@1562 688 /**
mcimadamore@1562 689 * Given a bound set containing {@code alpha :> beta} propagate lower bounds
mcimadamore@1562 690 * from beta to alpha; also propagate upper bounds from alpha to beta.
mcimadamore@1562 691 */
mcimadamore@1562 692 PROP_LOWER() {
mcimadamore@1562 693 public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) {
mcimadamore@1562 694 Infer infer = inferenceContext.infer();
mcimadamore@1562 695 for (Type b : uv.getBounds(InferenceBound.LOWER)) {
mcimadamore@1562 696 if (inferenceContext.inferenceVars().contains(b)) {
mcimadamore@1562 697 UndetVar uv2 = (UndetVar)inferenceContext.asFree(b);
mcimadamore@1898 698 if (uv2.isCaptured()) continue;
mcimadamore@1562 699 //alpha :> beta
mcimadamore@1628 700 //0. set beta <: alpha
mcimadamore@1891 701 uv2.addBound(InferenceBound.UPPER, uv, infer.types);
mcimadamore@1562 702 //1. copy alpha's upper to beta's
mcimadamore@1562 703 for (Type u : uv.getBounds(InferenceBound.UPPER)) {
mcimadamore@1562 704 uv2.addBound(InferenceBound.UPPER, inferenceContext.asInstType(u), infer.types);
mcimadamore@1562 705 }
mcimadamore@1562 706 //2. copy beta's lower to alpha's
mcimadamore@1562 707 for (Type l : uv2.getBounds(InferenceBound.LOWER)) {
mcimadamore@1562 708 uv.addBound(InferenceBound.LOWER, inferenceContext.asInstType(l), infer.types);
mcimadamore@1562 709 }
mcimadamore@1562 710 }
mcimadamore@1562 711 }
mcimadamore@1562 712 }
mcimadamore@1562 713 },
mcimadamore@1562 714 /**
mcimadamore@1562 715 * Given a bound set containing {@code alpha == beta} propagate lower/upper
mcimadamore@1562 716 * bounds from alpha to beta and back.
mcimadamore@1562 717 */
mcimadamore@1562 718 PROP_EQ() {
mcimadamore@1562 719 public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) {
mcimadamore@1562 720 Infer infer = inferenceContext.infer();
mcimadamore@1562 721 for (Type b : uv.getBounds(InferenceBound.EQ)) {
mcimadamore@1562 722 if (inferenceContext.inferenceVars().contains(b)) {
mcimadamore@1562 723 UndetVar uv2 = (UndetVar)inferenceContext.asFree(b);
mcimadamore@1898 724 if (uv2.isCaptured()) continue;
mcimadamore@1562 725 //alpha == beta
mcimadamore@1628 726 //0. set beta == alpha
mcimadamore@1891 727 uv2.addBound(InferenceBound.EQ, uv, infer.types);
mcimadamore@1562 728 //1. copy all alpha's bounds to beta's
mcimadamore@1562 729 for (InferenceBound ib : InferenceBound.values()) {
mcimadamore@1562 730 for (Type b2 : uv.getBounds(ib)) {
mcimadamore@1562 731 if (b2 != uv2) {
mcimadamore@1562 732 uv2.addBound(ib, inferenceContext.asInstType(b2), infer.types);
mcimadamore@1562 733 }
mcimadamore@1562 734 }
mcimadamore@1562 735 }
mcimadamore@1562 736 //2. copy all beta's bounds to alpha's
mcimadamore@1562 737 for (InferenceBound ib : InferenceBound.values()) {
mcimadamore@1562 738 for (Type b2 : uv2.getBounds(ib)) {
mcimadamore@1562 739 if (b2 != uv) {
mcimadamore@1562 740 uv.addBound(ib, inferenceContext.asInstType(b2), infer.types);
mcimadamore@1562 741 }
mcimadamore@1562 742 }
mcimadamore@1562 743 }
mcimadamore@1562 744 }
mcimadamore@1562 745 }
mcimadamore@1562 746 }
mcimadamore@1562 747 };
mcimadamore@1562 748
mcimadamore@1562 749 abstract void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn);
mcimadamore@1898 750
mcimadamore@1898 751 boolean accepts(UndetVar uv, InferenceContext inferenceContext) {
mcimadamore@1898 752 return !uv.isCaptured();
mcimadamore@1898 753 }
mcimadamore@1562 754 }
mcimadamore@1562 755
mcimadamore@1562 756 /** incorporation steps to be executed when running in legacy mode */
mcimadamore@1562 757 EnumSet<IncorporationStep> incorporationStepsLegacy = EnumSet.of(IncorporationStep.EQ_CHECK_LEGACY);
mcimadamore@1562 758
mcimadamore@1562 759 /** incorporation steps to be executed when running in graph mode */
mcimadamore@1562 760 EnumSet<IncorporationStep> incorporationStepsGraph =
mcimadamore@1562 761 EnumSet.complementOf(EnumSet.of(IncorporationStep.EQ_CHECK_LEGACY));
mcimadamore@1562 762
mcimadamore@1562 763 /**
mcimadamore@1562 764 * Make sure that the upper bounds we got so far lead to a solvable inference
mcimadamore@1562 765 * variable by making sure that a glb exists.
mcimadamore@1562 766 */
mcimadamore@1562 767 void checkCompatibleUpperBounds(UndetVar uv, InferenceContext inferenceContext) {
mcimadamore@1562 768 List<Type> hibounds =
mcimadamore@1562 769 Type.filter(uv.getBounds(InferenceBound.UPPER), new BoundFilter(inferenceContext));
mcimadamore@1562 770 Type hb = null;
mcimadamore@1562 771 if (hibounds.isEmpty())
mcimadamore@1562 772 hb = syms.objectType;
mcimadamore@1562 773 else if (hibounds.tail.isEmpty())
mcimadamore@1562 774 hb = hibounds.head;
mcimadamore@1562 775 else
mcimadamore@1562 776 hb = types.glb(hibounds);
mcimadamore@1562 777 if (hb == null || hb.isErroneous())
mcimadamore@1562 778 reportBoundError(uv, BoundErrorKind.BAD_UPPER);
mcimadamore@1562 779 }
mcimadamore@1562 780 //where
mcimadamore@1562 781 protected static class BoundFilter implements Filter<Type> {
mcimadamore@1562 782
mcimadamore@1562 783 InferenceContext inferenceContext;
mcimadamore@1562 784
mcimadamore@1562 785 public BoundFilter(InferenceContext inferenceContext) {
mcimadamore@1562 786 this.inferenceContext = inferenceContext;
mcimadamore@1562 787 }
mcimadamore@1562 788
mcimadamore@1562 789 @Override
mcimadamore@1562 790 public boolean accepts(Type t) {
mcimadamore@1562 791 return !t.isErroneous() && !inferenceContext.free(t) &&
mcimadamore@1562 792 !t.hasTag(BOT);
mcimadamore@1562 793 }
mcimadamore@1562 794 };
mcimadamore@1562 795
mcimadamore@1562 796 /**
mcimadamore@1562 797 * This enumeration defines all possible bound-checking related errors.
mcimadamore@1562 798 */
mcimadamore@1562 799 enum BoundErrorKind {
mcimadamore@1562 800 /**
mcimadamore@1562 801 * The (uninstantiated) inference variable has incompatible upper bounds.
mcimadamore@1562 802 */
mcimadamore@1562 803 BAD_UPPER() {
mcimadamore@1562 804 @Override
mcimadamore@1562 805 InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) {
mcimadamore@1562 806 return ex.setMessage("incompatible.upper.bounds", uv.qtype,
mcimadamore@1562 807 uv.getBounds(InferenceBound.UPPER));
mcimadamore@1562 808 }
mcimadamore@1562 809 },
mcimadamore@1562 810 /**
mcimadamore@1562 811 * An equality constraint is not compatible with an upper bound.
mcimadamore@1562 812 */
mcimadamore@1562 813 BAD_EQ_UPPER() {
mcimadamore@1562 814 @Override
mcimadamore@1562 815 InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) {
mcimadamore@1562 816 return ex.setMessage("incompatible.eq.upper.bounds", uv.qtype,
mcimadamore@1562 817 uv.getBounds(InferenceBound.EQ), uv.getBounds(InferenceBound.UPPER));
mcimadamore@1562 818 }
mcimadamore@1562 819 },
mcimadamore@1562 820 /**
mcimadamore@1562 821 * An equality constraint is not compatible with a lower bound.
mcimadamore@1562 822 */
mcimadamore@1562 823 BAD_EQ_LOWER() {
mcimadamore@1562 824 @Override
mcimadamore@1562 825 InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) {
mcimadamore@1562 826 return ex.setMessage("incompatible.eq.lower.bounds", uv.qtype,
mcimadamore@1562 827 uv.getBounds(InferenceBound.EQ), uv.getBounds(InferenceBound.LOWER));
mcimadamore@1562 828 }
mcimadamore@1562 829 },
mcimadamore@1562 830 /**
mcimadamore@1562 831 * Instantiated inference variable is not compatible with an upper bound.
mcimadamore@1562 832 */
mcimadamore@1562 833 UPPER() {
mcimadamore@1562 834 @Override
mcimadamore@1562 835 InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) {
mcimadamore@1562 836 return ex.setMessage("inferred.do.not.conform.to.upper.bounds", uv.inst,
mcimadamore@1562 837 uv.getBounds(InferenceBound.UPPER));
mcimadamore@1562 838 }
mcimadamore@1562 839 },
mcimadamore@1562 840 /**
mcimadamore@1562 841 * Instantiated inference variable is not compatible with a lower bound.
mcimadamore@1562 842 */
mcimadamore@1562 843 LOWER() {
mcimadamore@1562 844 @Override
mcimadamore@1562 845 InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) {
mcimadamore@1562 846 return ex.setMessage("inferred.do.not.conform.to.lower.bounds", uv.inst,
mcimadamore@1562 847 uv.getBounds(InferenceBound.LOWER));
mcimadamore@1562 848 }
mcimadamore@1562 849 },
mcimadamore@1562 850 /**
mcimadamore@1562 851 * Instantiated inference variable is not compatible with an equality constraint.
mcimadamore@1562 852 */
mcimadamore@1562 853 EQ() {
mcimadamore@1562 854 @Override
mcimadamore@1562 855 InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) {
mcimadamore@1562 856 return ex.setMessage("inferred.do.not.conform.to.eq.bounds", uv.inst,
mcimadamore@1562 857 uv.getBounds(InferenceBound.EQ));
mcimadamore@1562 858 }
mcimadamore@1562 859 };
mcimadamore@1562 860
mcimadamore@1562 861 abstract InapplicableMethodException setMessage(InferenceException ex, UndetVar uv);
mcimadamore@1562 862 }
mcimadamore@1562 863
mcimadamore@1562 864 /**
mcimadamore@1562 865 * Report a bound-checking error of given kind
mcimadamore@1562 866 */
mcimadamore@1562 867 void reportBoundError(UndetVar uv, BoundErrorKind bk) {
mcimadamore@1562 868 throw bk.setMessage(inferenceException, uv);
mcimadamore@1562 869 }
mcimadamore@1562 870 // </editor-fold>
mcimadamore@1562 871
mcimadamore@1562 872 // <editor-fold defaultstate="collapsed" desc="Inference engine">
mcimadamore@1562 873 /**
mcimadamore@1562 874 * Graph inference strategy - act as an input to the inference solver; a strategy is
mcimadamore@1562 875 * composed of two ingredients: (i) find a node to solve in the inference graph,
mcimadamore@1562 876 * and (ii) tell th engine when we are done fixing inference variables
mcimadamore@1562 877 */
mcimadamore@1562 878 interface GraphStrategy {
mcimadamore@1562 879 /**
mcimadamore@1562 880 * Pick the next node (leaf) to solve in the graph
mcimadamore@1562 881 */
mcimadamore@1562 882 Node pickNode(InferenceGraph g);
mcimadamore@1562 883 /**
mcimadamore@1562 884 * Is this the last step?
mcimadamore@1562 885 */
mcimadamore@1562 886 boolean done();
mcimadamore@1562 887 }
mcimadamore@1562 888
mcimadamore@1562 889 /**
mcimadamore@1562 890 * Simple solver strategy class that locates all leaves inside a graph
mcimadamore@1562 891 * and picks the first leaf as the next node to solve
mcimadamore@1562 892 */
mcimadamore@1562 893 abstract class LeafSolver implements GraphStrategy {
mcimadamore@1562 894 public Node pickNode(InferenceGraph g) {
mcimadamore@1562 895 Assert.check(!g.nodes.isEmpty(), "No nodes to solve!");
mcimadamore@1562 896 return g.nodes.get(0);
mcimadamore@1562 897 }
mcimadamore@1562 898 }
mcimadamore@1562 899
mcimadamore@1562 900 /**
mcimadamore@1562 901 * This solver uses an heuristic to pick the best leaf - the heuristic
mcimadamore@1562 902 * tries to select the node that has maximal probability to contain one
mcimadamore@1562 903 * or more inference variables in a given list
mcimadamore@1562 904 */
mcimadamore@1562 905 abstract class BestLeafSolver extends LeafSolver {
mcimadamore@1562 906
mcimadamore@1562 907 List<Type> varsToSolve;
mcimadamore@1562 908
mcimadamore@1562 909 BestLeafSolver(List<Type> varsToSolve) {
mcimadamore@1562 910 this.varsToSolve = varsToSolve;
mcimadamore@1562 911 }
mcimadamore@1562 912
mcimadamore@1562 913 /**
mcimadamore@1903 914 * Computes the minimum path that goes from a given node to any of the nodes
mcimadamore@1903 915 * containing a variable in {@code varsToSolve}. For any given path, the cost
mcimadamore@1903 916 * is computed as the total number of type-variables that should be eagerly
mcimadamore@1903 917 * instantiated across that path.
mcimadamore@1562 918 */
mcimadamore@1903 919 int computeMinPath(InferenceGraph g, Node n) {
mcimadamore@1903 920 return computeMinPath(g, n, List.<Node>nil(), 0);
mcimadamore@1903 921 }
mcimadamore@1903 922
mcimadamore@1903 923 int computeMinPath(InferenceGraph g, Node n, List<Node> path, int cost) {
mcimadamore@1903 924 if (path.contains(n)) return Integer.MAX_VALUE;
mcimadamore@1903 925 List<Node> path2 = path.prepend(n);
mcimadamore@1903 926 int cost2 = cost + n.data.size();
mcimadamore@1903 927 if (!Collections.disjoint(n.data, varsToSolve)) {
mcimadamore@1903 928 return cost2;
mcimadamore@1562 929 } else {
mcimadamore@1903 930 int bestPath = Integer.MAX_VALUE;
mcimadamore@1903 931 for (Node n2 : g.nodes) {
mcimadamore@1903 932 if (n2.deps.contains(n)) {
mcimadamore@1903 933 int res = computeMinPath(g, n2, path2, cost2);
mcimadamore@1903 934 if (res < bestPath) {
mcimadamore@1903 935 bestPath = res;
mcimadamore@1903 936 }
mcimadamore@1903 937 }
mcimadamore@1562 938 }
mcimadamore@1903 939 return bestPath;
mcimadamore@1562 940 }
mcimadamore@1562 941 }
mcimadamore@1562 942
mcimadamore@1562 943 /**
mcimadamore@1562 944 * Pick the leaf that minimize cost
mcimadamore@1562 945 */
mcimadamore@1562 946 @Override
mcimadamore@1562 947 public Node pickNode(final InferenceGraph g) {
mcimadamore@1903 948 final Map<Node, Integer> leavesMap = new HashMap<Node, Integer>();
mcimadamore@1562 949 for (Node n : g.nodes) {
mcimadamore@1562 950 if (n.isLeaf(n)) {
mcimadamore@1903 951 leavesMap.put(n, computeMinPath(g, n));
mcimadamore@1562 952 }
mcimadamore@1562 953 }
mcimadamore@1903 954 Assert.check(!leavesMap.isEmpty(), "No nodes to solve!");
mcimadamore@1903 955 TreeSet<Node> orderedLeaves = new TreeSet<Node>(new Comparator<Node>() {
mcimadamore@1562 956 public int compare(Node n1, Node n2) {
mcimadamore@1903 957 return leavesMap.get(n1) - leavesMap.get(n2);
mcimadamore@1562 958 }
mcimadamore@1562 959 });
mcimadamore@1903 960 orderedLeaves.addAll(leavesMap.keySet());
mcimadamore@1903 961 return orderedLeaves.first();
mcimadamore@1562 962 }
mcimadamore@1562 963 }
mcimadamore@1562 964
mcimadamore@1562 965 /**
mcimadamore@1562 966 * The inference process can be thought of as a sequence of steps. Each step
mcimadamore@1562 967 * instantiates an inference variable using a subset of the inference variable
mcimadamore@1562 968 * bounds, if certain condition are met. Decisions such as the sequence in which
mcimadamore@1562 969 * steps are applied, or which steps are to be applied are left to the inference engine.
mcimadamore@1562 970 */
mcimadamore@1562 971 enum InferenceStep {
mcimadamore@1562 972
mcimadamore@1562 973 /**
mcimadamore@1562 974 * Instantiate an inference variables using one of its (ground) equality
mcimadamore@1562 975 * constraints
mcimadamore@1562 976 */
mcimadamore@1562 977 EQ(InferenceBound.EQ) {
mcimadamore@1562 978 @Override
mcimadamore@1562 979 Type solve(UndetVar uv, InferenceContext inferenceContext) {
mcimadamore@1562 980 return filterBounds(uv, inferenceContext).head;
mcimadamore@1562 981 }
mcimadamore@1562 982 },
mcimadamore@1562 983 /**
mcimadamore@1562 984 * Instantiate an inference variables using its (ground) lower bounds. Such
mcimadamore@1562 985 * bounds are merged together using lub().
mcimadamore@1562 986 */
mcimadamore@1562 987 LOWER(InferenceBound.LOWER) {
mcimadamore@1562 988 @Override
mcimadamore@1562 989 Type solve(UndetVar uv, InferenceContext inferenceContext) {
mcimadamore@1562 990 Infer infer = inferenceContext.infer();
mcimadamore@1562 991 List<Type> lobounds = filterBounds(uv, inferenceContext);
vromero@1826 992 //note: lobounds should have at least one element
vromero@1826 993 Type owntype = lobounds.tail.tail == null ? lobounds.head : infer.types.lub(lobounds);
vromero@1826 994 if (owntype.isPrimitive() || owntype.hasTag(ERROR)) {
mcimadamore@1562 995 throw infer.inferenceException
mcimadamore@1562 996 .setMessage("no.unique.minimal.instance.exists",
mcimadamore@1562 997 uv.qtype, lobounds);
mcimadamore@1562 998 } else {
mcimadamore@1562 999 return owntype;
mcimadamore@1562 1000 }
mcimadamore@1562 1001 }
mcimadamore@1562 1002 },
mcimadamore@1562 1003 /**
mcimadamore@1896 1004 * Infer uninstantiated/unbound inference variables occurring in 'throws'
mcimadamore@1896 1005 * clause as RuntimeException
mcimadamore@1896 1006 */
mcimadamore@1896 1007 THROWS(InferenceBound.UPPER) {
mcimadamore@1896 1008 @Override
mcimadamore@1896 1009 public boolean accepts(UndetVar t, InferenceContext inferenceContext) {
mcimadamore@1896 1010 if ((t.qtype.tsym.flags() & Flags.THROWS) == 0) {
mcimadamore@1896 1011 //not a throws undet var
mcimadamore@1896 1012 return false;
mcimadamore@1896 1013 }
mcimadamore@1896 1014 if (t.getBounds(InferenceBound.EQ, InferenceBound.LOWER, InferenceBound.UPPER)
mcimadamore@1896 1015 .diff(t.getDeclaredBounds()).nonEmpty()) {
mcimadamore@1896 1016 //not an unbounded undet var
mcimadamore@1896 1017 return false;
mcimadamore@1896 1018 }
mcimadamore@1896 1019 Infer infer = inferenceContext.infer();
mcimadamore@1896 1020 for (Type db : t.getDeclaredBounds()) {
mcimadamore@1896 1021 if (t.isInterface()) continue;
mcimadamore@1896 1022 if (infer.types.asSuper(infer.syms.runtimeExceptionType, db.tsym) != null) {
mcimadamore@1896 1023 //declared bound is a supertype of RuntimeException
mcimadamore@1896 1024 return true;
mcimadamore@1896 1025 }
mcimadamore@1896 1026 }
mcimadamore@1896 1027 //declared bound is more specific then RuntimeException - give up
mcimadamore@1896 1028 return false;
mcimadamore@1896 1029 }
mcimadamore@1896 1030
mcimadamore@1896 1031 @Override
mcimadamore@1896 1032 Type solve(UndetVar uv, InferenceContext inferenceContext) {
mcimadamore@1896 1033 return inferenceContext.infer().syms.runtimeExceptionType;
mcimadamore@1896 1034 }
mcimadamore@1896 1035 },
mcimadamore@1896 1036 /**
mcimadamore@1562 1037 * Instantiate an inference variables using its (ground) upper bounds. Such
mcimadamore@1562 1038 * bounds are merged together using glb().
mcimadamore@1562 1039 */
mcimadamore@1562 1040 UPPER(InferenceBound.UPPER) {
mcimadamore@1562 1041 @Override
mcimadamore@1562 1042 Type solve(UndetVar uv, InferenceContext inferenceContext) {
mcimadamore@1562 1043 Infer infer = inferenceContext.infer();
mcimadamore@1562 1044 List<Type> hibounds = filterBounds(uv, inferenceContext);
vromero@1826 1045 //note: lobounds should have at least one element
vromero@1826 1046 Type owntype = hibounds.tail.tail == null ? hibounds.head : infer.types.glb(hibounds);
vromero@1826 1047 if (owntype.isPrimitive() || owntype.hasTag(ERROR)) {
mcimadamore@1562 1048 throw infer.inferenceException
mcimadamore@1562 1049 .setMessage("no.unique.maximal.instance.exists",
mcimadamore@1562 1050 uv.qtype, hibounds);
mcimadamore@1562 1051 } else {
mcimadamore@1562 1052 return owntype;
mcimadamore@1562 1053 }
mcimadamore@1562 1054 }
mcimadamore@1562 1055 },
mcimadamore@1562 1056 /**
mcimadamore@1562 1057 * Like the former; the only difference is that this step can only be applied
mcimadamore@1562 1058 * if all upper bounds are ground.
mcimadamore@1562 1059 */
mcimadamore@1562 1060 UPPER_LEGACY(InferenceBound.UPPER) {
mcimadamore@1562 1061 @Override
mcimadamore@1562 1062 public boolean accepts(UndetVar t, InferenceContext inferenceContext) {
mcimadamore@1898 1063 return !inferenceContext.free(t.getBounds(ib)) && !t.isCaptured();
mcimadamore@1562 1064 }
mcimadamore@1562 1065
mcimadamore@1562 1066 @Override
mcimadamore@1562 1067 Type solve(UndetVar uv, InferenceContext inferenceContext) {
mcimadamore@1562 1068 return UPPER.solve(uv, inferenceContext);
mcimadamore@1562 1069 }
mcimadamore@1898 1070 },
mcimadamore@1898 1071 /**
mcimadamore@1898 1072 * Like the former; the only difference is that this step can only be applied
mcimadamore@1898 1073 * if all upper/lower bounds are ground.
mcimadamore@1898 1074 */
mcimadamore@1898 1075 CAPTURED(InferenceBound.UPPER) {
mcimadamore@1898 1076 @Override
mcimadamore@1898 1077 public boolean accepts(UndetVar t, InferenceContext inferenceContext) {
mcimadamore@1898 1078 return !inferenceContext.free(t.getBounds(InferenceBound.UPPER, InferenceBound.LOWER));
mcimadamore@1898 1079 }
mcimadamore@1898 1080
mcimadamore@1898 1081 @Override
mcimadamore@1898 1082 Type solve(UndetVar uv, InferenceContext inferenceContext) {
mcimadamore@1898 1083 Infer infer = inferenceContext.infer();
mcimadamore@1898 1084 Type upper = UPPER.filterBounds(uv, inferenceContext).nonEmpty() ?
mcimadamore@1898 1085 UPPER.solve(uv, inferenceContext) :
mcimadamore@1898 1086 infer.syms.objectType;
mcimadamore@1898 1087 Type lower = LOWER.filterBounds(uv, inferenceContext).nonEmpty() ?
mcimadamore@1898 1088 LOWER.solve(uv, inferenceContext) :
mcimadamore@1898 1089 infer.syms.botType;
mcimadamore@1898 1090 CapturedType prevCaptured = (CapturedType)uv.qtype;
mcimadamore@1898 1091 return new CapturedType(prevCaptured.tsym.name, prevCaptured.tsym.owner, upper, lower, prevCaptured.wildcard);
mcimadamore@1898 1092 }
mcimadamore@1562 1093 };
mcimadamore@1562 1094
mcimadamore@1562 1095 final InferenceBound ib;
mcimadamore@1562 1096
mcimadamore@1562 1097 InferenceStep(InferenceBound ib) {
mcimadamore@1562 1098 this.ib = ib;
mcimadamore@1562 1099 }
mcimadamore@1562 1100
mcimadamore@1562 1101 /**
mcimadamore@1562 1102 * Find an instantiated type for a given inference variable within
mcimadamore@1562 1103 * a given inference context
mcimadamore@1562 1104 */
mcimadamore@1562 1105 abstract Type solve(UndetVar uv, InferenceContext inferenceContext);
mcimadamore@1562 1106
mcimadamore@1562 1107 /**
mcimadamore@1562 1108 * Can the inference variable be instantiated using this step?
mcimadamore@1562 1109 */
mcimadamore@1562 1110 public boolean accepts(UndetVar t, InferenceContext inferenceContext) {
mcimadamore@1898 1111 return filterBounds(t, inferenceContext).nonEmpty() && !t.isCaptured();
mcimadamore@1562 1112 }
mcimadamore@1562 1113
mcimadamore@1562 1114 /**
mcimadamore@1562 1115 * Return the subset of ground bounds in a given bound set (i.e. eq/lower/upper)
mcimadamore@1562 1116 */
mcimadamore@1562 1117 List<Type> filterBounds(UndetVar uv, InferenceContext inferenceContext) {
mcimadamore@1562 1118 return Type.filter(uv.getBounds(ib), new BoundFilter(inferenceContext));
mcimadamore@1562 1119 }
mcimadamore@1562 1120 }
mcimadamore@1562 1121
mcimadamore@1562 1122 /**
mcimadamore@1562 1123 * This enumeration defines the sequence of steps to be applied when the
mcimadamore@1562 1124 * solver works in legacy mode. The steps in this enumeration reflect
mcimadamore@1562 1125 * the behavior of old inference routine (see JLS SE 7 15.12.2.7/15.12.2.8).
mcimadamore@1562 1126 */
mcimadamore@1562 1127 enum LegacyInferenceSteps {
mcimadamore@1562 1128
mcimadamore@1562 1129 EQ_LOWER(EnumSet.of(InferenceStep.EQ, InferenceStep.LOWER)),
mcimadamore@1562 1130 EQ_UPPER(EnumSet.of(InferenceStep.EQ, InferenceStep.UPPER_LEGACY));
mcimadamore@1562 1131
mcimadamore@1562 1132 final EnumSet<InferenceStep> steps;
mcimadamore@1562 1133
mcimadamore@1562 1134 LegacyInferenceSteps(EnumSet<InferenceStep> steps) {
mcimadamore@1562 1135 this.steps = steps;
mcimadamore@1562 1136 }
mcimadamore@1562 1137 }
mcimadamore@1562 1138
mcimadamore@1562 1139 /**
mcimadamore@1562 1140 * This enumeration defines the sequence of steps to be applied when the
mcimadamore@1562 1141 * graph solver is used. This order is defined so as to maximize compatibility
mcimadamore@1562 1142 * w.r.t. old inference routine (see JLS SE 7 15.12.2.7/15.12.2.8).
mcimadamore@1562 1143 */
mcimadamore@1562 1144 enum GraphInferenceSteps {
mcimadamore@1562 1145
mcimadamore@1562 1146 EQ(EnumSet.of(InferenceStep.EQ)),
mcimadamore@1562 1147 EQ_LOWER(EnumSet.of(InferenceStep.EQ, InferenceStep.LOWER)),
mcimadamore@1898 1148 EQ_LOWER_THROWS_UPPER_CAPTURED(EnumSet.of(InferenceStep.EQ, InferenceStep.LOWER, InferenceStep.UPPER, InferenceStep.THROWS, InferenceStep.CAPTURED));
mcimadamore@1562 1149
mcimadamore@1562 1150 final EnumSet<InferenceStep> steps;
mcimadamore@1562 1151
mcimadamore@1562 1152 GraphInferenceSteps(EnumSet<InferenceStep> steps) {
mcimadamore@1562 1153 this.steps = steps;
mcimadamore@1562 1154 }
mcimadamore@1562 1155 }
mcimadamore@1562 1156
mcimadamore@1562 1157 /**
mcimadamore@1562 1158 * This is the graph inference solver - the solver organizes all inference variables in
mcimadamore@1562 1159 * a given inference context by bound dependencies - in the general case, such dependencies
mcimadamore@1562 1160 * would lead to a cyclic directed graph (hence the name); the dependency info is used to build
mcimadamore@1562 1161 * an acyclic graph, where all cyclic variables are bundled together. An inference
mcimadamore@1562 1162 * step corresponds to solving a node in the acyclic graph - this is done by
mcimadamore@1562 1163 * relying on a given strategy (see GraphStrategy).
mcimadamore@1562 1164 */
mcimadamore@1562 1165 class GraphSolver {
mcimadamore@1562 1166
mcimadamore@1562 1167 InferenceContext inferenceContext;
mcimadamore@1562 1168 Warner warn;
mcimadamore@1562 1169
mcimadamore@1562 1170 GraphSolver(InferenceContext inferenceContext, Warner warn) {
mcimadamore@1562 1171 this.inferenceContext = inferenceContext;
mcimadamore@1562 1172 this.warn = warn;
mcimadamore@1562 1173 }
mcimadamore@1562 1174
mcimadamore@1562 1175 /**
mcimadamore@1562 1176 * Solve variables in a given inference context. The amount of variables
mcimadamore@1562 1177 * to be solved, and the way in which the underlying acyclic graph is explored
mcimadamore@1562 1178 * depends on the selected solver strategy.
mcimadamore@1562 1179 */
mcimadamore@1562 1180 void solve(GraphStrategy sstrategy) {
mcimadamore@1562 1181 checkWithinBounds(inferenceContext, warn); //initial propagation of bounds
mcimadamore@1562 1182 InferenceGraph inferenceGraph = new InferenceGraph();
mcimadamore@1562 1183 while (!sstrategy.done()) {
mcimadamore@1562 1184 InferenceGraph.Node nodeToSolve = sstrategy.pickNode(inferenceGraph);
mcimadamore@1562 1185 List<Type> varsToSolve = List.from(nodeToSolve.data);
mcimadamore@1891 1186 List<Type> saved_undet = inferenceContext.save();
mcimadamore@1562 1187 try {
mcimadamore@1562 1188 //repeat until all variables are solved
mcimadamore@1562 1189 outer: while (Type.containsAny(inferenceContext.restvars(), varsToSolve)) {
mcimadamore@1562 1190 //for each inference phase
mcimadamore@1562 1191 for (GraphInferenceSteps step : GraphInferenceSteps.values()) {
mcimadamore@1562 1192 if (inferenceContext.solveBasic(varsToSolve, step.steps)) {
mcimadamore@1562 1193 checkWithinBounds(inferenceContext, warn);
mcimadamore@1562 1194 continue outer;
mcimadamore@1562 1195 }
mcimadamore@1562 1196 }
mcimadamore@1562 1197 //no progress
vromero@1826 1198 throw inferenceException.setMessage();
mcimadamore@1562 1199 }
mcimadamore@1562 1200 }
mcimadamore@1562 1201 catch (InferenceException ex) {
vromero@1826 1202 //did we fail because of interdependent ivars?
mcimadamore@1891 1203 inferenceContext.rollback(saved_undet);
mcimadamore@1562 1204 instantiateAsUninferredVars(varsToSolve, inferenceContext);
mcimadamore@1562 1205 checkWithinBounds(inferenceContext, warn);
mcimadamore@1562 1206 }
mcimadamore@1562 1207 inferenceGraph.deleteNode(nodeToSolve);
mcimadamore@1562 1208 }
mcimadamore@1562 1209 }
mcimadamore@1562 1210
mcimadamore@1562 1211 /**
mcimadamore@1562 1212 * The dependencies between the inference variables that need to be solved
mcimadamore@1562 1213 * form a (possibly cyclic) graph. This class reduces the original dependency graph
mcimadamore@1562 1214 * to an acyclic version, where cyclic nodes are folded into a single 'super node'.
mcimadamore@1562 1215 */
mcimadamore@1562 1216 class InferenceGraph {
mcimadamore@1562 1217
mcimadamore@1562 1218 /**
mcimadamore@1562 1219 * This class represents a node in the graph. Each node corresponds
mcimadamore@1562 1220 * to an inference variable and has edges (dependencies) on other
mcimadamore@1562 1221 * nodes. The node defines an entry point that can be used to receive
mcimadamore@1562 1222 * updates on the structure of the graph this node belongs to (used to
mcimadamore@1562 1223 * keep dependencies in sync).
mcimadamore@1562 1224 */
mcimadamore@1562 1225 class Node extends GraphUtils.TarjanNode<ListBuffer<Type>> {
mcimadamore@1562 1226
mcimadamore@1562 1227 Set<Node> deps;
mcimadamore@1562 1228
mcimadamore@1562 1229 Node(Type ivar) {
mcimadamore@1562 1230 super(ListBuffer.of(ivar));
mcimadamore@1562 1231 this.deps = new HashSet<Node>();
mcimadamore@1562 1232 }
mcimadamore@1562 1233
mcimadamore@1562 1234 @Override
mcimadamore@1562 1235 public Iterable<? extends Node> getDependencies() {
mcimadamore@1562 1236 return deps;
mcimadamore@1562 1237 }
mcimadamore@1562 1238
mcimadamore@1562 1239 @Override
mcimadamore@1562 1240 public String printDependency(GraphUtils.Node<ListBuffer<Type>> to) {
mcimadamore@1562 1241 StringBuilder buf = new StringBuilder();
mcimadamore@1562 1242 String sep = "";
mcimadamore@1562 1243 for (Type from : data) {
mcimadamore@1562 1244 UndetVar uv = (UndetVar)inferenceContext.asFree(from);
mcimadamore@1562 1245 for (Type bound : uv.getBounds(InferenceBound.values())) {
mcimadamore@1562 1246 if (bound.containsAny(List.from(to.data))) {
mcimadamore@1562 1247 buf.append(sep);
mcimadamore@1562 1248 buf.append(bound);
mcimadamore@1562 1249 sep = ",";
mcimadamore@1562 1250 }
mcimadamore@1562 1251 }
mcimadamore@1562 1252 }
mcimadamore@1562 1253 return buf.toString();
mcimadamore@1562 1254 }
mcimadamore@1562 1255
mcimadamore@1562 1256 boolean isLeaf(Node n) {
mcimadamore@1562 1257 //no deps, or only one self dep
mcimadamore@1562 1258 return (n.deps.isEmpty() ||
mcimadamore@1562 1259 n.deps.size() == 1 && n.deps.contains(n));
mcimadamore@1562 1260 }
mcimadamore@1562 1261
mcimadamore@1562 1262 void mergeWith(List<? extends Node> nodes) {
mcimadamore@1562 1263 for (Node n : nodes) {
mcimadamore@1562 1264 Assert.check(n.data.length() == 1, "Attempt to merge a compound node!");
mcimadamore@1562 1265 data.appendList(n.data);
mcimadamore@1562 1266 deps.addAll(n.deps);
mcimadamore@1562 1267 }
mcimadamore@1562 1268 //update deps
mcimadamore@1562 1269 Set<Node> deps2 = new HashSet<Node>();
mcimadamore@1562 1270 for (Node d : deps) {
mcimadamore@1562 1271 if (data.contains(d.data.first())) {
mcimadamore@1562 1272 deps2.add(this);
mcimadamore@1562 1273 } else {
mcimadamore@1562 1274 deps2.add(d);
mcimadamore@1562 1275 }
mcimadamore@1562 1276 }
mcimadamore@1562 1277 deps = deps2;
mcimadamore@1562 1278 }
mcimadamore@1562 1279
mcimadamore@1562 1280 void graphChanged(Node from, Node to) {
mcimadamore@1562 1281 if (deps.contains(from)) {
mcimadamore@1562 1282 deps.remove(from);
mcimadamore@1562 1283 if (to != null) {
mcimadamore@1562 1284 deps.add(to);
mcimadamore@1562 1285 }
mcimadamore@1562 1286 }
mcimadamore@1562 1287 }
mcimadamore@1562 1288 }
mcimadamore@1562 1289
mcimadamore@1562 1290 /** the nodes in the inference graph */
mcimadamore@1562 1291 ArrayList<Node> nodes;
mcimadamore@1562 1292
mcimadamore@1562 1293 InferenceGraph() {
mcimadamore@1562 1294 initNodes();
mcimadamore@1562 1295 }
mcimadamore@1562 1296
mcimadamore@1562 1297 /**
mcimadamore@1562 1298 * Delete a node from the graph. This update the underlying structure
mcimadamore@1562 1299 * of the graph (including dependencies) via listeners updates.
mcimadamore@1562 1300 */
mcimadamore@1562 1301 public void deleteNode(Node n) {
mcimadamore@1562 1302 Assert.check(nodes.contains(n));
mcimadamore@1562 1303 nodes.remove(n);
mcimadamore@1562 1304 notifyUpdate(n, null);
mcimadamore@1562 1305 }
mcimadamore@1562 1306
mcimadamore@1562 1307 /**
mcimadamore@1562 1308 * Notify all nodes of a change in the graph. If the target node is
mcimadamore@1562 1309 * {@code null} the source node is assumed to be removed.
mcimadamore@1562 1310 */
mcimadamore@1562 1311 void notifyUpdate(Node from, Node to) {
mcimadamore@1562 1312 for (Node n : nodes) {
mcimadamore@1562 1313 n.graphChanged(from, to);
mcimadamore@1562 1314 }
mcimadamore@1562 1315 }
mcimadamore@1562 1316
mcimadamore@1562 1317 /**
mcimadamore@1562 1318 * Create the graph nodes. First a simple node is created for every inference
mcimadamore@1562 1319 * variables to be solved. Then Tarjan is used to found all connected components
mcimadamore@1562 1320 * in the graph. For each component containing more than one node, a super node is
mcimadamore@1562 1321 * created, effectively replacing the original cyclic nodes.
mcimadamore@1562 1322 */
mcimadamore@1562 1323 void initNodes() {
mcimadamore@1608 1324 nodes = new ArrayList<Node>();
mcimadamore@1562 1325 for (Type t : inferenceContext.restvars()) {
mcimadamore@1562 1326 nodes.add(new Node(t));
mcimadamore@1562 1327 }
mcimadamore@1562 1328 for (Node n_i : nodes) {
mcimadamore@1562 1329 Type i = n_i.data.first();
mcimadamore@1562 1330 for (Node n_j : nodes) {
mcimadamore@1562 1331 Type j = n_j.data.first();
mcimadamore@1562 1332 UndetVar uv_i = (UndetVar)inferenceContext.asFree(i);
mcimadamore@1562 1333 if (Type.containsAny(uv_i.getBounds(InferenceBound.values()), List.of(j))) {
mcimadamore@1562 1334 //update i's deps
mcimadamore@1562 1335 n_i.deps.add(n_j);
mcimadamore@1562 1336 }
mcimadamore@1562 1337 }
mcimadamore@1562 1338 }
mcimadamore@1608 1339 ArrayList<Node> acyclicNodes = new ArrayList<Node>();
mcimadamore@1562 1340 for (List<? extends Node> conSubGraph : GraphUtils.tarjan(nodes)) {
mcimadamore@1562 1341 if (conSubGraph.length() > 1) {
mcimadamore@1562 1342 Node root = conSubGraph.head;
mcimadamore@1562 1343 root.mergeWith(conSubGraph.tail);
mcimadamore@1562 1344 for (Node n : conSubGraph) {
mcimadamore@1562 1345 notifyUpdate(n, root);
mcimadamore@1562 1346 }
mcimadamore@1562 1347 }
mcimadamore@1608 1348 acyclicNodes.add(conSubGraph.head);
mcimadamore@1562 1349 }
mcimadamore@1608 1350 nodes = acyclicNodes;
mcimadamore@1562 1351 }
mcimadamore@1562 1352
mcimadamore@1562 1353 /**
mcimadamore@1562 1354 * Debugging: dot representation of this graph
mcimadamore@1562 1355 */
mcimadamore@1562 1356 String toDot() {
mcimadamore@1562 1357 StringBuilder buf = new StringBuilder();
mcimadamore@1562 1358 for (Type t : inferenceContext.undetvars) {
mcimadamore@1562 1359 UndetVar uv = (UndetVar)t;
mcimadamore@1562 1360 buf.append(String.format("var %s - upper bounds = %s, lower bounds = %s, eq bounds = %s\\n",
mcimadamore@1562 1361 uv.qtype, uv.getBounds(InferenceBound.UPPER), uv.getBounds(InferenceBound.LOWER),
mcimadamore@1562 1362 uv.getBounds(InferenceBound.EQ)));
mcimadamore@1562 1363 }
mcimadamore@1562 1364 return GraphUtils.toDot(nodes, "inferenceGraph" + hashCode(), buf.toString());
mcimadamore@1562 1365 }
mcimadamore@1562 1366 }
mcimadamore@1562 1367 }
mcimadamore@1562 1368 // </editor-fold>
mcimadamore@1562 1369
mcimadamore@1562 1370 // <editor-fold defaultstate="collapsed" desc="Inference context">
mcimadamore@1562 1371 /**
mcimadamore@1550 1372 * Functional interface for defining inference callbacks. Certain actions
mcimadamore@1550 1373 * (i.e. subtyping checks) might need to be redone after all inference variables
mcimadamore@1550 1374 * have been fixed.
mcimadamore@1337 1375 */
mcimadamore@1550 1376 interface FreeTypeListener {
mcimadamore@1550 1377 void typesInferred(InferenceContext inferenceContext);
mcimadamore@1550 1378 }
mcimadamore@1337 1379
mcimadamore@1337 1380 /**
mcimadamore@1337 1381 * An inference context keeps track of the set of variables that are free
mcimadamore@1337 1382 * in the current context. It provides utility methods for opening/closing
mcimadamore@1337 1383 * types to their corresponding free/closed forms. It also provide hooks for
mcimadamore@1337 1384 * attaching deferred post-inference action (see PendingCheck). Finally,
mcimadamore@1337 1385 * it can be used as an entry point for performing upper/lower bound inference
mcimadamore@1337 1386 * (see InferenceKind).
mcimadamore@1337 1387 */
mcimadamore@1562 1388 class InferenceContext {
mcimadamore@1337 1389
mcimadamore@1337 1390 /** list of inference vars as undet vars */
mcimadamore@1337 1391 List<Type> undetvars;
mcimadamore@1337 1392
mcimadamore@1337 1393 /** list of inference vars in this context */
mcimadamore@1337 1394 List<Type> inferencevars;
mcimadamore@1337 1395
mcimadamore@1337 1396 java.util.Map<FreeTypeListener, List<Type>> freeTypeListeners =
mcimadamore@1337 1397 new java.util.HashMap<FreeTypeListener, List<Type>>();
mcimadamore@1337 1398
mcimadamore@1337 1399 List<FreeTypeListener> freetypeListeners = List.nil();
mcimadamore@1337 1400
mcimadamore@1550 1401 public InferenceContext(List<Type> inferencevars) {
mcimadamore@1550 1402 this.undetvars = Type.map(inferencevars, fromTypeVarFun);
mcimadamore@1337 1403 this.inferencevars = inferencevars;
mcimadamore@1337 1404 }
mcimadamore@1550 1405 //where
mcimadamore@1550 1406 Mapping fromTypeVarFun = new Mapping("fromTypeVarFunWithBounds") {
mcimadamore@1550 1407 // mapping that turns inference variables into undet vars
mcimadamore@1550 1408 public Type apply(Type t) {
mcimadamore@1898 1409 if (t.hasTag(TYPEVAR)) {
mcimadamore@1898 1410 TypeVar tv = (TypeVar)t;
mcimadamore@1898 1411 return tv.isCaptured() ?
mcimadamore@1898 1412 new CapturedUndetVar((CapturedType)tv, types) :
mcimadamore@1898 1413 new UndetVar(tv, types);
mcimadamore@1898 1414 } else {
mcimadamore@1898 1415 return t.map(this);
mcimadamore@1898 1416 }
mcimadamore@1550 1417 }
mcimadamore@1550 1418 };
mcimadamore@1337 1419
mcimadamore@1337 1420 /**
mcimadamore@1898 1421 * add a new inference var to this inference context
mcimadamore@1898 1422 */
mcimadamore@1898 1423 void addVar(TypeVar t) {
mcimadamore@1898 1424 this.undetvars = this.undetvars.prepend(fromTypeVarFun.apply(t));
mcimadamore@1898 1425 this.inferencevars = this.inferencevars.prepend(t);
mcimadamore@1898 1426 }
mcimadamore@1898 1427
mcimadamore@1898 1428 /**
mcimadamore@1337 1429 * returns the list of free variables (as type-variables) in this
mcimadamore@1337 1430 * inference context
mcimadamore@1337 1431 */
mcimadamore@1337 1432 List<Type> inferenceVars() {
mcimadamore@1337 1433 return inferencevars;
mcimadamore@1337 1434 }
mcimadamore@1337 1435
mcimadamore@1337 1436 /**
mcimadamore@1337 1437 * returns the list of uninstantiated variables (as type-variables) in this
mcimadamore@1550 1438 * inference context
mcimadamore@1337 1439 */
mcimadamore@1337 1440 List<Type> restvars() {
mcimadamore@1550 1441 return filterVars(new Filter<UndetVar>() {
mcimadamore@1550 1442 public boolean accepts(UndetVar uv) {
mcimadamore@1550 1443 return uv.inst == null;
mcimadamore@1337 1444 }
mcimadamore@1550 1445 });
mcimadamore@1550 1446 }
mcimadamore@1550 1447
mcimadamore@1550 1448 /**
mcimadamore@1550 1449 * returns the list of instantiated variables (as type-variables) in this
mcimadamore@1550 1450 * inference context
mcimadamore@1550 1451 */
mcimadamore@1550 1452 List<Type> instvars() {
mcimadamore@1550 1453 return filterVars(new Filter<UndetVar>() {
mcimadamore@1550 1454 public boolean accepts(UndetVar uv) {
mcimadamore@1550 1455 return uv.inst != null;
mcimadamore@1550 1456 }
mcimadamore@1550 1457 });
mcimadamore@1550 1458 }
mcimadamore@1550 1459
mcimadamore@1550 1460 /**
mcimadamore@1550 1461 * Get list of bounded inference variables (where bound is other than
mcimadamore@1550 1462 * declared bounds).
mcimadamore@1550 1463 */
mcimadamore@1550 1464 final List<Type> boundedVars() {
mcimadamore@1550 1465 return filterVars(new Filter<UndetVar>() {
mcimadamore@1550 1466 public boolean accepts(UndetVar uv) {
mcimadamore@1550 1467 return uv.getBounds(InferenceBound.UPPER)
mcimadamore@1550 1468 .diff(uv.getDeclaredBounds())
mcimadamore@1550 1469 .appendList(uv.getBounds(InferenceBound.EQ, InferenceBound.LOWER)).nonEmpty();
mcimadamore@1550 1470 }
mcimadamore@1550 1471 });
mcimadamore@1550 1472 }
mcimadamore@1550 1473
mcimadamore@1550 1474 private List<Type> filterVars(Filter<UndetVar> fu) {
mcimadamore@1550 1475 ListBuffer<Type> res = ListBuffer.lb();
mcimadamore@1550 1476 for (Type t : undetvars) {
mcimadamore@1550 1477 UndetVar uv = (UndetVar)t;
mcimadamore@1550 1478 if (fu.accepts(uv)) {
mcimadamore@1550 1479 res.append(uv.qtype);
mcimadamore@1550 1480 }
mcimadamore@1337 1481 }
mcimadamore@1550 1482 return res.toList();
mcimadamore@1337 1483 }
mcimadamore@1337 1484
mcimadamore@1337 1485 /**
mcimadamore@1337 1486 * is this type free?
mcimadamore@1337 1487 */
mcimadamore@1337 1488 final boolean free(Type t) {
mcimadamore@1337 1489 return t.containsAny(inferencevars);
mcimadamore@1337 1490 }
mcimadamore@1337 1491
mcimadamore@1337 1492 final boolean free(List<Type> ts) {
mcimadamore@1337 1493 for (Type t : ts) {
mcimadamore@1337 1494 if (free(t)) return true;
mcimadamore@1337 1495 }
mcimadamore@1337 1496 return false;
mcimadamore@1337 1497 }
mcimadamore@1337 1498
mcimadamore@1337 1499 /**
mcimadamore@1337 1500 * Returns a list of free variables in a given type
mcimadamore@1337 1501 */
mcimadamore@1337 1502 final List<Type> freeVarsIn(Type t) {
mcimadamore@1337 1503 ListBuffer<Type> buf = ListBuffer.lb();
mcimadamore@1337 1504 for (Type iv : inferenceVars()) {
mcimadamore@1337 1505 if (t.contains(iv)) {
mcimadamore@1337 1506 buf.add(iv);
mcimadamore@1337 1507 }
mcimadamore@1337 1508 }
mcimadamore@1337 1509 return buf.toList();
mcimadamore@1337 1510 }
mcimadamore@1337 1511
mcimadamore@1337 1512 final List<Type> freeVarsIn(List<Type> ts) {
mcimadamore@1337 1513 ListBuffer<Type> buf = ListBuffer.lb();
mcimadamore@1337 1514 for (Type t : ts) {
mcimadamore@1337 1515 buf.appendList(freeVarsIn(t));
mcimadamore@1337 1516 }
mcimadamore@1337 1517 ListBuffer<Type> buf2 = ListBuffer.lb();
mcimadamore@1337 1518 for (Type t : buf) {
mcimadamore@1337 1519 if (!buf2.contains(t)) {
mcimadamore@1337 1520 buf2.add(t);
mcimadamore@1337 1521 }
mcimadamore@1337 1522 }
mcimadamore@1337 1523 return buf2.toList();
mcimadamore@1337 1524 }
mcimadamore@1337 1525
mcimadamore@1337 1526 /**
mcimadamore@1337 1527 * Replace all free variables in a given type with corresponding
mcimadamore@1337 1528 * undet vars (used ahead of subtyping/compatibility checks to allow propagation
mcimadamore@1337 1529 * of inference constraints).
mcimadamore@1337 1530 */
mcimadamore@1550 1531 final Type asFree(Type t) {
mcimadamore@1337 1532 return types.subst(t, inferencevars, undetvars);
mcimadamore@1337 1533 }
mcimadamore@1337 1534
mcimadamore@1550 1535 final List<Type> asFree(List<Type> ts) {
mcimadamore@1337 1536 ListBuffer<Type> buf = ListBuffer.lb();
mcimadamore@1337 1537 for (Type t : ts) {
mcimadamore@1550 1538 buf.append(asFree(t));
mcimadamore@1337 1539 }
mcimadamore@1337 1540 return buf.toList();
mcimadamore@1337 1541 }
mcimadamore@1337 1542
mcimadamore@1337 1543 List<Type> instTypes() {
mcimadamore@1337 1544 ListBuffer<Type> buf = ListBuffer.lb();
mcimadamore@1337 1545 for (Type t : undetvars) {
mcimadamore@1337 1546 UndetVar uv = (UndetVar)t;
mcimadamore@1337 1547 buf.append(uv.inst != null ? uv.inst : uv.qtype);
mcimadamore@1337 1548 }
mcimadamore@1337 1549 return buf.toList();
mcimadamore@1337 1550 }
mcimadamore@1337 1551
mcimadamore@1337 1552 /**
mcimadamore@1337 1553 * Replace all free variables in a given type with corresponding
mcimadamore@1337 1554 * instantiated types - if one or more free variable has not been
mcimadamore@1337 1555 * fully instantiated, it will still be available in the resulting type.
mcimadamore@1337 1556 */
mcimadamore@1550 1557 Type asInstType(Type t) {
mcimadamore@1337 1558 return types.subst(t, inferencevars, instTypes());
mcimadamore@1337 1559 }
mcimadamore@1337 1560
mcimadamore@1550 1561 List<Type> asInstTypes(List<Type> ts) {
mcimadamore@1337 1562 ListBuffer<Type> buf = ListBuffer.lb();
mcimadamore@1337 1563 for (Type t : ts) {
mcimadamore@1550 1564 buf.append(asInstType(t));
mcimadamore@1337 1565 }
mcimadamore@1337 1566 return buf.toList();
mcimadamore@1337 1567 }
mcimadamore@1337 1568
mcimadamore@1337 1569 /**
mcimadamore@1337 1570 * Add custom hook for performing post-inference action
mcimadamore@1337 1571 */
mcimadamore@1337 1572 void addFreeTypeListener(List<Type> types, FreeTypeListener ftl) {
mcimadamore@1337 1573 freeTypeListeners.put(ftl, freeVarsIn(types));
mcimadamore@1337 1574 }
mcimadamore@1337 1575
mcimadamore@1337 1576 /**
mcimadamore@1337 1577 * Mark the inference context as complete and trigger evaluation
mcimadamore@1337 1578 * of all deferred checks.
mcimadamore@1337 1579 */
mcimadamore@1550 1580 void notifyChange() {
mcimadamore@1562 1581 notifyChange(inferencevars.diff(restvars()));
mcimadamore@1562 1582 }
mcimadamore@1562 1583
mcimadamore@1562 1584 void notifyChange(List<Type> inferredVars) {
mcimadamore@1337 1585 InferenceException thrownEx = null;
mcimadamore@1337 1586 for (Map.Entry<FreeTypeListener, List<Type>> entry :
mcimadamore@1337 1587 new HashMap<FreeTypeListener, List<Type>>(freeTypeListeners).entrySet()) {
mcimadamore@1562 1588 if (!Type.containsAny(entry.getValue(), inferencevars.diff(inferredVars))) {
mcimadamore@1337 1589 try {
mcimadamore@1337 1590 entry.getKey().typesInferred(this);
mcimadamore@1337 1591 freeTypeListeners.remove(entry.getKey());
mcimadamore@1337 1592 } catch (InferenceException ex) {
mcimadamore@1337 1593 if (thrownEx == null) {
mcimadamore@1337 1594 thrownEx = ex;
mcimadamore@1337 1595 }
mcimadamore@1337 1596 }
mcimadamore@1337 1597 }
mcimadamore@1337 1598 }
mcimadamore@1337 1599 //inference exception multiplexing - present any inference exception
mcimadamore@1337 1600 //thrown when processing listeners as a single one
mcimadamore@1337 1601 if (thrownEx != null) {
mcimadamore@1337 1602 throw thrownEx;
mcimadamore@1337 1603 }
mcimadamore@1337 1604 }
mcimadamore@1347 1605
mcimadamore@1562 1606 /**
mcimadamore@1562 1607 * Save the state of this inference context
mcimadamore@1562 1608 */
mcimadamore@1891 1609 List<Type> save() {
mcimadamore@1562 1610 ListBuffer<Type> buf = ListBuffer.lb();
mcimadamore@1562 1611 for (Type t : undetvars) {
mcimadamore@1562 1612 UndetVar uv = (UndetVar)t;
mcimadamore@1562 1613 UndetVar uv2 = new UndetVar((TypeVar)uv.qtype, types);
mcimadamore@1562 1614 for (InferenceBound ib : InferenceBound.values()) {
mcimadamore@1562 1615 for (Type b : uv.getBounds(ib)) {
mcimadamore@1562 1616 uv2.addBound(ib, b, types);
mcimadamore@1562 1617 }
mcimadamore@1562 1618 }
mcimadamore@1562 1619 uv2.inst = uv.inst;
mcimadamore@1562 1620 buf.add(uv2);
mcimadamore@1562 1621 }
mcimadamore@1891 1622 return buf.toList();
mcimadamore@1562 1623 }
mcimadamore@1562 1624
mcimadamore@1562 1625 /**
mcimadamore@1562 1626 * Restore the state of this inference context to the previous known checkpoint
mcimadamore@1562 1627 */
mcimadamore@1891 1628 void rollback(List<Type> saved_undet) {
mcimadamore@1891 1629 Assert.check(saved_undet != null && saved_undet.length() == undetvars.length());
mcimadamore@1891 1630 //restore bounds (note: we need to preserve the old instances)
mcimadamore@1891 1631 for (Type t : undetvars) {
mcimadamore@1891 1632 UndetVar uv = (UndetVar)t;
mcimadamore@1891 1633 UndetVar uv_saved = (UndetVar)saved_undet.head;
mcimadamore@1891 1634 for (InferenceBound ib : InferenceBound.values()) {
mcimadamore@1891 1635 uv.setBounds(ib, uv_saved.getBounds(ib));
mcimadamore@1891 1636 }
mcimadamore@1891 1637 uv.inst = uv_saved.inst;
mcimadamore@1891 1638 saved_undet = saved_undet.tail;
mcimadamore@1891 1639 }
mcimadamore@1562 1640 }
mcimadamore@1562 1641
mcimadamore@1562 1642 /**
mcimadamore@1562 1643 * Copy variable in this inference context to the given context
mcimadamore@1562 1644 */
mcimadamore@1562 1645 void dupTo(final InferenceContext that) {
mcimadamore@1562 1646 that.inferencevars = that.inferencevars.appendList(inferencevars);
mcimadamore@1562 1647 that.undetvars = that.undetvars.appendList(undetvars);
mcimadamore@1562 1648 //set up listeners to notify original inference contexts as
mcimadamore@1562 1649 //propagated vars are inferred in new context
mcimadamore@1562 1650 for (Type t : inferencevars) {
mcimadamore@1562 1651 that.freeTypeListeners.put(new FreeTypeListener() {
mcimadamore@1562 1652 public void typesInferred(InferenceContext inferenceContext) {
mcimadamore@1562 1653 InferenceContext.this.notifyChange();
mcimadamore@1562 1654 }
mcimadamore@1562 1655 }, List.of(t));
mcimadamore@1562 1656 }
mcimadamore@1562 1657 }
mcimadamore@1562 1658
mcimadamore@1562 1659 /**
mcimadamore@1562 1660 * Solve with given graph strategy.
mcimadamore@1562 1661 */
mcimadamore@1562 1662 private void solve(GraphStrategy ss, Warner warn) {
mcimadamore@1562 1663 GraphSolver s = new GraphSolver(this, warn);
mcimadamore@1562 1664 s.solve(ss);
mcimadamore@1562 1665 }
mcimadamore@1562 1666
mcimadamore@1562 1667 /**
mcimadamore@1562 1668 * Solve all variables in this context.
mcimadamore@1562 1669 */
mcimadamore@1562 1670 public void solve(Warner warn) {
mcimadamore@1562 1671 solve(new LeafSolver() {
mcimadamore@1562 1672 public boolean done() {
mcimadamore@1562 1673 return restvars().isEmpty();
mcimadamore@1562 1674 }
mcimadamore@1562 1675 }, warn);
mcimadamore@1562 1676 }
mcimadamore@1562 1677
mcimadamore@1562 1678 /**
mcimadamore@1562 1679 * Solve all variables in the given list.
mcimadamore@1562 1680 */
mcimadamore@1562 1681 public void solve(final List<Type> vars, Warner warn) {
mcimadamore@1562 1682 solve(new BestLeafSolver(vars) {
mcimadamore@1562 1683 public boolean done() {
mcimadamore@1562 1684 return !free(asInstTypes(vars));
mcimadamore@1562 1685 }
mcimadamore@1562 1686 }, warn);
mcimadamore@1562 1687 }
mcimadamore@1562 1688
mcimadamore@1562 1689 /**
mcimadamore@1562 1690 * Solve at least one variable in given list.
mcimadamore@1562 1691 */
mcimadamore@1562 1692 public void solveAny(List<Type> varsToSolve, Warner warn) {
mcimadamore@1562 1693 checkWithinBounds(this, warn); //propagate bounds
mcimadamore@1562 1694 List<Type> boundedVars = boundedVars().intersect(restvars()).intersect(varsToSolve);
mcimadamore@1562 1695 if (boundedVars.isEmpty()) {
mcimadamore@1562 1696 throw inferenceException.setMessage("cyclic.inference",
mcimadamore@1562 1697 freeVarsIn(varsToSolve));
mcimadamore@1562 1698 }
mcimadamore@1562 1699 solve(new BestLeafSolver(boundedVars) {
mcimadamore@1562 1700 public boolean done() {
mcimadamore@1562 1701 return instvars().intersect(varsToSolve).nonEmpty();
mcimadamore@1562 1702 }
mcimadamore@1562 1703 }, warn);
mcimadamore@1562 1704 }
mcimadamore@1562 1705
mcimadamore@1562 1706 /**
mcimadamore@1562 1707 * Apply a set of inference steps
mcimadamore@1562 1708 */
mcimadamore@1562 1709 private boolean solveBasic(EnumSet<InferenceStep> steps) {
mcimadamore@1562 1710 return solveBasic(inferencevars, steps);
mcimadamore@1562 1711 }
mcimadamore@1562 1712
mcimadamore@1562 1713 private boolean solveBasic(List<Type> varsToSolve, EnumSet<InferenceStep> steps) {
mcimadamore@1562 1714 boolean changed = false;
mcimadamore@1562 1715 for (Type t : varsToSolve.intersect(restvars())) {
mcimadamore@1550 1716 UndetVar uv = (UndetVar)asFree(t);
mcimadamore@1562 1717 for (InferenceStep step : steps) {
mcimadamore@1562 1718 if (step.accepts(uv, this)) {
mcimadamore@1562 1719 uv.inst = step.solve(uv, this);
mcimadamore@1562 1720 changed = true;
mcimadamore@1562 1721 break;
mcimadamore@1347 1722 }
mcimadamore@1347 1723 }
mcimadamore@1347 1724 }
mcimadamore@1562 1725 return changed;
mcimadamore@1562 1726 }
mcimadamore@1562 1727
mcimadamore@1562 1728 /**
mcimadamore@1562 1729 * Instantiate inference variables in legacy mode (JLS 15.12.2.7, 15.12.2.8).
mcimadamore@1562 1730 * During overload resolution, instantiation is done by doing a partial
mcimadamore@1562 1731 * inference process using eq/lower bound instantiation. During check,
mcimadamore@1562 1732 * we also instantiate any remaining vars by repeatedly using eq/upper
mcimadamore@1562 1733 * instantiation, until all variables are solved.
mcimadamore@1562 1734 */
mcimadamore@1562 1735 public void solveLegacy(boolean partial, Warner warn, EnumSet<InferenceStep> steps) {
mcimadamore@1562 1736 while (true) {
mcimadamore@1562 1737 boolean stuck = !solveBasic(steps);
mcimadamore@1562 1738 if (restvars().isEmpty() || partial) {
mcimadamore@1562 1739 //all variables have been instantiated - exit
mcimadamore@1562 1740 break;
mcimadamore@1562 1741 } else if (stuck) {
mcimadamore@1562 1742 //some variables could not be instantiated because of cycles in
mcimadamore@1562 1743 //upper bounds - provide a (possibly recursive) default instantiation
mcimadamore@1562 1744 instantiateAsUninferredVars(restvars(), this);
mcimadamore@1562 1745 break;
mcimadamore@1562 1746 } else {
mcimadamore@1562 1747 //some variables have been instantiated - replace newly instantiated
mcimadamore@1562 1748 //variables in remaining upper bounds and continue
mcimadamore@1562 1749 for (Type t : undetvars) {
mcimadamore@1562 1750 UndetVar uv = (UndetVar)t;
mcimadamore@1562 1751 uv.substBounds(inferenceVars(), instTypes(), types);
mcimadamore@1562 1752 }
mcimadamore@1562 1753 }
mcimadamore@1347 1754 }
mcimadamore@1562 1755 checkWithinBounds(this, warn);
mcimadamore@1562 1756 }
mcimadamore@1562 1757
mcimadamore@1562 1758 private Infer infer() {
mcimadamore@1562 1759 //back-door to infer
mcimadamore@1562 1760 return Infer.this;
mcimadamore@1347 1761 }
mcimadamore@895 1762 }
mcimadamore@1337 1763
mcimadamore@1550 1764 final InferenceContext emptyContext = new InferenceContext(List.<Type>nil());
mcimadamore@1550 1765 // </editor-fold>
mcimadamore@1337 1766 }

mercurial