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

Thu, 10 Jun 2010 17:09:56 -0700

author
jjg
date
Thu, 10 Jun 2010 17:09:56 -0700
changeset 582
366a7b9b5627
parent 581
f2fdd52e4e87
child 615
36c4ec4525b4
permissions
-rw-r--r--

6960407: Potential rebranding issues in openjdk/langtools repository sources
Reviewed-by: darcy

     1 /*
     2  * Copyright (c) 1999, 2009, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    24  */
    26 package com.sun.tools.javac.comp;
    28 import com.sun.tools.javac.util.*;
    29 import com.sun.tools.javac.util.List;
    30 import com.sun.tools.javac.code.*;
    31 import com.sun.tools.javac.code.Type.*;
    32 import com.sun.tools.javac.code.Type.ForAll.ConstraintKind;
    33 import com.sun.tools.javac.code.Symbol.*;
    34 import com.sun.tools.javac.util.JCDiagnostic;
    36 import static com.sun.tools.javac.code.TypeTags.*;
    38 /** Helper class for type parameter inference, used by the attribution phase.
    39  *
    40  *  <p><b>This is NOT part of any supported API.
    41  *  If you write code that depends on this, you do so at your own risk.
    42  *  This code and its internal interfaces are subject to change or
    43  *  deletion without notice.</b>
    44  */
    45 public class Infer {
    46     protected static final Context.Key<Infer> inferKey =
    47         new Context.Key<Infer>();
    49     /** A value for prototypes that admit any type, including polymorphic ones. */
    50     public static final Type anyPoly = new Type(NONE, null);
    52     Symtab syms;
    53     Types types;
    54     Check chk;
    55     Resolve rs;
    56     JCDiagnostic.Factory diags;
    58     public static Infer instance(Context context) {
    59         Infer instance = context.get(inferKey);
    60         if (instance == null)
    61             instance = new Infer(context);
    62         return instance;
    63     }
    65     protected Infer(Context context) {
    66         context.put(inferKey, this);
    67         syms = Symtab.instance(context);
    68         types = Types.instance(context);
    69         rs = Resolve.instance(context);
    70         chk = Check.instance(context);
    71         diags = JCDiagnostic.Factory.instance(context);
    72         ambiguousNoInstanceException =
    73             new NoInstanceException(true, diags);
    74         unambiguousNoInstanceException =
    75             new NoInstanceException(false, diags);
    76         invalidInstanceException =
    77             new InvalidInstanceException(diags);
    79     }
    81     public static class InferenceException extends RuntimeException {
    82         private static final long serialVersionUID = 0;
    84         JCDiagnostic diagnostic;
    85         JCDiagnostic.Factory diags;
    87         InferenceException(JCDiagnostic.Factory diags) {
    88             this.diagnostic = null;
    89             this.diags = diags;
    90         }
    92         InferenceException setMessage(String key, Object... args) {
    93             this.diagnostic = diags.fragment(key, args);
    94             return this;
    95         }
    97         public JCDiagnostic getDiagnostic() {
    98              return diagnostic;
    99          }
   100     }
   102     public static class NoInstanceException extends InferenceException {
   103         private static final long serialVersionUID = 1;
   105         boolean isAmbiguous; // exist several incomparable best instances?
   107         NoInstanceException(boolean isAmbiguous, JCDiagnostic.Factory diags) {
   108             super(diags);
   109             this.isAmbiguous = isAmbiguous;
   110         }
   111     }
   113     public static class InvalidInstanceException extends InferenceException {
   114         private static final long serialVersionUID = 2;
   116         InvalidInstanceException(JCDiagnostic.Factory diags) {
   117             super(diags);
   118         }
   119     }
   121     private final NoInstanceException ambiguousNoInstanceException;
   122     private final NoInstanceException unambiguousNoInstanceException;
   123     private final InvalidInstanceException invalidInstanceException;
   125 /***************************************************************************
   126  * Auxiliary type values and classes
   127  ***************************************************************************/
   129     /** A mapping that turns type variables into undetermined type variables.
   130      */
   131     Mapping fromTypeVarFun = new Mapping("fromTypeVarFun") {
   132             public Type apply(Type t) {
   133                 if (t.tag == TYPEVAR) return new UndetVar(t);
   134                 else return t.map(this);
   135             }
   136         };
   138     /** A mapping that returns its type argument with every UndetVar replaced
   139      *  by its `inst' field. Throws a NoInstanceException
   140      *  if this not possible because an `inst' field is null.
   141      */
   142     Mapping getInstFun = new Mapping("getInstFun") {
   143             public Type apply(Type t) {
   144                 switch (t.tag) {
   145                 case UNKNOWN:
   146                     throw ambiguousNoInstanceException
   147                         .setMessage("undetermined.type");
   148                 case UNDETVAR:
   149                     UndetVar that = (UndetVar) t;
   150                     if (that.inst == null)
   151                         throw ambiguousNoInstanceException
   152                             .setMessage("type.variable.has.undetermined.type",
   153                                         that.qtype);
   154                     return apply(that.inst);
   155                 default:
   156                     return t.map(this);
   157                 }
   158             }
   159         };
   161 /***************************************************************************
   162  * Mini/Maximization of UndetVars
   163  ***************************************************************************/
   165     /** Instantiate undetermined type variable to its minimal upper bound.
   166      *  Throw a NoInstanceException if this not possible.
   167      */
   168     void maximizeInst(UndetVar that, Warner warn) throws NoInstanceException {
   169         if (that.inst == null) {
   170             if (that.hibounds.isEmpty())
   171                 that.inst = syms.objectType;
   172             else if (that.hibounds.tail.isEmpty())
   173                 that.inst = that.hibounds.head;
   174             else
   175                 that.inst = types.glb(that.hibounds);
   176         }
   177         if (that.inst == null ||
   178             that.inst.isErroneous())
   179             throw ambiguousNoInstanceException
   180                 .setMessage("no.unique.maximal.instance.exists",
   181                             that.qtype, that.hibounds);
   182     }
   183     //where
   184         private boolean isSubClass(Type t, final List<Type> ts) {
   185             t = t.baseType();
   186             if (t.tag == TYPEVAR) {
   187                 List<Type> bounds = types.getBounds((TypeVar)t);
   188                 for (Type s : ts) {
   189                     if (!types.isSameType(t, s.baseType())) {
   190                         for (Type bound : bounds) {
   191                             if (!isSubClass(bound, List.of(s.baseType())))
   192                                 return false;
   193                         }
   194                     }
   195                 }
   196             } else {
   197                 for (Type s : ts) {
   198                     if (!t.tsym.isSubClass(s.baseType().tsym, types))
   199                         return false;
   200                 }
   201             }
   202             return true;
   203         }
   205     /** Instantiate undetermined type variable to the lub of all its lower bounds.
   206      *  Throw a NoInstanceException if this not possible.
   207      */
   208     void minimizeInst(UndetVar that, Warner warn) throws NoInstanceException {
   209         if (that.inst == null) {
   210             if (that.lobounds.isEmpty())
   211                 that.inst = syms.botType;
   212             else if (that.lobounds.tail.isEmpty())
   213                 that.inst = that.lobounds.head.isPrimitive() ? syms.errType : that.lobounds.head;
   214             else {
   215                 that.inst = types.lub(that.lobounds);
   216             }
   217             if (that.inst == null || that.inst.tag == ERROR)
   218                     throw ambiguousNoInstanceException
   219                         .setMessage("no.unique.minimal.instance.exists",
   220                                     that.qtype, that.lobounds);
   221             // VGJ: sort of inlined maximizeInst() below.  Adding
   222             // bounds can cause lobounds that are above hibounds.
   223             if (that.hibounds.isEmpty())
   224                 return;
   225             Type hb = null;
   226             if (that.hibounds.tail.isEmpty())
   227                 hb = that.hibounds.head;
   228             else for (List<Type> bs = that.hibounds;
   229                       bs.nonEmpty() && hb == null;
   230                       bs = bs.tail) {
   231                 if (isSubClass(bs.head, that.hibounds))
   232                     hb = types.fromUnknownFun.apply(bs.head);
   233             }
   234             if (hb == null ||
   235                 !types.isSubtypeUnchecked(hb, that.hibounds, warn) ||
   236                 !types.isSubtypeUnchecked(that.inst, hb, warn))
   237                 throw ambiguousNoInstanceException;
   238         }
   239     }
   241 /***************************************************************************
   242  * Exported Methods
   243  ***************************************************************************/
   245     /** Try to instantiate expression type `that' to given type `to'.
   246      *  If a maximal instantiation exists which makes this type
   247      *  a subtype of type `to', return the instantiated type.
   248      *  If no instantiation exists, or if several incomparable
   249      *  best instantiations exist throw a NoInstanceException.
   250      */
   251     public Type instantiateExpr(ForAll that,
   252                                 Type to,
   253                                 Warner warn) throws InferenceException {
   254         List<Type> undetvars = Type.map(that.tvars, fromTypeVarFun);
   255         for (List<Type> l = undetvars; l.nonEmpty(); l = l.tail) {
   256             UndetVar uv = (UndetVar) l.head;
   257             TypeVar tv = (TypeVar)uv.qtype;
   258             ListBuffer<Type> hibounds = new ListBuffer<Type>();
   259             for (Type t : that.getConstraints(tv, ConstraintKind.EXTENDS).prependList(types.getBounds(tv))) {
   260                 if (!t.containsSome(that.tvars) && t.tag != BOT) {
   261                     hibounds.append(t);
   262                 }
   263             }
   264             List<Type> inst = that.getConstraints(tv, ConstraintKind.EQUAL);
   265             if (inst.nonEmpty() && inst.head.tag != BOT) {
   266                 uv.inst = inst.head;
   267             }
   268             uv.hibounds = hibounds.toList();
   269         }
   270         Type qtype1 = types.subst(that.qtype, that.tvars, undetvars);
   271         if (!types.isSubtype(qtype1, to)) {
   272             throw unambiguousNoInstanceException
   273                 .setMessage("no.conforming.instance.exists",
   274                             that.tvars, that.qtype, to);
   275         }
   276         for (List<Type> l = undetvars; l.nonEmpty(); l = l.tail)
   277             maximizeInst((UndetVar) l.head, warn);
   278         // System.out.println(" = " + qtype1.map(getInstFun));//DEBUG
   280         // check bounds
   281         List<Type> targs = Type.map(undetvars, getInstFun);
   282         targs = types.subst(targs, that.tvars, targs);
   283         checkWithinBounds(that.tvars, targs, warn);
   284         return chk.checkType(warn.pos(), that.inst(targs, types), to);
   285     }
   287     /** Instantiate method type `mt' by finding instantiations of
   288      *  `tvars' so that method can be applied to `argtypes'.
   289      */
   290     public Type instantiateMethod(final Env<AttrContext> env,
   291                                   List<Type> tvars,
   292                                   MethodType mt,
   293                                   final Symbol msym,
   294                                   final List<Type> argtypes,
   295                                   final boolean allowBoxing,
   296                                   final boolean useVarargs,
   297                                   final Warner warn) throws InferenceException {
   298         //-System.err.println("instantiateMethod(" + tvars + ", " + mt + ", " + argtypes + ")"); //DEBUG
   299         List<Type> undetvars = Type.map(tvars, fromTypeVarFun);
   300         List<Type> formals = mt.argtypes;
   301         //need to capture exactly once - otherwise subsequent
   302         //applicability checks might fail
   303         final List<Type> capturedArgs = types.capture(argtypes);
   304         List<Type> actuals = capturedArgs;
   305         List<Type> actualsNoCapture = argtypes;
   306         // instantiate all polymorphic argument types and
   307         // set up lower bounds constraints for undetvars
   308         Type varargsFormal = useVarargs ? formals.last() : null;
   309         while (actuals.nonEmpty() && formals.head != varargsFormal) {
   310             Type formal = formals.head;
   311             Type actual = actuals.head.baseType();
   312             Type actualNoCapture = actualsNoCapture.head.baseType();
   313             if (actual.tag == FORALL)
   314                 actual = instantiateArg((ForAll)actual, formal, tvars, warn);
   315             Type undetFormal = types.subst(formal, tvars, undetvars);
   316             boolean works = allowBoxing
   317                 ? types.isConvertible(actual, undetFormal, warn)
   318                 : types.isSubtypeUnchecked(actual, undetFormal, warn);
   319             if (!works) {
   320                 throw unambiguousNoInstanceException
   321                     .setMessage("no.conforming.assignment.exists",
   322                                 tvars, actualNoCapture, formal);
   323             }
   324             formals = formals.tail;
   325             actuals = actuals.tail;
   326             actualsNoCapture = actualsNoCapture.tail;
   327         }
   328         if (formals.head != varargsFormal || // not enough args
   329             !useVarargs && actuals.nonEmpty()) { // too many args
   330             // argument lists differ in length
   331             throw unambiguousNoInstanceException
   332                 .setMessage("arg.length.mismatch");
   333         }
   335         // for varargs arguments as well
   336         if (useVarargs) {
   337             Type elemType = types.elemtype(varargsFormal);
   338             Type elemUndet = types.subst(elemType, tvars, undetvars);
   339             while (actuals.nonEmpty()) {
   340                 Type actual = actuals.head.baseType();
   341                 Type actualNoCapture = actualsNoCapture.head.baseType();
   342                 if (actual.tag == FORALL)
   343                     actual = instantiateArg((ForAll)actual, elemType, tvars, warn);
   344                 boolean works = types.isConvertible(actual, elemUndet, warn);
   345                 if (!works) {
   346                     throw unambiguousNoInstanceException
   347                         .setMessage("no.conforming.assignment.exists",
   348                                     tvars, actualNoCapture, elemType);
   349                 }
   350                 actuals = actuals.tail;
   351                 actualsNoCapture = actualsNoCapture.tail;
   352             }
   353         }
   355         // minimize as yet undetermined type variables
   356         for (Type t : undetvars)
   357             minimizeInst((UndetVar) t, warn);
   359         /** Type variables instantiated to bottom */
   360         ListBuffer<Type> restvars = new ListBuffer<Type>();
   362         /** Undet vars instantiated to bottom */
   363         final ListBuffer<Type> restundet = new ListBuffer<Type>();
   365         /** Instantiated types or TypeVars if under-constrained */
   366         ListBuffer<Type> insttypes = new ListBuffer<Type>();
   368         /** Instantiated types or UndetVars if under-constrained */
   369         ListBuffer<Type> undettypes = new ListBuffer<Type>();
   371         for (Type t : undetvars) {
   372             UndetVar uv = (UndetVar)t;
   373             if (uv.inst.tag == BOT) {
   374                 restvars.append(uv.qtype);
   375                 restundet.append(uv);
   376                 insttypes.append(uv.qtype);
   377                 undettypes.append(uv);
   378                 uv.inst = null;
   379             } else {
   380                 insttypes.append(uv.inst);
   381                 undettypes.append(uv.inst);
   382             }
   383         }
   384         checkWithinBounds(tvars, undettypes.toList(), warn);
   386         mt = (MethodType)types.subst(mt, tvars, insttypes.toList());
   388         if (!restvars.isEmpty()) {
   389             // if there are uninstantiated variables,
   390             // quantify result type with them
   391             final List<Type> inferredTypes = insttypes.toList();
   392             final List<Type> all_tvars = tvars; //this is the wrong tvars
   393             final MethodType mt2 = new MethodType(mt.argtypes, null, mt.thrown, syms.methodClass);
   394             mt2.restype = new ForAll(restvars.toList(), mt.restype) {
   395                 @Override
   396                 public List<Type> getConstraints(TypeVar tv, ConstraintKind ck) {
   397                     for (Type t : restundet.toList()) {
   398                         UndetVar uv = (UndetVar)t;
   399                         if (uv.qtype == tv) {
   400                             switch (ck) {
   401                                 case EXTENDS: return uv.hibounds;
   402                                 case SUPER: return uv.lobounds;
   403                                 case EQUAL: return uv.inst != null ? List.of(uv.inst) : List.<Type>nil();
   404                             }
   405                         }
   406                     }
   407                     return List.nil();
   408                 }
   410                 @Override
   411                 public Type inst(List<Type> inferred, Types types) throws NoInstanceException {
   412                     List<Type> formals = types.subst(mt2.argtypes, tvars, inferred);
   413                     if (!rs.argumentsAcceptable(capturedArgs, formals,
   414                            allowBoxing, useVarargs, warn)) {
   415                       // inferred method is not applicable
   416                       throw invalidInstanceException.setMessage("inferred.do.not.conform.to.params", formals, argtypes);
   417                     }
   418                     // check that inferred bounds conform to their bounds
   419                     checkWithinBounds(all_tvars,
   420                            types.subst(inferredTypes, tvars, inferred), warn);
   421                     if (useVarargs) {
   422                         chk.checkVararg(env.tree.pos(), formals, msym, env);
   423                     }
   424                     return super.inst(inferred, types);
   425             }};
   426             return mt2;
   427         }
   428         else if (!rs.argumentsAcceptable(capturedArgs, mt.getParameterTypes(), allowBoxing, useVarargs, warn)) {
   429             // inferred method is not applicable
   430             throw invalidInstanceException.setMessage("inferred.do.not.conform.to.params", mt.getParameterTypes(), argtypes);
   431         }
   432         else {
   433             // return instantiated version of method type
   434             return mt;
   435         }
   436     }
   437     //where
   439         /** Try to instantiate argument type `that' to given type `to'.
   440          *  If this fails, try to insantiate `that' to `to' where
   441          *  every occurrence of a type variable in `tvars' is replaced
   442          *  by an unknown type.
   443          */
   444         private Type instantiateArg(ForAll that,
   445                                     Type to,
   446                                     List<Type> tvars,
   447                                     Warner warn) throws InferenceException {
   448             List<Type> targs;
   449             try {
   450                 return instantiateExpr(that, to, warn);
   451             } catch (NoInstanceException ex) {
   452                 Type to1 = to;
   453                 for (List<Type> l = tvars; l.nonEmpty(); l = l.tail)
   454                     to1 = types.subst(to1, List.of(l.head), List.of(syms.unknownType));
   455                 return instantiateExpr(that, to1, warn);
   456             }
   457         }
   459     /** check that type parameters are within their bounds.
   460      */
   461     private void checkWithinBounds(List<Type> tvars,
   462                                    List<Type> arguments,
   463                                    Warner warn)
   464         throws InvalidInstanceException {
   465         for (List<Type> tvs = tvars, args = arguments;
   466              tvs.nonEmpty();
   467              tvs = tvs.tail, args = args.tail) {
   468             if (args.head instanceof UndetVar) continue;
   469             List<Type> bounds = types.subst(types.getBounds((TypeVar)tvs.head), tvars, arguments);
   470             if (!types.isSubtypeUnchecked(args.head, bounds, warn))
   471                 throw invalidInstanceException
   472                     .setMessage("inferred.do.not.conform.to.bounds",
   473                                 args.head, bounds);
   474         }
   475     }
   476 }

mercurial