langtools/src/share/classes/com/sun/tools/javac/comp/Infer.java
author jjg
Fri, 29 Aug 2008 11:10:12 -0700
changeset 1206 3a05355982a9
parent 1040 c0f5acfd9d15
child 1257 873b053bf757
permissions -rw-r--r--
6597471: unused imports in javax.tools.JavaCompiler 6597531: unused imports and unused private const. in com.sun.tools.javac.Server.java Reviewed-by: mcimadamore Contributed-by: davide.angelocola@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
735
372aa565a221 6719955: Update copyright year
xdono
parents: 324
diff changeset
     2
 * Copyright 1999-2008 Sun Microsystems, Inc.  All Rights Reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
06bc494ca11e Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
06bc494ca11e Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
06bc494ca11e Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
06bc494ca11e Initial load
duke
parents:
diff changeset
    23
 * have any questions.
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.javac.comp;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import com.sun.tools.javac.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import com.sun.tools.javac.util.List;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import com.sun.tools.javac.code.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import com.sun.tools.javac.code.Type.*;
1040
c0f5acfd9d15 6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents: 735
diff changeset
    32
import com.sun.tools.javac.util.JCDiagnostic;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import static com.sun.tools.javac.code.TypeTags.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
/** Helper class for type parameter inference, used by the attribution phase.
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
 *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
 *  you write code that depends on this, you do so at your own risk.
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
 *  This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
 *  deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
public class Infer {
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
    protected static final Context.Key<Infer> inferKey =
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
        new Context.Key<Infer>();
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
    /** A value for prototypes that admit any type, including polymorphic ones. */
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
    public static final Type anyPoly = new Type(NONE, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
    Symtab syms;
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
    Types types;
1040
c0f5acfd9d15 6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents: 735
diff changeset
    52
    JCDiagnostic.Factory diags;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
    public static Infer instance(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
        Infer instance = context.get(inferKey);
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
        if (instance == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
            instance = new Infer(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
        return instance;
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
    protected Infer(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
        context.put(inferKey, this);
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
        syms = Symtab.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
        types = Types.instance(context);
1040
c0f5acfd9d15 6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents: 735
diff changeset
    65
        diags = JCDiagnostic.Factory.instance(context);
c0f5acfd9d15 6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents: 735
diff changeset
    66
        ambiguousNoInstanceException =
c0f5acfd9d15 6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents: 735
diff changeset
    67
            new NoInstanceException(true, diags);
c0f5acfd9d15 6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents: 735
diff changeset
    68
        unambiguousNoInstanceException =
c0f5acfd9d15 6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents: 735
diff changeset
    69
            new NoInstanceException(false, diags);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
    public static class NoInstanceException extends RuntimeException {
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
        private static final long serialVersionUID = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
        boolean isAmbiguous; // exist several incomparable best instances?
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
        JCDiagnostic diagnostic;
1040
c0f5acfd9d15 6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents: 735
diff changeset
    78
        JCDiagnostic.Factory diags;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
1040
c0f5acfd9d15 6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents: 735
diff changeset
    80
        NoInstanceException(boolean isAmbiguous, JCDiagnostic.Factory diags) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
            this.diagnostic = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
            this.isAmbiguous = isAmbiguous;
1040
c0f5acfd9d15 6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents: 735
diff changeset
    83
            this.diags = diags;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
        NoInstanceException setMessage(String key) {
1040
c0f5acfd9d15 6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents: 735
diff changeset
    86
            this.diagnostic = diags.fragment(key);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
            return this;
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
        NoInstanceException setMessage(String key, Object arg1) {
1040
c0f5acfd9d15 6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents: 735
diff changeset
    90
            this.diagnostic = diags.fragment(key, arg1);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
            return this;
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
        NoInstanceException setMessage(String key, Object arg1, Object arg2) {
1040
c0f5acfd9d15 6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents: 735
diff changeset
    94
            this.diagnostic = diags.fragment(key, arg1, arg2);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
            return this;
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
        NoInstanceException setMessage(String key, Object arg1, Object arg2, Object arg3) {
1040
c0f5acfd9d15 6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents: 735
diff changeset
    98
            this.diagnostic = diags.fragment(key, arg1, arg2, arg3);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
            return this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
        public JCDiagnostic getDiagnostic() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
            return diagnostic;
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
    }
1040
c0f5acfd9d15 6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents: 735
diff changeset
   105
    private final NoInstanceException ambiguousNoInstanceException;
c0f5acfd9d15 6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents: 735
diff changeset
   106
    private final NoInstanceException unambiguousNoInstanceException;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
/***************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
 * Auxiliary type values and classes
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
 ***************************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
    /** A mapping that turns type variables into undetermined type variables.
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
    Mapping fromTypeVarFun = new Mapping("fromTypeVarFun") {
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
            public Type apply(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
                if (t.tag == TYPEVAR) return new UndetVar(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
                else return t.map(this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
    /** A mapping that returns its type argument with every UndetVar replaced
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
     *  by its `inst' field. Throws a NoInstanceException
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
     *  if this not possible because an `inst' field is null.
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
    Mapping getInstFun = new Mapping("getInstFun") {
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
            public Type apply(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
                switch (t.tag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
                case UNKNOWN:
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
                    throw ambiguousNoInstanceException
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
                        .setMessage("undetermined.type");
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
                case UNDETVAR:
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
                    UndetVar that = (UndetVar) t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
                    if (that.inst == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
                        throw ambiguousNoInstanceException
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
                            .setMessage("type.variable.has.undetermined.type",
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
                                        that.qtype);
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
                    return apply(that.inst);
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
                default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
                    return t.map(this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
/***************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
 * Mini/Maximization of UndetVars
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
 ***************************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
    /** Instantiate undetermined type variable to its minimal upper bound.
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
     *  Throw a NoInstanceException if this not possible.
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
    void maximizeInst(UndetVar that, Warner warn) throws NoInstanceException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
        if (that.inst == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
            if (that.hibounds.isEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
                that.inst = syms.objectType;
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
            else if (that.hibounds.tail.isEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
                that.inst = that.hibounds.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
            else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
                for (List<Type> bs = that.hibounds;
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
                     bs.nonEmpty() && that.inst == null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
                     bs = bs.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
                    // System.out.println("hibounds = " + that.hibounds);//DEBUG
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
                    if (isSubClass(bs.head, that.hibounds))
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
                        that.inst = types.fromUnknownFun.apply(bs.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
                }
324
c4029ac973a2 6569789: Compiler test lang/TYPE/type153/type15304/type15304.html fails since jdk7 b05
mcimadamore
parents: 162
diff changeset
   165
                if (that.inst == null) {
c4029ac973a2 6569789: Compiler test lang/TYPE/type153/type15304/type15304.html fails since jdk7 b05
mcimadamore
parents: 162
diff changeset
   166
                    int classCount = 0, interfaceCount = 0;
c4029ac973a2 6569789: Compiler test lang/TYPE/type153/type15304/type15304.html fails since jdk7 b05
mcimadamore
parents: 162
diff changeset
   167
                    for (Type t : that.hibounds) {
c4029ac973a2 6569789: Compiler test lang/TYPE/type153/type15304/type15304.html fails since jdk7 b05
mcimadamore
parents: 162
diff changeset
   168
                        if (t.tag == CLASS) {
c4029ac973a2 6569789: Compiler test lang/TYPE/type153/type15304/type15304.html fails since jdk7 b05
mcimadamore
parents: 162
diff changeset
   169
                            if (t.isInterface())
c4029ac973a2 6569789: Compiler test lang/TYPE/type153/type15304/type15304.html fails since jdk7 b05
mcimadamore
parents: 162
diff changeset
   170
                                interfaceCount++;
c4029ac973a2 6569789: Compiler test lang/TYPE/type153/type15304/type15304.html fails since jdk7 b05
mcimadamore
parents: 162
diff changeset
   171
                            else
c4029ac973a2 6569789: Compiler test lang/TYPE/type153/type15304/type15304.html fails since jdk7 b05
mcimadamore
parents: 162
diff changeset
   172
                                classCount++;
c4029ac973a2 6569789: Compiler test lang/TYPE/type153/type15304/type15304.html fails since jdk7 b05
mcimadamore
parents: 162
diff changeset
   173
                        }
c4029ac973a2 6569789: Compiler test lang/TYPE/type153/type15304/type15304.html fails since jdk7 b05
mcimadamore
parents: 162
diff changeset
   174
                    }
c4029ac973a2 6569789: Compiler test lang/TYPE/type153/type15304/type15304.html fails since jdk7 b05
mcimadamore
parents: 162
diff changeset
   175
                    if ((that.hibounds.size() == classCount + interfaceCount) && classCount == 1)
c4029ac973a2 6569789: Compiler test lang/TYPE/type153/type15304/type15304.html fails since jdk7 b05
mcimadamore
parents: 162
diff changeset
   176
                        that.inst = types.makeCompoundType(that.hibounds);
c4029ac973a2 6569789: Compiler test lang/TYPE/type153/type15304/type15304.html fails since jdk7 b05
mcimadamore
parents: 162
diff changeset
   177
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
                if (that.inst == null || !types.isSubtypeUnchecked(that.inst, that.hibounds, warn))
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
                    throw ambiguousNoInstanceException
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
                        .setMessage("no.unique.maximal.instance.exists",
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
                                    that.qtype, that.hibounds);
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
    //where
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
        private boolean isSubClass(Type t, final List<Type> ts) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
            t = t.baseType();
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
            if (t.tag == TYPEVAR) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
                List<Type> bounds = types.getBounds((TypeVar)t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
                for (Type s : ts) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
                    if (!types.isSameType(t, s.baseType())) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
                        for (Type bound : bounds) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
                            if (!isSubClass(bound, List.of(s.baseType())))
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
                                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
                for (Type s : ts) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
                    if (!t.tsym.isSubClass(s.baseType().tsym, types))
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
                        return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
            return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
    /** Instaniate undetermined type variable to the lub of all its lower bounds.
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
     *  Throw a NoInstanceException if this not possible.
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
    void minimizeInst(UndetVar that, Warner warn) throws NoInstanceException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
        if (that.inst == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
            if (that.lobounds.isEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
                that.inst = syms.botType;
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
            else if (that.lobounds.tail.isEmpty())
162
6620f2a8e265 6611449: Internal Error thrown during generic method/constructor invocation
mcimadamore
parents: 10
diff changeset
   215
                that.inst = that.lobounds.head.isPrimitive() ? syms.errType : that.lobounds.head;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
            else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
                that.inst = types.lub(that.lobounds);
162
6620f2a8e265 6611449: Internal Error thrown during generic method/constructor invocation
mcimadamore
parents: 10
diff changeset
   218
            }
6620f2a8e265 6611449: Internal Error thrown during generic method/constructor invocation
mcimadamore
parents: 10
diff changeset
   219
            if (that.inst == null || that.inst == syms.errType)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
                    throw ambiguousNoInstanceException
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
                        .setMessage("no.unique.minimal.instance.exists",
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
                                    that.qtype, that.lobounds);
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
            // VGJ: sort of inlined maximizeInst() below.  Adding
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
            // bounds can cause lobounds that are above hibounds.
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
            if (that.hibounds.isEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
                return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
            Type hb = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
            if (that.hibounds.tail.isEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
                hb = that.hibounds.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
            else for (List<Type> bs = that.hibounds;
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
                      bs.nonEmpty() && hb == null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
                      bs = bs.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
                if (isSubClass(bs.head, that.hibounds))
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
                    hb = types.fromUnknownFun.apply(bs.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
            if (hb == null ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
                !types.isSubtypeUnchecked(hb, that.hibounds, warn) ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
                !types.isSubtypeUnchecked(that.inst, hb, warn))
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
                throw ambiguousNoInstanceException;
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
/***************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
 * Exported Methods
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
 ***************************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
    /** Try to instantiate expression type `that' to given type `to'.
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
     *  If a maximal instantiation exists which makes this type
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
     *  a subtype of type `to', return the instantiated type.
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
     *  If no instantiation exists, or if several incomparable
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
     *  best instantiations exist throw a NoInstanceException.
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
    public Type instantiateExpr(ForAll that,
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
                                Type to,
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
                                Warner warn) throws NoInstanceException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
        List<Type> undetvars = Type.map(that.tvars, fromTypeVarFun);
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
        for (List<Type> l = undetvars; l.nonEmpty(); l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
            UndetVar v = (UndetVar) l.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
            ListBuffer<Type> hibounds = new ListBuffer<Type>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
            for (List<Type> l1 = types.getBounds((TypeVar) v.qtype); l1.nonEmpty(); l1 = l1.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
                if (!l1.head.containsSome(that.tvars)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
                    hibounds.append(l1.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
            v.hibounds = hibounds.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
        Type qtype1 = types.subst(that.qtype, that.tvars, undetvars);
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
        if (!types.isSubtype(qtype1, to)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
            throw unambiguousNoInstanceException
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
                .setMessage("no.conforming.instance.exists",
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
                            that.tvars, that.qtype, to);
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
        for (List<Type> l = undetvars; l.nonEmpty(); l = l.tail)
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
            maximizeInst((UndetVar) l.head, warn);
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
        // System.out.println(" = " + qtype1.map(getInstFun));//DEBUG
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
        // check bounds
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
        List<Type> targs = Type.map(undetvars, getInstFun);
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
        targs = types.subst(targs, that.tvars, targs);
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
        checkWithinBounds(that.tvars, targs, warn);
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
        return getInstFun.apply(qtype1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
    /** Instantiate method type `mt' by finding instantiations of
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
     *  `tvars' so that method can be applied to `argtypes'.
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
    public Type instantiateMethod(List<Type> tvars,
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
                                  MethodType mt,
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
                                  List<Type> argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
                                  boolean allowBoxing,
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
                                  boolean useVarargs,
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
                                  Warner warn) throws NoInstanceException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
        //-System.err.println("instantiateMethod(" + tvars + ", " + mt + ", " + argtypes + ")"); //DEBUG
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
        List<Type> undetvars = Type.map(tvars, fromTypeVarFun);
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
        List<Type> formals = mt.argtypes;
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
        // instantiate all polymorphic argument types and
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
        // set up lower bounds constraints for undetvars
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
        Type varargsFormal = useVarargs ? formals.last() : null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
        while (argtypes.nonEmpty() && formals.head != varargsFormal) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
            Type ft = formals.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
            Type at = argtypes.head.baseType();
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
            if (at.tag == FORALL)
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
                at = instantiateArg((ForAll) at, ft, tvars, warn);
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
            Type sft = types.subst(ft, tvars, undetvars);
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
            boolean works = allowBoxing
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
                ? types.isConvertible(at, sft, warn)
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
                : types.isSubtypeUnchecked(at, sft, warn);
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
            if (!works) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
                throw unambiguousNoInstanceException
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
                    .setMessage("no.conforming.assignment.exists",
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
                                tvars, at, ft);
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
            formals = formals.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
            argtypes = argtypes.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
        if (formals.head != varargsFormal || // not enough args
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
            !useVarargs && argtypes.nonEmpty()) { // too many args
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
            // argument lists differ in length
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
            throw unambiguousNoInstanceException
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
                .setMessage("arg.length.mismatch");
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
        // for varargs arguments as well
06bc494ca11e Initial load
duke
parents:
diff changeset
   326
        if (useVarargs) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
            Type elt = types.elemtype(varargsFormal);
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
            Type sft = types.subst(elt, tvars, undetvars);
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
            while (argtypes.nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
                Type ft = sft;
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
                Type at = argtypes.head.baseType();
06bc494ca11e Initial load
duke
parents:
diff changeset
   332
                if (at.tag == FORALL)
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
                    at = instantiateArg((ForAll) at, ft, tvars, warn);
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
                boolean works = types.isConvertible(at, sft, warn);
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
                if (!works) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
                    throw unambiguousNoInstanceException
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
                        .setMessage("no.conforming.assignment.exists",
06bc494ca11e Initial load
duke
parents:
diff changeset
   338
                                    tvars, at, ft);
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   340
                argtypes = argtypes.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   341
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   342
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
        // minimize as yet undetermined type variables
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
        for (Type t : undetvars)
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
            minimizeInst((UndetVar) t, warn);
06bc494ca11e Initial load
duke
parents:
diff changeset
   347
06bc494ca11e Initial load
duke
parents:
diff changeset
   348
        /** Type variables instantiated to bottom */
06bc494ca11e Initial load
duke
parents:
diff changeset
   349
        ListBuffer<Type> restvars = new ListBuffer<Type>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   350
06bc494ca11e Initial load
duke
parents:
diff changeset
   351
        /** Instantiated types or TypeVars if under-constrained */
06bc494ca11e Initial load
duke
parents:
diff changeset
   352
        ListBuffer<Type> insttypes = new ListBuffer<Type>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   353
06bc494ca11e Initial load
duke
parents:
diff changeset
   354
        /** Instantiated types or UndetVars if under-constrained */
06bc494ca11e Initial load
duke
parents:
diff changeset
   355
        ListBuffer<Type> undettypes = new ListBuffer<Type>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   356
06bc494ca11e Initial load
duke
parents:
diff changeset
   357
        for (Type t : undetvars) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   358
            UndetVar uv = (UndetVar)t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   359
            if (uv.inst.tag == BOT) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   360
                restvars.append(uv.qtype);
06bc494ca11e Initial load
duke
parents:
diff changeset
   361
                insttypes.append(uv.qtype);
06bc494ca11e Initial load
duke
parents:
diff changeset
   362
                undettypes.append(uv);
06bc494ca11e Initial load
duke
parents:
diff changeset
   363
                uv.inst = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   364
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   365
                insttypes.append(uv.inst);
06bc494ca11e Initial load
duke
parents:
diff changeset
   366
                undettypes.append(uv.inst);
06bc494ca11e Initial load
duke
parents:
diff changeset
   367
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   368
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   369
        checkWithinBounds(tvars, undettypes.toList(), warn);
06bc494ca11e Initial load
duke
parents:
diff changeset
   370
06bc494ca11e Initial load
duke
parents:
diff changeset
   371
        if (!restvars.isEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   372
            // if there are uninstantiated variables,
06bc494ca11e Initial load
duke
parents:
diff changeset
   373
            // quantify result type with them
06bc494ca11e Initial load
duke
parents:
diff changeset
   374
            mt = new MethodType(mt.argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
   375
                                new ForAll(restvars.toList(), mt.restype),
06bc494ca11e Initial load
duke
parents:
diff changeset
   376
                                mt.thrown, syms.methodClass);
06bc494ca11e Initial load
duke
parents:
diff changeset
   377
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   378
06bc494ca11e Initial load
duke
parents:
diff changeset
   379
        // return instantiated version of method type
06bc494ca11e Initial load
duke
parents:
diff changeset
   380
        return types.subst(mt, tvars, insttypes.toList());
06bc494ca11e Initial load
duke
parents:
diff changeset
   381
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   382
    //where
06bc494ca11e Initial load
duke
parents:
diff changeset
   383
06bc494ca11e Initial load
duke
parents:
diff changeset
   384
        /** Try to instantiate argument type `that' to given type `to'.
06bc494ca11e Initial load
duke
parents:
diff changeset
   385
         *  If this fails, try to insantiate `that' to `to' where
06bc494ca11e Initial load
duke
parents:
diff changeset
   386
         *  every occurrence of a type variable in `tvars' is replaced
06bc494ca11e Initial load
duke
parents:
diff changeset
   387
         *  by an unknown type.
06bc494ca11e Initial load
duke
parents:
diff changeset
   388
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   389
        private Type instantiateArg(ForAll that,
06bc494ca11e Initial load
duke
parents:
diff changeset
   390
                                    Type to,
06bc494ca11e Initial load
duke
parents:
diff changeset
   391
                                    List<Type> tvars,
06bc494ca11e Initial load
duke
parents:
diff changeset
   392
                                    Warner warn) throws NoInstanceException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   393
            List<Type> targs;
06bc494ca11e Initial load
duke
parents:
diff changeset
   394
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   395
                return instantiateExpr(that, to, warn);
06bc494ca11e Initial load
duke
parents:
diff changeset
   396
            } catch (NoInstanceException ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   397
                Type to1 = to;
06bc494ca11e Initial load
duke
parents:
diff changeset
   398
                for (List<Type> l = tvars; l.nonEmpty(); l = l.tail)
06bc494ca11e Initial load
duke
parents:
diff changeset
   399
                    to1 = types.subst(to1, List.of(l.head), List.of(syms.unknownType));
06bc494ca11e Initial load
duke
parents:
diff changeset
   400
                return instantiateExpr(that, to1, warn);
06bc494ca11e Initial load
duke
parents:
diff changeset
   401
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   402
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   403
06bc494ca11e Initial load
duke
parents:
diff changeset
   404
    /** check that type parameters are within their bounds.
06bc494ca11e Initial load
duke
parents:
diff changeset
   405
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   406
    private void checkWithinBounds(List<Type> tvars,
06bc494ca11e Initial load
duke
parents:
diff changeset
   407
                                   List<Type> arguments,
06bc494ca11e Initial load
duke
parents:
diff changeset
   408
                                   Warner warn)
06bc494ca11e Initial load
duke
parents:
diff changeset
   409
        throws NoInstanceException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   410
        for (List<Type> tvs = tvars, args = arguments;
06bc494ca11e Initial load
duke
parents:
diff changeset
   411
             tvs.nonEmpty();
06bc494ca11e Initial load
duke
parents:
diff changeset
   412
             tvs = tvs.tail, args = args.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   413
            if (args.head instanceof UndetVar) continue;
06bc494ca11e Initial load
duke
parents:
diff changeset
   414
            List<Type> bounds = types.subst(types.getBounds((TypeVar)tvs.head), tvars, arguments);
06bc494ca11e Initial load
duke
parents:
diff changeset
   415
            if (!types.isSubtypeUnchecked(args.head, bounds, warn))
06bc494ca11e Initial load
duke
parents:
diff changeset
   416
                throw unambiguousNoInstanceException
06bc494ca11e Initial load
duke
parents:
diff changeset
   417
                    .setMessage("inferred.do.not.conform.to.bounds",
06bc494ca11e Initial load
duke
parents:
diff changeset
   418
                                arguments, tvars);
06bc494ca11e Initial load
duke
parents:
diff changeset
   419
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   420
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   421
}