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