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

Mon, 11 Nov 2013 09:47:46 -0500

author
emc
date
Mon, 11 Nov 2013 09:47:46 -0500
changeset 2187
4788eb38cac5
parent 2157
963c57175e40
child 2302
f35effa10297
permissions
-rw-r--r--

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

mercurial