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

changeset 1114
05814303a056
parent 1087
3a2200681d69
child 1127
ca49d50318dc
equal deleted inserted replaced
1113:d346ab55031b 1114:05814303a056
32 import com.sun.tools.javac.util.List; 32 import com.sun.tools.javac.util.List;
33 import com.sun.tools.javac.code.*; 33 import com.sun.tools.javac.code.*;
34 import com.sun.tools.javac.code.Type.*; 34 import com.sun.tools.javac.code.Type.*;
35 import com.sun.tools.javac.code.Type.ForAll.ConstraintKind; 35 import com.sun.tools.javac.code.Type.ForAll.ConstraintKind;
36 import com.sun.tools.javac.code.Symbol.*; 36 import com.sun.tools.javac.code.Symbol.*;
37 import com.sun.tools.javac.util.JCDiagnostic; 37 import com.sun.tools.javac.comp.Resolve.VerboseResolutionMode;
38 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
38 39
39 import static com.sun.tools.javac.code.TypeTags.*; 40 import static com.sun.tools.javac.code.TypeTags.*;
40 41
41 /** Helper class for type parameter inference, used by the attribution phase. 42 /** Helper class for type parameter inference, used by the attribution phase.
42 * 43 *
54 55
55 Symtab syms; 56 Symtab syms;
56 Types types; 57 Types types;
57 Check chk; 58 Check chk;
58 Resolve rs; 59 Resolve rs;
60 Log log;
59 JCDiagnostic.Factory diags; 61 JCDiagnostic.Factory diags;
60 62
61 public static Infer instance(Context context) { 63 public static Infer instance(Context context) {
62 Infer instance = context.get(inferKey); 64 Infer instance = context.get(inferKey);
63 if (instance == null) 65 if (instance == null)
68 protected Infer(Context context) { 70 protected Infer(Context context) {
69 context.put(inferKey, this); 71 context.put(inferKey, this);
70 syms = Symtab.instance(context); 72 syms = Symtab.instance(context);
71 types = Types.instance(context); 73 types = Types.instance(context);
72 rs = Resolve.instance(context); 74 rs = Resolve.instance(context);
75 log = Log.instance(context);
73 chk = Check.instance(context); 76 chk = Check.instance(context);
74 diags = JCDiagnostic.Factory.instance(context); 77 diags = JCDiagnostic.Factory.instance(context);
75 ambiguousNoInstanceException = 78 ambiguousNoInstanceException =
76 new NoInstanceException(true, diags); 79 new NoInstanceException(true, diags);
77 unambiguousNoInstanceException = 80 unambiguousNoInstanceException =
458 if (!restvars.isEmpty()) { 461 if (!restvars.isEmpty()) {
459 // if there are uninstantiated variables, 462 // if there are uninstantiated variables,
460 // quantify result type with them 463 // quantify result type with them
461 final List<Type> inferredTypes = insttypes.toList(); 464 final List<Type> inferredTypes = insttypes.toList();
462 final List<Type> all_tvars = tvars; //this is the wrong tvars 465 final List<Type> all_tvars = tvars; //this is the wrong tvars
463 return new UninferredMethodType(mt, restvars.toList()) { 466 return new UninferredMethodType(env.tree.pos(), msym, mt, restvars.toList()) {
464 @Override 467 @Override
465 List<Type> getConstraints(TypeVar tv, ConstraintKind ck) { 468 List<Type> getConstraints(TypeVar tv, ConstraintKind ck) {
466 for (Type t : restundet.toList()) { 469 for (Type t : restundet.toList()) {
467 UndetVar uv = (UndetVar)t; 470 UndetVar uv = (UndetVar)t;
468 if (uv.qtype == tv) { 471 if (uv.qtype == tv) {
500 * A delegated type representing a partially uninferred method type. 503 * A delegated type representing a partially uninferred method type.
501 * The return type of a partially uninferred method type is a ForAll 504 * The return type of a partially uninferred method type is a ForAll
502 * type - when the return type is instantiated (see Infer.instantiateExpr) 505 * type - when the return type is instantiated (see Infer.instantiateExpr)
503 * the underlying method type is also updated. 506 * the underlying method type is also updated.
504 */ 507 */
505 static abstract class UninferredMethodType extends DelegatedType { 508 abstract class UninferredMethodType extends DelegatedType {
506 509
507 final List<Type> tvars; 510 final List<Type> tvars;
508 511 final Symbol msym;
509 public UninferredMethodType(MethodType mtype, List<Type> tvars) { 512 final DiagnosticPosition pos;
513
514 public UninferredMethodType(DiagnosticPosition pos, Symbol msym, MethodType mtype, List<Type> tvars) {
510 super(METHOD, new MethodType(mtype.argtypes, null, mtype.thrown, mtype.tsym)); 515 super(METHOD, new MethodType(mtype.argtypes, null, mtype.thrown, mtype.tsym));
511 this.tvars = tvars; 516 this.tvars = tvars;
517 this.msym = msym;
518 this.pos = pos;
512 asMethodType().restype = new UninferredReturnType(tvars, mtype.restype); 519 asMethodType().restype = new UninferredReturnType(tvars, mtype.restype);
513 } 520 }
514 521
515 @Override 522 @Override
516 public MethodType asMethodType() { 523 public MethodType asMethodType() {
541 } 548 }
542 @Override 549 @Override
543 public Type inst(List<Type> actuals, Types types) { 550 public Type inst(List<Type> actuals, Types types) {
544 Type newRestype = super.inst(actuals, types); 551 Type newRestype = super.inst(actuals, types);
545 instantiateReturnType(newRestype, actuals, types); 552 instantiateReturnType(newRestype, actuals, types);
553 if (rs.verboseResolutionMode.contains(VerboseResolutionMode.DEFERRED_INST)) {
554 log.note(pos, "deferred.method.inst", msym, UninferredMethodType.this.qtype, newRestype);
555 }
546 return newRestype; 556 return newRestype;
547 } 557 }
548 @Override 558 @Override
549 public List<Type> getConstraints(TypeVar tv, ConstraintKind ck) { 559 public List<Type> getConstraints(TypeVar tv, ConstraintKind ck) {
550 return UninferredMethodType.this.getConstraints(tv, ck); 560 return UninferredMethodType.this.getConstraints(tv, ck);

mercurial