langtools/src/share/classes/com/sun/tools/javac/code/Types.java
author mcimadamore
Thu, 24 Jul 2008 11:12:41 +0100
changeset 938 13aae74ca013
parent 735 372aa565a221
child 1044 7016e624ec3a
permissions -rw-r--r--
6594284: NPE thrown when calling a method on an intersection type Summary: javac should report an error when the capture of an actual type parameter does not exist Reviewed-by: jjg
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: 660
diff changeset
     2
 * Copyright 2003-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.code;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import java.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import com.sun.tools.javac.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import com.sun.tools.javac.util.List;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import com.sun.tools.javac.jvm.ClassReader;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import com.sun.tools.javac.comp.Infer;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import com.sun.tools.javac.comp.Check;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
import static com.sun.tools.javac.code.Type.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
import static com.sun.tools.javac.code.TypeTags.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
import static com.sun.tools.javac.code.Symbol.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
import static com.sun.tools.javac.code.Flags.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
import static com.sun.tools.javac.code.BoundKind.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
import static com.sun.tools.javac.util.ListBuffer.lb;
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
 * Utility class containing various operations on types.
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
 * <p>Unless other names are more illustrative, the following naming
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
 * conventions should be observed in this file:
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
 * <dl>
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
 * <dt>t</dt>
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
 * <dd>If the first argument to an operation is a type, it should be named t.</dd>
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
 * <dt>s</dt>
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
 * <dd>Similarly, if the second argument to an operation is a type, it should be named s.</dd>
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
 * <dt>ts</dt>
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
 * <dd>If an operations takes a list of types, the first should be named ts.</dd>
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
 * <dt>ss</dt>
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
 * <dd>A second list of types should be named ss.</dd>
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
 * </dl>
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
 * <p><b>This is NOT part of any API supported by Sun Microsystems.
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
 * If you write code that depends on this, you do so at your own risk.
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
 * This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
 * deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
