langtools/src/share/classes/com/sun/tools/javac/comp/Infer.java
changeset 14057 b4b0377b8dba
parent 14048 308d1cf8fe46
child 14058 c7ec7facdd20
equal deleted inserted replaced
14056:0ea78d6e0b7b 14057:b4b0377b8dba
    27 
    27 
    28 import com.sun.tools.javac.code.*;
    28 import com.sun.tools.javac.code.*;
    29 import com.sun.tools.javac.code.Symbol.*;
    29 import com.sun.tools.javac.code.Symbol.*;
    30 import com.sun.tools.javac.code.Type.*;
    30 import com.sun.tools.javac.code.Type.*;
    31 import com.sun.tools.javac.code.Type.UndetVar.InferenceBound;
    31 import com.sun.tools.javac.code.Type.UndetVar.InferenceBound;
       
    32 import com.sun.tools.javac.comp.DeferredAttr.AttrMode;
    32 import com.sun.tools.javac.comp.Resolve.InapplicableMethodException;
    33 import com.sun.tools.javac.comp.Resolve.InapplicableMethodException;
    33 import com.sun.tools.javac.comp.Resolve.VerboseResolutionMode;
    34 import com.sun.tools.javac.comp.Resolve.VerboseResolutionMode;
    34 import com.sun.tools.javac.tree.JCTree;
    35 import com.sun.tools.javac.tree.JCTree;
    35 import com.sun.tools.javac.tree.JCTree.JCTypeCast;
    36 import com.sun.tools.javac.tree.JCTree.JCTypeCast;
    36 import com.sun.tools.javac.tree.TreeInfo;
    37 import com.sun.tools.javac.tree.TreeInfo;
    60 
    61 
    61     Symtab syms;
    62     Symtab syms;
    62     Types types;
    63     Types types;
    63     Check chk;
    64     Check chk;
    64     Resolve rs;
    65     Resolve rs;
       
    66     DeferredAttr deferredAttr;
    65     Log log;
    67     Log log;
    66     JCDiagnostic.Factory diags;
    68     JCDiagnostic.Factory diags;
    67 
    69 
    68     public static Infer instance(Context context) {
    70     public static Infer instance(Context context) {
    69         Infer instance = context.get(inferKey);
    71         Infer instance = context.get(inferKey);
    75     protected Infer(Context context) {
    77     protected Infer(Context context) {
    76         context.put(inferKey, this);
    78         context.put(inferKey, this);
    77         syms = Symtab.instance(context);
    79         syms = Symtab.instance(context);
    78         types = Types.instance(context);
    80         types = Types.instance(context);
    79         rs = Resolve.instance(context);
    81         rs = Resolve.instance(context);
       
    82         deferredAttr = DeferredAttr.instance(context);
    80         log = Log.instance(context);
    83         log = Log.instance(context);
    81         chk = Check.instance(context);
    84         chk = Check.instance(context);
    82         diags = JCDiagnostic.Factory.instance(context);
    85         diags = JCDiagnostic.Factory.instance(context);
    83         inferenceException = new InferenceException(diags);
    86         inferenceException = new InferenceException(diags);
    84     }
    87     }
   185             InferenceContext inferenceContext,
   188             InferenceContext inferenceContext,
   186             MethodType mtype,
   189             MethodType mtype,
   187             Attr.ResultInfo resultInfo,
   190             Attr.ResultInfo resultInfo,
   188             Warner warn) throws InferenceException {
   191             Warner warn) throws InferenceException {
   189         Type to = resultInfo.pt;
   192         Type to = resultInfo.pt;
   190         if (to.tag == NONE) {
   193         if (to.tag == NONE || resultInfo.checkContext.inferenceContext().free(resultInfo.pt)) {
   191             to = mtype.getReturnType().tag <= VOID ?
   194             to = mtype.getReturnType().tag <= VOID ?
   192                     mtype.getReturnType() : syms.objectType;
   195                     mtype.getReturnType() : syms.objectType;
   193         }
   196         }
   194         Type qtype1 = inferenceContext.asFree(mtype.getReturnType(), types);
   197         Type qtype1 = inferenceContext.asFree(mtype.getReturnType(), types);
   195         if (!types.isSubtype(qtype1,
   198         if (!types.isSubtype(qtype1,
   266                                   Attr.ResultInfo resultInfo,
   269                                   Attr.ResultInfo resultInfo,
   267                                   Symbol msym,
   270                                   Symbol msym,
   268                                   List<Type> argtypes,
   271                                   List<Type> argtypes,
   269                                   boolean allowBoxing,
   272                                   boolean allowBoxing,
   270                                   boolean useVarargs,
   273                                   boolean useVarargs,
       
   274                                   Resolve.MethodResolutionContext resolveContext,
   271                                   Warner warn) throws InferenceException {
   275                                   Warner warn) throws InferenceException {
   272         //-System.err.println("instantiateMethod(" + tvars + ", " + mt + ", " + argtypes + ")"); //DEBUG
   276         //-System.err.println("instantiateMethod(" + tvars + ", " + mt + ", " + argtypes + ")"); //DEBUG
   273         final InferenceContext inferenceContext = new InferenceContext(tvars, this);
   277         final InferenceContext inferenceContext = new InferenceContext(tvars, this);
   274         inferenceException.clear();
   278         inferenceException.clear();
   275 
   279 
   276         try {
   280         try {
   277             rs.checkRawArgumentsAcceptable(env, inferenceContext, argtypes, mt.getParameterTypes(),
   281             rs.checkRawArgumentsAcceptable(env, msym, resolveContext.attrMode(), inferenceContext,
   278                     allowBoxing, useVarargs, warn, new InferenceCheckHandler(inferenceContext));
   282                     argtypes, mt.getParameterTypes(), allowBoxing, useVarargs, warn,
       
   283                     new InferenceCheckHandler(inferenceContext));
   279 
   284 
   280             // minimize as yet undetermined type variables
   285             // minimize as yet undetermined type variables
   281             for (Type t : inferenceContext.undetvars) {
   286             for (Type t : inferenceContext.undetvars) {
   282                 minimizeInst((UndetVar)t, warn);
   287                 minimizeInst((UndetVar)t, warn);
   283             }
   288             }
   467      * method signature. The target return type is computed from the immediately
   472      * method signature. The target return type is computed from the immediately
   468      * enclosing scope surrounding the polymorphic-signature call.
   473      * enclosing scope surrounding the polymorphic-signature call.
   469      */
   474      */
   470     Type instantiatePolymorphicSignatureInstance(Env<AttrContext> env,
   475     Type instantiatePolymorphicSignatureInstance(Env<AttrContext> env,
   471                                             MethodSymbol spMethod,  // sig. poly. method or null if none
   476                                             MethodSymbol spMethod,  // sig. poly. method or null if none
       
   477                                             Resolve.MethodResolutionContext resolveContext,
   472                                             List<Type> argtypes) {
   478                                             List<Type> argtypes) {
   473         final Type restype;
   479         final Type restype;
   474 
   480 
   475         //The return type for a polymorphic signature call is computed from
   481         //The return type for a polymorphic signature call is computed from
   476         //the enclosing tree E, as follows: if E is a cast, then use the
   482         //the enclosing tree E, as follows: if E is a cast, then use the
   496                 break;
   502                 break;
   497             default:
   503             default:
   498                 restype = syms.objectType;
   504                 restype = syms.objectType;
   499         }
   505         }
   500 
   506 
   501         List<Type> paramtypes = Type.map(argtypes, implicitArgType);
   507         List<Type> paramtypes = Type.map(argtypes, new ImplicitArgType(spMethod, resolveContext.step));
   502         List<Type> exType = spMethod != null ?
   508         List<Type> exType = spMethod != null ?
   503             spMethod.getThrownTypes() :
   509             spMethod.getThrownTypes() :
   504             List.of(syms.throwableType); // make it throw all exceptions
   510             List.of(syms.throwableType); // make it throw all exceptions
   505 
   511 
   506         MethodType mtype = new MethodType(paramtypes,
   512         MethodType mtype = new MethodType(paramtypes,
   508                                           exType,
   514                                           exType,
   509                                           syms.methodClass);
   515                                           syms.methodClass);
   510         return mtype;
   516         return mtype;
   511     }
   517     }
   512     //where
   518     //where
   513         Mapping implicitArgType = new Mapping ("implicitArgType") {
   519         class ImplicitArgType extends DeferredAttr.DeferredTypeMap {
   514                 public Type apply(Type t) {
   520 
   515                     t = types.erasure(t);
   521             public ImplicitArgType(Symbol msym, Resolve.MethodResolutionPhase phase) {
   516                     if (t.tag == BOT)
   522                 deferredAttr.super(AttrMode.SPECULATIVE, msym, phase);
   517                         // nulls type as the marker type Null (which has no instances)
   523             }
   518                         // infer as java.lang.Void for now
   524 
   519                         t = types.boxedClass(syms.voidType).type;
   525             public Type apply(Type t) {
   520                     return t;
   526                 t = types.erasure(super.apply(t));
   521                 }
   527                 if (t.tag == BOT)
   522         };
   528                     // nulls type as the marker type Null (which has no instances)
       
   529                     // infer as java.lang.Void for now
       
   530                     t = types.boxedClass(syms.voidType).type;
       
   531                 return t;
       
   532             }
       
   533         }
   523 
   534 
   524     /**
   535     /**
   525      * Mapping that turns inference variables into undet vars
   536      * Mapping that turns inference variables into undet vars
   526      * (used by inference context)
   537      * (used by inference context)
   527      */
   538      */
   706             //thrown when processing listeners as a single one
   717             //thrown when processing listeners as a single one
   707             if (thrownEx != null) {
   718             if (thrownEx != null) {
   708                 throw thrownEx;
   719                 throw thrownEx;
   709             }
   720             }
   710         }
   721         }
       
   722 
       
   723         void solveAny(List<Type> varsToSolve, Types types, Infer infer) {
       
   724             boolean progress = false;
       
   725             for (Type t : varsToSolve) {
       
   726                 UndetVar uv = (UndetVar)asFree(t, types);
       
   727                 if (uv.inst == null) {
       
   728                     infer.minimizeInst(uv, Warner.noWarnings);
       
   729                     if (uv.inst != null) {
       
   730                         progress = true;
       
   731                     }
       
   732                 }
       
   733             }
       
   734             if (!progress) {
       
   735                 throw infer.inferenceException.setMessage("cyclic.inference", varsToSolve);
       
   736             }
       
   737         }
   711     }
   738     }
   712 
   739 
   713     final InferenceContext emptyContext = new InferenceContext(List.<Type>nil(), this);
   740     final InferenceContext emptyContext = new InferenceContext(List.<Type>nil(), this);
   714 }
   741 }