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