265 Resolve.MethodCheck methodCheck, |
146 Resolve.MethodCheck methodCheck, |
266 Warner warn) throws InferenceException { |
147 Warner warn) throws InferenceException { |
267 //-System.err.println("instantiateMethod(" + tvars + ", " + mt + ", " + argtypes + ")"); //DEBUG |
148 //-System.err.println("instantiateMethod(" + tvars + ", " + mt + ", " + argtypes + ")"); //DEBUG |
268 final InferenceContext inferenceContext = new InferenceContext(tvars); |
149 final InferenceContext inferenceContext = new InferenceContext(tvars); |
269 inferenceException.clear(); |
150 inferenceException.clear(); |
270 |
|
271 DeferredAttr.DeferredAttrContext deferredAttrContext = |
|
272 resolveContext.deferredAttrContext(msym, inferenceContext, resultInfo, warn); |
|
273 |
|
274 try { |
151 try { |
275 methodCheck.argumentsAcceptable(env, deferredAttrContext, argtypes, mt.getParameterTypes(), warn); |
152 DeferredAttr.DeferredAttrContext deferredAttrContext = |
276 |
153 resolveContext.deferredAttrContext(msym, inferenceContext, resultInfo, warn); |
277 if (resultInfo != null && allowEarlyReturnConstraints && |
154 |
|
155 methodCheck.argumentsAcceptable(env, deferredAttrContext, |
|
156 argtypes, mt.getParameterTypes(), warn); |
|
157 |
|
158 if (allowGraphInference && |
|
159 resultInfo != null && |
278 !warn.hasNonSilentLint(Lint.LintCategory.UNCHECKED)) { |
160 !warn.hasNonSilentLint(Lint.LintCategory.UNCHECKED)) { |
279 generateReturnConstraints(mt, inferenceContext, resultInfo); |
161 //inject return constraints earlier |
|
162 checkWithinBounds(inferenceContext, warn); //propagation |
|
163 generateReturnConstraints(resultInfo, mt, inferenceContext); |
|
164 //propagate outwards if needed |
|
165 if (resultInfo.checkContext.inferenceContext().free(resultInfo.pt)) { |
|
166 //propagate inference context outwards and exit |
|
167 inferenceContext.dupTo(resultInfo.checkContext.inferenceContext()); |
|
168 deferredAttrContext.complete(); |
|
169 return mt; |
|
170 } |
280 } |
171 } |
281 |
172 |
282 deferredAttrContext.complete(); |
173 deferredAttrContext.complete(); |
283 |
174 |
284 // minimize as yet undetermined type variables |
175 // minimize as yet undetermined type variables |
285 for (Type t : inferenceContext.undetvars) { |
176 if (allowGraphInference) { |
286 minimizeInst((UndetVar)t, warn); |
177 inferenceContext.solve(warn); |
287 } |
178 } else { |
288 |
179 inferenceContext.solveLegacy(true, warn, LegacyInferenceSteps.EQ_LOWER.steps); //minimizeInst |
289 checkWithinBounds(inferenceContext, warn); |
180 } |
290 |
181 |
291 mt = (MethodType)inferenceContext.asInstType(mt); |
182 mt = (MethodType)inferenceContext.asInstType(mt); |
292 |
183 |
293 List<Type> restvars = inferenceContext.restvars(); |
184 if (!allowGraphInference && |
294 |
185 inferenceContext.restvars().nonEmpty() && |
295 if (!restvars.isEmpty()) { |
186 resultInfo != null && |
296 if (resultInfo != null && !warn.hasNonSilentLint(Lint.LintCategory.UNCHECKED)) { |
187 !warn.hasNonSilentLint(Lint.LintCategory.UNCHECKED)) { |
297 if (!allowEarlyReturnConstraints) { |
188 generateReturnConstraints(resultInfo, mt, inferenceContext); |
298 generateReturnConstraints(mt, inferenceContext, resultInfo); |
189 inferenceContext.solveLegacy(false, warn, LegacyInferenceSteps.EQ_UPPER.steps); //maximizeInst |
299 } |
190 mt = (MethodType)inferenceContext.asInstType(mt); |
300 instantiateUninferred(env.tree.pos(), inferenceContext, mt, resultInfo, warn); |
191 } |
301 checkWithinBounds(inferenceContext, warn); |
192 |
302 mt = (MethodType)inferenceContext.asInstType(mt); |
193 if (resultInfo != null && rs.verboseResolutionMode.contains(VerboseResolutionMode.DEFERRED_INST)) { |
303 if (rs.verboseResolutionMode.contains(VerboseResolutionMode.DEFERRED_INST)) { |
194 log.note(env.tree.pos, "deferred.method.inst", msym, mt, resultInfo.pt); |
304 log.note(env.tree.pos, "deferred.method.inst", msym, mt, resultInfo.pt); |
|
305 } |
|
306 } |
|
307 } |
195 } |
308 |
196 |
309 // return instantiated version of method type |
197 // return instantiated version of method type |
310 return mt; |
198 return mt; |
311 } finally { |
199 } finally { |
312 inferenceContext.notifyChange(); |
200 if (resultInfo != null || !allowGraphInference) { |
|
201 inferenceContext.notifyChange(); |
|
202 } else { |
|
203 inferenceContext.notifyChange(inferenceContext.boundedVars()); |
|
204 } |
|
205 } |
|
206 } |
|
207 |
|
208 /** |
|
209 * Generate constraints from the generic method's return type. If the method |
|
210 * call occurs in a context where a type T is expected, use the expected |
|
211 * type to derive more constraints on the generic method inference variables. |
|
212 */ |
|
213 void generateReturnConstraints(Attr.ResultInfo resultInfo, |
|
214 MethodType mt, InferenceContext inferenceContext) { |
|
215 Type qtype1 = inferenceContext.asFree(mt.getReturnType()); |
|
216 Type to = returnConstraintTarget(qtype1, resultInfo.pt); |
|
217 Assert.check(allowGraphInference || !resultInfo.checkContext.inferenceContext().free(to), |
|
218 "legacy inference engine cannot handle constraints on both sides of a subtyping assertion"); |
|
219 //we need to skip capture? |
|
220 Warner retWarn = new Warner(); |
|
221 if (!resultInfo.checkContext.compatible(qtype1, resultInfo.checkContext.inferenceContext().asFree(to), retWarn) || |
|
222 //unchecked conversion is not allowed |
|
223 retWarn.hasLint(Lint.LintCategory.UNCHECKED)) { |
|
224 throw inferenceException |
|
225 .setMessage("infer.no.conforming.instance.exists", |
|
226 inferenceContext.restvars(), mt.getReturnType(), to); |
313 } |
227 } |
314 } |
228 } |
315 //where |
229 //where |
316 void generateReturnConstraints(Type mt, InferenceContext inferenceContext, Attr.ResultInfo resultInfo) { |
230 private Type returnConstraintTarget(Type from, Type to) { |
317 if (resultInfo != null) { |
231 if (from.hasTag(VOID)) { |
318 Type to = resultInfo.pt; |
232 return syms.voidType; |
319 if (to.hasTag(NONE) || resultInfo.checkContext.inferenceContext().free(resultInfo.pt)) { |
233 } else if (to.hasTag(NONE)) { |
320 to = mt.getReturnType().isPrimitiveOrVoid() ? |
234 return from.isPrimitive() ? from : syms.objectType; |
321 mt.getReturnType() : syms.objectType; |
235 } else if (from.hasTag(UNDETVAR) && to.isPrimitive()) { |
322 } |
236 if (!allowGraphInference) { |
323 Type qtype1 = inferenceContext.asFree(mt.getReturnType()); |
237 //if legacy, just return boxed type |
324 Warner retWarn = new Warner(); |
238 return types.boxedClass(to).type; |
325 if (!resultInfo.checkContext.compatible(qtype1, qtype1.hasTag(UNDETVAR) ? types.boxedTypeOrType(to) : to, retWarn) || |
239 } |
326 //unchecked conversion is not allowed |
240 //if graph inference we need to skip conflicting boxed bounds... |
327 retWarn.hasLint(Lint.LintCategory.UNCHECKED)) { |
241 UndetVar uv = (UndetVar)from; |
328 throw inferenceException |
242 for (Type t : uv.getBounds(InferenceBound.EQ, InferenceBound.LOWER)) { |
329 .setMessage("infer.no.conforming.instance.exists", |
243 Type boundAsPrimitive = types.unboxedType(t); |
330 inferenceContext.restvars(), mt.getReturnType(), to); |
244 if (boundAsPrimitive == null) continue; |
331 } |
245 if (types.isConvertible(boundAsPrimitive, to)) { |
332 } |
246 //effectively skip return-type constraint generation (compatibility) |
333 } |
247 return syms.objectType; |
334 |
248 } |
335 /** check that type parameters are within their bounds. |
249 } |
336 */ |
250 return types.boxedClass(to).type; |
337 void checkWithinBounds(InferenceContext inferenceContext, |
251 } else { |
338 Warner warn) throws InferenceException { |
252 return to; |
339 //step 1 - check compatibility of instantiated type w.r.t. initial bounds |
253 } |
340 for (Type t : inferenceContext.undetvars) { |
254 } |
|
255 |
|
256 /** |
|
257 * Infer cyclic inference variables as described in 15.12.2.8. |
|
258 */ |
|
259 private void instantiateAsUninferredVars(List<Type> vars, InferenceContext inferenceContext) { |
|
260 ListBuffer<Type> todo = ListBuffer.lb(); |
|
261 //step 1 - create fresh tvars |
|
262 for (Type t : vars) { |
|
263 UndetVar uv = (UndetVar)inferenceContext.asFree(t); |
|
264 List<Type> upperBounds = uv.getBounds(InferenceBound.UPPER); |
|
265 if (Type.containsAny(upperBounds, vars)) { |
|
266 TypeSymbol fresh_tvar = new TypeSymbol(Flags.SYNTHETIC, uv.qtype.tsym.name, null, uv.qtype.tsym.owner); |
|
267 fresh_tvar.type = new TypeVar(fresh_tvar, types.makeCompoundType(uv.getBounds(InferenceBound.UPPER)), null); |
|
268 todo.append(uv); |
|
269 uv.inst = fresh_tvar.type; |
|
270 } else if (upperBounds.nonEmpty()) { |
|
271 uv.inst = types.glb(upperBounds); |
|
272 } else { |
|
273 uv.inst = syms.objectType; |
|
274 } |
|
275 } |
|
276 //step 2 - replace fresh tvars in their bounds |
|
277 List<Type> formals = vars; |
|
278 for (Type t : todo) { |
341 UndetVar uv = (UndetVar)t; |
279 UndetVar uv = (UndetVar)t; |
342 uv.substBounds(inferenceContext.inferenceVars(), inferenceContext.instTypes(), types); |
280 TypeVar ct = (TypeVar)uv.inst; |
343 checkCompatibleUpperBounds(uv, inferenceContext.inferenceVars()); |
281 ct.bound = types.glb(inferenceContext.asInstTypes(types.getBounds(ct))); |
344 if (!inferenceContext.restvars().contains(uv.qtype)) { |
282 if (ct.bound.isErroneous()) { |
345 Type inst = inferenceContext.asInstType(t); |
283 //report inference error if glb fails |
346 for (Type u : uv.getBounds(InferenceBound.UPPER)) { |
284 reportBoundError(uv, BoundErrorKind.BAD_UPPER); |
347 if (!types.isSubtypeUnchecked(inst, inferenceContext.asFree(u), warn)) { |
285 } |
348 reportBoundError(uv, BoundErrorKind.UPPER); |
286 formals = formals.tail; |
349 } |
287 } |
350 } |
288 } |
351 for (Type l : uv.getBounds(InferenceBound.LOWER)) { |
|
352 Assert.check(!inferenceContext.free(l)); |
|
353 if (!types.isSubtypeUnchecked(l, inst, warn)) { |
|
354 reportBoundError(uv, BoundErrorKind.LOWER); |
|
355 } |
|
356 } |
|
357 for (Type e : uv.getBounds(InferenceBound.EQ)) { |
|
358 Assert.check(!inferenceContext.free(e)); |
|
359 if (!types.isSameType(inst, e)) { |
|
360 reportBoundError(uv, BoundErrorKind.EQ); |
|
361 } |
|
362 } |
|
363 } |
|
364 } |
|
365 |
|
366 //step 2 - check that eq bounds are consistent w.r.t. eq/lower bounds |
|
367 for (Type t : inferenceContext.undetvars) { |
|
368 UndetVar uv = (UndetVar)t; |
|
369 //check eq bounds consistency |
|
370 Type eq = null; |
|
371 for (Type e : uv.getBounds(InferenceBound.EQ)) { |
|
372 Assert.check(!inferenceContext.free(e)); |
|
373 if (eq != null && !types.isSameType(e, eq)) { |
|
374 reportBoundError(uv, BoundErrorKind.EQ); |
|
375 } |
|
376 eq = e; |
|
377 for (Type l : uv.getBounds(InferenceBound.LOWER)) { |
|
378 Assert.check(!inferenceContext.free(l)); |
|
379 if (!types.isSubtypeUnchecked(l, e, warn)) { |
|
380 reportBoundError(uv, BoundErrorKind.BAD_EQ_LOWER); |
|
381 } |
|
382 } |
|
383 for (Type u : uv.getBounds(InferenceBound.UPPER)) { |
|
384 if (inferenceContext.free(u)) continue; |
|
385 if (!types.isSubtypeUnchecked(e, u, warn)) { |
|
386 reportBoundError(uv, BoundErrorKind.BAD_EQ_UPPER); |
|
387 } |
|
388 } |
|
389 } |
|
390 } |
|
391 } |
|
392 |
|
393 void checkCompatibleUpperBounds(UndetVar uv, List<Type> tvars) { |
|
394 // VGJ: sort of inlined maximizeInst() below. Adding |
|
395 // bounds can cause lobounds that are above hibounds. |
|
396 ListBuffer<Type> hiboundsNoVars = ListBuffer.lb(); |
|
397 for (Type t : Type.filter(uv.getBounds(InferenceBound.UPPER), boundFilter)) { |
|
398 if (!t.containsAny(tvars)) { |
|
399 hiboundsNoVars.append(t); |
|
400 } |
|
401 } |
|
402 List<Type> hibounds = hiboundsNoVars.toList(); |
|
403 Type hb = null; |
|
404 if (hibounds.isEmpty()) |
|
405 hb = syms.objectType; |
|
406 else if (hibounds.tail.isEmpty()) |
|
407 hb = hibounds.head; |
|
408 else |
|
409 hb = types.glb(hibounds); |
|
410 if (hb == null || hb.isErroneous()) |
|
411 reportBoundError(uv, BoundErrorKind.BAD_UPPER); |
|
412 } |
|
413 |
|
414 enum BoundErrorKind { |
|
415 BAD_UPPER() { |
|
416 @Override |
|
417 InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) { |
|
418 return ex.setMessage("incompatible.upper.bounds", uv.qtype, |
|
419 uv.getBounds(InferenceBound.UPPER)); |
|
420 } |
|
421 }, |
|
422 BAD_EQ_UPPER() { |
|
423 @Override |
|
424 InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) { |
|
425 return ex.setMessage("incompatible.eq.upper.bounds", uv.qtype, |
|
426 uv.getBounds(InferenceBound.EQ), uv.getBounds(InferenceBound.UPPER)); |
|
427 } |
|
428 }, |
|
429 BAD_EQ_LOWER() { |
|
430 @Override |
|
431 InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) { |
|
432 return ex.setMessage("incompatible.eq.lower.bounds", uv.qtype, |
|
433 uv.getBounds(InferenceBound.EQ), uv.getBounds(InferenceBound.LOWER)); |
|
434 } |
|
435 }, |
|
436 UPPER() { |
|
437 @Override |
|
438 InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) { |
|
439 return ex.setMessage("inferred.do.not.conform.to.upper.bounds", uv.inst, |
|
440 uv.getBounds(InferenceBound.UPPER)); |
|
441 } |
|
442 }, |
|
443 LOWER() { |
|
444 @Override |
|
445 InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) { |
|
446 return ex.setMessage("inferred.do.not.conform.to.lower.bounds", uv.inst, |
|
447 uv.getBounds(InferenceBound.LOWER)); |
|
448 } |
|
449 }, |
|
450 EQ() { |
|
451 @Override |
|
452 InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) { |
|
453 return ex.setMessage("inferred.do.not.conform.to.eq.bounds", uv.inst, |
|
454 uv.getBounds(InferenceBound.EQ)); |
|
455 } |
|
456 }; |
|
457 |
|
458 abstract InapplicableMethodException setMessage(InferenceException ex, UndetVar uv); |
|
459 } |
|
460 //where |
|
461 void reportBoundError(UndetVar uv, BoundErrorKind bk) { |
|
462 throw bk.setMessage(inferenceException, uv); |
|
463 } |
|
464 |
|
465 // <editor-fold desc="functional interface instantiation"> |
|
466 /** |
|
467 * This method is used to infer a suitable target functional interface in case |
|
468 * the original parameterized interface contains wildcards. An inference process |
|
469 * is applied so that wildcard bounds, as well as explicit lambda/method ref parameters |
|
470 * (where applicable) are used to constraint the solution. |
|
471 */ |
|
472 public Type instantiateFunctionalInterface(DiagnosticPosition pos, Type funcInterface, |
|
473 List<Type> paramTypes, Check.CheckContext checkContext) { |
|
474 if (types.capture(funcInterface) == funcInterface) { |
|
475 //if capture doesn't change the type then return the target unchanged |
|
476 //(this means the target contains no wildcards!) |
|
477 return funcInterface; |
|
478 } else { |
|
479 Type formalInterface = funcInterface.tsym.type; |
|
480 InferenceContext funcInterfaceContext = |
|
481 new InferenceContext(funcInterface.tsym.type.getTypeArguments()); |
|
482 Assert.check(paramTypes != null); |
|
483 //get constraints from explicit params (this is done by |
|
484 //checking that explicit param types are equal to the ones |
|
485 //in the functional interface descriptors) |
|
486 List<Type> descParameterTypes = types.findDescriptorType(formalInterface).getParameterTypes(); |
|
487 if (descParameterTypes.size() != paramTypes.size()) { |
|
488 checkContext.report(pos, diags.fragment("incompatible.arg.types.in.lambda")); |
|
489 return types.createErrorType(funcInterface); |
|
490 } |
|
491 for (Type p : descParameterTypes) { |
|
492 if (!types.isSameType(funcInterfaceContext.asFree(p), paramTypes.head)) { |
|
493 checkContext.report(pos, diags.fragment("no.suitable.functional.intf.inst", funcInterface)); |
|
494 return types.createErrorType(funcInterface); |
|
495 } |
|
496 paramTypes = paramTypes.tail; |
|
497 } |
|
498 List<Type> actualTypeargs = funcInterface.getTypeArguments(); |
|
499 for (Type t : funcInterfaceContext.undetvars) { |
|
500 UndetVar uv = (UndetVar)t; |
|
501 if (funcInterfaceContext.boundedVars().contains(uv.qtype)) { |
|
502 minimizeInst(uv, types.noWarnings); |
|
503 if (uv.inst == null && |
|
504 Type.filter(uv.getBounds(InferenceBound.UPPER), boundFilter).nonEmpty()) { |
|
505 maximizeInst(uv, types.noWarnings); |
|
506 } |
|
507 } else { |
|
508 uv.inst = actualTypeargs.head; |
|
509 } |
|
510 Assert.check(uv.inst != null); |
|
511 actualTypeargs = actualTypeargs.tail; |
|
512 } |
|
513 Type owntype = funcInterfaceContext.asInstType(formalInterface); |
|
514 if (!chk.checkValidGenericType(owntype)) { |
|
515 //if the inferred functional interface type is not well-formed, |
|
516 //or if it's not a subtype of the original target, issue an error |
|
517 checkContext.report(pos, diags.fragment("no.suitable.functional.intf.inst", funcInterface)); |
|
518 } |
|
519 return owntype; |
|
520 } |
|
521 } |
|
522 // </editor-fold> |
|
523 |
289 |
524 /** |
290 /** |
525 * Compute a synthetic method type corresponding to the requested polymorphic |
291 * Compute a synthetic method type corresponding to the requested polymorphic |
526 * method signature. The target return type is computed from the immediately |
292 * method signature. The target return type is computed from the immediately |
527 * enclosing scope surrounding the polymorphic-signature call. |
293 * enclosing scope surrounding the polymorphic-signature call. |
585 return t; |
351 return t; |
586 } |
352 } |
587 } |
353 } |
588 |
354 |
589 /** |
355 /** |
|
356 * This method is used to infer a suitable target SAM in case the original |
|
357 * SAM type contains one or more wildcards. An inference process is applied |
|
358 * so that wildcard bounds, as well as explicit lambda/method ref parameters |
|
359 * (where applicable) are used to constraint the solution. |
|
360 */ |
|
361 public Type instantiateFunctionalInterface(DiagnosticPosition pos, Type funcInterface, |
|
362 List<Type> paramTypes, Check.CheckContext checkContext) { |
|
363 if (types.capture(funcInterface) == funcInterface) { |
|
364 //if capture doesn't change the type then return the target unchanged |
|
365 //(this means the target contains no wildcards!) |
|
366 return funcInterface; |
|
367 } else { |
|
368 Type formalInterface = funcInterface.tsym.type; |
|
369 InferenceContext funcInterfaceContext = |
|
370 new InferenceContext(funcInterface.tsym.type.getTypeArguments()); |
|
371 |
|
372 Assert.check(paramTypes != null); |
|
373 //get constraints from explicit params (this is done by |
|
374 //checking that explicit param types are equal to the ones |
|
375 //in the functional interface descriptors) |
|
376 List<Type> descParameterTypes = types.findDescriptorType(formalInterface).getParameterTypes(); |
|
377 if (descParameterTypes.size() != paramTypes.size()) { |
|
378 checkContext.report(pos, diags.fragment("incompatible.arg.types.in.lambda")); |
|
379 return types.createErrorType(funcInterface); |
|
380 } |
|
381 for (Type p : descParameterTypes) { |
|
382 if (!types.isSameType(funcInterfaceContext.asFree(p), paramTypes.head)) { |
|
383 checkContext.report(pos, diags.fragment("no.suitable.functional.intf.inst", funcInterface)); |
|
384 return types.createErrorType(funcInterface); |
|
385 } |
|
386 paramTypes = paramTypes.tail; |
|
387 } |
|
388 |
|
389 try { |
|
390 funcInterfaceContext.solve(funcInterfaceContext.boundedVars(), types.noWarnings); |
|
391 } catch (InferenceException ex) { |
|
392 checkContext.report(pos, diags.fragment("no.suitable.functional.intf.inst", funcInterface)); |
|
393 } |
|
394 |
|
395 List<Type> actualTypeargs = funcInterface.getTypeArguments(); |
|
396 for (Type t : funcInterfaceContext.undetvars) { |
|
397 UndetVar uv = (UndetVar)t; |
|
398 if (uv.inst == null) { |
|
399 uv.inst = actualTypeargs.head; |
|
400 } |
|
401 actualTypeargs = actualTypeargs.tail; |
|
402 } |
|
403 |
|
404 Type owntype = funcInterfaceContext.asInstType(formalInterface); |
|
405 if (!chk.checkValidGenericType(owntype)) { |
|
406 //if the inferred functional interface type is not well-formed, |
|
407 //or if it's not a subtype of the original target, issue an error |
|
408 checkContext.report(pos, diags.fragment("no.suitable.functional.intf.inst", funcInterface)); |
|
409 } |
|
410 return owntype; |
|
411 } |
|
412 } |
|
413 // </editor-fold> |
|
414 |
|
415 // <editor-fold defaultstate="collapsed" desc="Bound checking"> |
|
416 /** |
|
417 * Check bounds and perform incorporation |
|
418 */ |
|
419 void checkWithinBounds(InferenceContext inferenceContext, |
|
420 Warner warn) throws InferenceException { |
|
421 MultiUndetVarListener mlistener = new MultiUndetVarListener(inferenceContext.undetvars); |
|
422 try { |
|
423 while (true) { |
|
424 mlistener.reset(); |
|
425 if (!allowGraphInference) { |
|
426 //in legacy mode we lack of transitivity, so bound check |
|
427 //cannot be run in parallel with other incoprporation rounds |
|
428 for (Type t : inferenceContext.undetvars) { |
|
429 UndetVar uv = (UndetVar)t; |
|
430 IncorporationStep.CHECK_BOUNDS.apply(uv, inferenceContext, warn); |
|
431 } |
|
432 } |
|
433 for (Type t : inferenceContext.undetvars) { |
|
434 UndetVar uv = (UndetVar)t; |
|
435 //bound incorporation |
|
436 EnumSet<IncorporationStep> incorporationSteps = allowGraphInference ? |
|
437 incorporationStepsGraph : incorporationStepsLegacy; |
|
438 for (IncorporationStep is : incorporationSteps) { |
|
439 is.apply(uv, inferenceContext, warn); |
|
440 } |
|
441 } |
|
442 if (!mlistener.changed || !allowGraphInference) break; |
|
443 } |
|
444 } |
|
445 finally { |
|
446 mlistener.detach(); |
|
447 } |
|
448 } |
|
449 //where |
|
450 /** |
|
451 * This listener keeps track of changes on a group of inference variable |
|
452 * bounds. Note: the listener must be detached (calling corresponding |
|
453 * method) to make sure that the underlying inference variable is |
|
454 * left in a clean state. |
|
455 */ |
|
456 class MultiUndetVarListener implements UndetVar.UndetVarListener { |
|
457 |
|
458 int rounds; |
|
459 boolean changed; |
|
460 List<Type> undetvars; |
|
461 |
|
462 public MultiUndetVarListener(List<Type> undetvars) { |
|
463 this.undetvars = undetvars; |
|
464 for (Type t : undetvars) { |
|
465 UndetVar uv = (UndetVar)t; |
|
466 uv.listener = this; |
|
467 } |
|
468 } |
|
469 |
|
470 public void varChanged(UndetVar uv, Set<InferenceBound> ibs) { |
|
471 //avoid non-termination |
|
472 if (rounds < MAX_INCORPORATION_STEPS) { |
|
473 changed = true; |
|
474 } |
|
475 } |
|
476 |
|
477 void reset() { |
|
478 rounds++; |
|
479 changed = false; |
|
480 } |
|
481 |
|
482 void detach() { |
|
483 for (Type t : undetvars) { |
|
484 UndetVar uv = (UndetVar)t; |
|
485 uv.listener = null; |
|
486 } |
|
487 } |
|
488 }; |
|
489 |
|
490 /** max number of incorporation rounds */ |
|
491 static final int MAX_INCORPORATION_STEPS = 100; |
|
492 |
|
493 /** |
|
494 * This enumeration defines an entry point for doing inference variable |
|
495 * bound incorporation - it can be used to inject custom incorporation |
|
496 * logic into the basic bound checking routine |
|
497 */ |
|
498 enum IncorporationStep { |
|
499 /** |
|
500 * Performs basic bound checking - i.e. is the instantiated type for a given |
|
501 * inference variable compatible with its bounds? |
|
502 */ |
|
503 CHECK_BOUNDS() { |
|
504 public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) { |
|
505 Infer infer = inferenceContext.infer(); |
|
506 uv.substBounds(inferenceContext.inferenceVars(), inferenceContext.instTypes(), infer.types); |
|
507 infer.checkCompatibleUpperBounds(uv, inferenceContext); |
|
508 if (uv.inst != null) { |
|
509 Type inst = uv.inst; |
|
510 for (Type u : uv.getBounds(InferenceBound.UPPER)) { |
|
511 if (!infer.types.isSubtypeUnchecked(inst, inferenceContext.asFree(u), warn)) { |
|
512 infer.reportBoundError(uv, BoundErrorKind.UPPER); |
|
513 } |
|
514 } |
|
515 for (Type l : uv.getBounds(InferenceBound.LOWER)) { |
|
516 if (!infer.types.isSubtypeUnchecked(inferenceContext.asFree(l), inst, warn)) { |
|
517 infer.reportBoundError(uv, BoundErrorKind.LOWER); |
|
518 } |
|
519 } |
|
520 for (Type e : uv.getBounds(InferenceBound.EQ)) { |
|
521 if (!infer.types.isSameType(inst, inferenceContext.asFree(e))) { |
|
522 infer.reportBoundError(uv, BoundErrorKind.EQ); |
|
523 } |
|
524 } |
|
525 } |
|
526 } |
|
527 }, |
|
528 /** |
|
529 * Check consistency of equality constraints. This is a slightly more aggressive |
|
530 * inference routine that is designed as to maximize compatibility with JDK 7. |
|
531 * Note: this is not used in graph mode. |
|
532 */ |
|
533 EQ_CHECK_LEGACY() { |
|
534 public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) { |
|
535 Infer infer = inferenceContext.infer(); |
|
536 Type eq = null; |
|
537 for (Type e : uv.getBounds(InferenceBound.EQ)) { |
|
538 Assert.check(!inferenceContext.free(e)); |
|
539 if (eq != null && !infer.types.isSameType(e, eq)) { |
|
540 infer.reportBoundError(uv, BoundErrorKind.EQ); |
|
541 } |
|
542 eq = e; |
|
543 for (Type l : uv.getBounds(InferenceBound.LOWER)) { |
|
544 Assert.check(!inferenceContext.free(l)); |
|
545 if (!infer.types.isSubtypeUnchecked(l, e, warn)) { |
|
546 infer.reportBoundError(uv, BoundErrorKind.BAD_EQ_LOWER); |
|
547 } |
|
548 } |
|
549 for (Type u : uv.getBounds(InferenceBound.UPPER)) { |
|
550 if (inferenceContext.free(u)) continue; |
|
551 if (!infer.types.isSubtypeUnchecked(e, u, warn)) { |
|
552 infer.reportBoundError(uv, BoundErrorKind.BAD_EQ_UPPER); |
|
553 } |
|
554 } |
|
555 } |
|
556 } |
|
557 }, |
|
558 /** |
|
559 * Check consistency of equality constraints. |
|
560 */ |
|
561 EQ_CHECK() { |
|
562 public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) { |
|
563 Infer infer = inferenceContext.infer(); |
|
564 for (Type e : uv.getBounds(InferenceBound.EQ)) { |
|
565 if (e.containsAny(inferenceContext.inferenceVars())) continue; |
|
566 for (Type u : uv.getBounds(InferenceBound.UPPER)) { |
|
567 if (!infer.types.isSubtypeUnchecked(e, inferenceContext.asFree(u), warn)) { |
|
568 infer.reportBoundError(uv, BoundErrorKind.BAD_EQ_UPPER); |
|
569 } |
|
570 } |
|
571 for (Type l : uv.getBounds(InferenceBound.LOWER)) { |
|
572 if (!infer.types.isSubtypeUnchecked(inferenceContext.asFree(l), e, warn)) { |
|
573 infer.reportBoundError(uv, BoundErrorKind.BAD_EQ_LOWER); |
|
574 } |
|
575 } |
|
576 } |
|
577 } |
|
578 }, |
|
579 /** |
|
580 * Given a bound set containing {@code alpha <: T} and {@code alpha :> S} |
|
581 * perform {@code S <: T} (which could lead to new bounds). |
|
582 */ |
|
583 CROSS_UPPER_LOWER() { |
|
584 public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) { |
|
585 Infer infer = inferenceContext.infer(); |
|
586 for (Type b1 : uv.getBounds(InferenceBound.UPPER)) { |
|
587 for (Type b2 : uv.getBounds(InferenceBound.LOWER)) { |
|
588 if (!inferenceContext.inferenceVars().contains(b1) && |
|
589 !inferenceContext.inferenceVars().contains(b2) && |
|
590 infer.types.asSuper(b2, b1.tsym) != null) { |
|
591 infer.types.isSubtypeUnchecked(inferenceContext.asFree(b2), inferenceContext.asFree(b1)); |
|
592 } |
|
593 } |
|
594 } |
|
595 } |
|
596 }, |
|
597 /** |
|
598 * Given a bound set containing {@code alpha <: T} and {@code alpha == S} |
|
599 * perform {@code S <: T} (which could lead to new bounds). |
|
600 */ |
|
601 CROSS_UPPER_EQ() { |
|
602 public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) { |
|
603 Infer infer = inferenceContext.infer(); |
|
604 for (Type b1 : uv.getBounds(InferenceBound.UPPER)) { |
|
605 for (Type b2 : uv.getBounds(InferenceBound.EQ)) { |
|
606 if (!inferenceContext.inferenceVars().contains(b1) && |
|
607 !inferenceContext.inferenceVars().contains(b2) && |
|
608 infer.types.asSuper(b2, b1.tsym) != null) { |
|
609 infer.types.isSubtypeUnchecked(inferenceContext.asFree(b2), inferenceContext.asFree(b1)); |
|
610 } |
|
611 } |
|
612 } |
|
613 } |
|
614 }, |
|
615 /** |
|
616 * Given a bound set containing {@code alpha :> S} and {@code alpha == T} |
|
617 * perform {@code S <: T} (which could lead to new bounds). |
|
618 */ |
|
619 CROSS_EQ_LOWER() { |
|
620 public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) { |
|
621 Infer infer = inferenceContext.infer(); |
|
622 for (Type b1 : uv.getBounds(InferenceBound.EQ)) { |
|
623 for (Type b2 : uv.getBounds(InferenceBound.LOWER)) { |
|
624 if (!inferenceContext.inferenceVars().contains(b1) && |
|
625 !inferenceContext.inferenceVars().contains(b2) && |
|
626 infer.types.asSuper(b2, b1.tsym) != null) { |
|
627 infer.types.isSubtypeUnchecked(inferenceContext.asFree(b2), inferenceContext.asFree(b1)); |
|
628 } |
|
629 } |
|
630 } |
|
631 } |
|
632 }, |
|
633 /** |
|
634 * Given a bound set containing {@code alpha <: beta} propagate lower bounds |
|
635 * from alpha to beta; also propagate upper bounds from beta to alpha. |
|
636 */ |
|
637 PROP_UPPER() { |
|
638 public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) { |
|
639 Infer infer = inferenceContext.infer(); |
|
640 for (Type b : uv.getBounds(InferenceBound.UPPER)) { |
|
641 if (inferenceContext.inferenceVars().contains(b)) { |
|
642 UndetVar uv2 = (UndetVar)inferenceContext.asFree(b); |
|
643 //alpha <: beta |
|
644 //1. copy alpha's lower to beta's |
|
645 for (Type l : uv.getBounds(InferenceBound.LOWER)) { |
|
646 uv2.addBound(InferenceBound.LOWER, inferenceContext.asInstType(l), infer.types); |
|
647 } |
|
648 //2. copy beta's upper to alpha's |
|
649 for (Type u : uv2.getBounds(InferenceBound.UPPER)) { |
|
650 uv.addBound(InferenceBound.UPPER, inferenceContext.asInstType(u), infer.types); |
|
651 } |
|
652 } |
|
653 } |
|
654 } |
|
655 }, |
|
656 /** |
|
657 * Given a bound set containing {@code alpha :> beta} propagate lower bounds |
|
658 * from beta to alpha; also propagate upper bounds from alpha to beta. |
|
659 */ |
|
660 PROP_LOWER() { |
|
661 public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) { |
|
662 Infer infer = inferenceContext.infer(); |
|
663 for (Type b : uv.getBounds(InferenceBound.LOWER)) { |
|
664 if (inferenceContext.inferenceVars().contains(b)) { |
|
665 UndetVar uv2 = (UndetVar)inferenceContext.asFree(b); |
|
666 //alpha :> beta |
|
667 //1. copy alpha's upper to beta's |
|
668 for (Type u : uv.getBounds(InferenceBound.UPPER)) { |
|
669 uv2.addBound(InferenceBound.UPPER, inferenceContext.asInstType(u), infer.types); |
|
670 } |
|
671 //2. copy beta's lower to alpha's |
|
672 for (Type l : uv2.getBounds(InferenceBound.LOWER)) { |
|
673 uv.addBound(InferenceBound.LOWER, inferenceContext.asInstType(l), infer.types); |
|
674 } |
|
675 } |
|
676 } |
|
677 } |
|
678 }, |
|
679 /** |
|
680 * Given a bound set containing {@code alpha == beta} propagate lower/upper |
|
681 * bounds from alpha to beta and back. |
|
682 */ |
|
683 PROP_EQ() { |
|
684 public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) { |
|
685 Infer infer = inferenceContext.infer(); |
|
686 for (Type b : uv.getBounds(InferenceBound.EQ)) { |
|
687 if (inferenceContext.inferenceVars().contains(b)) { |
|
688 UndetVar uv2 = (UndetVar)inferenceContext.asFree(b); |
|
689 //alpha == beta |
|
690 //1. copy all alpha's bounds to beta's |
|
691 for (InferenceBound ib : InferenceBound.values()) { |
|
692 for (Type b2 : uv.getBounds(ib)) { |
|
693 if (b2 != uv2) { |
|
694 uv2.addBound(ib, inferenceContext.asInstType(b2), infer.types); |
|
695 } |
|
696 } |
|
697 } |
|
698 //2. copy all beta's bounds to alpha's |
|
699 for (InferenceBound ib : InferenceBound.values()) { |
|
700 for (Type b2 : uv2.getBounds(ib)) { |
|
701 if (b2 != uv) { |
|
702 uv.addBound(ib, inferenceContext.asInstType(b2), infer.types); |
|
703 } |
|
704 } |
|
705 } |
|
706 } |
|
707 } |
|
708 } |
|
709 }; |
|
710 |
|
711 abstract void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn); |
|
712 } |
|
713 |
|
714 /** incorporation steps to be executed when running in legacy mode */ |
|
715 EnumSet<IncorporationStep> incorporationStepsLegacy = EnumSet.of(IncorporationStep.EQ_CHECK_LEGACY); |
|
716 |
|
717 /** incorporation steps to be executed when running in graph mode */ |
|
718 EnumSet<IncorporationStep> incorporationStepsGraph = |
|
719 EnumSet.complementOf(EnumSet.of(IncorporationStep.EQ_CHECK_LEGACY)); |
|
720 |
|
721 /** |
|
722 * Make sure that the upper bounds we got so far lead to a solvable inference |
|
723 * variable by making sure that a glb exists. |
|
724 */ |
|
725 void checkCompatibleUpperBounds(UndetVar uv, InferenceContext inferenceContext) { |
|
726 List<Type> hibounds = |
|
727 Type.filter(uv.getBounds(InferenceBound.UPPER), new BoundFilter(inferenceContext)); |
|
728 Type hb = null; |
|
729 if (hibounds.isEmpty()) |
|
730 hb = syms.objectType; |
|
731 else if (hibounds.tail.isEmpty()) |
|
732 hb = hibounds.head; |
|
733 else |
|
734 hb = types.glb(hibounds); |
|
735 if (hb == null || hb.isErroneous()) |
|
736 reportBoundError(uv, BoundErrorKind.BAD_UPPER); |
|
737 } |
|
738 //where |
|
739 protected static class BoundFilter implements Filter<Type> { |
|
740 |
|
741 InferenceContext inferenceContext; |
|
742 |
|
743 public BoundFilter(InferenceContext inferenceContext) { |
|
744 this.inferenceContext = inferenceContext; |
|
745 } |
|
746 |
|
747 @Override |
|
748 public boolean accepts(Type t) { |
|
749 return !t.isErroneous() && !inferenceContext.free(t) && |
|
750 !t.hasTag(BOT); |
|
751 } |
|
752 }; |
|
753 |
|
754 /** |
|
755 * This enumeration defines all possible bound-checking related errors. |
|
756 */ |
|
757 enum BoundErrorKind { |
|
758 /** |
|
759 * The (uninstantiated) inference variable has incompatible upper bounds. |
|
760 */ |
|
761 BAD_UPPER() { |
|
762 @Override |
|
763 InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) { |
|
764 return ex.setMessage("incompatible.upper.bounds", uv.qtype, |
|
765 uv.getBounds(InferenceBound.UPPER)); |
|
766 } |
|
767 }, |
|
768 /** |
|
769 * An equality constraint is not compatible with an upper bound. |
|
770 */ |
|
771 BAD_EQ_UPPER() { |
|
772 @Override |
|
773 InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) { |
|
774 return ex.setMessage("incompatible.eq.upper.bounds", uv.qtype, |
|
775 uv.getBounds(InferenceBound.EQ), uv.getBounds(InferenceBound.UPPER)); |
|
776 } |
|
777 }, |
|
778 /** |
|
779 * An equality constraint is not compatible with a lower bound. |
|
780 */ |
|
781 BAD_EQ_LOWER() { |
|
782 @Override |
|
783 InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) { |
|
784 return ex.setMessage("incompatible.eq.lower.bounds", uv.qtype, |
|
785 uv.getBounds(InferenceBound.EQ), uv.getBounds(InferenceBound.LOWER)); |
|
786 } |
|
787 }, |
|
788 /** |
|
789 * Instantiated inference variable is not compatible with an upper bound. |
|
790 */ |
|
791 UPPER() { |
|
792 @Override |
|
793 InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) { |
|
794 return ex.setMessage("inferred.do.not.conform.to.upper.bounds", uv.inst, |
|
795 uv.getBounds(InferenceBound.UPPER)); |
|
796 } |
|
797 }, |
|
798 /** |
|
799 * Instantiated inference variable is not compatible with a lower bound. |
|
800 */ |
|
801 LOWER() { |
|
802 @Override |
|
803 InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) { |
|
804 return ex.setMessage("inferred.do.not.conform.to.lower.bounds", uv.inst, |
|
805 uv.getBounds(InferenceBound.LOWER)); |
|
806 } |
|
807 }, |
|
808 /** |
|
809 * Instantiated inference variable is not compatible with an equality constraint. |
|
810 */ |
|
811 EQ() { |
|
812 @Override |
|
813 InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) { |
|
814 return ex.setMessage("inferred.do.not.conform.to.eq.bounds", uv.inst, |
|
815 uv.getBounds(InferenceBound.EQ)); |
|
816 } |
|
817 }; |
|
818 |
|
819 abstract InapplicableMethodException setMessage(InferenceException ex, UndetVar uv); |
|
820 } |
|
821 |
|
822 /** |
|
823 * Report a bound-checking error of given kind |
|
824 */ |
|
825 void reportBoundError(UndetVar uv, BoundErrorKind bk) { |
|
826 throw bk.setMessage(inferenceException, uv); |
|
827 } |
|
828 // </editor-fold> |
|
829 |
|
830 // <editor-fold defaultstate="collapsed" desc="Inference engine"> |
|
831 /** |
|
832 * Graph inference strategy - act as an input to the inference solver; a strategy is |
|
833 * composed of two ingredients: (i) find a node to solve in the inference graph, |
|
834 * and (ii) tell th engine when we are done fixing inference variables |
|
835 */ |
|
836 interface GraphStrategy { |
|
837 /** |
|
838 * Pick the next node (leaf) to solve in the graph |
|
839 */ |
|
840 Node pickNode(InferenceGraph g); |
|
841 /** |
|
842 * Is this the last step? |
|
843 */ |
|
844 boolean done(); |
|
845 } |
|
846 |
|
847 /** |
|
848 * Simple solver strategy class that locates all leaves inside a graph |
|
849 * and picks the first leaf as the next node to solve |
|
850 */ |
|
851 abstract class LeafSolver implements GraphStrategy { |
|
852 public Node pickNode(InferenceGraph g) { |
|
853 Assert.check(!g.nodes.isEmpty(), "No nodes to solve!"); |
|
854 return g.nodes.get(0); |
|
855 } |
|
856 } |
|
857 |
|
858 /** |
|
859 * This solver uses an heuristic to pick the best leaf - the heuristic |
|
860 * tries to select the node that has maximal probability to contain one |
|
861 * or more inference variables in a given list |
|
862 */ |
|
863 abstract class BestLeafSolver extends LeafSolver { |
|
864 |
|
865 List<Type> varsToSolve; |
|
866 |
|
867 BestLeafSolver(List<Type> varsToSolve) { |
|
868 this.varsToSolve = varsToSolve; |
|
869 } |
|
870 |
|
871 /** |
|
872 * Computes the cost associated with a given node; the cost is computed |
|
873 * as the total number of type-variables that should be eagerly instantiated |
|
874 * in order to get to some of the variables in {@code varsToSolve} from |
|
875 * a given node |
|
876 */ |
|
877 void computeCostIfNeeded(Node n, Map<Node, Integer> costMap) { |
|
878 if (costMap.containsKey(n)) { |
|
879 return; |
|
880 } else if (!Collections.disjoint(n.data, varsToSolve)) { |
|
881 costMap.put(n, n.data.size()); |
|
882 } else { |
|
883 int subcost = Integer.MAX_VALUE; |
|
884 costMap.put(n, subcost); //avoid loops |
|
885 for (Node n2 : n.getDependencies()) { |
|
886 computeCostIfNeeded(n2, costMap); |
|
887 subcost = Math.min(costMap.get(n2), subcost); |
|
888 } |
|
889 //update cost map to reflect real cost |
|
890 costMap.put(n, subcost == Integer.MAX_VALUE ? |
|
891 Integer.MAX_VALUE : |
|
892 n.data.size() + subcost); |
|
893 } |
|
894 } |
|
895 |
|
896 /** |
|
897 * Pick the leaf that minimize cost |
|
898 */ |
|
899 @Override |
|
900 public Node pickNode(final InferenceGraph g) { |
|
901 final Map<Node, Integer> costMap = new HashMap<Node, Integer>(); |
|
902 ArrayList<Node> leaves = new ArrayList<Node>(); |
|
903 for (Node n : g.nodes) { |
|
904 computeCostIfNeeded(n, costMap); |
|
905 if (n.isLeaf(n)) { |
|
906 leaves.add(n); |
|
907 } |
|
908 } |
|
909 Assert.check(!leaves.isEmpty(), "No nodes to solve!"); |
|
910 Collections.sort(leaves, new java.util.Comparator<Node>() { |
|
911 public int compare(Node n1, Node n2) { |
|
912 return costMap.get(n1) - costMap.get(n2); |
|
913 } |
|
914 }); |
|
915 return leaves.get(0); |
|
916 } |
|
917 } |
|
918 |
|
919 /** |
|
920 * The inference process can be thought of as a sequence of steps. Each step |
|
921 * instantiates an inference variable using a subset of the inference variable |
|
922 * bounds, if certain condition are met. Decisions such as the sequence in which |
|
923 * steps are applied, or which steps are to be applied are left to the inference engine. |
|
924 */ |
|
925 enum InferenceStep { |
|
926 |
|
927 /** |
|
928 * Instantiate an inference variables using one of its (ground) equality |
|
929 * constraints |
|
930 */ |
|
931 EQ(InferenceBound.EQ) { |
|
932 @Override |
|
933 Type solve(UndetVar uv, InferenceContext inferenceContext) { |
|
934 return filterBounds(uv, inferenceContext).head; |
|
935 } |
|
936 }, |
|
937 /** |
|
938 * Instantiate an inference variables using its (ground) lower bounds. Such |
|
939 * bounds are merged together using lub(). |
|
940 */ |
|
941 LOWER(InferenceBound.LOWER) { |
|
942 @Override |
|
943 Type solve(UndetVar uv, InferenceContext inferenceContext) { |
|
944 Infer infer = inferenceContext.infer(); |
|
945 List<Type> lobounds = filterBounds(uv, inferenceContext); |
|
946 Type owntype = infer.types.lub(lobounds); |
|
947 if (owntype.hasTag(ERROR)) { |
|
948 throw infer.inferenceException |
|
949 .setMessage("no.unique.minimal.instance.exists", |
|
950 uv.qtype, lobounds); |
|
951 } else { |
|
952 return owntype; |
|
953 } |
|
954 } |
|
955 }, |
|
956 /** |
|
957 * Instantiate an inference variables using its (ground) upper bounds. Such |
|
958 * bounds are merged together using glb(). |
|
959 */ |
|
960 UPPER(InferenceBound.UPPER) { |
|
961 @Override |
|
962 Type solve(UndetVar uv, InferenceContext inferenceContext) { |
|
963 Infer infer = inferenceContext.infer(); |
|
964 List<Type> hibounds = filterBounds(uv, inferenceContext); |
|
965 Type owntype = infer.types.glb(hibounds); |
|
966 if (owntype.isErroneous()) { |
|
967 throw infer.inferenceException |
|
968 .setMessage("no.unique.maximal.instance.exists", |
|
969 uv.qtype, hibounds); |
|
970 } else { |
|
971 return owntype; |
|
972 } |
|
973 } |
|
974 }, |
|
975 /** |
|
976 * Like the former; the only difference is that this step can only be applied |
|
977 * if all upper bounds are ground. |
|
978 */ |
|
979 UPPER_LEGACY(InferenceBound.UPPER) { |
|
980 @Override |
|
981 public boolean accepts(UndetVar t, InferenceContext inferenceContext) { |
|
982 return !inferenceContext.free(t.getBounds(ib)); |
|
983 } |
|
984 |
|
985 @Override |
|
986 Type solve(UndetVar uv, InferenceContext inferenceContext) { |
|
987 return UPPER.solve(uv, inferenceContext); |
|
988 } |
|
989 }; |
|
990 |
|
991 final InferenceBound ib; |
|
992 |
|
993 InferenceStep(InferenceBound ib) { |
|
994 this.ib = ib; |
|
995 } |
|
996 |
|
997 /** |
|
998 * Find an instantiated type for a given inference variable within |
|
999 * a given inference context |
|
1000 */ |
|
1001 abstract Type solve(UndetVar uv, InferenceContext inferenceContext); |
|
1002 |
|
1003 /** |
|
1004 * Can the inference variable be instantiated using this step? |
|
1005 */ |
|
1006 public boolean accepts(UndetVar t, InferenceContext inferenceContext) { |
|
1007 return filterBounds(t, inferenceContext).nonEmpty(); |
|
1008 } |
|
1009 |
|
1010 /** |
|
1011 * Return the subset of ground bounds in a given bound set (i.e. eq/lower/upper) |
|
1012 */ |
|
1013 List<Type> filterBounds(UndetVar uv, InferenceContext inferenceContext) { |
|
1014 return Type.filter(uv.getBounds(ib), new BoundFilter(inferenceContext)); |
|
1015 } |
|
1016 } |
|
1017 |
|
1018 /** |
|
1019 * This enumeration defines the sequence of steps to be applied when the |
|
1020 * solver works in legacy mode. The steps in this enumeration reflect |
|
1021 * the behavior of old inference routine (see JLS SE 7 15.12.2.7/15.12.2.8). |
|
1022 */ |
|
1023 enum LegacyInferenceSteps { |
|
1024 |
|
1025 EQ_LOWER(EnumSet.of(InferenceStep.EQ, InferenceStep.LOWER)), |
|
1026 EQ_UPPER(EnumSet.of(InferenceStep.EQ, InferenceStep.UPPER_LEGACY)); |
|
1027 |
|
1028 final EnumSet<InferenceStep> steps; |
|
1029 |
|
1030 LegacyInferenceSteps(EnumSet<InferenceStep> steps) { |
|
1031 this.steps = steps; |
|
1032 } |
|
1033 } |
|
1034 |
|
1035 /** |
|
1036 * This enumeration defines the sequence of steps to be applied when the |
|
1037 * graph solver is used. This order is defined so as to maximize compatibility |
|
1038 * w.r.t. old inference routine (see JLS SE 7 15.12.2.7/15.12.2.8). |
|
1039 */ |
|
1040 enum GraphInferenceSteps { |
|
1041 |
|
1042 EQ(EnumSet.of(InferenceStep.EQ)), |
|
1043 EQ_LOWER(EnumSet.of(InferenceStep.EQ, InferenceStep.LOWER)), |
|
1044 EQ_LOWER_UPPER(EnumSet.of(InferenceStep.EQ, InferenceStep.LOWER, InferenceStep.UPPER)); |
|
1045 |
|
1046 final EnumSet<InferenceStep> steps; |
|
1047 |
|
1048 GraphInferenceSteps(EnumSet<InferenceStep> steps) { |
|
1049 this.steps = steps; |
|
1050 } |
|
1051 } |
|
1052 |
|
1053 /** |
|
1054 * This is the graph inference solver - the solver organizes all inference variables in |
|
1055 * a given inference context by bound dependencies - in the general case, such dependencies |
|
1056 * would lead to a cyclic directed graph (hence the name); the dependency info is used to build |
|
1057 * an acyclic graph, where all cyclic variables are bundled together. An inference |
|
1058 * step corresponds to solving a node in the acyclic graph - this is done by |
|
1059 * relying on a given strategy (see GraphStrategy). |
|
1060 */ |
|
1061 class GraphSolver { |
|
1062 |
|
1063 InferenceContext inferenceContext; |
|
1064 Warner warn; |
|
1065 |
|
1066 GraphSolver(InferenceContext inferenceContext, Warner warn) { |
|
1067 this.inferenceContext = inferenceContext; |
|
1068 this.warn = warn; |
|
1069 } |
|
1070 |
|
1071 /** |
|
1072 * Solve variables in a given inference context. The amount of variables |
|
1073 * to be solved, and the way in which the underlying acyclic graph is explored |
|
1074 * depends on the selected solver strategy. |
|
1075 */ |
|
1076 void solve(GraphStrategy sstrategy) { |
|
1077 checkWithinBounds(inferenceContext, warn); //initial propagation of bounds |
|
1078 InferenceGraph inferenceGraph = new InferenceGraph(); |
|
1079 while (!sstrategy.done()) { |
|
1080 InferenceGraph.Node nodeToSolve = sstrategy.pickNode(inferenceGraph); |
|
1081 List<Type> varsToSolve = List.from(nodeToSolve.data); |
|
1082 inferenceContext.save(); |
|
1083 try { |
|
1084 //repeat until all variables are solved |
|
1085 outer: while (Type.containsAny(inferenceContext.restvars(), varsToSolve)) { |
|
1086 //for each inference phase |
|
1087 for (GraphInferenceSteps step : GraphInferenceSteps.values()) { |
|
1088 if (inferenceContext.solveBasic(varsToSolve, step.steps)) { |
|
1089 checkWithinBounds(inferenceContext, warn); |
|
1090 continue outer; |
|
1091 } |
|
1092 } |
|
1093 //no progress |
|
1094 throw inferenceException; |
|
1095 } |
|
1096 } |
|
1097 catch (InferenceException ex) { |
|
1098 inferenceContext.rollback(); |
|
1099 instantiateAsUninferredVars(varsToSolve, inferenceContext); |
|
1100 checkWithinBounds(inferenceContext, warn); |
|
1101 } |
|
1102 inferenceGraph.deleteNode(nodeToSolve); |
|
1103 } |
|
1104 } |
|
1105 |
|
1106 /** |
|
1107 * The dependencies between the inference variables that need to be solved |
|
1108 * form a (possibly cyclic) graph. This class reduces the original dependency graph |
|
1109 * to an acyclic version, where cyclic nodes are folded into a single 'super node'. |
|
1110 */ |
|
1111 class InferenceGraph { |
|
1112 |
|
1113 /** |
|
1114 * This class represents a node in the graph. Each node corresponds |
|
1115 * to an inference variable and has edges (dependencies) on other |
|
1116 * nodes. The node defines an entry point that can be used to receive |
|
1117 * updates on the structure of the graph this node belongs to (used to |
|
1118 * keep dependencies in sync). |
|
1119 */ |
|
1120 class Node extends GraphUtils.TarjanNode<ListBuffer<Type>> { |
|
1121 |
|
1122 Set<Node> deps; |
|
1123 |
|
1124 Node(Type ivar) { |
|
1125 super(ListBuffer.of(ivar)); |
|
1126 this.deps = new HashSet<Node>(); |
|
1127 } |
|
1128 |
|
1129 @Override |
|
1130 public Iterable<? extends Node> getDependencies() { |
|
1131 return deps; |
|
1132 } |
|
1133 |
|
1134 @Override |
|
1135 public String printDependency(GraphUtils.Node<ListBuffer<Type>> to) { |
|
1136 StringBuilder buf = new StringBuilder(); |
|
1137 String sep = ""; |
|
1138 for (Type from : data) { |
|
1139 UndetVar uv = (UndetVar)inferenceContext.asFree(from); |
|
1140 for (Type bound : uv.getBounds(InferenceBound.values())) { |
|
1141 if (bound.containsAny(List.from(to.data))) { |
|
1142 buf.append(sep); |
|
1143 buf.append(bound); |
|
1144 sep = ","; |
|
1145 } |
|
1146 } |
|
1147 } |
|
1148 return buf.toString(); |
|
1149 } |
|
1150 |
|
1151 boolean isLeaf(Node n) { |
|
1152 //no deps, or only one self dep |
|
1153 return (n.deps.isEmpty() || |
|
1154 n.deps.size() == 1 && n.deps.contains(n)); |
|
1155 } |
|
1156 |
|
1157 void mergeWith(List<? extends Node> nodes) { |
|
1158 for (Node n : nodes) { |
|
1159 Assert.check(n.data.length() == 1, "Attempt to merge a compound node!"); |
|
1160 data.appendList(n.data); |
|
1161 deps.addAll(n.deps); |
|
1162 } |
|
1163 //update deps |
|
1164 Set<Node> deps2 = new HashSet<Node>(); |
|
1165 for (Node d : deps) { |
|
1166 if (data.contains(d.data.first())) { |
|
1167 deps2.add(this); |
|
1168 } else { |
|
1169 deps2.add(d); |
|
1170 } |
|
1171 } |
|
1172 deps = deps2; |
|
1173 } |
|
1174 |
|
1175 void graphChanged(Node from, Node to) { |
|
1176 if (deps.contains(from)) { |
|
1177 deps.remove(from); |
|
1178 if (to != null) { |
|
1179 deps.add(to); |
|
1180 } |
|
1181 } |
|
1182 } |
|
1183 } |
|
1184 |
|
1185 /** the nodes in the inference graph */ |
|
1186 ArrayList<Node> nodes; |
|
1187 |
|
1188 InferenceGraph() { |
|
1189 initNodes(); |
|
1190 } |
|
1191 |
|
1192 /** |
|
1193 * Delete a node from the graph. This update the underlying structure |
|
1194 * of the graph (including dependencies) via listeners updates. |
|
1195 */ |
|
1196 public void deleteNode(Node n) { |
|
1197 Assert.check(nodes.contains(n)); |
|
1198 nodes.remove(n); |
|
1199 notifyUpdate(n, null); |
|
1200 } |
|
1201 |
|
1202 /** |
|
1203 * Notify all nodes of a change in the graph. If the target node is |
|
1204 * {@code null} the source node is assumed to be removed. |
|
1205 */ |
|
1206 void notifyUpdate(Node from, Node to) { |
|
1207 for (Node n : nodes) { |
|
1208 n.graphChanged(from, to); |
|
1209 } |
|
1210 } |
|
1211 |
|
1212 /** |
|
1213 * Create the graph nodes. First a simple node is created for every inference |
|
1214 * variables to be solved. Then Tarjan is used to found all connected components |
|
1215 * in the graph. For each component containing more than one node, a super node is |
|
1216 * created, effectively replacing the original cyclic nodes. |
|
1217 */ |
|
1218 void initNodes() { |
|
1219 ArrayList<Node> nodes = new ArrayList<Node>(); |
|
1220 for (Type t : inferenceContext.restvars()) { |
|
1221 nodes.add(new Node(t)); |
|
1222 } |
|
1223 for (Node n_i : nodes) { |
|
1224 Type i = n_i.data.first(); |
|
1225 for (Node n_j : nodes) { |
|
1226 Type j = n_j.data.first(); |
|
1227 UndetVar uv_i = (UndetVar)inferenceContext.asFree(i); |
|
1228 if (Type.containsAny(uv_i.getBounds(InferenceBound.values()), List.of(j))) { |
|
1229 //update i's deps |
|
1230 n_i.deps.add(n_j); |
|
1231 //update j's deps - only if i's bounds contain _exactly_ j |
|
1232 if (uv_i.getBounds(InferenceBound.values()).contains(j)) { |
|
1233 n_j.deps.add(n_i); |
|
1234 } |
|
1235 } |
|
1236 } |
|
1237 } |
|
1238 this.nodes = new ArrayList<Node>(); |
|
1239 for (List<? extends Node> conSubGraph : GraphUtils.tarjan(nodes)) { |
|
1240 if (conSubGraph.length() > 1) { |
|
1241 Node root = conSubGraph.head; |
|
1242 root.mergeWith(conSubGraph.tail); |
|
1243 for (Node n : conSubGraph) { |
|
1244 notifyUpdate(n, root); |
|
1245 } |
|
1246 } |
|
1247 this.nodes.add(conSubGraph.head); |
|
1248 } |
|
1249 } |
|
1250 |
|
1251 /** |
|
1252 * Debugging: dot representation of this graph |
|
1253 */ |
|
1254 String toDot() { |
|
1255 StringBuilder buf = new StringBuilder(); |
|
1256 for (Type t : inferenceContext.undetvars) { |
|
1257 UndetVar uv = (UndetVar)t; |
|
1258 buf.append(String.format("var %s - upper bounds = %s, lower bounds = %s, eq bounds = %s\\n", |
|
1259 uv.qtype, uv.getBounds(InferenceBound.UPPER), uv.getBounds(InferenceBound.LOWER), |
|
1260 uv.getBounds(InferenceBound.EQ))); |
|
1261 } |
|
1262 return GraphUtils.toDot(nodes, "inferenceGraph" + hashCode(), buf.toString()); |
|
1263 } |
|
1264 } |
|
1265 } |
|
1266 // </editor-fold> |
|
1267 |
|
1268 // <editor-fold defaultstate="collapsed" desc="Inference context"> |
|
1269 /** |
590 * Functional interface for defining inference callbacks. Certain actions |
1270 * Functional interface for defining inference callbacks. Certain actions |
591 * (i.e. subtyping checks) might need to be redone after all inference variables |
1271 * (i.e. subtyping checks) might need to be redone after all inference variables |
592 * have been fixed. |
1272 * have been fixed. |
593 */ |
1273 */ |
594 interface FreeTypeListener { |
1274 interface FreeTypeListener { |