public class Types {
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
    protected static final Context.Key<Types> typesKey =
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
        new Context.Key<Types>();
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
    final Symtab syms;
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
    final Name.Table names;
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
    final boolean allowBoxing;
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
    final ClassReader reader;
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
    final Source source;
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
    final Check chk;
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
    List<Warner> warnStack = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
    final Name capturedName;
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
    // <editor-fold defaultstate="collapsed" desc="Instantiating">
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
    public static Types instance(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
        Types instance = context.get(typesKey);
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
        if (instance == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
            instance = new Types(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
        return instance;
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
    protected Types(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
        context.put(typesKey, this);
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
        syms = Symtab.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
        names = Name.Table.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
        allowBoxing = Source.instance(context).allowBoxing();
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
        reader = ClassReader.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
        source = Source.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
        chk = Check.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
        capturedName = names.fromString("<captured wildcard>");
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
    // <editor-fold defaultstate="collapsed" desc="upperBound">
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
     * The "rvalue conversion".<br>
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
     * The upper bound of most types is the type
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
     * itself.  Wildcards, on the other hand have upper
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
     * and lower bounds.
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
     * @param t a type
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
     * @return the upper bound of the given type
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
    public Type upperBound(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
        return upperBound.visit(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
        private final MapVisitor<Void> upperBound = new MapVisitor<Void>() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
            public Type visitWildcardType(WildcardType t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
                if (t.isSuperBound())
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
                    return t.bound == null ? syms.objectType : t.bound.bound;
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
                else
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
                    return visit(t.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
            public Type visitCapturedType(CapturedType t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
                return visit(t.bound);
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
    // <editor-fold defaultstate="collapsed" desc="lowerBound">
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
     * The "lvalue conversion".<br>
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
     * The lower bound of most types is the type
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
     * itself.  Wildcards, on the other hand have upper
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
     * and lower bounds.
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
     * @param t a type
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
     * @return the lower bound of the given type
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
    public Type lowerBound(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
        return lowerBound.visit(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
        private final MapVisitor<Void> lowerBound = new MapVisitor<Void>() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
            public Type visitWildcardType(WildcardType t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
                return t.isExtendsBound() ? syms.botType : visit(t.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
            public Type visitCapturedType(CapturedType t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
                return visit(t.getLowerBound());
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
    // <editor-fold defaultstate="collapsed" desc="isUnbounded">
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
     * Checks that all the arguments to a class are unbounded
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
     * wildcards or something else that doesn't make any restrictions
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
     * on the arguments. If a class isUnbounded, a raw super- or
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
     * subclass can be cast to it without a warning.
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
     * @param t a type
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
     * @return true iff the given type is unbounded or raw
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
    public boolean isUnbounded(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
        return isUnbounded.visit(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
        private final UnaryVisitor<Boolean> isUnbounded = new UnaryVisitor<Boolean>() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
            public Boolean visitType(Type t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
                return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
            public Boolean visitClassType(ClassType t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
                List<Type> parms = t.tsym.type.allparams();
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
                List<Type> args = t.allparams();
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
                while (parms.nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
                    WildcardType unb = new WildcardType(syms.objectType,
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
                                                        BoundKind.UNBOUND,
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
                                                        syms.boundClass,
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
                                                        (TypeVar)parms.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
                    if (!containsType(args.head, unb))
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
                        return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
                    parms = parms.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
                    args = args.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
                return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
    // <editor-fold defaultstate="collapsed" desc="asSub">
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
     * Return the least specific subtype of t that starts with symbol
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
     * sym.  If none exists, return null.  The least specific subtype
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
     * is determined as follows:
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
     * <p>If there is exactly one parameterized instance of sym that is a
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
     * subtype of t, that parameterized instance is returned.<br>
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
     * Otherwise, if the plain type or raw type `sym' is a subtype of
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
     * type t, the type `sym' itself is returned.  Otherwise, null is
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
     * returned.
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
    public Type asSub(Type t, Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
        return asSub.visit(t, sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
        private final SimpleVisitor<Type,Symbol> asSub = new SimpleVisitor<Type,Symbol>() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
            public Type visitType(Type t, Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
                return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
            public Type visitClassType(ClassType t, Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
                if (t.tsym == sym)
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
                    return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
                Type base = asSuper(sym.type, t.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
                if (base == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
                    return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
                ListBuffer<Type> from = new ListBuffer<Type>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
                ListBuffer<Type> to = new ListBuffer<Type>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
                try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
                    adapt(base, t, from, to);
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
                } catch (AdaptFailure ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
                    return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
                Type res = subst(sym.type, from.toList(), to.toList());
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
                if (!isSubtype(res, t))
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
                    return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
                ListBuffer<Type> openVars = new ListBuffer<Type>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
                for (List<Type> l = sym.type.allparams();
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
                     l.nonEmpty(); l = l.tail)
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
                    if (res.contains(l.head) && !t.contains(l.head))
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
                        openVars.append(l.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
                if (openVars.nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
                    if (t.isRaw()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
                        // The subtype of a raw type is raw
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
                        res = erasure(res);
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
                    } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
                        // Unbound type arguments default to ?
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
                        List<Type> opens = openVars.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
                        ListBuffer<Type> qs = new ListBuffer<Type>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
                        for (List<Type> iter = opens; iter.nonEmpty(); iter = iter.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
                            qs.append(new WildcardType(syms.objectType, BoundKind.UNBOUND, syms.boundClass, (TypeVar) iter.head));
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
                        res = subst(res, opens, qs.toList());
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
                return res;
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
            public Type visitErrorType(ErrorType t, Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
                return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
    // <editor-fold defaultstate="collapsed" desc="isConvertible">
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
     * Is t a subtype of or convertiable via boxing/unboxing
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
     * convertions to s?
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
    public boolean isConvertible(Type t, Type s, Warner warn) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
        boolean tPrimitive = t.isPrimitive();
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
        boolean sPrimitive = s.isPrimitive();
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
        if (tPrimitive == sPrimitive)
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
            return isSubtypeUnchecked(t, s, warn);
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
        if (!allowBoxing) return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
        return tPrimitive
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
            ? isSubtype(boxedClass(t).type, s)
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
            : isSubtype(unboxedType(t), s);
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
     * Is t a subtype of or convertiable via boxing/unboxing
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
     * convertions to s?
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
    public boolean isConvertible(Type t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
        return isConvertible(t, s, Warner.noWarnings);
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
    // <editor-fold defaultstate="collapsed" desc="isSubtype">
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
     * Is t an unchecked subtype of s?
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
    public boolean isSubtypeUnchecked(Type t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
        return isSubtypeUnchecked(t, s, Warner.noWarnings);
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
     * Is t an unchecked subtype of s?
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
    public boolean isSubtypeUnchecked(Type t, Type s, Warner warn) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
        if (t.tag == ARRAY && s.tag == ARRAY) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
            return (((ArrayType)t).elemtype.tag <= lastBaseTag)
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
                ? isSameType(elemtype(t), elemtype(s))
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
                : isSubtypeUnchecked(elemtype(t), elemtype(s), warn);
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
        } else if (isSubtype(t, s)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
            return true;
660
16c478b6af1d 6507024: casting an array to a generic type results in a 'capture#69 of ?' type error
mcimadamore
parents: 514
diff changeset
   304
        }
16c478b6af1d 6507024: casting an array to a generic type results in a 'capture#69 of ?' type error
mcimadamore
parents: 514
diff changeset
   305
        else if (t.tag == TYPEVAR) {
16c478b6af1d 6507024: casting an array to a generic type results in a 'capture#69 of ?' type error
mcimadamore
parents: 514
diff changeset
   306
            return isSubtypeUnchecked(t.getUpperBound(), s, warn);
16c478b6af1d 6507024: casting an array to a generic type results in a 'capture#69 of ?' type error
mcimadamore
parents: 514
diff changeset
   307
        }
16c478b6af1d 6507024: casting an array to a generic type results in a 'capture#69 of ?' type error
mcimadamore
parents: 514
diff changeset
   308
        else if (!s.isRaw()) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
            Type t2 = asSuper(t, s.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
            if (t2 != null && t2.isRaw()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
                if (isReifiable(s))
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
                    warn.silentUnchecked();
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
                else
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
                    warn.warnUnchecked();
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
                return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
        return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
     * Is t a subtype of s?<br>
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
     * (not defined for Method and ForAll types)
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
    final public boolean isSubtype(Type t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   326
        return isSubtype(t, s, true);
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
    final public boolean isSubtypeNoCapture(Type t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
        return isSubtype(t, s, false);
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
    public boolean isSubtype(Type t, Type s, boolean capture) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   332
        if (t == s)
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
            return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
        if (s.tag >= firstPartialTag)
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
            return isSuperType(s, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
06bc494ca11e Initial load
duke
parents:
diff changeset
   338
        Type lower = lowerBound(s);
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
        if (s != lower)
06bc494ca11e Initial load
duke
parents:
diff changeset
   340
            return isSubtype(capture ? capture(t) : t, lower, false);
06bc494ca11e Initial load
duke
parents:
diff changeset
   341
06bc494ca11e Initial load
duke
parents:
diff changeset
   342
        return isSubtype.visit(capture ? capture(t) : t, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
        private TypeRelation isSubtype = new TypeRelation()
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
        {
06bc494ca11e Initial load
duke
parents:
diff changeset
   347
            public Boolean visitType(Type t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   348
                switch (t.tag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   349
                case BYTE: case CHAR:
06bc494ca11e Initial load
duke
parents:
diff changeset
   350
                    return (t.tag == s.tag ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   351
                              t.tag + 2 <= s.tag && s.tag <= DOUBLE);
06bc494ca11e Initial load
duke
parents:
diff changeset
   352
                case SHORT: case INT: case LONG: case FLOAT: case DOUBLE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   353
                    return t.tag <= s.tag && s.tag <= DOUBLE;
06bc494ca11e Initial load
duke
parents:
diff changeset
   354
                case BOOLEAN: case VOID:
06bc494ca11e Initial load
duke
parents:
diff changeset
   355
                    return t.tag == s.tag;
06bc494ca11e Initial load
duke
parents:
diff changeset
   356
                case TYPEVAR:
06bc494ca11e Initial load
duke
parents:
diff changeset
   357
                    return isSubtypeNoCapture(t.getUpperBound(), s);
06bc494ca11e Initial load
duke
parents:
diff changeset
   358
                case BOT:
06bc494ca11e Initial load
duke
parents:
diff changeset
   359
                    return
06bc494ca11e Initial load
duke
parents:
diff changeset
   360
                        s.tag == BOT || s.tag == CLASS ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   361
                        s.tag == ARRAY || s.tag == TYPEVAR;
06bc494ca11e Initial load
duke
parents:
diff changeset
   362
                case NONE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   363
                    return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   364
                default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   365
                    throw new AssertionError("isSubtype " + t.tag);
06bc494ca11e Initial load
duke
parents:
diff changeset
   366
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   367
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   368
06bc494ca11e Initial load
duke
parents:
diff changeset
   369
            private Set<TypePair> cache = new HashSet<TypePair>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   370
06bc494ca11e Initial load
duke
parents:
diff changeset
   371
            private boolean containsTypeRecursive(Type t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   372
                TypePair pair = new TypePair(t, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
   373
                if (cache.add(pair)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   374
                    try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   375
                        return containsType(t.getTypeArguments(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   376
                                            s.getTypeArguments());
06bc494ca11e Initial load
duke
parents:
diff changeset
   377
                    } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   378
                        cache.remove(pair);
06bc494ca11e Initial load
duke
parents:
diff changeset
   379
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   380
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   381
                    return containsType(t.getTypeArguments(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   382
                                        rewriteSupers(s).getTypeArguments());
06bc494ca11e Initial load
duke
parents:
diff changeset
   383
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   384
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   385
06bc494ca11e Initial load
duke
parents:
diff changeset
   386
            private Type rewriteSupers(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   387
                if (!t.isParameterized())
06bc494ca11e Initial load
duke
parents:
diff changeset
   388
                    return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   389
                ListBuffer<Type> from = lb();
06bc494ca11e Initial load
duke
parents:
diff changeset
   390
                ListBuffer<Type> to = lb();
06bc494ca11e Initial load
duke
parents:
diff changeset
   391
                adaptSelf(t, from, to);
06bc494ca11e Initial load
duke
parents:
diff changeset
   392
                if (from.isEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
   393
                    return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   394
                ListBuffer<Type> rewrite = lb();
06bc494ca11e Initial load
duke
parents:
diff changeset
   395
                boolean changed = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   396
                for (Type orig : to.toList()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   397
                    Type s = rewriteSupers(orig);
06bc494ca11e Initial load
duke
parents:
diff changeset
   398
                    if (s.isSuperBound() && !s.isExtendsBound()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   399
                        s = new WildcardType(syms.objectType,
06bc494ca11e Initial load
duke
parents:
diff changeset
   400
                                             BoundKind.UNBOUND,
06bc494ca11e Initial load
duke
parents:
diff changeset
   401
                                             syms.boundClass);
06bc494ca11e Initial load
duke
parents:
diff changeset
   402
                        changed = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   403
                    } else if (s != orig) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   404
                        s = new WildcardType(upperBound(s),
06bc494ca11e Initial load
duke
parents:
diff changeset
   405
                                             BoundKind.EXTENDS,
06bc494ca11e Initial load
duke
parents:
diff changeset
   406
                                             syms.boundClass);
06bc494ca11e Initial load
duke
parents:
diff changeset
   407
                        changed = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   408
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   409
                    rewrite.append(s);
06bc494ca11e Initial load
duke
parents:
diff changeset
   410
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   411
                if (changed)
06bc494ca11e Initial load
duke
parents:
diff changeset
   412
                    return subst(t.tsym.type, from.toList(), rewrite.toList());
06bc494ca11e Initial load
duke
parents:
diff changeset
   413
                else
06bc494ca11e Initial load
duke
parents:
diff changeset
   414
                    return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   415
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   416
06bc494ca11e Initial load
duke
parents:
diff changeset
   417
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   418
            public Boolean visitClassType(ClassType t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   419
                Type sup = asSuper(t, s.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   420
                return sup != null
06bc494ca11e Initial load
duke
parents:
diff changeset
   421
                    && sup.tsym == s.tsym
06bc494ca11e Initial load
duke
parents:
diff changeset
   422
                    // You're not allowed to write
06bc494ca11e Initial load
duke
parents:
diff changeset
   423
                    //     Vector<Object> vec = new Vector<String>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   424
                    // But with wildcards you can write
06bc494ca11e Initial load
duke
parents:
diff changeset
   425
                    //     Vector<? extends Object> vec = new Vector<String>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   426
                    // which means that subtype checking must be done
06bc494ca11e Initial load
duke
parents:
diff changeset
   427
                    // here instead of same-type checking (via containsType).
06bc494ca11e Initial load
duke
parents:
diff changeset
   428
                    && (!s.isParameterized() || containsTypeRecursive(s, sup))
06bc494ca11e Initial load
duke
parents:
diff changeset
   429
                    && isSubtypeNoCapture(sup.getEnclosingType(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   430
                                          s.getEnclosingType());
06bc494ca11e Initial load
duke
parents:
diff changeset
   431
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   432
06bc494ca11e Initial load
duke
parents:
diff changeset
   433
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   434
            public Boolean visitArrayType(ArrayType t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   435
                if (s.tag == ARRAY) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   436
                    if (t.elemtype.tag <= lastBaseTag)
06bc494ca11e Initial load
duke
parents:
diff changeset
   437
                        return isSameType(t.elemtype, elemtype(s));
06bc494ca11e Initial load
duke
parents:
diff changeset
   438
                    else
06bc494ca11e Initial load
duke
parents:
diff changeset
   439
                        return isSubtypeNoCapture(t.elemtype, elemtype(s));
06bc494ca11e Initial load
duke
parents:
diff changeset
   440
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   441
06bc494ca11e Initial load
duke
parents:
diff changeset
   442
                if (s.tag == CLASS) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   443
                    Name sname = s.tsym.getQualifiedName();
06bc494ca11e Initial load
duke
parents:
diff changeset
   444
                    return sname == names.java_lang_Object
06bc494ca11e Initial load
duke
parents:
diff changeset
   445
                        || sname == names.java_lang_Cloneable
06bc494ca11e Initial load
duke
parents:
diff changeset
   446
                        || sname == names.java_io_Serializable;
06bc494ca11e Initial load
duke
parents:
diff changeset
   447
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   448
06bc494ca11e Initial load
duke
parents:
diff changeset
   449
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   450
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   451
06bc494ca11e Initial load
duke
parents:
diff changeset
   452
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   453
            public Boolean visitUndetVar(UndetVar t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   454
                //todo: test against origin needed? or replace with substitution?
06bc494ca11e Initial load
duke
parents:
diff changeset
   455
                if (t == s || t.qtype == s || s.tag == ERROR || s.tag == UNKNOWN)
06bc494ca11e Initial load
duke
parents:
diff changeset
   456
                    return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   457
06bc494ca11e Initial load
duke
parents:
diff changeset
   458
                if (t.inst != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   459
                    return isSubtypeNoCapture(t.inst, s); // TODO: ", warn"?
06bc494ca11e Initial load
duke
parents:
diff changeset
   460
06bc494ca11e Initial load
duke
parents:
diff changeset
   461
                t.hibounds = t.hibounds.prepend(s);
06bc494ca11e Initial load
duke
parents:
diff changeset
   462
                return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   463
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   464
06bc494ca11e Initial load
duke
parents:
diff changeset
   465
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   466
            public Boolean visitErrorType(ErrorType t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   467
                return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   468
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   469
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
   470
06bc494ca11e Initial load
duke
parents:
diff changeset
   471
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   472
     * Is t a subtype of every type in given list `ts'?<br>
06bc494ca11e Initial load
duke
parents:
diff changeset
   473
     * (not defined for Method and ForAll types)<br>
06bc494ca11e Initial load
duke
parents:
diff changeset
   474
     * Allows unchecked conversions.
06bc494ca11e Initial load
duke
parents:
diff changeset
   475
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   476
    public boolean isSubtypeUnchecked(Type t, List<Type> ts, Warner warn) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   477
        for (List<Type> l = ts; l.nonEmpty(); l = l.tail)
06bc494ca11e Initial load
duke
parents:
diff changeset
   478
            if (!isSubtypeUnchecked(t, l.head, warn))
06bc494ca11e Initial load
duke
parents:
diff changeset
   479
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   480
        return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   481
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   482
06bc494ca11e Initial load
duke
parents:
diff changeset
   483
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   484
     * Are corresponding elements of ts subtypes of ss?  If lists are
06bc494ca11e Initial load
duke
parents:
diff changeset
   485
     * of different length, return false.
06bc494ca11e Initial load
duke
parents:
diff changeset
   486
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   487
    public boolean isSubtypes(List<Type> ts, List<Type> ss) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   488
        while (ts.tail != null && ss.tail != null
06bc494ca11e Initial load
duke
parents:
diff changeset
   489
               /*inlined: ts.nonEmpty() && ss.nonEmpty()*/ &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   490
               isSubtype(ts.head, ss.head)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   491
            ts = ts.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   492
            ss = ss.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   493
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   494
        return ts.tail == null && ss.tail == null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   495
        /*inlined: ts.isEmpty() && ss.isEmpty();*/
06bc494ca11e Initial load
duke
parents:
diff changeset
   496
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   497
06bc494ca11e Initial load
duke
parents:
diff changeset
   498
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   499
     * Are corresponding elements of ts subtypes of ss, allowing
06bc494ca11e Initial load
duke
parents:
diff changeset
   500
     * unchecked conversions?  If lists are of different length,
06bc494ca11e Initial load
duke
parents:
diff changeset
   501
     * return false.
06bc494ca11e Initial load
duke
parents:
diff changeset
   502
     **/
06bc494ca11e Initial load
duke
parents:
diff changeset
   503
    public boolean isSubtypesUnchecked(List<Type> ts, List<Type> ss, Warner warn) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   504
        while (ts.tail != null && ss.tail != null
06bc494ca11e Initial load
duke
parents:
diff changeset
   505
               /*inlined: ts.nonEmpty() && ss.nonEmpty()*/ &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   506
               isSubtypeUnchecked(ts.head, ss.head, warn)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   507
            ts = ts.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   508
            ss = ss.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   509
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   510
        return ts.tail == null && ss.tail == null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   511
        /*inlined: ts.isEmpty() && ss.isEmpty();*/
06bc494ca11e Initial load
duke
parents:
diff changeset
   512
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   513
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
   514
06bc494ca11e Initial load
duke
parents:
diff changeset
   515
    // <editor-fold defaultstate="collapsed" desc="isSuperType">
06bc494ca11e Initial load
duke
parents:
diff changeset
   516
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   517
     * Is t a supertype of s?
06bc494ca11e Initial load
duke
parents:
diff changeset
   518
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   519
    public boolean isSuperType(Type t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   520
        switch (t.tag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   521
        case ERROR:
06bc494ca11e Initial load
duke
parents:
diff changeset
   522
            return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   523
        case UNDETVAR: {
06bc494ca11e Initial load
duke
parents:
diff changeset
   524
            UndetVar undet = (UndetVar)t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   525
            if (t == s ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   526
                undet.qtype == s ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   527
                s.tag == ERROR ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   528
                s.tag == BOT) return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   529
            if (undet.inst != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   530
                return isSubtype(s, undet.inst);
06bc494ca11e Initial load
duke
parents:
diff changeset
   531
            undet.lobounds = undet.lobounds.prepend(s);
06bc494ca11e Initial load
duke
parents:
diff changeset
   532
            return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   533
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   534
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   535
            return isSubtype(s, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   536
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   537
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   538
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
   539
06bc494ca11e Initial load
duke
parents:
diff changeset
   540
    // <editor-fold defaultstate="collapsed" desc="isSameType">
06bc494ca11e Initial load
duke
parents:
diff changeset
   541
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   542
     * Are corresponding elements of the lists the same type?  If
06bc494ca11e Initial load
duke
parents:
diff changeset
   543
     * lists are of different length, return false.
06bc494ca11e Initial load
duke
parents:
diff changeset
   544
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   545
    public boolean isSameTypes(List<Type> ts, List<Type> ss) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   546
        while (ts.tail != null && ss.tail != null
06bc494ca11e Initial load
duke
parents:
diff changeset
   547
               /*inlined: ts.nonEmpty() && ss.nonEmpty()*/ &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   548
               isSameType(ts.head, ss.head)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   549
            ts = ts.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   550
            ss = ss.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   551
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   552
        return ts.tail == null && ss.tail == null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   553
        /*inlined: ts.isEmpty() && ss.isEmpty();*/
06bc494ca11e Initial load
duke
parents:
diff changeset
   554
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   555
06bc494ca11e Initial load
duke
parents:
diff changeset
   556
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   557
     * Is t the same type as s?
06bc494ca11e Initial load
duke
parents:
diff changeset
   558
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   559
    public boolean isSameType(Type t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   560
        return isSameType.visit(t, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
   561
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   562
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
   563
        private TypeRelation isSameType = new TypeRelation() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   564
06bc494ca11e Initial load
duke
parents:
diff changeset
   565
            public Boolean visitType(Type t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   566
                if (t == s)
06bc494ca11e Initial load
duke
parents:
diff changeset
   567
                    return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   568
06bc494ca11e Initial load
duke
parents:
diff changeset
   569
                if (s.tag >= firstPartialTag)
06bc494ca11e Initial load
duke
parents:
diff changeset
   570
                    return visit(s, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   571
06bc494ca11e Initial load
duke
parents:
diff changeset
   572
                switch (t.tag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   573
                case BYTE: case CHAR: case SHORT: case INT: case LONG: case FLOAT:
06bc494ca11e Initial load
duke
parents:
diff changeset
   574
                case DOUBLE: case BOOLEAN: case VOID: case BOT: case NONE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   575
                    return t.tag == s.tag;
06bc494ca11e Initial load
duke
parents:
diff changeset
   576
                case TYPEVAR:
06bc494ca11e Initial load
duke
parents:
diff changeset
   577
                    return s.isSuperBound()
06bc494ca11e Initial load
duke
parents:
diff changeset
   578
                        && !s.isExtendsBound()
06bc494ca11e Initial load
duke
parents:
diff changeset
   579
                        && visit(t, upperBound(s));
06bc494ca11e Initial load
duke
parents:
diff changeset
   580
                default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   581
                    throw new AssertionError("isSameType " + t.tag);
06bc494ca11e Initial load
duke
parents:
diff changeset
   582
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   583
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   584
06bc494ca11e Initial load
duke
parents:
diff changeset
   585
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   586
            public Boolean visitWildcardType(WildcardType t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   587
                if (s.tag >= firstPartialTag)
06bc494ca11e Initial load
duke
parents:
diff changeset
   588
                    return visit(s, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   589
                else
06bc494ca11e Initial load
duke
parents:
diff changeset
   590
                    return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   591
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   592
06bc494ca11e Initial load
duke
parents:
diff changeset
   593
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   594
            public Boolean visitClassType(ClassType t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   595
                if (t == s)
06bc494ca11e Initial load
duke
parents:
diff changeset
   596
                    return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   597
06bc494ca11e Initial load
duke
parents:
diff changeset
   598
                if (s.tag >= firstPartialTag)
06bc494ca11e Initial load
duke
parents:
diff changeset
   599
                    return visit(s, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   600
06bc494ca11e Initial load
duke
parents:
diff changeset
   601
                if (s.isSuperBound() && !s.isExtendsBound())
06bc494ca11e Initial load
duke
parents:
diff changeset
   602
                    return visit(t, upperBound(s)) && visit(t, lowerBound(s));
06bc494ca11e Initial load
duke
parents:
diff changeset
   603
06bc494ca11e Initial load
duke
parents:
diff changeset
   604
                if (t.isCompound() && s.isCompound()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   605
                    if (!visit(supertype(t), supertype(s)))
06bc494ca11e Initial load
duke
parents:
diff changeset
   606
                        return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   607
06bc494ca11e Initial load
duke
parents:
diff changeset
   608
                    HashSet<SingletonType> set = new HashSet<SingletonType>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   609
                    for (Type x : interfaces(t))
06bc494ca11e Initial load
duke
parents:
diff changeset
   610
                        set.add(new SingletonType(x));
06bc494ca11e Initial load
duke
parents:
diff changeset
   611
                    for (Type x : interfaces(s)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   612
                        if (!set.remove(new SingletonType(x)))
06bc494ca11e Initial load
duke
parents:
diff changeset
   613
                            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   614
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   615
                    return (set.size() == 0);
06bc494ca11e Initial load
duke
parents:
diff changeset
   616
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   617
                return t.tsym == s.tsym
06bc494ca11e Initial load
duke
parents:
diff changeset
   618
                    && visit(t.getEnclosingType(), s.getEnclosingType())
06bc494ca11e Initial load
duke
parents:
diff changeset
   619
                    && containsTypeEquivalent(t.getTypeArguments(), s.getTypeArguments());
06bc494ca11e Initial load
duke
parents:
diff changeset
   620
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   621
06bc494ca11e Initial load
duke
parents:
diff changeset
   622
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   623
            public Boolean visitArrayType(ArrayType t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   624
                if (t == s)
06bc494ca11e Initial load
duke
parents:
diff changeset
   625
                    return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   626
06bc494ca11e Initial load
duke
parents:
diff changeset
   627
                if (s.tag >= firstPartialTag)
06bc494ca11e Initial load
duke
parents:
diff changeset
   628
                    return visit(s, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   629
06bc494ca11e Initial load
duke
parents:
diff changeset
   630
                return s.tag == ARRAY
06bc494ca11e Initial load
duke
parents:
diff changeset
   631
                    && containsTypeEquivalent(t.elemtype, elemtype(s));
06bc494ca11e Initial load
duke
parents:
diff changeset
   632
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   633
06bc494ca11e Initial load
duke
parents:
diff changeset
   634
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   635
            public Boolean visitMethodType(MethodType t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   636
                // isSameType for methods does not take thrown
06bc494ca11e Initial load
duke
parents:
diff changeset
   637
                // exceptions into account!
06bc494ca11e Initial load
duke
parents:
diff changeset
   638
                return hasSameArgs(t, s) && visit(t.getReturnType(), s.getReturnType());
06bc494ca11e Initial load
duke
parents:
diff changeset
   639
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   640
06bc494ca11e Initial load
duke
parents:
diff changeset
   641
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   642
            public Boolean visitPackageType(PackageType t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   643
                return t == s;
06bc494ca11e Initial load
duke
parents:
diff changeset
   644
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   645
06bc494ca11e Initial load
duke
parents:
diff changeset
   646
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   647
            public Boolean visitForAll(ForAll t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   648
                if (s.tag != FORALL)
06bc494ca11e Initial load
duke
parents:
diff changeset
   649
                    return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   650
06bc494ca11e Initial load
duke
parents:
diff changeset
   651
                ForAll forAll = (ForAll)s;
06bc494ca11e Initial load
duke
parents:
diff changeset
   652
                return hasSameBounds(t, forAll)
06bc494ca11e Initial load
duke
parents:
diff changeset
   653
                    && visit(t.qtype, subst(forAll.qtype, forAll.tvars, t.tvars));
06bc494ca11e Initial load
duke
parents:
diff changeset
   654
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   655
06bc494ca11e Initial load
duke
parents:
diff changeset
   656
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   657
            public Boolean visitUndetVar(UndetVar t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   658
                if (s.tag == WILDCARD)
06bc494ca11e Initial load
duke
parents:
diff changeset
   659
                    // FIXME, this might be leftovers from before capture conversion
06bc494ca11e Initial load
duke
parents:
diff changeset
   660
                    return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   661
06bc494ca11e Initial load
duke
parents:
diff changeset
   662
                if (t == s || t.qtype == s || s.tag == ERROR || s.tag == UNKNOWN)
06bc494ca11e Initial load
duke
parents:
diff changeset
   663
                    return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   664
06bc494ca11e Initial load
duke
parents:
diff changeset
   665
                if (t.inst != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   666
                    return visit(t.inst, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
   667
06bc494ca11e Initial load
duke
parents:
diff changeset
   668
                t.inst = fromUnknownFun.apply(s);
06bc494ca11e Initial load
duke
parents:
diff changeset
   669
                for (List<Type> l = t.lobounds; l.nonEmpty(); l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   670
                    if (!isSubtype(l.head, t.inst))
06bc494ca11e Initial load
duke
parents:
diff changeset
   671
                        return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   672
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   673
                for (List<Type> l = t.hibounds; l.nonEmpty(); l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   674
                    if (!isSubtype(t.inst, l.head))
06bc494ca11e Initial load
duke
parents:
diff changeset
   675
                        return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   676
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   677
                return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   678
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   679
06bc494ca11e Initial load
duke
parents:
diff changeset
   680
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   681
            public Boolean visitErrorType(ErrorType t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   682
                return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   683
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   684
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
   685
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
   686
06bc494ca11e Initial load
duke
parents:
diff changeset
   687
    // <editor-fold defaultstate="collapsed" desc="fromUnknownFun">
06bc494ca11e Initial load
duke
parents:
diff changeset
   688
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   689
     * A mapping that turns all unknown types in this type to fresh
06bc494ca11e Initial load
duke
parents:
diff changeset
   690
     * unknown variables.
06bc494ca11e Initial load
duke
parents:
diff changeset
   691
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   692
    public Mapping fromUnknownFun = new Mapping("fromUnknownFun") {
06bc494ca11e Initial load
duke
parents:
diff changeset
   693
            public Type apply(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   694
                if (t.tag == UNKNOWN) return new UndetVar(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   695
                else return t.map(this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   696
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   697
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
   698
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
   699
06bc494ca11e Initial load
duke
parents:
diff changeset
   700
    // <editor-fold defaultstate="collapsed" desc="Contains Type">
06bc494ca11e Initial load
duke
parents:
diff changeset
   701
    public boolean containedBy(Type t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   702
        switch (t.tag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   703
        case UNDETVAR:
06bc494ca11e Initial load
duke
parents:
diff changeset
   704
            if (s.tag == WILDCARD) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   705
                UndetVar undetvar = (UndetVar)t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   706
06bc494ca11e Initial load
duke
parents:
diff changeset
   707
                // Because of wildcard capture, s must be on the left
06bc494ca11e Initial load
duke
parents:
diff changeset
   708
                // hand side of an assignment.  Furthermore, t is an
06bc494ca11e Initial load
duke
parents:
diff changeset
   709
                // underconstrained type variable, for example, one
06bc494ca11e Initial load
duke
parents:
diff changeset
   710
                // that is only used in the return type of a method.
06bc494ca11e Initial load
duke
parents:
diff changeset
   711
                // If the type variable is truly underconstrained, it
06bc494ca11e Initial load
duke
parents:
diff changeset
   712
                // cannot have any low bounds:
06bc494ca11e Initial load
duke
parents:
diff changeset
   713
                assert undetvar.lobounds.isEmpty() : undetvar;
06bc494ca11e Initial load
duke
parents:
diff changeset
   714
06bc494ca11e Initial load
duke
parents:
diff changeset
   715
                undetvar.inst = glb(upperBound(s), undetvar.inst);
06bc494ca11e Initial load
duke
parents:
diff changeset
   716
                return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   717
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   718
                return isSameType(t, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
   719
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   720
        case ERROR:
06bc494ca11e Initial load
duke
parents:
diff changeset
   721
            return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   722
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   723
            return containsType(s, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   724
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   725
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   726
06bc494ca11e Initial load
duke
parents:
diff changeset
   727
    boolean containsType(List<Type> ts, List<Type> ss) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   728
        while (ts.nonEmpty() && ss.nonEmpty()
06bc494ca11e Initial load
duke
parents:
diff changeset
   729
               && containsType(ts.head, ss.head)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   730
            ts = ts.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   731
            ss = ss.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   732
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   733
        return ts.isEmpty() && ss.isEmpty();
06bc494ca11e Initial load
duke
parents:
diff changeset
   734
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   735
06bc494ca11e Initial load
duke
parents:
diff changeset
   736
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   737
     * Check if t contains s.
06bc494ca11e Initial load
duke
parents:
diff changeset
   738
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   739
     * <p>T contains S if:
06bc494ca11e Initial load
duke
parents:
diff changeset
   740
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   741
     * <p>{@code L(T) <: L(S) && U(S) <: U(T)}
06bc494ca11e Initial load
duke
parents:
diff changeset
   742
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   743
     * <p>This relation is only used by ClassType.isSubtype(), that
06bc494ca11e Initial load
duke
parents:
diff changeset
   744
     * is,
06bc494ca11e Initial load
duke
parents:
diff changeset
   745
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   746
     * <p>{@code C<S> <: C<T> if T contains S.}
06bc494ca11e Initial load
duke
parents:
diff changeset
   747
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   748
     * <p>Because of F-bounds, this relation can lead to infinite
06bc494ca11e Initial load
duke
parents:
diff changeset
   749
     * recursion.  Thus we must somehow break that recursion.  Notice
06bc494ca11e Initial load
duke
parents:
diff changeset
   750
     * that containsType() is only called from ClassType.isSubtype().
06bc494ca11e Initial load
duke
parents:
diff changeset
   751
     * Since the arguments have already been checked against their
06bc494ca11e Initial load
duke
parents:
diff changeset
   752
     * bounds, we know:
06bc494ca11e Initial load
duke
parents:
diff changeset
   753
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   754
     * <p>{@code U(S) <: U(T) if T is "super" bound (U(T) *is* the bound)}
06bc494ca11e Initial load
duke
parents:
diff changeset
   755
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   756
     * <p>{@code L(T) <: L(S) if T is "extends" bound (L(T) is bottom)}
06bc494ca11e Initial load
duke
parents:
diff changeset
   757
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   758
     * @param t a type
06bc494ca11e Initial load
duke
parents:
diff changeset
   759
     * @param s a type
06bc494ca11e Initial load
duke
parents:
diff changeset
   760
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   761
    public boolean containsType(Type t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   762
        return containsType.visit(t, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
   763
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   764
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
   765
        private TypeRelation containsType = new TypeRelation() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   766
06bc494ca11e Initial load
duke
parents:
diff changeset
   767
            private Type U(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   768
                while (t.tag == WILDCARD) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   769
                    WildcardType w = (WildcardType)t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   770
                    if (w.isSuperBound())
06bc494ca11e Initial load
duke
parents:
diff changeset
   771
                        return w.bound == null ? syms.objectType : w.bound.bound;
06bc494ca11e Initial load
duke
parents:
diff changeset
   772
                    else
06bc494ca11e Initial load
duke
parents:
diff changeset
   773
                        t = w.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
   774
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   775
                return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   776
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   777
06bc494ca11e Initial load
duke
parents:
diff changeset
   778
            private Type L(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   779
                while (t.tag == WILDCARD) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   780
                    WildcardType w = (WildcardType)t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   781
                    if (w.isExtendsBound())
06bc494ca11e Initial load
duke
parents:
diff changeset
   782
                        return syms.botType;
06bc494ca11e Initial load
duke
parents:
diff changeset
   783
                    else
06bc494ca11e Initial load
duke
parents:
diff changeset
   784
                        t = w.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
   785
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   786
                return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   787
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   788
06bc494ca11e Initial load
duke
parents:
diff changeset
   789
            public Boolean visitType(Type t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   790
                if (s.tag >= firstPartialTag)
06bc494ca11e Initial load
duke
parents:
diff changeset
   791
                    return containedBy(s, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   792
                else
06bc494ca11e Initial load
duke
parents:
diff changeset
   793
                    return isSameType(t, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
   794
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   795
06bc494ca11e Initial load
duke
parents:
diff changeset
   796
            void debugContainsType(WildcardType t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   797
                System.err.println();
06bc494ca11e Initial load
duke
parents:
diff changeset
   798
                System.err.format(" does %s contain %s?%n", t, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
   799
                System.err.format(" %s U(%s) <: U(%s) %s = %s%n",
06bc494ca11e Initial load
duke
parents:
diff changeset
   800
                                  upperBound(s), s, t, U(t),
06bc494ca11e Initial load
duke
parents:
diff changeset
   801
                                  t.isSuperBound()
06bc494ca11e Initial load
duke
parents:
diff changeset
   802
                                  || isSubtypeNoCapture(upperBound(s), U(t)));
06bc494ca11e Initial load
duke
parents:
diff changeset
   803
                System.err.format(" %s L(%s) <: L(%s) %s = %s%n",
06bc494ca11e Initial load
duke
parents:
diff changeset
   804
                                  L(t), t, s, lowerBound(s),
06bc494ca11e Initial load
duke
parents:
diff changeset
   805
                                  t.isExtendsBound()
06bc494ca11e Initial load
duke
parents:
diff changeset
   806
                                  || isSubtypeNoCapture(L(t), lowerBound(s)));
06bc494ca11e Initial load
duke
parents:
diff changeset
   807
                System.err.println();
06bc494ca11e Initial load
duke
parents:
diff changeset
   808
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   809
06bc494ca11e Initial load
duke
parents:
diff changeset
   810
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   811
            public Boolean visitWildcardType(WildcardType t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   812
                if (s.tag >= firstPartialTag)
06bc494ca11e Initial load
duke
parents:
diff changeset
   813
                    return containedBy(s, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   814
                else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   815
                    // debugContainsType(t, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
   816
                    return isSameWildcard(t, s)
06bc494ca11e Initial load
duke
parents:
diff changeset
   817
                        || isCaptureOf(s, t)
06bc494ca11e Initial load
duke
parents:
diff changeset
   818
                        || ((t.isExtendsBound() || isSubtypeNoCapture(L(t), lowerBound(s))) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   819
                            (t.isSuperBound() || isSubtypeNoCapture(upperBound(s), U(t))));
06bc494ca11e Initial load
duke
parents:
diff changeset
   820
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   821
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   822
06bc494ca11e Initial load
duke
parents:
diff changeset
   823
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   824
            public Boolean visitUndetVar(UndetVar t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   825
                if (s.tag != WILDCARD)
06bc494ca11e Initial load
duke
parents:
diff changeset
   826
                    return isSameType(t, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
   827
                else
06bc494ca11e Initial load
duke
parents:
diff changeset
   828
                    return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   829
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   830
06bc494ca11e Initial load
duke
parents:
diff changeset
   831
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   832
            public Boolean visitErrorType(ErrorType t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   833
                return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   834
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   835
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
   836
06bc494ca11e Initial load
duke
parents:
diff changeset
   837
    public boolean isCaptureOf(Type s, WildcardType t) {
938
13aae74ca013 6594284: NPE thrown when calling a method on an intersection type
mcimadamore
parents: 735
diff changeset
   838
        if (s.tag != TYPEVAR || !((TypeVar)s).isCaptured())
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   839
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   840
        return isSameWildcard(t, ((CapturedType)s).wildcard);
06bc494ca11e Initial load
duke
parents:
diff changeset
   841
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   842
06bc494ca11e Initial load
duke
parents:
diff changeset
   843
    public boolean isSameWildcard(WildcardType t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   844
        if (s.tag != WILDCARD)
06bc494ca11e Initial load
duke
parents:
diff changeset
   845
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   846
        WildcardType w = (WildcardType)s;
06bc494ca11e Initial load
duke
parents:
diff changeset
   847
        return w.kind == t.kind && w.type == t.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
   848
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   849
06bc494ca11e Initial load
duke
parents:
diff changeset
   850
    public boolean containsTypeEquivalent(List<Type> ts, List<Type> ss) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   851
        while (ts.nonEmpty() && ss.nonEmpty()
06bc494ca11e Initial load
duke
parents:
diff changeset
   852
               && containsTypeEquivalent(ts.head, ss.head)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   853
            ts = ts.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   854
            ss = ss.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   855
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   856
        return ts.isEmpty() && ss.isEmpty();
06bc494ca11e Initial load
duke
parents:
diff changeset
   857
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   858
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
   859
06bc494ca11e Initial load
duke
parents:
diff changeset
   860
    // <editor-fold defaultstate="collapsed" desc="isCastable">
06bc494ca11e Initial load
duke
parents:
diff changeset
   861
    public boolean isCastable(Type t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   862
        return isCastable(t, s, Warner.noWarnings);
06bc494ca11e Initial load
duke
parents:
diff changeset
   863
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   864
06bc494ca11e Initial load
duke
parents:
diff changeset
   865
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   866
     * Is t is castable to s?<br>
06bc494ca11e Initial load
duke
parents:
diff changeset
   867
     * s is assumed to be an erased type.<br>
06bc494ca11e Initial load
duke
parents:
diff changeset
   868
     * (not defined for Method and ForAll types).
06bc494ca11e Initial load
duke
parents:
diff changeset
   869
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   870
    public boolean isCastable(Type t, Type s, Warner warn) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   871
        if (t == s)
06bc494ca11e Initial load
duke
parents:
diff changeset
   872
            return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   873
06bc494ca11e Initial load
duke
parents:
diff changeset
   874
        if (t.isPrimitive() != s.isPrimitive())
06bc494ca11e Initial load
duke
parents:
diff changeset
   875
            return allowBoxing && isConvertible(t, s, warn);
06bc494ca11e Initial load
duke
parents:
diff changeset
   876
06bc494ca11e Initial load
duke
parents:
diff changeset
   877
        if (warn != warnStack.head) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   878
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   879
                warnStack = warnStack.prepend(warn);
06bc494ca11e Initial load
duke
parents:
diff changeset
   880
                return isCastable.visit(t, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
   881
            } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   882
                warnStack = warnStack.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   883
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   884
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   885
            return isCastable.visit(t, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
   886
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   887
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   888
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
   889
        private TypeRelation isCastable = new TypeRelation() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   890
06bc494ca11e Initial load
duke
parents:
diff changeset
   891
            public Boolean visitType(Type t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   892
                if (s.tag == ERROR)
06bc494ca11e Initial load
duke
parents:
diff changeset
   893
                    return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   894
06bc494ca11e Initial load
duke
parents:
diff changeset
   895
                switch (t.tag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   896
                case BYTE: case CHAR: case SHORT: case INT: case LONG: case FLOAT:
06bc494ca11e Initial load
duke
parents:
diff changeset
   897
                case DOUBLE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   898
                    return s.tag <= DOUBLE;
06bc494ca11e Initial load
duke
parents:
diff changeset
   899
                case BOOLEAN:
06bc494ca11e Initial load
duke
parents:
diff changeset
   900
                    return s.tag == BOOLEAN;
06bc494ca11e Initial load
duke
parents:
diff changeset
   901
                case VOID:
06bc494ca11e Initial load
duke
parents:
diff changeset
   902
                    return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   903
                case BOT:
06bc494ca11e Initial load
duke
parents:
diff changeset
   904
                    return isSubtype(t, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
   905
                default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   906
                    throw new AssertionError();
06bc494ca11e Initial load
duke
parents:
diff changeset
   907
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   908
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   909
06bc494ca11e Initial load
duke
parents:
diff changeset
   910
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   911
            public Boolean visitWildcardType(WildcardType t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   912
                return isCastable(upperBound(t), s, warnStack.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
   913
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   914
06bc494ca11e Initial load
duke
parents:
diff changeset
   915
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   916
            public Boolean visitClassType(ClassType t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   917
                if (s.tag == ERROR || s.tag == BOT)
06bc494ca11e Initial load
duke
parents:
diff changeset
   918
                    return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   919
06bc494ca11e Initial load
duke
parents:
diff changeset
   920
                if (s.tag == TYPEVAR) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   921
                    if (isCastable(s.getUpperBound(), t, Warner.noWarnings)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   922
                        warnStack.head.warnUnchecked();
06bc494ca11e Initial load
duke
parents:
diff changeset
   923
                        return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   924
                    } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   925
                        return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   926
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   927
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   928
06bc494ca11e Initial load
duke
parents:
diff changeset
   929
                if (t.isCompound()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   930
                    if (!visit(supertype(t), s))
06bc494ca11e Initial load
duke
parents:
diff changeset
   931
                        return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   932
                    for (Type intf : interfaces(t)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   933
                        if (!visit(intf, s))
06bc494ca11e Initial load
duke
parents:
diff changeset
   934
                            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   935
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   936
                    return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   937
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   938
06bc494ca11e Initial load
duke
parents:
diff changeset
   939
                if (s.isCompound()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   940
                    // call recursively to reuse the above code
06bc494ca11e Initial load
duke
parents:
diff changeset
   941
                    return visitClassType((ClassType)s, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   942
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   943
06bc494ca11e Initial load
duke
parents:
diff changeset
   944
                if (s.tag == CLASS || s.tag == ARRAY) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   945
                    boolean upcast;
06bc494ca11e Initial load
duke
parents:
diff changeset
   946
                    if ((upcast = isSubtype(erasure(t), erasure(s)))
06bc494ca11e Initial load
duke
parents:
diff changeset
   947
                        || isSubtype(erasure(s), erasure(t))) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   948
                        if (!upcast && s.tag == ARRAY) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   949
                            if (!isReifiable(s))
06bc494ca11e Initial load
duke
parents:
diff changeset
   950
                                warnStack.head.warnUnchecked();
06bc494ca11e Initial load
duke
parents:
diff changeset
   951
                            return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   952
                        } else if (s.isRaw()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   953
                            return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   954
                        } else if (t.isRaw()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   955
                            if (!isUnbounded(s))
06bc494ca11e Initial load
duke
parents:
diff changeset
   956
                                warnStack.head.warnUnchecked();
06bc494ca11e Initial load
duke
parents:
diff changeset
   957
                            return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   958
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   959
                        // Assume |a| <: |b|
06bc494ca11e Initial load
duke
parents:
diff changeset
   960
                        final Type a = upcast ? t : s;
06bc494ca11e Initial load
duke
parents:
diff changeset
   961
                        final Type b = upcast ? s : t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   962
                        final boolean HIGH = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   963
                        final boolean LOW = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   964
                        final boolean DONT_REWRITE_TYPEVARS = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   965
                        Type aHigh = rewriteQuantifiers(a, HIGH, DONT_REWRITE_TYPEVARS);
06bc494ca11e Initial load
duke
parents:
diff changeset
   966
                        Type aLow  = rewriteQuantifiers(a, LOW,  DONT_REWRITE_TYPEVARS);
06bc494ca11e Initial load
duke
parents:
diff changeset
   967
                        Type bHigh = rewriteQuantifiers(b, HIGH, DONT_REWRITE_TYPEVARS);
06bc494ca11e Initial load
duke
parents:
diff changeset
   968
                        Type bLow  = rewriteQuantifiers(b, LOW,  DONT_REWRITE_TYPEVARS);
06bc494ca11e Initial load
duke
parents:
diff changeset
   969
                        Type lowSub = asSub(bLow, aLow.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   970
                        Type highSub = (lowSub == null) ? null : asSub(bHigh, aHigh.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   971
                        if (highSub == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   972
                            final boolean REWRITE_TYPEVARS = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   973
                            aHigh = rewriteQuantifiers(a, HIGH, REWRITE_TYPEVARS);
06bc494ca11e Initial load
duke
parents:
diff changeset
   974
                            aLow  = rewriteQuantifiers(a, LOW,  REWRITE_TYPEVARS);
06bc494ca11e Initial load
duke
parents:
diff changeset
   975
                            bHigh = rewriteQuantifiers(b, HIGH, REWRITE_TYPEVARS);
06bc494ca11e Initial load
duke
parents:
diff changeset
   976
                            bLow  = rewriteQuantifiers(b, LOW,  REWRITE_TYPEVARS);
06bc494ca11e Initial load
duke
parents:
diff changeset
   977
                            lowSub = asSub(bLow, aLow.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   978
                            highSub = (lowSub == null) ? null : asSub(bHigh, aHigh.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   979
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   980
                        if (highSub != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   981
                            assert a.tsym == highSub.tsym && a.tsym == lowSub.tsym
06bc494ca11e Initial load
duke
parents:
diff changeset
   982
                                : a.tsym + " != " + highSub.tsym + " != " + lowSub.tsym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   983
                            if (!disjointTypes(aHigh.getTypeArguments(), highSub.getTypeArguments())
06bc494ca11e Initial load
duke
parents:
diff changeset
   984
                                && !disjointTypes(aHigh.getTypeArguments(), lowSub.getTypeArguments())
06bc494ca11e Initial load
duke
parents:
diff changeset
   985
                                && !disjointTypes(aLow.getTypeArguments(), highSub.getTypeArguments())
06bc494ca11e Initial load
duke
parents:
diff changeset
   986
                                && !disjointTypes(aLow.getTypeArguments(), lowSub.getTypeArguments())) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   987
                                if (upcast ? giveWarning(a, highSub) || giveWarning(a, lowSub)
06bc494ca11e Initial load
duke
parents:
diff changeset
   988
                                           : giveWarning(highSub, a) || giveWarning(lowSub, a))
06bc494ca11e Initial load
duke
parents:
diff changeset
   989
                                    warnStack.head.warnUnchecked();
06bc494ca11e Initial load
duke
parents:
diff changeset
   990
                                return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   991
                            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   992
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   993
                        if (isReifiable(s))
06bc494ca11e Initial load
duke
parents:
diff changeset
   994
                            return isSubtypeUnchecked(a, b);
06bc494ca11e Initial load
duke
parents:
diff changeset
   995
                        else
06bc494ca11e Initial load
duke
parents:
diff changeset
   996
                            return isSubtypeUnchecked(a, b, warnStack.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
   997
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   998
06bc494ca11e Initial load
duke
parents:
diff changeset
   999
                    // Sidecast
06bc494ca11e Initial load
duke
parents:
diff changeset
  1000
                    if (s.tag == CLASS) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1001
                        if ((s.tsym.flags() & INTERFACE) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1002
                            return ((t.tsym.flags() & FINAL) == 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1003
                                ? sideCast(t, s, warnStack.head)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1004
                                : sideCastFinal(t, s, warnStack.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1005
                        } else if ((t.tsym.flags() & INTERFACE) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1006
                            return ((s.tsym.flags() & FINAL) == 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1007
                                ? sideCast(t, s, warnStack.head)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1008
                                : sideCastFinal(t, s, warnStack.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1009
                        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1010
                            // unrelated class types
06bc494ca11e Initial load
duke
parents:
diff changeset
  1011
                            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1012
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1013
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1014
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1015
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1016
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1017
06bc494ca11e Initial load
duke
parents:
diff changeset
  1018
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1019
            public Boolean visitArrayType(ArrayType t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1020
                switch (s.tag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1021
                case ERROR:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1022
                case BOT:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1023
                    return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1024
                case TYPEVAR:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1025
                    if (isCastable(s, t, Warner.noWarnings)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1026
                        warnStack.head.warnUnchecked();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1027
                        return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1028
                    } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1029
                        return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1030
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1031
                case CLASS:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1032
                    return isSubtype(t, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1033
                case ARRAY:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1034
                    if (elemtype(t).tag <= lastBaseTag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1035
                        return elemtype(t).tag == elemtype(s).tag;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1036
                    } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1037
                        return visit(elemtype(t), elemtype(s));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1038
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1039
                default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1040
                    return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1041
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1042
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1043
06bc494ca11e Initial load
duke
parents:
diff changeset
  1044
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1045
            public Boolean visitTypeVar(TypeVar t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1046
                switch (s.tag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1047
                case ERROR:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1048
                case BOT:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1049
                    return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1050
                case TYPEVAR:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1051
                    if (isSubtype(t, s)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1052
                        return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1053
                    } else if (isCastable(t.bound, s, Warner.noWarnings)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1054
                        warnStack.head.warnUnchecked();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1055
                        return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1056
                    } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1057
                        return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1058
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1059
                default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1060
                    return isCastable(t.bound, s, warnStack.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1061
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1062
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1063
06bc494ca11e Initial load
duke
parents:
diff changeset
  1064
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1065
            public Boolean visitErrorType(ErrorType t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1066
                return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1067
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1068
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
  1069
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  1070
06bc494ca11e Initial load
duke
parents:
diff changeset
  1071
    // <editor-fold defaultstate="collapsed" desc="disjointTypes">
06bc494ca11e Initial load
duke
parents:
diff changeset
  1072
    public boolean disjointTypes(List<Type> ts, List<Type> ss) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1073
        while (ts.tail != null && ss.tail != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1074
            if (disjointType(ts.head, ss.head)) return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1075
            ts = ts.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1076
            ss = ss.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1077
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1078
        return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1079
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1080
06bc494ca11e Initial load
duke
parents:
diff changeset
  1081
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1082
     * Two types or wildcards are considered disjoint if it can be
06bc494ca11e Initial load
duke
parents:
diff changeset
  1083
     * proven that no type can be contained in both. It is
06bc494ca11e Initial load
duke
parents:
diff changeset
  1084
     * conservative in that it is allowed to say that two types are
06bc494ca11e Initial load
duke
parents:
diff changeset
  1085
     * not disjoint, even though they actually are.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1086
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  1087
     * The type C<X> is castable to C<Y> exactly if X and Y are not
06bc494ca11e Initial load
duke
parents:
diff changeset
  1088
     * disjoint.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1089
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1090
    public boolean disjointType(Type t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1091
        return disjointType.visit(t, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1092
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1093
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
  1094
        private TypeRelation disjointType = new TypeRelation() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1095
06bc494ca11e Initial load
duke
parents:
diff changeset
  1096
            private Set<TypePair> cache = new HashSet<TypePair>();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1097
06bc494ca11e Initial load
duke
parents:
diff changeset
  1098
            public Boolean visitType(Type t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1099
                if (s.tag == WILDCARD)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1100
                    return visit(s, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1101
                else
06bc494ca11e Initial load
duke
parents:
diff changeset
  1102
                    return notSoftSubtypeRecursive(t, s) || notSoftSubtypeRecursive(s, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1103
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1104
06bc494ca11e Initial load
duke
parents:
diff changeset
  1105
            private boolean isCastableRecursive(Type t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1106
                TypePair pair = new TypePair(t, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1107
                if (cache.add(pair)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1108
                    try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1109
                        return Types.this.isCastable(t, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1110
                    } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1111
                        cache.remove(pair);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1112
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1113
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1114
                    return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1115
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1116
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1117
06bc494ca11e Initial load
duke
parents:
diff changeset
  1118
            private boolean notSoftSubtypeRecursive(Type t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1119
                TypePair pair = new TypePair(t, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1120
                if (cache.add(pair)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1121
                    try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1122
                        return Types.this.notSoftSubtype(t, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1123
                    } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1124
                        cache.remove(pair);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1125
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1126
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1127
                    return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1128
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1129
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1130
06bc494ca11e Initial load
duke
parents:
diff changeset
  1131
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1132
            public Boolean visitWildcardType(WildcardType t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1133
                if (t.isUnbound())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1134
                    return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1135
06bc494ca11e Initial load
duke
parents:
diff changeset
  1136
                if (s.tag != WILDCARD) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1137
                    if (t.isExtendsBound())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1138
                        return notSoftSubtypeRecursive(s, t.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1139
                    else // isSuperBound()
06bc494ca11e Initial load
duke
parents:
diff changeset
  1140
                        return notSoftSubtypeRecursive(t.type, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1141
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1142
06bc494ca11e Initial load
duke
parents:
diff changeset
  1143
                if (s.isUnbound())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1144
                    return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1145
06bc494ca11e Initial load
duke
parents:
diff changeset
  1146
                if (t.isExtendsBound()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1147
                    if (s.isExtendsBound())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1148
                        return !isCastableRecursive(t.type, upperBound(s));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1149
                    else if (s.isSuperBound())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1150
                        return notSoftSubtypeRecursive(lowerBound(s), t.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1151
                } else if (t.isSuperBound()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1152
                    if (s.isExtendsBound())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1153
                        return notSoftSubtypeRecursive(t.type, upperBound(s));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1154
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1155
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1156
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1157
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
  1158
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  1159
06bc494ca11e Initial load
duke
parents:
diff changeset
  1160
    // <editor-fold defaultstate="collapsed" desc="lowerBoundArgtypes">
06bc494ca11e Initial load
duke
parents:
diff changeset
  1161
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1162
     * Returns the lower bounds of the formals of a method.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1163
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1164
    public List<Type> lowerBoundArgtypes(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1165
        return map(t.getParameterTypes(), lowerBoundMapping);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1166
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1167
    private final Mapping lowerBoundMapping = new Mapping("lowerBound") {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1168
            public Type apply(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1169
                return lowerBound(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1170
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1171
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
  1172
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  1173
06bc494ca11e Initial load
duke
parents:
diff changeset
  1174
    // <editor-fold defaultstate="collapsed" desc="notSoftSubtype">
06bc494ca11e Initial load
duke
parents:
diff changeset
  1175
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1176
     * This relation answers the question: is impossible that
06bc494ca11e Initial load
duke
parents:
diff changeset
  1177
     * something of type `t' can be a subtype of `s'? This is
06bc494ca11e Initial load
duke
parents:
diff changeset
  1178
     * different from the question "is `t' not a subtype of `s'?"
06bc494ca11e Initial load
duke
parents:
diff changeset
  1179
     * when type variables are involved: Integer is not a subtype of T
06bc494ca11e Initial load
duke
parents:
diff changeset
  1180
     * where <T extends Number> but it is not true that Integer cannot
06bc494ca11e Initial load
duke
parents:
diff changeset
  1181
     * possibly be a subtype of T.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1182
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1183
    public boolean notSoftSubtype(Type t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1184
        if (t == s) return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1185
        if (t.tag == TYPEVAR) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1186
            TypeVar tv = (TypeVar) t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1187
            if (s.tag == TYPEVAR)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1188
                s = s.getUpperBound();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1189
            return !isCastable(tv.bound,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1190
                               s,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1191
                               Warner.noWarnings);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1192
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1193
        if (s.tag != WILDCARD)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1194
            s = upperBound(s);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1195
        if (s.tag == TYPEVAR)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1196
            s = s.getUpperBound();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1197
        return !isSubtype(t, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1198
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1199
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  1200
06bc494ca11e Initial load
duke
parents:
diff changeset
  1201
    // <editor-fold defaultstate="collapsed" desc="isReifiable">
06bc494ca11e Initial load
duke
parents:
diff changeset
  1202
    public boolean isReifiable(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1203
        return isReifiable.visit(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1204
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1205
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
  1206
        private UnaryVisitor<Boolean> isReifiable = new UnaryVisitor<Boolean>() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1207
06bc494ca11e Initial load
duke
parents:
diff changeset
  1208
            public Boolean visitType(Type t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1209
                return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1210
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1211
06bc494ca11e Initial load
duke
parents:
diff changeset
  1212
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1213
            public Boolean visitClassType(ClassType t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1214
                if (!t.isParameterized())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1215
                    return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1216
06bc494ca11e Initial load
duke
parents:
diff changeset
  1217
                for (Type param : t.allparams()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1218
                    if (!param.isUnbound())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1219
                        return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1220
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1221
                return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1222
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1223
06bc494ca11e Initial load
duke
parents:
diff changeset
  1224
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1225
            public Boolean visitArrayType(ArrayType t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1226
                return visit(t.elemtype);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1227
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1228
06bc494ca11e Initial load
duke
parents:
diff changeset
  1229
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1230
            public Boolean visitTypeVar(TypeVar t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1231
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1232
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1233
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
  1234
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  1235
06bc494ca11e Initial load
duke
parents:
diff changeset
  1236
    // <editor-fold defaultstate="collapsed" desc="Array Utils">
06bc494ca11e Initial load
duke
parents:
diff changeset
  1237
    public boolean isArray(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1238
        while (t.tag == WILDCARD)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1239
            t = upperBound(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1240
        return t.tag == ARRAY;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1241
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1242
06bc494ca11e Initial load
duke
parents:
diff changeset
  1243
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1244
     * The element type of an array.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1245
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1246
    public Type elemtype(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1247
        switch (t.tag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1248
        case WILDCARD:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1249
            return elemtype(upperBound(t));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1250
        case ARRAY:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1251
            return ((ArrayType)t).elemtype;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1252
        case FORALL:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1253
            return elemtype(((ForAll)t).qtype);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1254
        case ERROR:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1255
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1256
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1257
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1258
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1259
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1260
06bc494ca11e Initial load
duke
parents:
diff changeset
  1261
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1262
     * Mapping to take element type of an arraytype
06bc494ca11e Initial load
duke
parents:
diff changeset
  1263
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1264
    private Mapping elemTypeFun = new Mapping ("elemTypeFun") {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1265
        public Type apply(Type t) { return elemtype(t); }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1266
    };
06bc494ca11e Initial load
duke
parents:
diff changeset
  1267
06bc494ca11e Initial load
duke
parents:
diff changeset
  1268
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1269
     * The number of dimensions of an array type.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1270
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1271
    public int dimensions(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1272
        int result = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1273
        while (t.tag == ARRAY) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1274
            result++;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1275
            t = elemtype(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1276
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1277
        return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1278
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1279
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  1280
06bc494ca11e Initial load
duke
parents:
diff changeset
  1281
    // <editor-fold defaultstate="collapsed" desc="asSuper">
06bc494ca11e Initial load
duke
parents:
diff changeset
  1282
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1283
     * Return the (most specific) base type of t that starts with the
06bc494ca11e Initial load
duke
parents:
diff changeset
  1284
     * given symbol.  If none exists, return null.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1285
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  1286
     * @param t a type
06bc494ca11e Initial load
duke
parents:
diff changeset
  1287
     * @param sym a symbol
06bc494ca11e Initial load
duke
parents:
diff changeset
  1288
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1289
    public Type asSuper(Type t, Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1290
        return asSuper.visit(t, sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1291
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1292
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
  1293
        private SimpleVisitor<Type,Symbol> asSuper = new SimpleVisitor<Type,Symbol>() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1294
06bc494ca11e Initial load
duke
parents:
diff changeset
  1295
            public Type visitType(Type t, Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1296
                return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1297
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1298
06bc494ca11e Initial load
duke
parents:
diff changeset
  1299
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1300
            public Type visitClassType(ClassType t, Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1301
                if (t.tsym == sym)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1302
                    return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1303
06bc494ca11e Initial load
duke
parents:
diff changeset
  1304
                Type st = supertype(t);
326
d51f30ce6796 6531090: Cannot access methods/fields of a captured type belonging to an intersection type
mcimadamore
parents: 162
diff changeset
  1305
                if (st.tag == CLASS || st.tag == TYPEVAR || st.tag == ERROR) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1306
                    Type x = asSuper(st, sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1307
                    if (x != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1308
                        return x;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1309
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1310
                if ((sym.flags() & INTERFACE) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1311
                    for (List<Type> l = interfaces(t); l.nonEmpty(); l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1312
                        Type x = asSuper(l.head, sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1313
                        if (x != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1314
                            return x;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1315
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1316
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1317
                return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1318
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1319
06bc494ca11e Initial load
duke
parents:
diff changeset
  1320
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1321
            public Type visitArrayType(ArrayType t, Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1322
                return isSubtype(t, sym.type) ? sym.type : null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1323
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1324
06bc494ca11e Initial load
duke
parents:
diff changeset
  1325
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1326
            public Type visitTypeVar(TypeVar t, Symbol sym) {
326
d51f30ce6796 6531090: Cannot access methods/fields of a captured type belonging to an intersection type
mcimadamore
parents: 162
diff changeset
  1327
                if (t.tsym == sym)
d51f30ce6796 6531090: Cannot access methods/fields of a captured type belonging to an intersection type
mcimadamore
parents: 162
diff changeset
  1328
                    return t;
d51f30ce6796 6531090: Cannot access methods/fields of a captured type belonging to an intersection type
mcimadamore
parents: 162
diff changeset
  1329
                else
d51f30ce6796 6531090: Cannot access methods/fields of a captured type belonging to an intersection type
mcimadamore
parents: 162
diff changeset
  1330
                    return asSuper(t.bound, sym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1331
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1332
06bc494ca11e Initial load
duke
parents:
diff changeset
  1333
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1334
            public Type visitErrorType(ErrorType t, Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1335
                return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1336
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1337
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
  1338
06bc494ca11e Initial load
duke
parents:
diff changeset
  1339
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1340
     * Return the base type of t or any of its outer types that starts
06bc494ca11e Initial load
duke
parents:
diff changeset
  1341
     * with the given symbol.  If none exists, return null.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1342
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  1343
     * @param t a type
06bc494ca11e Initial load
duke
parents:
diff changeset
  1344
     * @param sym a symbol
06bc494ca11e Initial load
duke
parents:
diff changeset
  1345
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1346
    public Type asOuterSuper(Type t, Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1347
        switch (t.tag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1348
        case CLASS:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1349
            do {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1350
                Type s = asSuper(t, sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1351
                if (s != null) return s;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1352
                t = t.getEnclosingType();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1353
            } while (t.tag == CLASS);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1354
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1355
        case ARRAY:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1356
            return isSubtype(t, sym.type) ? sym.type : null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1357
        case TYPEVAR:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1358
            return asSuper(t, sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1359
        case ERROR:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1360
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1361
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1362
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1363
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1364
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1365
06bc494ca11e Initial load
duke
parents:
diff changeset
  1366
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1367
     * Return the base type of t or any of its enclosing types that
06bc494ca11e Initial load
duke
parents:
diff changeset
  1368
     * starts with the given symbol.  If none exists, return null.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1369
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  1370
     * @param t a type
06bc494ca11e Initial load
duke
parents:
diff changeset
  1371
     * @param sym a symbol
06bc494ca11e Initial load
duke
parents:
diff changeset
  1372
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1373
    public Type asEnclosingSuper(Type t, Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1374
        switch (t.tag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1375
        case CLASS:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1376
            do {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1377
                Type s = asSuper(t, sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1378
                if (s != null) return s;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1379
                Type outer = t.getEnclosingType();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1380
                t = (outer.tag == CLASS) ? outer :
06bc494ca11e Initial load
duke
parents:
diff changeset
  1381
                    (t.tsym.owner.enclClass() != null) ? t.tsym.owner.enclClass().type :
06bc494ca11e Initial load
duke
parents:
diff changeset
  1382
                    Type.noType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1383
            } while (t.tag == CLASS);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1384
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1385
        case ARRAY:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1386
            return isSubtype(t, sym.type) ? sym.type : null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1387
        case TYPEVAR:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1388
            return asSuper(t, sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1389
        case ERROR:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1390
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1391
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1392
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1393
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1394
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1395
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  1396
06bc494ca11e Initial load
duke
parents:
diff changeset
  1397
    // <editor-fold defaultstate="collapsed" desc="memberType">
06bc494ca11e Initial load
duke
parents:
diff changeset
  1398
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1399
     * The type of given symbol, seen as a member of t.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1400
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  1401
     * @param t a type
06bc494ca11e Initial load
duke
parents:
diff changeset
  1402
     * @param sym a symbol
06bc494ca11e Initial load
duke
parents:
diff changeset
  1403
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1404
    public Type memberType(Type t, Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1405
        return (sym.flags() & STATIC) != 0
06bc494ca11e Initial load
duke
parents:
diff changeset
  1406
            ? sym.type
06bc494ca11e Initial load
duke
parents:
diff changeset
  1407
            : memberType.visit(t, sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1408
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1409
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
  1410
        private SimpleVisitor<Type,Symbol> memberType = new SimpleVisitor<Type,Symbol>() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1411
06bc494ca11e Initial load
duke
parents:
diff changeset
  1412
            public Type visitType(Type t, Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1413
                return sym.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1414
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1415
06bc494ca11e Initial load
duke
parents:
diff changeset
  1416
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1417
            public Type visitWildcardType(WildcardType t, Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1418
                return memberType(upperBound(t), sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1419
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1420
06bc494ca11e Initial load
duke
parents:
diff changeset
  1421
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1422
            public Type visitClassType(ClassType t, Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1423
                Symbol owner = sym.owner;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1424
                long flags = sym.flags();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1425
                if (((flags & STATIC) == 0) && owner.type.isParameterized()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1426
                    Type base = asOuterSuper(t, owner);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1427
                    if (base != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1428
                        List<Type> ownerParams = owner.type.allparams();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1429
                        List<Type> baseParams = base.allparams();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1430
                        if (ownerParams.nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1431
                            if (baseParams.isEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1432
                                // then base is a raw type
06bc494ca11e Initial load
duke
parents:
diff changeset
  1433
                                return erasure(sym.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1434
                            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1435
                                return subst(sym.type, ownerParams, baseParams);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1436
                            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1437
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1438
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1439
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1440
                return sym.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1441
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1442
06bc494ca11e Initial load
duke
parents:
diff changeset
  1443
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1444
            public Type visitTypeVar(TypeVar t, Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1445
                return memberType(t.bound, sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1446
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1447
06bc494ca11e Initial load
duke
parents:
diff changeset
  1448
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1449
            public Type visitErrorType(ErrorType t, Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1450
                return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1451
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1452
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
  1453
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  1454
06bc494ca11e Initial load
duke
parents:
diff changeset
  1455
    // <editor-fold defaultstate="collapsed" desc="isAssignable">
06bc494ca11e Initial load
duke
parents:
diff changeset
  1456
    public boolean isAssignable(Type t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1457
        return isAssignable(t, s, Warner.noWarnings);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1458
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1459
06bc494ca11e Initial load
duke
parents:
diff changeset
  1460
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1461
     * Is t assignable to s?<br>
06bc494ca11e Initial load
duke
parents:
diff changeset
  1462
     * Equivalent to subtype except for constant values and raw
06bc494ca11e Initial load
duke
parents:
diff changeset
  1463
     * types.<br>
06bc494ca11e Initial load
duke
parents:
diff changeset
  1464
     * (not defined for Method and ForAll types)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1465
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1466
    public boolean isAssignable(Type t, Type s, Warner warn) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1467
        if (t.tag == ERROR)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1468
            return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1469
        if (t.tag <= INT && t.constValue() != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1470
            int value = ((Number)t.constValue()).intValue();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1471
            switch (s.tag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1472
            case BYTE:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1473
                if (Byte.MIN_VALUE <= value && value <= Byte.MAX_VALUE)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1474
                    return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1475
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1476
            case CHAR:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1477
                if (Character.MIN_VALUE <= value && value <= Character.MAX_VALUE)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1478
                    return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1479
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1480
            case SHORT:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1481
                if (Short.MIN_VALUE <= value && value <= Short.MAX_VALUE)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1482
                    return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1483
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1484
            case INT:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1485
                return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1486
            case CLASS:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1487
                switch (unboxedType(s).tag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1488
                case BYTE:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1489
                case CHAR:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1490
                case SHORT:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1491
                    return isAssignable(t, unboxedType(s), warn);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1492
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1493
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1494
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1495
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1496
        return isConvertible(t, s, warn);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1497
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1498
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  1499
06bc494ca11e Initial load
duke
parents:
diff changeset
  1500
    // <editor-fold defaultstate="collapsed" desc="erasure">
06bc494ca11e Initial load
duke
parents:
diff changeset
  1501
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1502
     * The erasure of t {@code |t|} -- the type that results when all
06bc494ca11e Initial load
duke
parents:
diff changeset
  1503
     * type parameters in t are deleted.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1504
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1505
    public Type erasure(Type t) {
514
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  1506
        return erasure(t, false);
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  1507
    }
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  1508
    //where
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  1509
    private Type erasure(Type t, boolean recurse) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1510
        if (t.tag <= lastBaseTag)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1511
            return t; /* fast special case */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1512
        else
514
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  1513
            return erasure.visit(t, recurse);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1514
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1515
    // where
514
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  1516
        private SimpleVisitor<Type, Boolean> erasure = new SimpleVisitor<Type, Boolean>() {
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  1517
            public Type visitType(Type t, Boolean recurse) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1518
                if (t.tag <= lastBaseTag)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1519
                    return t; /*fast special case*/
06bc494ca11e Initial load
duke
parents:
diff changeset
  1520
                else
514
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  1521
                    return t.map(recurse ? erasureRecFun : erasureFun);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1522
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1523
06bc494ca11e Initial load
duke
parents:
diff changeset
  1524
            @Override
514
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  1525
            public Type visitWildcardType(WildcardType t, Boolean recurse) {
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  1526
                return erasure(upperBound(t), recurse);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1527
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1528
06bc494ca11e Initial load
duke
parents:
diff changeset
  1529
            @Override
514
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  1530
            public Type visitClassType(ClassType t, Boolean recurse) {
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  1531
                Type erased = t.tsym.erasure(Types.this);
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  1532
                if (recurse) {
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  1533
                    erased = new ErasedClassType(erased.getEnclosingType(),erased.tsym);
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  1534
                }
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  1535
                return erased;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1536
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1537
06bc494ca11e Initial load
duke
parents:
diff changeset
  1538
            @Override
514
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  1539
            public Type visitTypeVar(TypeVar t, Boolean recurse) {
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  1540
                return erasure(t.bound, recurse);
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  1541
            }
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  1542
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  1543
            @Override
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  1544
            public Type visitErrorType(ErrorType t, Boolean recurse) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1545
                return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1546
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1547
        };
514
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  1548
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1549
    private Mapping erasureFun = new Mapping ("erasure") {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1550
            public Type apply(Type t) { return erasure(t); }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1551
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
  1552
514
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  1553
    private Mapping erasureRecFun = new Mapping ("erasureRecursive") {
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  1554
        public Type apply(Type t) { return erasureRecursive(t); }
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  1555
    };
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  1556
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1557
    public List<Type> erasure(List<Type> ts) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1558
        return Type.map(ts, erasureFun);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1559
    }
514
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  1560
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  1561
    public Type erasureRecursive(Type t) {
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  1562
        return erasure(t, true);
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  1563
    }
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  1564
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  1565
    public List<Type> erasureRecursive(List<Type> ts) {
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  1566
        return Type.map(ts, erasureRecFun);
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  1567
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1568
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  1569
06bc494ca11e Initial load
duke
parents:
diff changeset
  1570
    // <editor-fold defaultstate="collapsed" desc="makeCompoundType">
06bc494ca11e Initial load
duke
parents:
diff changeset
  1571
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1572
     * Make a compound type from non-empty list of types
06bc494ca11e Initial load
duke
parents:
diff changeset
  1573
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  1574
     * @param bounds            the types from which the compound type is formed
06bc494ca11e Initial load
duke
parents:
diff changeset
  1575
     * @param supertype         is objectType if all bounds are interfaces,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1576
     *                          null otherwise.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1577
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1578
    public Type makeCompoundType(List<Type> bounds,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1579
                                 Type supertype) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1580
        ClassSymbol bc =
06bc494ca11e Initial load
duke
parents:
diff changeset
  1581
            new ClassSymbol(ABSTRACT|PUBLIC|SYNTHETIC|COMPOUND|ACYCLIC,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1582
                            Type.moreInfo
06bc494ca11e Initial load
duke
parents:
diff changeset
  1583
                                ? names.fromString(bounds.toString())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1584
                                : names.empty,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1585
                            syms.noSymbol);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1586
        if (bounds.head.tag == TYPEVAR)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1587
            // error condition, recover
06bc494ca11e Initial load
duke
parents:
diff changeset
  1588
            bc.erasure_field = syms.objectType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1589
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
  1590
            bc.erasure_field = erasure(bounds.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1591
        bc.members_field = new Scope(bc);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1592
        ClassType bt = (ClassType)bc.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1593
        bt.allparams_field = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1594
        if (supertype != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1595
            bt.supertype_field = supertype;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1596
            bt.interfaces_field = bounds;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1597
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1598
            bt.supertype_field = bounds.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1599
            bt.interfaces_field = bounds.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1600
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1601
        assert bt.supertype_field.tsym.completer != null
06bc494ca11e Initial load
duke
parents:
diff changeset
  1602
            || !bt.supertype_field.isInterface()
06bc494ca11e Initial load
duke
parents:
diff changeset
  1603
            : bt.supertype_field;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1604
        return bt;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1605
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1606
06bc494ca11e Initial load
duke
parents:
diff changeset
  1607
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1608
     * Same as {@link #makeCompoundType(List,Type)}, except that the
06bc494ca11e Initial load
duke
parents:
diff changeset
  1609
     * second parameter is computed directly. Note that this might
06bc494ca11e Initial load
duke
parents:
diff changeset
  1610
     * cause a symbol completion.  Hence, this version of
06bc494ca11e Initial load
duke
parents:
diff changeset
  1611
     * makeCompoundType may not be called during a classfile read.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1612
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1613
    public Type makeCompoundType(List<Type> bounds) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1614
        Type supertype = (bounds.head.tsym.flags() & INTERFACE) != 0 ?
06bc494ca11e Initial load
duke
parents:
diff changeset
  1615
            supertype(bounds.head) : null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1616
        return makeCompoundType(bounds, supertype);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1617
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1618
06bc494ca11e Initial load
duke
parents:
diff changeset
  1619
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1620
     * A convenience wrapper for {@link #makeCompoundType(List)}; the
06bc494ca11e Initial load
duke
parents:
diff changeset
  1621
     * arguments are converted to a list and passed to the other
06bc494ca11e Initial load
duke
parents:
diff changeset
  1622
     * method.  Note that this might cause a symbol completion.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1623
     * Hence, this version of makeCompoundType may not be called
06bc494ca11e Initial load
duke
parents:
diff changeset
  1624
     * during a classfile read.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1625
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1626
    public Type makeCompoundType(Type bound1, Type bound2) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1627
        return makeCompoundType(List.of(bound1, bound2));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1628
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1629
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  1630
06bc494ca11e Initial load
duke
parents:
diff changeset
  1631
    // <editor-fold defaultstate="collapsed" desc="supertype">
06bc494ca11e Initial load
duke
parents:
diff changeset
  1632
    public Type supertype(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1633
        return supertype.visit(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1634
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1635
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
  1636
        private UnaryVisitor<Type> supertype = new UnaryVisitor<Type>() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1637
06bc494ca11e Initial load
duke
parents:
diff changeset
  1638
            public Type visitType(Type t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1639
                // A note on wildcards: there is no good way to
06bc494ca11e Initial load
duke
parents:
diff changeset
  1640
                // determine a supertype for a super bounded wildcard.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1641
                return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1642
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1643
06bc494ca11e Initial load
duke
parents:
diff changeset
  1644
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1645
            public Type visitClassType(ClassType t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1646
                if (t.supertype_field == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1647
                    Type supertype = ((ClassSymbol)t.tsym).getSuperclass();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1648
                    // An interface has no superclass; its supertype is Object.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1649
                    if (t.isInterface())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1650
                        supertype = ((ClassType)t.tsym.type).supertype_field;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1651
                    if (t.supertype_field == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1652
                        List<Type> actuals = classBound(t).allparams();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1653
                        List<Type> formals = t.tsym.type.allparams();
514
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  1654
                        if (t.hasErasedSupertypes()) {
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  1655
                            t.supertype_field = erasureRecursive(supertype);
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  1656
                        } else if (formals.nonEmpty()) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1657
                            t.supertype_field = subst(supertype, formals, actuals);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1658
                        }
514
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  1659
                        else {
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  1660
                            t.supertype_field = supertype;
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  1661
                        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1662
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1663
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1664
                return t.supertype_field;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1665
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1666
06bc494ca11e Initial load
duke
parents:
diff changeset
  1667
            /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1668
             * The supertype is always a class type. If the type
06bc494ca11e Initial load
duke
parents:
diff changeset
  1669
             * variable's bounds start with a class type, this is also
06bc494ca11e Initial load
duke
parents:
diff changeset
  1670
             * the supertype.  Otherwise, the supertype is
06bc494ca11e Initial load
duke
parents:
diff changeset
  1671
             * java.lang.Object.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1672
             */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1673
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1674
            public Type visitTypeVar(TypeVar t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1675
                if (t.bound.tag == TYPEVAR ||
06bc494ca11e Initial load
duke
parents:
diff changeset
  1676
                    (!t.bound.isCompound() && !t.bound.isInterface())) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1677
                    return t.bound;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1678
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1679
                    return supertype(t.bound);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1680
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1681
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1682
06bc494ca11e Initial load
duke
parents:
diff changeset
  1683
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1684
            public Type visitArrayType(ArrayType t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1685
                if (t.elemtype.isPrimitive() || isSameType(t.elemtype, syms.objectType))
06bc494ca11e Initial load
duke
parents:
diff changeset
  1686
                    return arraySuperType();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1687
                else
06bc494ca11e Initial load
duke
parents:
diff changeset
  1688
                    return new ArrayType(supertype(t.elemtype), t.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1689
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1690
06bc494ca11e Initial load
duke
parents:
diff changeset
  1691
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1692
            public Type visitErrorType(ErrorType t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1693
                return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1694
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1695
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
  1696
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  1697
06bc494ca11e Initial load
duke
parents:
diff changeset
  1698
    // <editor-fold defaultstate="collapsed" desc="interfaces">
06bc494ca11e Initial load
duke
parents:
diff changeset
  1699
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1700
     * Return the interfaces implemented by this class.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1701
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1702
    public List<Type> interfaces(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1703
        return interfaces.visit(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1704
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1705
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
  1706
        private UnaryVisitor<List<Type>> interfaces = new UnaryVisitor<List<Type>>() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1707
06bc494ca11e Initial load
duke
parents:
diff changeset
  1708
            public List<Type> visitType(Type t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1709
                return List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1710
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1711
06bc494ca11e Initial load
duke
parents:
diff changeset
  1712
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1713
            public List<Type> visitClassType(ClassType t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1714
                if (t.interfaces_field == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1715
                    List<Type> interfaces = ((ClassSymbol)t.tsym).getInterfaces();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1716
                    if (t.interfaces_field == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1717
                        // If t.interfaces_field is null, then t must
06bc494ca11e Initial load
duke
parents:
diff changeset
  1718
                        // be a parameterized type (not to be confused
06bc494ca11e Initial load
duke
parents:
diff changeset
  1719
                        // with a generic type declaration).
06bc494ca11e Initial load
duke
parents:
diff changeset
  1720
                        // Terminology:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1721
                        //    Parameterized type: List<String>
06bc494ca11e Initial load
duke
parents:
diff changeset
  1722
                        //    Generic type declaration: class List<E> { ... }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1723
                        // So t corresponds to List<String> and
06bc494ca11e Initial load
duke
parents:
diff changeset
  1724
                        // t.tsym.type corresponds to List<E>.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1725
                        // The reason t must be parameterized type is
06bc494ca11e Initial load
duke
parents:
diff changeset
  1726
                        // that completion will happen as a side
06bc494ca11e Initial load
duke
parents:
diff changeset
  1727
                        // effect of calling
06bc494ca11e Initial load
duke
parents:
diff changeset
  1728
                        // ClassSymbol.getInterfaces.  Since
06bc494ca11e Initial load
duke
parents:
diff changeset
  1729
                        // t.interfaces_field is null after
06bc494ca11e Initial load
duke
parents:
diff changeset
  1730
                        // completion, we can assume that t is not the
06bc494ca11e Initial load
duke
parents:
diff changeset
  1731
                        // type of a class/interface declaration.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1732
                        assert t != t.tsym.type : t.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1733
                        List<Type> actuals = t.allparams();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1734
                        List<Type> formals = t.tsym.type.allparams();
514
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  1735
                        if (t.hasErasedSupertypes()) {
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  1736
                            t.interfaces_field = erasureRecursive(interfaces);
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  1737
                        } else if (formals.nonEmpty()) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1738
                            t.interfaces_field =
06bc494ca11e Initial load
duke
parents:
diff changeset
  1739
                                upperBounds(subst(interfaces, formals, actuals));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1740
                        }
514
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  1741
                        else {
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  1742
                            t.interfaces_field = interfaces;
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  1743
                        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1744
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1745
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1746
                return t.interfaces_field;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1747
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1748
06bc494ca11e Initial load
duke
parents:
diff changeset
  1749
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1750
            public List<Type> visitTypeVar(TypeVar t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1751
                if (t.bound.isCompound())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1752
                    return interfaces(t.bound);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1753
06bc494ca11e Initial load
duke
parents:
diff changeset
  1754
                if (t.bound.isInterface())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1755
                    return List.of(t.bound);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1756
06bc494ca11e Initial load
duke
parents:
diff changeset
  1757
                return List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1758
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1759
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
  1760
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  1761
06bc494ca11e Initial load
duke
parents:
diff changeset
  1762
    // <editor-fold defaultstate="collapsed" desc="isDerivedRaw">
06bc494ca11e Initial load
duke
parents:
diff changeset
  1763
    Map<Type,Boolean> isDerivedRawCache = new HashMap<Type,Boolean>();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1764
06bc494ca11e Initial load
duke
parents:
diff changeset
  1765
    public boolean isDerivedRaw(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1766
        Boolean result = isDerivedRawCache.get(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1767
        if (result == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1768
            result = isDerivedRawInternal(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1769
            isDerivedRawCache.put(t, result);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1770
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1771
        return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1772
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1773
06bc494ca11e Initial load
duke
parents:
diff changeset
  1774
    public boolean isDerivedRawInternal(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1775
        if (t.isErroneous())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1776
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1777
        return
06bc494ca11e Initial load
duke
parents:
diff changeset
  1778
            t.isRaw() ||
06bc494ca11e Initial load
duke
parents:
diff changeset
  1779
            supertype(t) != null && isDerivedRaw(supertype(t)) ||
06bc494ca11e Initial load
duke
parents:
diff changeset
  1780
            isDerivedRaw(interfaces(t));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1781
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1782
06bc494ca11e Initial load
duke
parents:
diff changeset
  1783
    public boolean isDerivedRaw(List<Type> ts) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1784
        List<Type> l = ts;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1785
        while (l.nonEmpty() && !isDerivedRaw(l.head)) l = l.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1786
        return l.nonEmpty();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1787
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1788
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  1789
06bc494ca11e Initial load
duke
parents:
diff changeset
  1790
    // <editor-fold defaultstate="collapsed" desc="setBounds">
06bc494ca11e Initial load
duke
parents:
diff changeset
  1791
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1792
     * Set the bounds field of the given type variable to reflect a
06bc494ca11e Initial load
duke
parents:
diff changeset
  1793
     * (possibly multiple) list of bounds.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1794
     * @param t                 a type variable
06bc494ca11e Initial load
duke
parents:
diff changeset
  1795
     * @param bounds            the bounds, must be nonempty
06bc494ca11e Initial load
duke
parents:
diff changeset
  1796
     * @param supertype         is objectType if all bounds are interfaces,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1797
     *                          null otherwise.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1798
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1799
    public void setBounds(TypeVar t, List<Type> bounds, Type supertype) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1800
        if (bounds.tail.isEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1801
            t.bound = bounds.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1802
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
  1803
            t.bound = makeCompoundType(bounds, supertype);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1804
        t.rank_field = -1;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1805
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1806
06bc494ca11e Initial load
duke
parents:
diff changeset
  1807
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1808
     * Same as {@link #setBounds(Type.TypeVar,List,Type)}, except that
06bc494ca11e Initial load
duke
parents:
diff changeset
  1809
     * third parameter is computed directly.  Note that this test
06bc494ca11e Initial load
duke
parents:
diff changeset
  1810
     * might cause a symbol completion.  Hence, this version of
06bc494ca11e Initial load
duke
parents:
diff changeset
  1811
     * setBounds may not be called during a classfile read.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1812
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1813
    public void setBounds(TypeVar t, List<Type> bounds) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1814
        Type supertype = (bounds.head.tsym.flags() & INTERFACE) != 0 ?
06bc494ca11e Initial load
duke
parents:
diff changeset
  1815
            supertype(bounds.head) : null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1816
        setBounds(t, bounds, supertype);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1817
        t.rank_field = -1;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1818
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1819
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  1820
06bc494ca11e Initial load
duke
parents:
diff changeset
  1821
    // <editor-fold defaultstate="collapsed" desc="getBounds">
06bc494ca11e Initial load
duke
parents:
diff changeset
  1822
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1823
     * Return list of bounds of the given type variable.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1824
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1825
    public List<Type> getBounds(TypeVar t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1826
        if (t.bound.isErroneous() || !t.bound.isCompound())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1827
            return List.of(t.bound);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1828
        else if ((erasure(t).tsym.flags() & INTERFACE) == 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1829
            return interfaces(t).prepend(supertype(t));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1830
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
  1831
            // No superclass was given in bounds.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1832
            // In this case, supertype is Object, erasure is first interface.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1833
            return interfaces(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1834
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1835
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  1836
06bc494ca11e Initial load
duke
parents:
diff changeset
  1837
    // <editor-fold defaultstate="collapsed" desc="classBound">
06bc494ca11e Initial load
duke
parents:
diff changeset
  1838
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1839
     * If the given type is a (possibly selected) type variable,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1840
     * return the bounding class of this type, otherwise return the
06bc494ca11e Initial load
duke
parents:
diff changeset
  1841
     * type itself.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1842
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1843
    public Type classBound(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1844
        return classBound.visit(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1845
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1846
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
  1847
        private UnaryVisitor<Type> classBound = new UnaryVisitor<Type>() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1848
06bc494ca11e Initial load
duke
parents:
diff changeset
  1849
            public Type visitType(Type t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1850
                return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1851
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1852
06bc494ca11e Initial load
duke
parents:
diff changeset
  1853
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1854
            public Type visitClassType(ClassType t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1855
                Type outer1 = classBound(t.getEnclosingType());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1856
                if (outer1 != t.getEnclosingType())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1857
                    return new ClassType(outer1, t.getTypeArguments(), t.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1858
                else
06bc494ca11e Initial load
duke
parents:
diff changeset
  1859
                    return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1860
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1861
06bc494ca11e Initial load
duke
parents:
diff changeset
  1862
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1863
            public Type visitTypeVar(TypeVar t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1864
                return classBound(supertype(t));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1865
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1866
06bc494ca11e Initial load
duke
parents:
diff changeset
  1867
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1868
            public Type visitErrorType(ErrorType t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1869
                return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1870
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1871
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
  1872
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  1873
06bc494ca11e Initial load
duke
parents:
diff changeset
  1874
    // <editor-fold defaultstate="collapsed" desc="sub signature / override equivalence">
06bc494ca11e Initial load
duke
parents:
diff changeset
  1875
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1876
     * Returns true iff the first signature is a <em>sub
06bc494ca11e Initial load
duke
parents:
diff changeset
  1877
     * signature</em> of the other.  This is <b>not</b> an equivalence
06bc494ca11e Initial load
duke
parents:
diff changeset
  1878
     * relation.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1879
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  1880
     * @see "The Java Language Specification, Third Ed. (8.4.2)."
06bc494ca11e Initial load
duke
parents:
diff changeset
  1881
     * @see #overrideEquivalent(Type t, Type s)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1882
     * @param t first signature (possibly raw).
06bc494ca11e Initial load
duke
parents:
diff changeset
  1883
     * @param s second signature (could be subjected to erasure).
06bc494ca11e Initial load
duke
parents:
diff changeset
  1884
     * @return true if t is a sub signature of s.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1885
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1886
    public boolean isSubSignature(Type t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1887
        return hasSameArgs(t, s) || hasSameArgs(t, erasure(s));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1888
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1889
06bc494ca11e Initial load
duke
parents:
diff changeset
  1890
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1891
     * Returns true iff these signatures are related by <em>override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1892
     * equivalence</em>.  This is the natural extension of
06bc494ca11e Initial load
duke
parents:
diff changeset
  1893
     * isSubSignature to an equivalence relation.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1894
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  1895
     * @see "The Java Language Specification, Third Ed. (8.4.2)."
06bc494ca11e Initial load
duke
parents:
diff changeset
  1896
     * @see #isSubSignature(Type t, Type s)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1897
     * @param t a signature (possible raw, could be subjected to
06bc494ca11e Initial load
duke
parents:
diff changeset
  1898
     * erasure).
06bc494ca11e Initial load
duke
parents:
diff changeset
  1899
     * @param s a signature (possible raw, could be subjected to
06bc494ca11e Initial load
duke
parents:
diff changeset
  1900
     * erasure).
06bc494ca11e Initial load
duke
parents:
diff changeset
  1901
     * @return true if either argument is a sub signature of the other.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1902
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1903
    public boolean overrideEquivalent(Type t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1904
        return hasSameArgs(t, s) ||
06bc494ca11e Initial load
duke
parents:
diff changeset
  1905
            hasSameArgs(t, erasure(s)) || hasSameArgs(erasure(t), s);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1906
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1907
06bc494ca11e Initial load
duke
parents:
diff changeset
  1908
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1909
     * Does t have the same arguments as s?  It is assumed that both
06bc494ca11e Initial load
duke
parents:
diff changeset
  1910
     * types are (possibly polymorphic) method types.  Monomorphic
06bc494ca11e Initial load
duke
parents:
diff changeset
  1911
     * method types "have the same arguments", if their argument lists
06bc494ca11e Initial load
duke
parents:
diff changeset
  1912
     * are equal.  Polymorphic method types "have the same arguments",
06bc494ca11e Initial load
duke
parents:
diff changeset
  1913
     * if they have the same arguments after renaming all type
06bc494ca11e Initial load
duke
parents:
diff changeset
  1914
     * variables of one to corresponding type variables in the other,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1915
     * where correspondence is by position in the type parameter list.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1916
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1917
    public boolean hasSameArgs(Type t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1918
        return hasSameArgs.visit(t, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1919
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1920
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
  1921
        private TypeRelation hasSameArgs = new TypeRelation() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1922
06bc494ca11e Initial load
duke
parents:
diff changeset
  1923
            public Boolean visitType(Type t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1924
                throw new AssertionError();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1925
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1926
06bc494ca11e Initial load
duke
parents:
diff changeset
  1927
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1928
            public Boolean visitMethodType(MethodType t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1929
                return s.tag == METHOD
06bc494ca11e Initial load
duke
parents:
diff changeset
  1930
                    && containsTypeEquivalent(t.argtypes, s.getParameterTypes());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1931
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1932
06bc494ca11e Initial load
duke
parents:
diff changeset
  1933
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1934
            public Boolean visitForAll(ForAll t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1935
                if (s.tag != FORALL)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1936
                    return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1937
06bc494ca11e Initial load
duke
parents:
diff changeset
  1938
                ForAll forAll = (ForAll)s;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1939
                return hasSameBounds(t, forAll)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1940
                    && visit(t.qtype, subst(forAll.qtype, forAll.tvars, t.tvars));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1941
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1942
06bc494ca11e Initial load
duke
parents:
diff changeset
  1943
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1944
            public Boolean visitErrorType(ErrorType t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1945
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1946
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1947
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
  1948
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  1949
06bc494ca11e Initial load
duke
parents:
diff changeset
  1950
    // <editor-fold defaultstate="collapsed" desc="subst">
06bc494ca11e Initial load
duke
parents:
diff changeset
  1951
    public List<Type> subst(List<Type> ts,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1952
                            List<Type> from,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1953
                            List<Type> to) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1954
        return new Subst(from, to).subst(ts);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1955
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1956
06bc494ca11e Initial load
duke
parents:
diff changeset
  1957
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1958
     * Substitute all occurrences of a type in `from' with the
06bc494ca11e Initial load
duke
parents:
diff changeset
  1959
     * corresponding type in `to' in 't'. Match lists `from' and `to'
06bc494ca11e Initial load
duke
parents:
diff changeset
  1960
     * from the right: If lists have different length, discard leading
06bc494ca11e Initial load
duke
parents:
diff changeset
  1961
     * elements of the longer list.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1962
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1963
    public Type subst(Type t, List<Type> from, List<Type> to) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1964
        return new Subst(from, to).subst(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1965
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1966
06bc494ca11e Initial load
duke
parents:
diff changeset
  1967
    private class Subst extends UnaryVisitor<Type> {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1968
        List<Type> from;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1969
        List<Type> to;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1970
06bc494ca11e Initial load
duke
parents:
diff changeset
  1971
        public Subst(List<Type> from, List<Type> to) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1972
            int fromLength = from.length();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1973
            int toLength = to.length();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1974
            while (fromLength > toLength) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1975
                fromLength--;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1976
                from = from.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1977
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1978
            while (fromLength < toLength) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1979
                toLength--;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1980
                to = to.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1981
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1982
            this.from = from;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1983
            this.to = to;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1984
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1985
06bc494ca11e Initial load
duke
parents:
diff changeset
  1986
        Type subst(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1987
            if (from.tail == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1988
                return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1989
            else
06bc494ca11e Initial load
duke
parents:
diff changeset
  1990
                return visit(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1991
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1992
06bc494ca11e Initial load
duke
parents:
diff changeset
  1993
        List<Type> subst(List<Type> ts) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1994
            if (from.tail == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1995
                return ts;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1996
            boolean wild = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1997
            if (ts.nonEmpty() && from.nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1998
                Type head1 = subst(ts.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1999
                List<Type> tail1 = subst(ts.tail);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2000
                if (head1 != ts.head || tail1 != ts.tail)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2001
                    return tail1.prepend(head1);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2002
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2003
            return ts;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2004
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2005
06bc494ca11e Initial load
duke
parents:
diff changeset
  2006
        public Type visitType(Type t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2007
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2008
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2009
06bc494ca11e Initial load
duke
parents:
diff changeset
  2010
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  2011
        public Type visitMethodType(MethodType t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2012
            List<Type> argtypes = subst(t.argtypes);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2013
            Type restype = subst(t.restype);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2014
            List<Type> thrown = subst(t.thrown);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2015
            if (argtypes == t.argtypes &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  2016
                restype == t.restype &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  2017
                thrown == t.thrown)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2018
                return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2019
            else
06bc494ca11e Initial load
duke
parents:
diff changeset
  2020
                return new MethodType(argtypes, restype, thrown, t.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2021
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2022
06bc494ca11e Initial load
duke
parents:
diff changeset
  2023
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  2024
        public Type visitTypeVar(TypeVar t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2025
            for (List<Type> from = this.from, to = this.to;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2026
                 from.nonEmpty();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2027
                 from = from.tail, to = to.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2028
                if (t == from.head) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2029
                    return to.head.withTypeVar(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2030
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2031
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2032
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2033
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2034
06bc494ca11e Initial load
duke
parents:
diff changeset
  2035
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  2036
        public Type visitClassType(ClassType t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2037
            if (!t.isCompound()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2038
                List<Type> typarams = t.getTypeArguments();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2039
                List<Type> typarams1 = subst(typarams);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2040
                Type outer = t.getEnclosingType();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2041
                Type outer1 = subst(outer);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2042
                if (typarams1 == typarams && outer1 == outer)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2043
                    return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2044
                else
06bc494ca11e Initial load
duke
parents:
diff changeset
  2045
                    return new ClassType(outer1, typarams1, t.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2046
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2047
                Type st = subst(supertype(t));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2048
                List<Type> is = upperBounds(subst(interfaces(t)));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2049
                if (st == supertype(t) && is == interfaces(t))
06bc494ca11e Initial load
duke
parents:
diff changeset
  2050
                    return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2051
                else
06bc494ca11e Initial load
duke
parents:
diff changeset
  2052
                    return makeCompoundType(is.prepend(st));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2053
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2054
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2055
06bc494ca11e Initial load
duke
parents:
diff changeset
  2056
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  2057
        public Type visitWildcardType(WildcardType t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2058
            Type bound = t.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2059
            if (t.kind != BoundKind.UNBOUND)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2060
                bound = subst(bound);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2061
            if (bound == t.type) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2062
                return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2063
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2064
                if (t.isExtendsBound() && bound.isExtendsBound())
06bc494ca11e Initial load
duke
parents:
diff changeset
  2065
                    bound = upperBound(bound);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2066
                return new WildcardType(bound, t.kind, syms.boundClass, t.bound);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2067
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2068
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2069
06bc494ca11e Initial load
duke
parents:
diff changeset
  2070
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  2071
        public Type visitArrayType(ArrayType t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2072
            Type elemtype = subst(t.elemtype);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2073
            if (elemtype == t.elemtype)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2074
                return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2075
            else
06bc494ca11e Initial load
duke
parents:
diff changeset
  2076
                return new ArrayType(upperBound(elemtype), t.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2077
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2078
06bc494ca11e Initial load
duke
parents:
diff changeset
  2079
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  2080
        public Type visitForAll(ForAll t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2081
            List<Type> tvars1 = substBounds(t.tvars, from, to);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2082
            Type qtype1 = subst(t.qtype);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2083
            if (tvars1 == t.tvars && qtype1 == t.qtype) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2084
                return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2085
            } else if (tvars1 == t.tvars) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2086
                return new ForAll(tvars1, qtype1);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2087
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2088
                return new ForAll(tvars1, Types.this.subst(qtype1, t.tvars, tvars1));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2089
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2090
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2091
06bc494ca11e Initial load
duke
parents:
diff changeset
  2092
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  2093
        public Type visitErrorType(ErrorType t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2094
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2095
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2096
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2097
06bc494ca11e Initial load
duke
parents:
diff changeset
  2098
    public List<Type> substBounds(List<Type> tvars,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2099
                                  List<Type> from,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2100
                                  List<Type> to) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2101
        if (tvars.isEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
  2102
            return tvars;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2103
        if (tvars.tail.isEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
  2104
            // fast common case
06bc494ca11e Initial load
duke
parents:
diff changeset
  2105
            return List.<Type>of(substBound((TypeVar)tvars.head, from, to));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2106
        ListBuffer<Type> newBoundsBuf = lb();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2107
        boolean changed = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2108
        // calculate new bounds
06bc494ca11e Initial load
duke
parents:
diff changeset
  2109
        for (Type t : tvars) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2110
            TypeVar tv = (TypeVar) t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2111
            Type bound = subst(tv.bound, from, to);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2112
            if (bound != tv.bound)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2113
                changed = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2114
            newBoundsBuf.append(bound);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2115
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2116
        if (!changed)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2117
            return tvars;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2118
        ListBuffer<Type> newTvars = lb();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2119
        // create new type variables without bounds
06bc494ca11e Initial load
duke
parents:
diff changeset
  2120
        for (Type t : tvars) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2121
            newTvars.append(new TypeVar(t.tsym, null, syms.botType));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2122
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2123
        // the new bounds should use the new type variables in place
06bc494ca11e Initial load
duke
parents:
diff changeset
  2124
        // of the old
06bc494ca11e Initial load
duke
parents:
diff changeset
  2125
        List<Type> newBounds = newBoundsBuf.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2126
        from = tvars;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2127
        to = newTvars.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2128
        for (; !newBounds.isEmpty(); newBounds = newBounds.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2129
            newBounds.head = subst(newBounds.head, from, to);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2130
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2131
        newBounds = newBoundsBuf.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2132
        // set the bounds of new type variables to the new bounds
06bc494ca11e Initial load
duke
parents:
diff changeset
  2133
        for (Type t : newTvars.toList()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2134
            TypeVar tv = (TypeVar) t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2135
            tv.bound = newBounds.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2136
            newBounds = newBounds.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2137
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2138
        return newTvars.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2139
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2140
06bc494ca11e Initial load
duke
parents:
diff changeset
  2141
    public TypeVar substBound(TypeVar t, List<Type> from, List<Type> to) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2142
        Type bound1 = subst(t.bound, from, to);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2143
        if (bound1 == t.bound)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2144
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2145
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
  2146
            return new TypeVar(t.tsym, bound1, syms.botType);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2147
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2148
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  2149
06bc494ca11e Initial load
duke
parents:
diff changeset
  2150
    // <editor-fold defaultstate="collapsed" desc="hasSameBounds">
06bc494ca11e Initial load
duke
parents:
diff changeset
  2151
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  2152
     * Does t have the same bounds for quantified variables as s?
06bc494ca11e Initial load
duke
parents:
diff changeset
  2153
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2154
    boolean hasSameBounds(ForAll t, ForAll s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2155
        List<Type> l1 = t.tvars;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2156
        List<Type> l2 = s.tvars;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2157
        while (l1.nonEmpty() && l2.nonEmpty() &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  2158
               isSameType(l1.head.getUpperBound(),
06bc494ca11e Initial load
duke
parents:
diff changeset
  2159
                          subst(l2.head.getUpperBound(),
06bc494ca11e Initial load
duke
parents:
diff changeset
  2160
                                s.tvars,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2161
                                t.tvars))) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2162
            l1 = l1.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2163
            l2 = l2.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2164
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2165
        return l1.isEmpty() && l2.isEmpty();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2166
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2167
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  2168
06bc494ca11e Initial load
duke
parents:
diff changeset
  2169
    // <editor-fold defaultstate="collapsed" desc="newInstances">
06bc494ca11e Initial load
duke
parents:
diff changeset
  2170
    /** Create new vector of type variables from list of variables
06bc494ca11e Initial load
duke
parents:
diff changeset
  2171
     *  changing all recursive bounds from old to new list.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2172
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2173
    public List<Type> newInstances(List<Type> tvars) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2174
        List<Type> tvars1 = Type.map(tvars, newInstanceFun);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2175
        for (List<Type> l = tvars1; l.nonEmpty(); l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2176
            TypeVar tv = (TypeVar) l.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2177
            tv.bound = subst(tv.bound, tvars, tvars1);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2178
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2179
        return tvars1;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2180
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2181
    static private Mapping newInstanceFun = new Mapping("newInstanceFun") {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2182
            public Type apply(Type t) { return new TypeVar(t.tsym, t.getUpperBound(), t.getLowerBound()); }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2183
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
  2184
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  2185
06bc494ca11e Initial load
duke
parents:
diff changeset
  2186
    // <editor-fold defaultstate="collapsed" desc="rank">
06bc494ca11e Initial load
duke
parents:
diff changeset
  2187
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  2188
     * The rank of a class is the length of the longest path between
06bc494ca11e Initial load
duke
parents:
diff changeset
  2189
     * the class and java.lang.Object in the class inheritance
06bc494ca11e Initial load
duke
parents:
diff changeset
  2190
     * graph. Undefined for all but reference types.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2191
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2192
    public int rank(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2193
        switch(t.tag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2194
        case CLASS: {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2195
            ClassType cls = (ClassType)t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2196
            if (cls.rank_field < 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2197
                Name fullname = cls.tsym.getQualifiedName();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2198
                if (fullname == fullname.table.java_lang_Object)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2199
                    cls.rank_field = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2200
                else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2201
                    int r = rank(supertype(cls));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2202
                    for (List<Type> l = interfaces(cls);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2203
                         l.nonEmpty();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2204
                         l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2205
                        if (rank(l.head) > r)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2206
                            r = rank(l.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2207
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2208
                    cls.rank_field = r + 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2209
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2210
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2211
            return cls.rank_field;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2212
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2213
        case TYPEVAR: {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2214
            TypeVar tvar = (TypeVar)t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2215
            if (tvar.rank_field < 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2216
                int r = rank(supertype(tvar));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2217
                for (List<Type> l = interfaces(tvar);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2218
                     l.nonEmpty();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2219
                     l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2220
                    if (rank(l.head) > r) r = rank(l.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2221
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2222
                tvar.rank_field = r + 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2223
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2224
            return tvar.rank_field;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2225
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2226
        case ERROR:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2227
            return 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2228
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2229
            throw new AssertionError();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2230
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2231
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2232
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  2233
06bc494ca11e Initial load
duke
parents:
diff changeset
  2234
    // <editor-fold defaultstate="collapsed" desc="toString">
06bc494ca11e Initial load
duke
parents:
diff changeset
  2235
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  2236
     * This toString is slightly more descriptive than the one on Type.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2237
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2238
    public String toString(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2239
        if (t.tag == FORALL) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2240
            ForAll forAll = (ForAll)t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2241
            return typaramsString(forAll.tvars) + forAll.qtype;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2242
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2243
        return "" + t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2244
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2245
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
  2246
        private String typaramsString(List<Type> tvars) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2247
            StringBuffer s = new StringBuffer();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2248
            s.append('<');
06bc494ca11e Initial load
duke
parents:
diff changeset
  2249
            boolean first = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2250
            for (Type t : tvars) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2251
                if (!first) s.append(", ");
06bc494ca11e Initial load
duke
parents:
diff changeset
  2252
                first = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2253
                appendTyparamString(((TypeVar)t), s);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2254
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2255
            s.append('>');
06bc494ca11e Initial load
duke
parents:
diff changeset
  2256
            return s.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2257
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2258
        private void appendTyparamString(TypeVar t, StringBuffer buf) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2259
            buf.append(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2260
            if (t.bound == null ||
06bc494ca11e Initial load
duke
parents:
diff changeset
  2261
                t.bound.tsym.getQualifiedName() == names.java_lang_Object)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2262
                return;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2263
            buf.append(" extends "); // Java syntax; no need for i18n
06bc494ca11e Initial load
duke
parents:
diff changeset
  2264
            Type bound = t.bound;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2265
            if (!bound.isCompound()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2266
                buf.append(bound);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2267
            } else if ((erasure(t).tsym.flags() & INTERFACE) == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2268
                buf.append(supertype(t));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2269
                for (Type intf : interfaces(t)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2270
                    buf.append('&');
06bc494ca11e Initial load
duke
parents:
diff changeset
  2271
                    buf.append(intf);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2272
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2273
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2274
                // No superclass was given in bounds.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2275
                // In this case, supertype is Object, erasure is first interface.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2276
                boolean first = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2277
                for (Type intf : interfaces(t)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2278
                    if (!first) buf.append('&');
06bc494ca11e Initial load
duke
parents:
diff changeset
  2279
                    first = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2280
                    buf.append(intf);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2281
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2282
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2283
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2284
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  2285
06bc494ca11e Initial load
duke
parents:
diff changeset
  2286
    // <editor-fold defaultstate="collapsed" desc="Determining least upper bounds of types">
06bc494ca11e Initial load
duke
parents:
diff changeset
  2287
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  2288
     * A cache for closures.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2289
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  2290
     * <p>A closure is a list of all the supertypes and interfaces of
06bc494ca11e Initial load
duke
parents:
diff changeset
  2291
     * a class or interface type, ordered by ClassSymbol.precedes
06bc494ca11e Initial load
duke
parents:
diff changeset
  2292
     * (that is, subclasses come first, arbitrary but fixed
06bc494ca11e Initial load
duke
parents:
diff changeset
  2293
     * otherwise).
06bc494ca11e Initial load
duke
parents:
diff changeset
  2294
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2295
    private Map<Type,List<Type>> closureCache = new HashMap<Type,List<Type>>();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2296
06bc494ca11e Initial load
duke
parents:
diff changeset
  2297
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  2298
     * Returns the closure of a class or interface type.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2299
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2300
    public List<Type> closure(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2301
        List<Type> cl = closureCache.get(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2302
        if (cl == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2303
            Type st = supertype(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2304
            if (!t.isCompound()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2305
                if (st.tag == CLASS) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2306
                    cl = insert(closure(st), t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2307
                } else if (st.tag == TYPEVAR) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2308
                    cl = closure(st).prepend(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2309
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2310
                    cl = List.of(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2311
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2312
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2313
                cl = closure(supertype(t));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2314
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2315
            for (List<Type> l = interfaces(t); l.nonEmpty(); l = l.tail)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2316
                cl = union(cl, closure(l.head));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2317
            closureCache.put(t, cl);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2318
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2319
        return cl;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2320
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2321
06bc494ca11e Initial load
duke
parents:
diff changeset
  2322
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  2323
     * Insert a type in a closure
06bc494ca11e Initial load
duke
parents:
diff changeset
  2324
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2325
    public List<Type> insert(List<Type> cl, Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2326
        if (cl.isEmpty() || t.tsym.precedes(cl.head.tsym, this)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2327
            return cl.prepend(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2328
        } else if (cl.head.tsym.precedes(t.tsym, this)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2329
            return insert(cl.tail, t).prepend(cl.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2330
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2331
            return cl;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2332
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2333
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2334
06bc494ca11e Initial load
duke
parents:
diff changeset
  2335
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  2336
     * Form the union of two closures
06bc494ca11e Initial load
duke
parents:
diff changeset
  2337
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2338
    public List<Type> union(List<Type> cl1, List<Type> cl2) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2339
        if (cl1.isEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2340
            return cl2;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2341
        } else if (cl2.isEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2342
            return cl1;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2343
        } else if (cl1.head.tsym.precedes(cl2.head.tsym, this)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2344
            return union(cl1.tail, cl2).prepend(cl1.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2345
        } else if (cl2.head.tsym.precedes(cl1.head.tsym, this)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2346
            return union(cl1, cl2.tail).prepend(cl2.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2347
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2348
            return union(cl1.tail, cl2.tail).prepend(cl1.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2349
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2350
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2351
06bc494ca11e Initial load
duke
parents:
diff changeset
  2352
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  2353
     * Intersect two closures
06bc494ca11e Initial load
duke
parents:
diff changeset
  2354
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2355
    public List<Type> intersect(List<Type> cl1, List<Type> cl2) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2356
        if (cl1 == cl2)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2357
            return cl1;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2358
        if (cl1.isEmpty() || cl2.isEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
  2359
            return List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2360
        if (cl1.head.tsym.precedes(cl2.head.tsym, this))
06bc494ca11e Initial load
duke
parents:
diff changeset
  2361
            return intersect(cl1.tail, cl2);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2362
        if (cl2.head.tsym.precedes(cl1.head.tsym, this))
06bc494ca11e Initial load
duke
parents:
diff changeset
  2363
            return intersect(cl1, cl2.tail);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2364
        if (isSameType(cl1.head, cl2.head))
06bc494ca11e Initial load
duke
parents:
diff changeset
  2365
            return intersect(cl1.tail, cl2.tail).prepend(cl1.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2366
        if (cl1.head.tsym == cl2.head.tsym &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  2367
            cl1.head.tag == CLASS && cl2.head.tag == CLASS) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2368
            if (cl1.head.isParameterized() && cl2.head.isParameterized()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2369
                Type merge = merge(cl1.head,cl2.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2370
                return intersect(cl1.tail, cl2.tail).prepend(merge);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2371
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2372
            if (cl1.head.isRaw() || cl2.head.isRaw())
06bc494ca11e Initial load
duke
parents:
diff changeset
  2373
                return intersect(cl1.tail, cl2.tail).prepend(erasure(cl1.head));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2374
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2375
        return intersect(cl1.tail, cl2.tail);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2376
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2377
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
  2378
        class TypePair {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2379
            final Type t1;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2380
            final Type t2;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2381
            TypePair(Type t1, Type t2) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2382
                this.t1 = t1;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2383
                this.t2 = t2;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2384
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2385
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  2386
            public int hashCode() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2387
                return 127 * Types.this.hashCode(t1) + Types.this.hashCode(t2);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2388
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2389
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  2390
            public boolean equals(Object obj) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2391
                if (!(obj instanceof TypePair))
06bc494ca11e Initial load
duke
parents:
diff changeset
  2392
                    return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2393
                TypePair typePair = (TypePair)obj;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2394
                return isSameType(t1, typePair.t1)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2395
                    && isSameType(t2, typePair.t2);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2396
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2397
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2398
        Set<TypePair> mergeCache = new HashSet<TypePair>();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2399
        private Type merge(Type c1, Type c2) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2400
            ClassType class1 = (ClassType) c1;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2401
            List<Type> act1 = class1.getTypeArguments();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2402
            ClassType class2 = (ClassType) c2;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2403
            List<Type> act2 = class2.getTypeArguments();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2404
            ListBuffer<Type> merged = new ListBuffer<Type>();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2405
            List<Type> typarams = class1.tsym.type.getTypeArguments();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2406
06bc494ca11e Initial load
duke
parents:
diff changeset
  2407
            while (act1.nonEmpty() && act2.nonEmpty() && typarams.nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2408
                if (containsType(act1.head, act2.head)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2409
                    merged.append(act1.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2410
                } else if (containsType(act2.head, act1.head)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2411
                    merged.append(act2.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2412
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2413
                    TypePair pair = new TypePair(c1, c2);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2414
                    Type m;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2415
                    if (mergeCache.add(pair)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2416
                        m = new WildcardType(lub(upperBound(act1.head),
06bc494ca11e Initial load
duke
parents:
diff changeset
  2417
                                                 upperBound(act2.head)),
06bc494ca11e Initial load
duke
parents:
diff changeset
  2418
                                             BoundKind.EXTENDS,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2419
                                             syms.boundClass);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2420
                        mergeCache.remove(pair);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2421
                    } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2422
                        m = new WildcardType(syms.objectType,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2423
                                             BoundKind.UNBOUND,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2424
                                             syms.boundClass);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2425
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2426
                    merged.append(m.withTypeVar(typarams.head));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2427
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2428
                act1 = act1.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2429
                act2 = act2.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2430
                typarams = typarams.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2431
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2432
            assert(act1.isEmpty() && act2.isEmpty() && typarams.isEmpty());
06bc494ca11e Initial load
duke
parents:
diff changeset
  2433
            return new ClassType(class1.getEnclosingType(), merged.toList(), class1.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2434
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2435
06bc494ca11e Initial load
duke
parents:
diff changeset
  2436
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  2437
     * Return the minimum type of a closure, a compound type if no
06bc494ca11e Initial load
duke
parents:
diff changeset
  2438
     * unique minimum exists.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2439
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2440
    private Type compoundMin(List<Type> cl) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2441
        if (cl.isEmpty()) return syms.objectType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2442
        List<Type> compound = closureMin(cl);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2443
        if (compound.isEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
  2444
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2445
        else if (compound.tail.isEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
  2446
            return compound.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2447
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
  2448
            return makeCompoundType(compound);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2449
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2450
06bc494ca11e Initial load
duke
parents:
diff changeset
  2451
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  2452
     * Return the minimum types of a closure, suitable for computing
06bc494ca11e Initial load
duke
parents:
diff changeset
  2453
     * compoundMin or glb.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2454
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2455
    private List<Type> closureMin(List<Type> cl) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2456
        ListBuffer<Type> classes = lb();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2457
        ListBuffer<Type> interfaces = lb();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2458
        while (!cl.isEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2459
            Type current = cl.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2460
            if (current.isInterface())
06bc494ca11e Initial load
duke
parents:
diff changeset
  2461
                interfaces.append(current);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2462
            else
06bc494ca11e Initial load
duke
parents:
diff changeset
  2463
                classes.append(current);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2464
            ListBuffer<Type> candidates = lb();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2465
            for (Type t : cl.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2466
                if (!isSubtypeNoCapture(current, t))
06bc494ca11e Initial load
duke
parents:
diff changeset
  2467
                    candidates.append(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2468
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2469
            cl = candidates.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2470
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2471
        return classes.appendList(interfaces).toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2472
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2473
06bc494ca11e Initial load
duke
parents:
diff changeset
  2474
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  2475
     * Return the least upper bound of pair of types.  if the lub does
06bc494ca11e Initial load
duke
parents:
diff changeset
  2476
     * not exist return null.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2477
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2478
    public Type lub(Type t1, Type t2) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2479
        return lub(List.of(t1, t2));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2480
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2481
06bc494ca11e Initial load
duke
parents:
diff changeset
  2482
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  2483
     * Return the least upper bound (lub) of set of types.  If the lub
06bc494ca11e Initial load
duke
parents:
diff changeset
  2484
     * does not exist return the type of null (bottom).
06bc494ca11e Initial load
duke
parents:
diff changeset
  2485
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2486
    public Type lub(List<Type> ts) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2487
        final int ARRAY_BOUND = 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2488
        final int CLASS_BOUND = 2;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2489
        int boundkind = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2490
        for (Type t : ts) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2491
            switch (t.tag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2492
            case CLASS:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2493
                boundkind |= CLASS_BOUND;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2494
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2495
            case ARRAY:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2496
                boundkind |= ARRAY_BOUND;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2497
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2498
            case  TYPEVAR:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2499
                do {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2500
                    t = t.getUpperBound();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2501
                } while (t.tag == TYPEVAR);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2502
                if (t.tag == ARRAY) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2503
                    boundkind |= ARRAY_BOUND;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2504
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2505
                    boundkind |= CLASS_BOUND;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2506
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2507
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2508
            default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2509
                if (t.isPrimitive())
162
6620f2a8e265 6611449: Internal Error thrown during generic method/constructor invocation
mcimadamore
parents: 10
diff changeset
  2510
                    return syms.errType;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2511
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2512
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2513
        switch (boundkind) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2514
        case 0:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2515
            return syms.botType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2516
06bc494ca11e Initial load
duke
parents:
diff changeset
  2517
        case ARRAY_BOUND:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2518
            // calculate lub(A[], B[])
06bc494ca11e Initial load
duke
parents:
diff changeset
  2519
            List<Type> elements = Type.map(ts, elemTypeFun);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2520
            for (Type t : elements) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2521
                if (t.isPrimitive()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2522
                    // if a primitive type is found, then return
06bc494ca11e Initial load
duke
parents:
diff changeset
  2523
                    // arraySuperType unless all the types are the
06bc494ca11e Initial load
duke
parents:
diff changeset
  2524
                    // same
06bc494ca11e Initial load
duke
parents:
diff changeset
  2525
                    Type first = ts.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2526
                    for (Type s : ts.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2527
                        if (!isSameType(first, s)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2528
                             // lub(int[], B[]) is Cloneable & Serializable
06bc494ca11e Initial load
duke
parents:
diff changeset
  2529
                            return arraySuperType();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2530
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2531
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2532
                    // all the array types are the same, return one
06bc494ca11e Initial load
duke
parents:
diff changeset
  2533
                    // lub(int[], int[]) is int[]
06bc494ca11e Initial load
duke
parents:
diff changeset
  2534
                    return first;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2535
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2536
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2537
            // lub(A[], B[]) is lub(A, B)[]
06bc494ca11e Initial load
duke
parents:
diff changeset
  2538
            return new ArrayType(lub(elements), syms.arrayClass);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2539
06bc494ca11e Initial load
duke
parents:
diff changeset
  2540
        case CLASS_BOUND:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2541
            // calculate lub(A, B)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2542
            while (ts.head.tag != CLASS && ts.head.tag != TYPEVAR)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2543
                ts = ts.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2544
            assert !ts.isEmpty();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2545
            List<Type> cl = closure(ts.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2546
            for (Type t : ts.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2547
                if (t.tag == CLASS || t.tag == TYPEVAR)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2548
                    cl = intersect(cl, closure(t));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2549
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2550
            return compoundMin(cl);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2551
06bc494ca11e Initial load
duke
parents:
diff changeset
  2552
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2553
            // calculate lub(A, B[])
06bc494ca11e Initial load
duke
parents:
diff changeset
  2554
            List<Type> classes = List.of(arraySuperType());
06bc494ca11e Initial load
duke
parents:
diff changeset
  2555
            for (Type t : ts) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2556
                if (t.tag != ARRAY) // Filter out any arrays
06bc494ca11e Initial load
duke
parents:
diff changeset
  2557
                    classes = classes.prepend(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2558
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2559
            // lub(A, B[]) is lub(A, arraySuperType)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2560
            return lub(classes);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2561
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2562
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2563
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
  2564
        private Type arraySuperType = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2565
        private Type arraySuperType() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2566
            // initialized lazily to avoid problems during compiler startup
06bc494ca11e Initial load
duke
parents:
diff changeset
  2567
            if (arraySuperType == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2568
                synchronized (this) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2569
                    if (arraySuperType == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2570
                        // JLS 10.8: all arrays implement Cloneable and Serializable.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2571
                        arraySuperType = makeCompoundType(List.of(syms.serializableType,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2572
                                                                  syms.cloneableType),
06bc494ca11e Initial load
duke
parents:
diff changeset
  2573
                                                          syms.objectType);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2574
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2575
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2576
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2577
            return arraySuperType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2578
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2579
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  2580
06bc494ca11e Initial load
duke
parents:
diff changeset
  2581
    // <editor-fold defaultstate="collapsed" desc="Greatest lower bound">
06bc494ca11e Initial load
duke
parents:
diff changeset
  2582
    public Type glb(Type t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2583
        if (s == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2584
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2585
        else if (isSubtypeNoCapture(t, s))
06bc494ca11e Initial load
duke
parents:
diff changeset
  2586
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2587
        else if (isSubtypeNoCapture(s, t))
06bc494ca11e Initial load
duke
parents:
diff changeset
  2588
            return s;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2589
06bc494ca11e Initial load
duke
parents:
diff changeset
  2590
        List<Type> closure = union(closure(t), closure(s));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2591
        List<Type> bounds = closureMin(closure);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2592
06bc494ca11e Initial load
duke
parents:
diff changeset
  2593
        if (bounds.isEmpty()) {             // length == 0
06bc494ca11e Initial load
duke
parents:
diff changeset
  2594
            return syms.objectType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2595
        } else if (bounds.tail.isEmpty()) { // length == 1
06bc494ca11e Initial load
duke
parents:
diff changeset
  2596
            return bounds.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2597
        } else {                            // length > 1
06bc494ca11e Initial load
duke
parents:
diff changeset
  2598
            int classCount = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2599
            for (Type bound : bounds)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2600
                if (!bound.isInterface())
06bc494ca11e Initial load
duke
parents:
diff changeset
  2601
                    classCount++;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2602
            if (classCount > 1)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2603
                return syms.errType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2604
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2605
        return makeCompoundType(bounds);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2606
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2607
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  2608
06bc494ca11e Initial load
duke
parents:
diff changeset
  2609
    // <editor-fold defaultstate="collapsed" desc="hashCode">
06bc494ca11e Initial load
duke
parents:
diff changeset
  2610
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  2611
     * Compute a hash code on a type.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2612
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2613
    public static int hashCode(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2614
        return hashCode.visit(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2615
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2616
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
  2617
        private static final UnaryVisitor<Integer> hashCode = new UnaryVisitor<Integer>() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2618
06bc494ca11e Initial load
duke
parents:
diff changeset
  2619
            public Integer visitType(Type t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2620
                return t.tag;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2621
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2622
06bc494ca11e Initial load
duke
parents:
diff changeset
  2623
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  2624
            public Integer visitClassType(ClassType t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2625
                int result = visit(t.getEnclosingType());
06bc494ca11e Initial load
duke
parents:
diff changeset
  2626
                result *= 127;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2627
                result += t.tsym.flatName().hashCode();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2628
                for (Type s : t.getTypeArguments()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2629
                    result *= 127;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2630
                    result += visit(s);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2631
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2632
                return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2633
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2634
06bc494ca11e Initial load
duke
parents:
diff changeset
  2635
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  2636
            public Integer visitWildcardType(WildcardType t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2637
                int result = t.kind.hashCode();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2638
                if (t.type != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2639
                    result *= 127;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2640
                    result += visit(t.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2641
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2642
                return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2643
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2644
06bc494ca11e Initial load
duke
parents:
diff changeset
  2645
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  2646
            public Integer visitArrayType(ArrayType t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2647
                return visit(t.elemtype) + 12;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2648
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2649
06bc494ca11e Initial load
duke
parents:
diff changeset
  2650
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  2651
            public Integer visitTypeVar(TypeVar t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2652
                return System.identityHashCode(t.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2653
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2654
06bc494ca11e Initial load
duke
parents:
diff changeset
  2655
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  2656
            public Integer visitUndetVar(UndetVar t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2657
                return System.identityHashCode(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2658
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2659
06bc494ca11e Initial load
duke
parents:
diff changeset
  2660
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  2661
            public Integer visitErrorType(ErrorType t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2662
                return 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2663
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2664
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
  2665
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  2666
06bc494ca11e Initial load
duke
parents:
diff changeset
  2667
    // <editor-fold defaultstate="collapsed" desc="Return-Type-Substitutable">
06bc494ca11e Initial load
duke
parents:
diff changeset
  2668
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  2669
     * Does t have a result that is a subtype of the result type of s,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2670
     * suitable for covariant returns?  It is assumed that both types
06bc494ca11e Initial load
duke
parents:
diff changeset
  2671
     * are (possibly polymorphic) method types.  Monomorphic method
06bc494ca11e Initial load
duke
parents:
diff changeset
  2672
     * types are handled in the obvious way.  Polymorphic method types
06bc494ca11e Initial load
duke
parents:
diff changeset
  2673
     * require renaming all type variables of one to corresponding
06bc494ca11e Initial load
duke
parents:
diff changeset
  2674
     * type variables in the other, where correspondence is by
06bc494ca11e Initial load
duke
parents:
diff changeset
  2675
     * position in the type parameter list. */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2676
    public boolean resultSubtype(Type t, Type s, Warner warner) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2677
        List<Type> tvars = t.getTypeArguments();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2678
        List<Type> svars = s.getTypeArguments();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2679
        Type tres = t.getReturnType();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2680
        Type sres = subst(s.getReturnType(), svars, tvars);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2681
        return covariantReturnType(tres, sres, warner);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2682
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2683
06bc494ca11e Initial load
duke
parents:
diff changeset
  2684
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  2685
     * Return-Type-Substitutable.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2686
     * @see <a href="http://java.sun.com/docs/books/jls/">The Java
06bc494ca11e Initial load
duke
parents:
diff changeset
  2687
     * Language Specification, Third Ed. (8.4.5)</a>
06bc494ca11e Initial load
duke
parents:
diff changeset
  2688
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2689
    public boolean returnTypeSubstitutable(Type r1, Type r2) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2690
        if (hasSameArgs(r1, r2))
06bc494ca11e Initial load
duke
parents:
diff changeset
  2691
            return resultSubtype(r1, r2, Warner.noWarnings);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2692
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
  2693
            return covariantReturnType(r1.getReturnType(),
06bc494ca11e Initial load
duke
parents:
diff changeset
  2694
                                       erasure(r2.getReturnType()),
06bc494ca11e Initial load
duke
parents:
diff changeset
  2695
                                       Warner.noWarnings);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2696
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2697
06bc494ca11e Initial load
duke
parents:
diff changeset
  2698
    public boolean returnTypeSubstitutable(Type r1,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2699
                                           Type r2, Type r2res,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2700
                                           Warner warner) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2701
        if (isSameType(r1.getReturnType(), r2res))
06bc494ca11e Initial load
duke
parents:
diff changeset
  2702
            return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2703
        if (r1.getReturnType().isPrimitive() || r2res.isPrimitive())
06bc494ca11e Initial load
duke
parents:
diff changeset
  2704
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2705
06bc494ca11e Initial load
duke
parents:
diff changeset
  2706
        if (hasSameArgs(r1, r2))
06bc494ca11e Initial load
duke
parents:
diff changeset
  2707
            return covariantReturnType(r1.getReturnType(), r2res, warner);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2708
        if (!source.allowCovariantReturns())
06bc494ca11e Initial load
duke
parents:
diff changeset
  2709
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2710
        if (isSubtypeUnchecked(r1.getReturnType(), r2res, warner))
06bc494ca11e Initial load
duke
parents:
diff changeset
  2711
            return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2712
        if (!isSubtype(r1.getReturnType(), erasure(r2res)))
06bc494ca11e Initial load
duke
parents:
diff changeset
  2713
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2714
        warner.warnUnchecked();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2715
        return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2716
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2717
06bc494ca11e Initial load
duke
parents:
diff changeset
  2718
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  2719
     * Is t an appropriate return type in an overrider for a
06bc494ca11e Initial load
duke
parents:
diff changeset
  2720
     * method that returns s?
06bc494ca11e Initial load
duke
parents:
diff changeset
  2721
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2722
    public boolean covariantReturnType(Type t, Type s, Warner warner) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2723
        return
06bc494ca11e Initial load
duke
parents:
diff changeset
  2724
            isSameType(t, s) ||
06bc494ca11e Initial load
duke
parents:
diff changeset
  2725
            source.allowCovariantReturns() &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  2726
            !t.isPrimitive() &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  2727
            !s.isPrimitive() &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  2728
            isAssignable(t, s, warner);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2729
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2730
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  2731
06bc494ca11e Initial load
duke
parents:
diff changeset
  2732
    // <editor-fold defaultstate="collapsed" desc="Box/unbox support">
06bc494ca11e Initial load
duke
parents:
diff changeset
  2733
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  2734
     * Return the class that boxes the given primitive.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2735
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2736
    public ClassSymbol boxedClass(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2737
        return reader.enterClass(syms.boxedName[t.tag]);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2738
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2739
06bc494ca11e Initial load
duke
parents:
diff changeset
  2740
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  2741
     * Return the primitive type corresponding to a boxed type.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2742
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2743
    public Type unboxedType(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2744
        if (allowBoxing) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2745
            for (int i=0; i<syms.boxedName.length; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2746
                Name box = syms.boxedName[i];
06bc494ca11e Initial load
duke
parents:
diff changeset
  2747
                if (box != null &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  2748
                    asSuper(t, reader.enterClass(box)) != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2749
                    return syms.typeOfTag[i];
06bc494ca11e Initial load
duke
parents:
diff changeset
  2750
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2751
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2752
        return Type.noType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2753
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2754
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  2755
06bc494ca11e Initial load
duke
parents:
diff changeset
  2756
    // <editor-fold defaultstate="collapsed" desc="Capture conversion">
06bc494ca11e Initial load
duke
parents:
diff changeset
  2757
    /*
06bc494ca11e Initial load
duke
parents:
diff changeset
  2758
     * JLS 3rd Ed. 5.1.10 Capture Conversion:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2759
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  2760
     * Let G name a generic type declaration with n formal type
06bc494ca11e Initial load
duke
parents:
diff changeset
  2761
     * parameters A1 ... An with corresponding bounds U1 ... Un. There
06bc494ca11e Initial load
duke
parents:
diff changeset
  2762
     * exists a capture conversion from G<T1 ... Tn> to G<S1 ... Sn>,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2763
     * where, for 1 <= i <= n:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2764
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  2765
     * + If Ti is a wildcard type argument (4.5.1) of the form ? then
06bc494ca11e Initial load
duke
parents:
diff changeset
  2766
     *   Si is a fresh type variable whose upper bound is
06bc494ca11e Initial load
duke
parents:
diff changeset
  2767
     *   Ui[A1 := S1, ..., An := Sn] and whose lower bound is the null
06bc494ca11e Initial load
duke
parents:
diff changeset
  2768
     *   type.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2769
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  2770
     * + If Ti is a wildcard type argument of the form ? extends Bi,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2771
     *   then Si is a fresh type variable whose upper bound is
06bc494ca11e Initial load
duke
parents:
diff changeset
  2772
     *   glb(Bi, Ui[A1 := S1, ..., An := Sn]) and whose lower bound is
06bc494ca11e Initial load
duke
parents:
diff changeset
  2773
     *   the null type, where glb(V1,... ,Vm) is V1 & ... & Vm. It is
06bc494ca11e Initial load
duke
parents:
diff changeset
  2774
     *   a compile-time error if for any two classes (not interfaces)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2775
     *   Vi and Vj,Vi is not a subclass of Vj or vice versa.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2776
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  2777
     * + If Ti is a wildcard type argument of the form ? super Bi,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2778
     *   then Si is a fresh type variable whose upper bound is
06bc494ca11e Initial load
duke
parents:
diff changeset
  2779
     *   Ui[A1 := S1, ..., An := Sn] and whose lower bound is Bi.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2780
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  2781
     * + Otherwise, Si = Ti.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2782
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  2783
     * Capture conversion on any type other than a parameterized type
06bc494ca11e Initial load
duke
parents:
diff changeset
  2784
     * (4.5) acts as an identity conversion (5.1.1). Capture
06bc494ca11e Initial load
duke
parents:
diff changeset
  2785
     * conversions never require a special action at run time and
06bc494ca11e Initial load
duke
parents:
diff changeset
  2786
     * therefore never throw an exception at run time.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2787
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  2788
     * Capture conversion is not applied recursively.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2789
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2790
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  2791
     * Capture conversion as specified by JLS 3rd Ed.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2792
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2793
    public Type capture(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2794
        if (t.tag != CLASS)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2795
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2796
        ClassType cls = (ClassType)t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2797
        if (cls.isRaw() || !cls.isParameterized())
06bc494ca11e Initial load
duke
parents:
diff changeset
  2798
            return cls;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2799
06bc494ca11e Initial load
duke
parents:
diff changeset
  2800
        ClassType G = (ClassType)cls.asElement().asType();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2801
        List<Type> A = G.getTypeArguments();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2802
        List<Type> T = cls.getTypeArguments();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2803
        List<Type> S = freshTypeVariables(T);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2804
06bc494ca11e Initial load
duke
parents:
diff changeset
  2805
        List<Type> currentA = A;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2806
        List<Type> currentT = T;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2807
        List<Type> currentS = S;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2808
        boolean captured = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2809
        while (!currentA.isEmpty() &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  2810
               !currentT.isEmpty() &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  2811
               !currentS.isEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2812
            if (currentS.head != currentT.head) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2813
                captured = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2814
                WildcardType Ti = (WildcardType)currentT.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2815
                Type Ui = currentA.head.getUpperBound();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2816
                CapturedType Si = (CapturedType)currentS.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2817
                if (Ui == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2818
                    Ui = syms.objectType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2819
                switch (Ti.kind) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2820
                case UNBOUND:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2821
                    Si.bound = subst(Ui, A, S);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2822
                    Si.lower = syms.botType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2823
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2824
                case EXTENDS:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2825
                    Si.bound = glb(Ti.getExtendsBound(), subst(Ui, A, S));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2826
                    Si.lower = syms.botType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2827
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2828
                case SUPER:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2829
                    Si.bound = subst(Ui, A, S);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2830
                    Si.lower = Ti.getSuperBound();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2831
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2832
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2833
                if (Si.bound == Si.lower)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2834
                    currentS.head = Si.bound;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2835
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2836
            currentA = currentA.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2837
            currentT = currentT.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2838
            currentS = currentS.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2839
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2840
        if (!currentA.isEmpty() || !currentT.isEmpty() || !currentS.isEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
  2841
            return erasure(t); // some "rare" type involved
06bc494ca11e Initial load
duke
parents:
diff changeset
  2842
06bc494ca11e Initial load
duke
parents:
diff changeset
  2843
        if (captured)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2844
            return new ClassType(cls.getEnclosingType(), S, cls.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2845
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
  2846
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2847
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2848
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
  2849
        private List<Type> freshTypeVariables(List<Type> types) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2850
            ListBuffer<Type> result = lb();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2851
            for (Type t : types) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2852
                if (t.tag == WILDCARD) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2853
                    Type bound = ((WildcardType)t).getExtendsBound();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2854
                    if (bound == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2855
                        bound = syms.objectType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2856
                    result.append(new CapturedType(capturedName,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2857
                                                   syms.noSymbol,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2858
                                                   bound,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2859
                                                   syms.botType,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2860
                                                   (WildcardType)t));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2861
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2862
                    result.append(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2863
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2864
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2865
            return result.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2866
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2867
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  2868
06bc494ca11e Initial load
duke
parents:
diff changeset
  2869
    // <editor-fold defaultstate="collapsed" desc="Internal utility methods">
06bc494ca11e Initial load
duke
parents:
diff changeset
  2870
    private List<Type> upperBounds(List<Type> ss) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2871
        if (ss.isEmpty()) return ss;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2872
        Type head = upperBound(ss.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2873
        List<Type> tail = upperBounds(ss.tail);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2874
        if (head != ss.head || tail != ss.tail)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2875
            return tail.prepend(head);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2876
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
  2877
            return ss;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2878
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2879
06bc494ca11e Initial load
duke
parents:
diff changeset
  2880
    private boolean sideCast(Type from, Type to, Warner warn) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2881
        // We are casting from type $from$ to type $to$, which are
06bc494ca11e Initial load
duke
parents:
diff changeset
  2882
        // non-final unrelated types.  This method
06bc494ca11e Initial load
duke
parents:
diff changeset
  2883
        // tries to reject a cast by transferring type parameters
06bc494ca11e Initial load
duke
parents:
diff changeset
  2884
        // from $to$ to $from$ by common superinterfaces.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2885
        boolean reverse = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2886
        Type target = to;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2887
        if ((to.tsym.flags() & INTERFACE) == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2888
            assert (from.tsym.flags() & INTERFACE) != 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2889
            reverse = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2890
            to = from;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2891
            from = target;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2892
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2893
        List<Type> commonSupers = superClosure(to, erasure(from));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2894
        boolean giveWarning = commonSupers.isEmpty();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2895
        // The arguments to the supers could be unified here to
06bc494ca11e Initial load
duke
parents:
diff changeset
  2896
        // get a more accurate analysis
06bc494ca11e Initial load
duke
parents:
diff changeset
  2897
        while (commonSupers.nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2898
            Type t1 = asSuper(from, commonSupers.head.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2899
            Type t2 = commonSupers.head; // same as asSuper(to, commonSupers.head.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2900
            if (disjointTypes(t1.getTypeArguments(), t2.getTypeArguments()))
06bc494ca11e Initial load
duke
parents:
diff changeset
  2901
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2902
            giveWarning = giveWarning || (reverse ? giveWarning(t2, t1) : giveWarning(t1, t2));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2903
            commonSupers = commonSupers.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2904
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2905
        if (giveWarning && !isReifiable(to))
06bc494ca11e Initial load
duke
parents:
diff changeset
  2906
            warn.warnUnchecked();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2907
        if (!source.allowCovariantReturns())
06bc494ca11e Initial load
duke
parents:
diff changeset
  2908
            // reject if there is a common method signature with
06bc494ca11e Initial load
duke
parents:
diff changeset
  2909
            // incompatible return types.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2910
            chk.checkCompatibleAbstracts(warn.pos(), from, to);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2911
        return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2912
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2913
06bc494ca11e Initial load
duke
parents:
diff changeset
  2914
    private boolean sideCastFinal(Type from, Type to, Warner warn) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2915
        // We are casting from type $from$ to type $to$, which are
06bc494ca11e Initial load
duke
parents:
diff changeset
  2916
        // unrelated types one of which is final and the other of
06bc494ca11e Initial load
duke
parents:
diff changeset
  2917
        // which is an interface.  This method
06bc494ca11e Initial load
duke
parents:
diff changeset
  2918
        // tries to reject a cast by transferring type parameters
06bc494ca11e Initial load
duke
parents:
diff changeset
  2919
        // from the final class to the interface.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2920
        boolean reverse = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2921
        Type target = to;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2922
        if ((to.tsym.flags() & INTERFACE) == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2923
            assert (from.tsym.flags() & INTERFACE) != 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2924
            reverse = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2925
            to = from;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2926
            from = target;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2927
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2928
        assert (from.tsym.flags() & FINAL) != 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2929
        Type t1 = asSuper(from, to.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2930
        if (t1 == null) return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2931
        Type t2 = to;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2932
        if (disjointTypes(t1.getTypeArguments(), t2.getTypeArguments()))
06bc494ca11e Initial load
duke
parents:
diff changeset
  2933
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2934
        if (!source.allowCovariantReturns())
06bc494ca11e Initial load
duke
parents:
diff changeset
  2935
            // reject if there is a common method signature with
06bc494ca11e Initial load
duke
parents:
diff changeset
  2936
            // incompatible return types.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2937
            chk.checkCompatibleAbstracts(warn.pos(), from, to);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2938
        if (!isReifiable(target) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  2939
            (reverse ? giveWarning(t2, t1) : giveWarning(t1, t2)))
06bc494ca11e Initial load
duke
parents:
diff changeset
  2940
            warn.warnUnchecked();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2941
        return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2942
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2943
06bc494ca11e Initial load
duke
parents:
diff changeset
  2944
    private boolean giveWarning(Type from, Type to) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2945
        // To and from are (possibly different) parameterizations
06bc494ca11e Initial load
duke
parents:
diff changeset
  2946
        // of the same class or interface
06bc494ca11e Initial load
duke
parents:
diff changeset
  2947
        return to.isParameterized() && !containsType(to.getTypeArguments(), from.getTypeArguments());
06bc494ca11e Initial load
duke
parents:
diff changeset
  2948
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2949
06bc494ca11e Initial load
duke
parents:
diff changeset
  2950
    private List<Type> superClosure(Type t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2951
        List<Type> cl = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2952
        for (List<Type> l = interfaces(t); l.nonEmpty(); l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2953
            if (isSubtype(s, erasure(l.head))) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2954
                cl = insert(cl, l.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2955
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2956
                cl = union(cl, superClosure(l.head, s));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2957
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2958
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2959
        return cl;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2960
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2961
06bc494ca11e Initial load
duke
parents:
diff changeset
  2962
    private boolean containsTypeEquivalent(Type t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2963
        return
06bc494ca11e Initial load
duke
parents:
diff changeset
  2964
            isSameType(t, s) || // shortcut
06bc494ca11e Initial load
duke
parents:
diff changeset
  2965
            containsType(t, s) && containsType(s, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2966
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2967
06bc494ca11e Initial load
duke
parents:
diff changeset
  2968
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  2969
     * Adapt a type by computing a substitution which maps a source
06bc494ca11e Initial load
duke
parents:
diff changeset
  2970
     * type to a target type.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2971
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  2972
     * @param source    the source type
06bc494ca11e Initial load
duke
parents:
diff changeset
  2973
     * @param target    the target type
06bc494ca11e Initial load
duke
parents:
diff changeset
  2974
     * @param from      the type variables of the computed substitution
06bc494ca11e Initial load
duke
parents:
diff changeset
  2975
     * @param to        the types of the computed substitution.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2976
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2977
    public void adapt(Type source,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2978
                       Type target,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2979
                       ListBuffer<Type> from,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2980
                       ListBuffer<Type> to) throws AdaptFailure {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2981
        Map<Symbol,Type> mapping = new HashMap<Symbol,Type>();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2982
        adaptRecursive(source, target, from, to, mapping);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2983
        List<Type> fromList = from.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2984
        List<Type> toList = to.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2985
        while (!fromList.isEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2986
            Type val = mapping.get(fromList.head.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2987
            if (toList.head != val)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2988
                toList.head = val;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2989
            fromList = fromList.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2990
            toList = toList.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2991
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2992
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2993
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
  2994
        private void adaptRecursive(Type source,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2995
                                    Type target,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2996
                                    ListBuffer<Type> from,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2997
                                    ListBuffer<Type> to,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2998
                                    Map<Symbol,Type> mapping) throws AdaptFailure {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2999
            if (source.tag == TYPEVAR) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3000
                // Check to see if there is
06bc494ca11e Initial load
duke
parents:
diff changeset
  3001
                // already a mapping for $source$, in which case
06bc494ca11e Initial load
duke
parents:
diff changeset
  3002
                // the old mapping will be merged with the new
06bc494ca11e Initial load
duke
parents:
diff changeset
  3003
                Type val = mapping.get(source.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3004
                if (val != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3005
                    if (val.isSuperBound() && target.isSuperBound()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3006
                        val = isSubtype(lowerBound(val), lowerBound(target))
06bc494ca11e Initial load
duke
parents:
diff changeset
  3007
                            ? target : val;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3008
                    } else if (val.isExtendsBound() && target.isExtendsBound()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3009
                        val = isSubtype(upperBound(val), upperBound(target))
06bc494ca11e Initial load
duke
parents:
diff changeset
  3010
                            ? val : target;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3011
                    } else if (!isSameType(val, target)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3012
                        throw new AdaptFailure();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3013
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3014
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3015
                    val = target;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3016
                    from.append(source);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3017
                    to.append(target);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3018
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3019
                mapping.put(source.tsym, val);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3020
            } else if (source.tag == target.tag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3021
                switch (source.tag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3022
                    case CLASS:
06bc494ca11e Initial load
duke
parents:
diff changeset
  3023
                        adapt(source.allparams(), target.allparams(),
06bc494ca11e Initial load
duke
parents:
diff changeset
  3024
                              from, to, mapping);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3025
                        break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3026
                    case ARRAY:
06bc494ca11e Initial load
duke
parents:
diff changeset
  3027
                        adaptRecursive(elemtype(source), elemtype(target),
06bc494ca11e Initial load
duke
parents:
diff changeset
  3028
                                       from, to, mapping);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3029
                        break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3030
                    case WILDCARD:
06bc494ca11e Initial load
duke
parents:
diff changeset
  3031
                        if (source.isExtendsBound()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3032
                            adaptRecursive(upperBound(source), upperBound(target),
06bc494ca11e Initial load
duke
parents:
diff changeset
  3033
                                           from, to, mapping);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3034
                        } else if (source.isSuperBound()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3035
                            adaptRecursive(lowerBound(source), lowerBound(target),
06bc494ca11e Initial load
duke
parents:
diff changeset
  3036
                                           from, to, mapping);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3037
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3038
                        break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3039
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3040
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3041
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3042
        public static class AdaptFailure extends Exception {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3043
            static final long serialVersionUID = -7490231548272701566L;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3044
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3045
06bc494ca11e Initial load
duke
parents:
diff changeset
  3046
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  3047
     * Adapt a type by computing a substitution which maps a list of
06bc494ca11e Initial load
duke
parents:
diff changeset
  3048
     * source types to a list of target types.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3049
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  3050
     * @param source    the source type
06bc494ca11e Initial load
duke
parents:
diff changeset
  3051
     * @param target    the target type
06bc494ca11e Initial load
duke
parents:
diff changeset
  3052
     * @param from      the type variables of the computed substitution
06bc494ca11e Initial load
duke
parents:
diff changeset
  3053
     * @param to        the types of the computed substitution.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3054
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3055
    private void adapt(List<Type> source,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3056
                       List<Type> target,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3057
                       ListBuffer<Type> from,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3058
                       ListBuffer<Type> to,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3059
                       Map<Symbol,Type> mapping) throws AdaptFailure {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3060
        if (source.length() == target.length()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3061
            while (source.nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3062
                adaptRecursive(source.head, target.head, from, to, mapping);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3063
                source = source.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3064
                target = target.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3065
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3066
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3067
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3068
06bc494ca11e Initial load
duke
parents:
diff changeset
  3069
    private void adaptSelf(Type t,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3070
                           ListBuffer<Type> from,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3071
                           ListBuffer<Type> to) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3072
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3073
            //if (t.tsym.type != t)
06bc494ca11e Initial load
duke
parents:
diff changeset
  3074
                adapt(t.tsym.type, t, from, to);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3075
        } catch (AdaptFailure ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3076
            // Adapt should never fail calculating a mapping from
06bc494ca11e Initial load
duke
parents:
diff changeset
  3077
            // t.tsym.type to t as there can be no merge problem.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3078
            throw new AssertionError(ex);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3079
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3080
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3081
06bc494ca11e Initial load
duke
parents:
diff changeset
  3082
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  3083
     * Rewrite all type variables (universal quantifiers) in the given
06bc494ca11e Initial load
duke
parents:
diff changeset
  3084
     * type to wildcards (existential quantifiers).  This is used to
06bc494ca11e Initial load
duke
parents:
diff changeset
  3085
     * determine if a cast is allowed.  For example, if high is true
06bc494ca11e Initial load
duke
parents:
diff changeset
  3086
     * and {@code T <: Number}, then {@code List<T>} is rewritten to
06bc494ca11e Initial load
duke
parents:
diff changeset
  3087
     * {@code List<?  extends Number>}.  Since {@code List<Integer> <:
06bc494ca11e Initial load
duke
parents:
diff changeset
  3088
     * List<? extends Number>} a {@code List<T>} can be cast to {@code
06bc494ca11e Initial load
duke
parents:
diff changeset
  3089
     * List<Integer>} with a warning.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3090
     * @param t a type
06bc494ca11e Initial load
duke
parents:
diff changeset
  3091
     * @param high if true return an upper bound; otherwise a lower
06bc494ca11e Initial load
duke
parents:
diff changeset
  3092
     * bound
06bc494ca11e Initial load
duke
parents:
diff changeset
  3093
     * @param rewriteTypeVars only rewrite captured wildcards if false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3094
     * otherwise rewrite all type variables
06bc494ca11e Initial load
duke
parents:
diff changeset
  3095
     * @return the type rewritten with wildcards (existential
06bc494ca11e Initial load
duke
parents:
diff changeset
  3096
     * quantifiers) only
06bc494ca11e Initial load
duke
parents:
diff changeset
  3097
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3098
    private Type rewriteQuantifiers(Type t, boolean high, boolean rewriteTypeVars) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3099
        ListBuffer<Type> from = new ListBuffer<Type>();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3100
        ListBuffer<Type> to = new ListBuffer<Type>();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3101
        adaptSelf(t, from, to);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3102
        ListBuffer<Type> rewritten = new ListBuffer<Type>();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3103
        List<Type> formals = from.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3104
        boolean changed = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3105
        for (Type arg : to.toList()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3106
            Type bound;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3107
            if (rewriteTypeVars && arg.tag == TYPEVAR) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3108
                TypeVar tv = (TypeVar)arg;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3109
                bound = high ? tv.bound : syms.botType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3110
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3111
                bound = high ? upperBound(arg) : lowerBound(arg);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3112
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3113
            Type newarg = bound;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3114
            if (arg != bound) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3115
                changed = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3116
                newarg = high ? makeExtendsWildcard(bound, (TypeVar)formals.head)
06bc494ca11e Initial load
duke
parents:
diff changeset
  3117
                              : makeSuperWildcard(bound, (TypeVar)formals.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3118
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3119
            rewritten.append(newarg);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3120
            formals = formals.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3121
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3122
        if (changed)
06bc494ca11e Initial load
duke
parents:
diff changeset
  3123
            return subst(t.tsym.type, from.toList(), rewritten.toList());
06bc494ca11e Initial load
duke
parents:
diff changeset
  3124
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
  3125
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3126
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3127
06bc494ca11e Initial load
duke
parents:
diff changeset
  3128
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  3129
     * Create a wildcard with the given upper (extends) bound; create
06bc494ca11e Initial load
duke
parents:
diff changeset
  3130
     * an unbounded wildcard if bound is Object.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3131
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  3132
     * @param bound the upper bound
06bc494ca11e Initial load
duke
parents:
diff changeset
  3133
     * @param formal the formal type parameter that will be
06bc494ca11e Initial load
duke
parents:
diff changeset
  3134
     * substituted by the wildcard
06bc494ca11e Initial load
duke
parents:
diff changeset
  3135
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3136
    private WildcardType makeExtendsWildcard(Type bound, TypeVar formal) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3137
        if (bound == syms.objectType) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3138
            return new WildcardType(syms.objectType,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3139
                                    BoundKind.UNBOUND,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3140
                                    syms.boundClass,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3141
                                    formal);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3142
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3143
            return new WildcardType(bound,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3144
                                    BoundKind.EXTENDS,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3145
                                    syms.boundClass,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3146
                                    formal);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3147
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3148
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3149
06bc494ca11e Initial load
duke
parents:
diff changeset
  3150
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  3151
     * Create a wildcard with the given lower (super) bound; create an
06bc494ca11e Initial load
duke
parents:
diff changeset
  3152
     * unbounded wildcard if bound is bottom (type of {@code null}).
06bc494ca11e Initial load
duke
parents:
diff changeset
  3153
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  3154
     * @param bound the lower bound
06bc494ca11e Initial load
duke
parents:
diff changeset
  3155
     * @param formal the formal type parameter that will be
06bc494ca11e Initial load
duke
parents:
diff changeset
  3156
     * substituted by the wildcard
06bc494ca11e Initial load
duke
parents:
diff changeset
  3157
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3158
    private WildcardType makeSuperWildcard(Type bound, TypeVar formal) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3159
        if (bound.tag == BOT) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3160
            return new WildcardType(syms.objectType,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3161
                                    BoundKind.UNBOUND,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3162
                                    syms.boundClass,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3163
                                    formal);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3164
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3165
            return new WildcardType(bound,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3166
                                    BoundKind.SUPER,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3167
                                    syms.boundClass,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3168
                                    formal);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3169
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3170
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3171
06bc494ca11e Initial load
duke
parents:
diff changeset
  3172
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  3173
     * A wrapper for a type that allows use in sets.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3174
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3175
    class SingletonType {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3176
        final Type t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3177
        SingletonType(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3178
            this.t = t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3179
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3180
        public int hashCode() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3181
            return Types.this.hashCode(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3182
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3183
        public boolean equals(Object obj) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3184
            return (obj instanceof SingletonType) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  3185
                isSameType(t, ((SingletonType)obj).t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3186
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3187
        public String toString() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3188
            return t.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3189
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3190
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3191
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  3192
06bc494ca11e Initial load
duke
parents:
diff changeset
  3193
    // <editor-fold defaultstate="collapsed" desc="Visitors">
06bc494ca11e Initial load
duke
parents:
diff changeset
  3194
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  3195
     * A default visitor for types.  All visitor methods except
06bc494ca11e Initial load
duke
parents:
diff changeset
  3196
     * visitType are implemented by delegating to visitType.  Concrete
06bc494ca11e Initial load
duke
parents:
diff changeset
  3197
     * subclasses must provide an implementation of visitType and can
06bc494ca11e Initial load
duke
parents:
diff changeset
  3198
     * override other methods as needed.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3199
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  3200
     * @param <R> the return type of the operation implemented by this
06bc494ca11e Initial load
duke
parents:
diff changeset
  3201
     * visitor; use Void if no return type is needed.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3202
     * @param <S> the type of the second argument (the first being the
06bc494ca11e Initial load
duke
parents:
diff changeset
  3203
     * type itself) of the operation implemented by this visitor; use
06bc494ca11e Initial load
duke
parents:
diff changeset
  3204
     * Void if a second argument is not needed.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3205
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3206
    public static abstract class DefaultTypeVisitor<R,S> implements Type.Visitor<R,S> {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3207
        final public R visit(Type t, S s)               { return t.accept(this, s); }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3208
        public R visitClassType(ClassType t, S s)       { return visitType(t, s); }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3209
        public R visitWildcardType(WildcardType t, S s) { return visitType(t, s); }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3210
        public R visitArrayType(ArrayType t, S s)       { return visitType(t, s); }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3211
        public R visitMethodType(MethodType t, S s)     { return visitType(t, s); }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3212
        public R visitPackageType(PackageType t, S s)   { return visitType(t, s); }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3213
        public R visitTypeVar(TypeVar t, S s)           { return visitType(t, s); }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3214
        public R visitCapturedType(CapturedType t, S s) { return visitType(t, s); }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3215
        public R visitForAll(ForAll t, S s)             { return visitType(t, s); }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3216
        public R visitUndetVar(UndetVar t, S s)         { return visitType(t, s); }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3217
        public R visitErrorType(ErrorType t, S s)       { return visitType(t, s); }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3218
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3219
06bc494ca11e Initial load
duke
parents:
diff changeset
  3220
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  3221
     * A <em>simple</em> visitor for types.  This visitor is simple as
06bc494ca11e Initial load
duke
parents:
diff changeset
  3222
     * captured wildcards, for-all types (generic methods), and
06bc494ca11e Initial load
duke
parents:
diff changeset
  3223
     * undetermined type variables (part of inference) are hidden.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3224
     * Captured wildcards are hidden by treating them as type
06bc494ca11e Initial load
duke
parents:
diff changeset
  3225
     * variables and the rest are hidden by visiting their qtypes.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3226
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  3227
     * @param <R> the return type of the operation implemented by this
06bc494ca11e Initial load
duke
parents:
diff changeset
  3228
     * visitor; use Void if no return type is needed.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3229
     * @param <S> the type of the second argument (the first being the
06bc494ca11e Initial load
duke
parents:
diff changeset
  3230
     * type itself) of the operation implemented by this visitor; use
06bc494ca11e Initial load
duke
parents:
diff changeset
  3231
     * Void if a second argument is not needed.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3232
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3233
    public static abstract class SimpleVisitor<R,S> extends DefaultTypeVisitor<R,S> {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3234
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  3235
        public R visitCapturedType(CapturedType t, S s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3236
            return visitTypeVar(t, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3237
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3238
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  3239
        public R visitForAll(ForAll t, S s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3240
            return visit(t.qtype, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3241
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3242
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  3243
        public R visitUndetVar(UndetVar t, S s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3244
            return visit(t.qtype, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3245
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3246
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3247
06bc494ca11e Initial load
duke
parents:
diff changeset
  3248
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  3249
     * A plain relation on types.  That is a 2-ary function on the
06bc494ca11e Initial load
duke
parents:
diff changeset
  3250
     * form Type&nbsp;&times;&nbsp;Type&nbsp;&rarr;&nbsp;Boolean.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3251
     * <!-- In plain text: Type x Type -> Boolean -->
06bc494ca11e Initial load
duke
parents:
diff changeset
  3252
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3253
    public static abstract class TypeRelation extends SimpleVisitor<Boolean,Type> {}
06bc494ca11e Initial load
duke
parents:
diff changeset
  3254
06bc494ca11e Initial load
duke
parents:
diff changeset
  3255
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  3256
     * A convenience visitor for implementing operations that only
06bc494ca11e Initial load
duke
parents:
diff changeset
  3257
     * require one argument (the type itself), that is, unary
06bc494ca11e Initial load
duke
parents:
diff changeset
  3258
     * operations.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3259
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  3260
     * @param <R> the return type of the operation implemented by this
06bc494ca11e Initial load
duke
parents:
diff changeset
  3261
     * visitor; use Void if no return type is needed.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3262
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3263
    public static abstract class UnaryVisitor<R> extends SimpleVisitor<R,Void> {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3264
        final public R visit(Type t) { return t.accept(this, null); }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3265
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3266
06bc494ca11e Initial load
duke
parents:
diff changeset
  3267
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  3268
     * A visitor for implementing a mapping from types to types.  The
06bc494ca11e Initial load
duke
parents:
diff changeset
  3269
     * default behavior of this class is to implement the identity
06bc494ca11e Initial load
duke
parents:
diff changeset
  3270
     * mapping (mapping a type to itself).  This can be overridden in
06bc494ca11e Initial load
duke
parents:
diff changeset
  3271
     * subclasses.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3272
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  3273
     * @param <S> the type of the second argument (the first being the
06bc494ca11e Initial load
duke
parents:
diff changeset
  3274
     * type itself) of this mapping; use Void if a second argument is
06bc494ca11e Initial load
duke
parents:
diff changeset
  3275
     * not needed.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3276
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3277
    public static class MapVisitor<S> extends DefaultTypeVisitor<Type,S> {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3278
        final public Type visit(Type t) { return t.accept(this, null); }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3279
        public Type visitType(Type t, S s) { return t; }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3280
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3281
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  3282
}