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