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

Wed, 17 Jul 2013 14:04:01 +0100

author
mcimadamore
date
Wed, 17 Jul 2013 14:04:01 +0100
changeset 1896
44e27378f523
parent 1891
42b3c5e92461
child 1897
866c87c01285
permissions
-rw-r--r--

8012242: Lambda compatibility and checked exceptions
Summary: Inference variables in 'throws' clause with no constraints should be inferred as RuntimeException
Reviewed-by: jjg, vromero

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

mercurial