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

Tue, 12 Feb 2013 19:25:09 +0000

author
mcimadamore
date
Tue, 12 Feb 2013 19:25:09 +0000
changeset 1562
2154ed9ff6c8
parent 1551
8cdd96f2fdb9
child 1608
133a0a0c2cbc
permissions
-rw-r--r--

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

mercurial