langtools/src/share/classes/com/sun/tools/javac/code/Types.java
author mcimadamore
Wed, 06 Feb 2013 14:03:39 +0000
changeset 15705 c4124695db0c
parent 15385 ee1eebe7e210
child 15718 8e54c8e43d38
permissions -rw-r--r--
8007463: Cleanup inference related classes Summary: Make Infer.InferenceContext an inner class; adjust bound replacement logic in Type.UndetVar Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
     2
 * Copyright (c) 2003, 2013, 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;
14719
cd65a651beb3 8004068: Fix build problems caused by on-demand imports
jjg
parents: 14547
diff changeset
    29
import java.util.Comparator;
cd65a651beb3 8004068: Fix build problems caused by on-demand imports
jjg
parents: 14547
diff changeset
    30
import java.util.HashSet;
cd65a651beb3 8004068: Fix build problems caused by on-demand imports
jjg
parents: 14547
diff changeset
    31
import java.util.HashMap;
cd65a651beb3 8004068: Fix build problems caused by on-demand imports
jjg
parents: 14547
diff changeset
    32
import java.util.Locale;
cd65a651beb3 8004068: Fix build problems caused by on-demand imports
jjg
parents: 14547
diff changeset
    33
import java.util.Map;
cd65a651beb3 8004068: Fix build problems caused by on-demand imports
jjg
parents: 14547
diff changeset
    34
import java.util.Set;
cd65a651beb3 8004068: Fix build problems caused by on-demand imports
jjg
parents: 14547
diff changeset
    35
import java.util.WeakHashMap;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
    37
import javax.lang.model.type.TypeKind;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
    38
6575
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents: 6353
diff changeset
    39
import com.sun.tools.javac.code.Attribute.RetentionPolicy;
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 7637
diff changeset
    40
import com.sun.tools.javac.code.Lint.LintCategory;
14048
308d1cf8fe46 7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents: 13689
diff changeset
    41
import com.sun.tools.javac.code.Type.UndetVar.InferenceBound;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
import com.sun.tools.javac.comp.Check;
14258
8d2148961366 8000663: clean up langtools imports
jjg
parents: 14058
diff changeset
    43
import com.sun.tools.javac.jvm.ClassReader;
8d2148961366 8000663: clean up langtools imports
jjg
parents: 14058
diff changeset
    44
import com.sun.tools.javac.util.*;
8d2148961366 8000663: clean up langtools imports
jjg
parents: 14058
diff changeset
    45
import static com.sun.tools.javac.code.BoundKind.*;
8d2148961366 8000663: clean up langtools imports
jjg
parents: 14058
diff changeset
    46
import static com.sun.tools.javac.code.Flags.*;
8242
3873b4aaf4a8 7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents: 8230
diff changeset
    47
import static com.sun.tools.javac.code.Scope.*;
14258
8d2148961366 8000663: clean up langtools imports
jjg
parents: 14058
diff changeset
    48
import static com.sun.tools.javac.code.Symbol.*;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
import static com.sun.tools.javac.code.Type.*;
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
    50
import static com.sun.tools.javac.code.TypeTag.*;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
import static com.sun.tools.javac.util.ListBuffer.lb;
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
 * Utility class containing various operations on types.
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
 * <p>Unless other names are more illustrative, the following naming
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
 * conventions should be observed in this file:
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
 * <dl>
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
 * <dt>t</dt>
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
 * <dd>If the first argument to an operation is a type, it should be named t.</dd>
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
 * <dt>s</dt>
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
 * <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
    64
 * <dt>ts</dt>
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
 * <dd>If an operations takes a list of types, the first should be named ts.</dd>
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
 * <dt>ss</dt>
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
 * <dd>A second list of types should be named ss.</dd>
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
 * </dl>
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5654
diff changeset
    70
 * <p><b>This is NOT part of any supported API.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
 * If you write code that depends on this, you do so at your own risk.
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
 * This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
 * deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
public class Types {
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
    protected static final Context.Key<Types> typesKey =
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
        new Context.Key<Types>();
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
    final Symtab syms;
1471
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1469
diff changeset
    80
    final JavacMessages messages;
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 1257
diff changeset
    81
    final Names names;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
    final boolean allowBoxing;
9595
a7dea6cbd5f7 7038363: cast from object to primitive should be for source >= 1.7
jjg
parents: 9303
diff changeset
    83
    final boolean allowCovariantReturns;
a7dea6cbd5f7 7038363: cast from object to primitive should be for source >= 1.7
jjg
parents: 9303
diff changeset
    84
    final boolean allowObjectToPrimitiveCast;
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
    85
    final boolean allowDefaultMethods;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
    final ClassReader reader;
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
    final Check chk;
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
    88
    JCDiagnostic.Factory diags;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
    List<Warner> warnStack = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
    final Name capturedName;
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
    91
    private final FunctionDescriptorLookupError functionDescriptorLookupError;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14443
diff changeset
    93
    public final Warner noWarnings;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14443
diff changeset
    94
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
    // <editor-fold defaultstate="collapsed" desc="Instantiating">
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
    public static Types instance(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
        Types instance = context.get(typesKey);
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
        if (instance == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
            instance = new Types(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
        return instance;
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
    protected Types(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
        context.put(typesKey, this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
        syms = Symtab.instance(context);
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 1257
diff changeset
   106
        names = Names.instance(context);
9595
a7dea6cbd5f7 7038363: cast from object to primitive should be for source >= 1.7
jjg
parents: 9303
diff changeset
   107
        Source source = Source.instance(context);
a7dea6cbd5f7 7038363: cast from object to primitive should be for source >= 1.7
jjg
parents: 9303
diff changeset
   108
        allowBoxing = source.allowBoxing();
a7dea6cbd5f7 7038363: cast from object to primitive should be for source >= 1.7
jjg
parents: 9303
diff changeset
   109
        allowCovariantReturns = source.allowCovariantReturns();
a7dea6cbd5f7 7038363: cast from object to primitive should be for source >= 1.7
jjg
parents: 9303
diff changeset
   110
        allowObjectToPrimitiveCast = source.allowObjectToPrimitiveCast();
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
   111
        allowDefaultMethods = source.allowDefaultMethods();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
        reader = ClassReader.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
        chk = Check.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
        capturedName = names.fromString("<captured wildcard>");
1471
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1469
diff changeset
   115
        messages = JavacMessages.instance(context);
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   116
        diags = JCDiagnostic.Factory.instance(context);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   117
        functionDescriptorLookupError = new FunctionDescriptorLookupError();
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14443
diff changeset
   118
        noWarnings = new Warner(null);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
    // <editor-fold defaultstate="collapsed" desc="upperBound">
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
     * The "rvalue conversion".<br>
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
     * The upper bound of most types is the type
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
     * itself.  Wildcards, on the other hand have upper
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
     * and lower bounds.
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
     * @param t a type
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
     * @return the upper bound of the given type
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
    public Type upperBound(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
        return upperBound.visit(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
        private final MapVisitor<Void> upperBound = new MapVisitor<Void>() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
            public Type visitWildcardType(WildcardType t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
                if (t.isSuperBound())
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
                    return t.bound == null ? syms.objectType : t.bound.bound;
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
                else
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
                    return visit(t.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
            public Type visitCapturedType(CapturedType t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
                return visit(t.bound);
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
    // <editor-fold defaultstate="collapsed" desc="lowerBound">
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
     * The "lvalue conversion".<br>
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
     * The lower bound of most types is the type
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
     * itself.  Wildcards, on the other hand have upper
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
     * and lower bounds.
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
     * @param t a type
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
     * @return the lower bound of the given type
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
    public Type lowerBound(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
        return lowerBound.visit(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
        private final MapVisitor<Void> lowerBound = new MapVisitor<Void>() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
            public Type visitWildcardType(WildcardType t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
                return t.isExtendsBound() ? syms.botType : visit(t.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
            public Type visitCapturedType(CapturedType t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
                return visit(t.getLowerBound());
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
    // <editor-fold defaultstate="collapsed" desc="isUnbounded">
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
     * Checks that all the arguments to a class are unbounded
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
     * wildcards or something else that doesn't make any restrictions
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
     * on the arguments. If a class isUnbounded, a raw super- or
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
     * subclass can be cast to it without a warning.
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
     * @param t a type
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
     * @return true iff the given type is unbounded or raw
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
    public boolean isUnbounded(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
        return isUnbounded.visit(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
        private final UnaryVisitor<Boolean> isUnbounded = new UnaryVisitor<Boolean>() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
            public Boolean visitType(Type t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
                return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
            public Boolean visitClassType(ClassType t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
                List<Type> parms = t.tsym.type.allparams();
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
                List<Type> args = t.allparams();
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
                while (parms.nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
                    WildcardType unb = new WildcardType(syms.objectType,
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
                                                        BoundKind.UNBOUND,
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
                                                        syms.boundClass,
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
                                                        (TypeVar)parms.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
                    if (!containsType(args.head, unb))
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
                        return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
                    parms = parms.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
                    args = args.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
                return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
    // <editor-fold defaultstate="collapsed" desc="asSub">
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
     * Return the least specific subtype of t that starts with symbol
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
     * sym.  If none exists, return null.  The least specific subtype
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
     * is determined as follows:
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
     * <p>If there is exactly one parameterized instance of sym that is a
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
     * subtype of t, that parameterized instance is returned.<br>
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
     * Otherwise, if the plain type or raw type `sym' is a subtype of
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
     * type t, the type `sym' itself is returned.  Otherwise, null is
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
     * returned.
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
    public Type asSub(Type t, Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
        return asSub.visit(t, sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
        private final SimpleVisitor<Type,Symbol> asSub = new SimpleVisitor<Type,Symbol>() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
            public Type visitType(Type t, Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
                return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
            public Type visitClassType(ClassType t, Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
                if (t.tsym == sym)
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
                    return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
                Type base = asSuper(sym.type, t.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
                if (base == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
                    return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
                ListBuffer<Type> from = new ListBuffer<Type>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
                ListBuffer<Type> to = new ListBuffer<Type>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
                try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
                    adapt(base, t, from, to);
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
                } catch (AdaptFailure ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
                    return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
                Type res = subst(sym.type, from.toList(), to.toList());
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
                if (!isSubtype(res, t))
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
                    return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
                ListBuffer<Type> openVars = new ListBuffer<Type>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
                for (List<Type> l = sym.type.allparams();
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
                     l.nonEmpty(); l = l.tail)
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
                    if (res.contains(l.head) && !t.contains(l.head))
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
                        openVars.append(l.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
                if (openVars.nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
                    if (t.isRaw()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
                        // The subtype of a raw type is raw
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
                        res = erasure(res);
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
                    } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
                        // Unbound type arguments default to ?
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
                        List<Type> opens = openVars.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
                        ListBuffer<Type> qs = new ListBuffer<Type>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
                        for (List<Type> iter = opens; iter.nonEmpty(); iter = iter.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
                            qs.append(new WildcardType(syms.objectType, BoundKind.UNBOUND, syms.boundClass, (TypeVar) iter.head));
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
                        res = subst(res, opens, qs.toList());
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
                return res;
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
            public Type visitErrorType(ErrorType t, Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
                return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
    // <editor-fold defaultstate="collapsed" desc="isConvertible">
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
    /**
10452
c8f32009b413 7085024: internal error; cannot instantiate Foo
mcimadamore
parents: 9812
diff changeset
   287
     * Is t a subtype of or convertible via boxing/unboxing
c8f32009b413 7085024: internal error; cannot instantiate Foo
mcimadamore
parents: 9812
diff changeset
   288
     * conversion to s?
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
    public boolean isConvertible(Type t, Type s, Warner warn) {
10452
c8f32009b413 7085024: internal error; cannot instantiate Foo
mcimadamore
parents: 9812
diff changeset
   291
        if (t.tag == ERROR)
c8f32009b413 7085024: internal error; cannot instantiate Foo
mcimadamore
parents: 9812
diff changeset
   292
            return true;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
        boolean tPrimitive = t.isPrimitive();
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
        boolean sPrimitive = s.isPrimitive();
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 7637
diff changeset
   295
        if (tPrimitive == sPrimitive) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
            return isSubtypeUnchecked(t, s, warn);
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 7637
diff changeset
   297
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
        if (!allowBoxing) return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
        return tPrimitive
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
            ? isSubtype(boxedClass(t).type, s)
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
            : isSubtype(unboxedType(t), s);
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
     * Is t a subtype of or convertiable via boxing/unboxing
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
     * convertions to s?
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
    public boolean isConvertible(Type t, Type s) {
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14443
diff changeset
   309
        return isConvertible(t, s, noWarnings);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   313
    // <editor-fold defaultstate="collapsed" desc="findSam">
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   314
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   315
    /**
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   316
     * Exception used to report a function descriptor lookup failure. The exception
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   317
     * wraps a diagnostic that can be used to generate more details error
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   318
     * messages.
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   319
     */
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   320
    public static class FunctionDescriptorLookupError extends RuntimeException {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   321
        private static final long serialVersionUID = 0;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   322
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   323
        JCDiagnostic diagnostic;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   324
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   325
        FunctionDescriptorLookupError() {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   326
            this.diagnostic = null;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   327
        }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   328
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   329
        FunctionDescriptorLookupError setMessage(JCDiagnostic diag) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   330
            this.diagnostic = diag;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   331
            return this;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   332
        }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   333
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   334
        public JCDiagnostic getDiagnostic() {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   335
            return diagnostic;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   336
        }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   337
    }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   338
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   339
    /**
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   340
     * A cache that keeps track of function descriptors associated with given
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   341
     * functional interfaces.
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   342
     */
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   343
    class DescriptorCache {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   344
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   345
        private WeakHashMap<TypeSymbol, Entry> _map = new WeakHashMap<TypeSymbol, Entry>();
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   346
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   347
        class FunctionDescriptor {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   348
            Symbol descSym;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   349
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   350
            FunctionDescriptor(Symbol descSym) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   351
                this.descSym = descSym;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   352
            }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   353
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   354
            public Symbol getSymbol() {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   355
                return descSym;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   356
            }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   357
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15361
diff changeset
   358
            public Type getType(Type site) {
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15361
diff changeset
   359
                if (capture(site) != site) {
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15361
diff changeset
   360
                    Type formalInterface = site.tsym.type;
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15361
diff changeset
   361
                    ListBuffer<Type> typeargs = ListBuffer.lb();
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15361
diff changeset
   362
                    List<Type> actualTypeargs = site.getTypeArguments();
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15361
diff changeset
   363
                    //simply replace the wildcards with its bound
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15361
diff changeset
   364
                    for (Type t : formalInterface.getTypeArguments()) {
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15361
diff changeset
   365
                        if (actualTypeargs.head.hasTag(WILDCARD)) {
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15361
diff changeset
   366
                            WildcardType wt = (WildcardType)actualTypeargs.head;
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15361
diff changeset
   367
                            typeargs.append(wt.type);
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15361
diff changeset
   368
                        } else {
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15361
diff changeset
   369
                            typeargs.append(actualTypeargs.head);
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15361
diff changeset
   370
                        }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15361
diff changeset
   371
                        actualTypeargs = actualTypeargs.tail;
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15361
diff changeset
   372
                    }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15361
diff changeset
   373
                    site = subst(formalInterface, formalInterface.getTypeArguments(), typeargs.toList());
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15361
diff changeset
   374
                    if (!chk.checkValidGenericType(site)) {
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15361
diff changeset
   375
                        //if the inferred functional interface type is not well-formed,
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15361
diff changeset
   376
                        //or if it's not a subtype of the original target, issue an error
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15361
diff changeset
   377
                        throw failure(diags.fragment("no.suitable.functional.intf.inst", site));
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15361
diff changeset
   378
                    }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15361
diff changeset
   379
                }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15361
diff changeset
   380
                return memberType(site, descSym);
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   381
            }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   382
        }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   383
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   384
        class Entry {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   385
            final FunctionDescriptor cachedDescRes;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   386
            final int prevMark;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   387
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   388
            public Entry(FunctionDescriptor cachedDescRes,
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   389
                    int prevMark) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   390
                this.cachedDescRes = cachedDescRes;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   391
                this.prevMark = prevMark;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   392
            }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   393
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   394
            boolean matches(int mark) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   395
                return  this.prevMark == mark;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   396
            }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   397
        }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   398
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   399
        FunctionDescriptor get(TypeSymbol origin) throws FunctionDescriptorLookupError {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   400
            Entry e = _map.get(origin);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   401
            CompoundScope members = membersClosure(origin.type, false);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   402
            if (e == null ||
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   403
                    !e.matches(members.getMark())) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   404
                FunctionDescriptor descRes = findDescriptorInternal(origin, members);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   405
                _map.put(origin, new Entry(descRes, members.getMark()));
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   406
                return descRes;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   407
            }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   408
            else {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   409
                return e.cachedDescRes;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   410
            }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   411
        }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   412
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   413
        /**
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   414
         * Compute the function descriptor associated with a given functional interface
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   415
         */
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   416
        public FunctionDescriptor findDescriptorInternal(TypeSymbol origin, CompoundScope membersCache) throws FunctionDescriptorLookupError {
15361
01f1828683e6 8005299: Add FunctionalInterface checking to javac
mcimadamore
parents: 14949
diff changeset
   417
            if (!origin.isInterface() || (origin.flags() & ANNOTATION) != 0) {
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   418
                //t must be an interface
15361
01f1828683e6 8005299: Add FunctionalInterface checking to javac
mcimadamore
parents: 14949
diff changeset
   419
                throw failure("not.a.functional.intf", origin);
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   420
            }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   421
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   422
            final ListBuffer<Symbol> abstracts = ListBuffer.lb();
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   423
            for (Symbol sym : membersCache.getElements(new DescriptorFilter(origin))) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   424
                Type mtype = memberType(origin.type, sym);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   425
                if (abstracts.isEmpty() ||
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   426
                        (sym.name == abstracts.first().name &&
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   427
                        overrideEquivalent(mtype, memberType(origin.type, abstracts.first())))) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   428
                    abstracts.append(sym);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   429
                } else {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   430
                    //the target method(s) should be the only abstract members of t
15361
01f1828683e6 8005299: Add FunctionalInterface checking to javac
mcimadamore
parents: 14949
diff changeset
   431
                    throw failure("not.a.functional.intf.1",  origin,
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   432
                            diags.fragment("incompatible.abstracts", Kinds.kindName(origin), origin));
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   433
                }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   434
            }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   435
            if (abstracts.isEmpty()) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   436
                //t must define a suitable non-generic method
15361
01f1828683e6 8005299: Add FunctionalInterface checking to javac
mcimadamore
parents: 14949
diff changeset
   437
                throw failure("not.a.functional.intf.1", origin,
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   438
                            diags.fragment("no.abstracts", Kinds.kindName(origin), origin));
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   439
            } else if (abstracts.size() == 1) {
14723
46aa71a5e4e0 8004102: Add support for generic functional descriptors
mcimadamore
parents: 14719
diff changeset
   440
                return new FunctionDescriptor(abstracts.first());
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   441
            } else { // size > 1
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   442
                FunctionDescriptor descRes = mergeDescriptors(origin, abstracts.toList());
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   443
                if (descRes == null) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   444
                    //we can get here if the functional interface is ill-formed
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   445
                    ListBuffer<JCDiagnostic> descriptors = ListBuffer.lb();
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   446
                    for (Symbol desc : abstracts) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   447
                        String key = desc.type.getThrownTypes().nonEmpty() ?
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   448
                                "descriptor.throws" : "descriptor";
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   449
                        descriptors.append(diags.fragment(key, desc.name,
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   450
                                desc.type.getParameterTypes(),
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   451
                                desc.type.getReturnType(),
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   452
                                desc.type.getThrownTypes()));
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   453
                    }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   454
                    JCDiagnostic.MultilineDiagnostic incompatibleDescriptors =
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   455
                            new JCDiagnostic.MultilineDiagnostic(diags.fragment("incompatible.descs.in.functional.intf",
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   456
                            Kinds.kindName(origin), origin), descriptors.toList());
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   457
                    throw failure(incompatibleDescriptors);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   458
                }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   459
                return descRes;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   460
            }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   461
        }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   462
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   463
        /**
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   464
         * Compute a synthetic type for the target descriptor given a list
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   465
         * of override-equivalent methods in the functional interface type.
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   466
         * The resulting method type is a method type that is override-equivalent
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   467
         * and return-type substitutable with each method in the original list.
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   468
         */
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   469
        private FunctionDescriptor mergeDescriptors(TypeSymbol origin, List<Symbol> methodSyms) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   470
            //pick argument types - simply take the signature that is a
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   471
            //subsignature of all other signatures in the list (as per JLS 8.4.2)
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   472
            List<Symbol> mostSpecific = List.nil();
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   473
            outer: for (Symbol msym1 : methodSyms) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   474
                Type mt1 = memberType(origin.type, msym1);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   475
                for (Symbol msym2 : methodSyms) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   476
                    Type mt2 = memberType(origin.type, msym2);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   477
                    if (!isSubSignature(mt1, mt2)) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   478
                        continue outer;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   479
                    }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   480
                }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   481
                mostSpecific = mostSpecific.prepend(msym1);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   482
            }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   483
            if (mostSpecific.isEmpty()) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   484
                return null;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   485
            }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   486
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   487
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   488
            //pick return types - this is done in two phases: (i) first, the most
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   489
            //specific return type is chosen using strict subtyping; if this fails,
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   490
            //a second attempt is made using return type substitutability (see JLS 8.4.5)
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   491
            boolean phase2 = false;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   492
            Symbol bestSoFar = null;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   493
            while (bestSoFar == null) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   494
                outer: for (Symbol msym1 : mostSpecific) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   495
                    Type mt1 = memberType(origin.type, msym1);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   496
                    for (Symbol msym2 : methodSyms) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   497
                        Type mt2 = memberType(origin.type, msym2);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   498
                        if (phase2 ?
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   499
                                !returnTypeSubstitutable(mt1, mt2) :
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   500
                                !isSubtypeInternal(mt1.getReturnType(), mt2.getReturnType())) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   501
                            continue outer;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   502
                        }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   503
                    }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   504
                    bestSoFar = msym1;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   505
                }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   506
                if (phase2) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   507
                    break;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   508
                } else {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   509
                    phase2 = true;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   510
                }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   511
            }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   512
            if (bestSoFar == null) return null;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   513
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   514
            //merge thrown types - form the intersection of all the thrown types in
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   515
            //all the signatures in the list
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   516
            List<Type> thrown = null;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   517
            for (Symbol msym1 : methodSyms) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   518
                Type mt1 = memberType(origin.type, msym1);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   519
                thrown = (thrown == null) ?
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   520
                    mt1.getThrownTypes() :
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   521
                    chk.intersect(mt1.getThrownTypes(), thrown);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   522
            }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   523
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   524
            final List<Type> thrown1 = thrown;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   525
            return new FunctionDescriptor(bestSoFar) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   526
                @Override
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   527
                public Type getType(Type origin) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   528
                    Type mt = memberType(origin, getSymbol());
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   529
                    return new MethodType(mt.getParameterTypes(), mt.getReturnType(), thrown1, syms.methodClass);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   530
                }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   531
            };
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   532
        }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   533
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   534
        boolean isSubtypeInternal(Type s, Type t) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   535
            return (s.isPrimitive() && t.isPrimitive()) ?
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   536
                    isSameType(t, s) :
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   537
                    isSubtype(s, t);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   538
        }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   539
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   540
        FunctionDescriptorLookupError failure(String msg, Object... args) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   541
            return failure(diags.fragment(msg, args));
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   542
        }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   543
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   544
        FunctionDescriptorLookupError failure(JCDiagnostic diag) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   545
            return functionDescriptorLookupError.setMessage(diag);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   546
        }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   547
    }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   548
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   549
    private DescriptorCache descCache = new DescriptorCache();
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   550
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   551
    /**
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   552
     * Find the method descriptor associated to this class symbol - if the
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   553
     * symbol 'origin' is not a functional interface, an exception is thrown.
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   554
     */
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   555
    public Symbol findDescriptorSymbol(TypeSymbol origin) throws FunctionDescriptorLookupError {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   556
        return descCache.get(origin).getSymbol();
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   557
    }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   558
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   559
    /**
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   560
     * Find the type of the method descriptor associated to this class symbol -
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   561
     * if the symbol 'origin' is not a functional interface, an exception is thrown.
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   562
     */
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   563
    public Type findDescriptorType(Type origin) throws FunctionDescriptorLookupError {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   564
        return descCache.get(origin.tsym).getType(origin);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   565
    }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   566
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   567
    /**
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   568
     * Is given type a functional interface?
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   569
     */
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   570
    public boolean isFunctionalInterface(TypeSymbol tsym) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   571
        try {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   572
            findDescriptorSymbol(tsym);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   573
            return true;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   574
        } catch (FunctionDescriptorLookupError ex) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   575
            return false;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   576
        }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   577
    }
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15361
diff changeset
   578
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15361
diff changeset
   579
    public boolean isFunctionalInterface(Type site) {
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15361
diff changeset
   580
        try {
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15361
diff changeset
   581
            findDescriptorType(site);
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15361
diff changeset
   582
            return true;
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15361
diff changeset
   583
        } catch (FunctionDescriptorLookupError ex) {
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15361
diff changeset
   584
            return false;
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15361
diff changeset
   585
        }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15361
diff changeset
   586
    }
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   587
    // </editor-fold>
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   588
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   589
   /**
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   590
    * Scope filter used to skip methods that should be ignored (such as methods
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   591
    * overridden by j.l.Object) during function interface conversion/marker interface checks
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   592
    */
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   593
    class DescriptorFilter implements Filter<Symbol> {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   594
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   595
       TypeSymbol origin;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   596
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   597
       DescriptorFilter(TypeSymbol origin) {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   598
           this.origin = origin;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   599
       }
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   600
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   601
       @Override
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   602
       public boolean accepts(Symbol sym) {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   603
           return sym.kind == Kinds.MTH &&
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   604
                   (sym.flags() & (ABSTRACT | DEFAULT)) == ABSTRACT &&
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   605
                   !overridesObjectMethod(origin, sym) &&
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   606
                   (interfaceCandidates(origin.type, (MethodSymbol)sym).head.flags() & DEFAULT) == 0;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   607
       }
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   608
    };
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   609
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   610
    // <editor-fold defaultstate="collapsed" desc="isMarker">
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   611
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   612
    /**
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   613
     * A cache that keeps track of marker interfaces
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   614
     */
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   615
    class MarkerCache {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   616
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   617
        private WeakHashMap<TypeSymbol, Entry> _map = new WeakHashMap<TypeSymbol, Entry>();
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   618
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   619
        class Entry {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   620
            final boolean isMarkerIntf;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   621
            final int prevMark;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   622
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   623
            public Entry(boolean isMarkerIntf,
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   624
                    int prevMark) {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   625
                this.isMarkerIntf = isMarkerIntf;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   626
                this.prevMark = prevMark;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   627
            }
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   628
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   629
            boolean matches(int mark) {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   630
                return  this.prevMark == mark;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   631
            }
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   632
        }
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   633
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   634
        boolean get(TypeSymbol origin) throws FunctionDescriptorLookupError {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   635
            Entry e = _map.get(origin);
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   636
            CompoundScope members = membersClosure(origin.type, false);
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   637
            if (e == null ||
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   638
                    !e.matches(members.getMark())) {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   639
                boolean isMarkerIntf = isMarkerInterfaceInternal(origin, members);
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   640
                _map.put(origin, new Entry(isMarkerIntf, members.getMark()));
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   641
                return isMarkerIntf;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   642
            }
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   643
            else {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   644
                return e.isMarkerIntf;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   645
            }
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   646
        }
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   647
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   648
        /**
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   649
         * Is given symbol a marker interface
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   650
         */
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   651
        public boolean isMarkerInterfaceInternal(TypeSymbol origin, CompoundScope membersCache) throws FunctionDescriptorLookupError {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   652
            return !origin.isInterface() ?
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   653
                    false :
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   654
                    !membersCache.getElements(new DescriptorFilter(origin)).iterator().hasNext();
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   655
        }
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   656
    }
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   657
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   658
    private MarkerCache markerCache = new MarkerCache();
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   659
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   660
    /**
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   661
     * Is given type a marker interface?
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   662
     */
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   663
    public boolean isMarkerInterface(Type site) {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   664
        return markerCache.get(site.tsym);
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   665
    }
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   666
    // </editor-fold>
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   667
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   668
    // <editor-fold defaultstate="collapsed" desc="isSubtype">
06bc494ca11e Initial load
duke
parents:
diff changeset
   669
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   670
     * Is t an unchecked subtype of s?
06bc494ca11e Initial load
duke
parents:
diff changeset
   671
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   672
    public boolean isSubtypeUnchecked(Type t, Type s) {
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14443
diff changeset
   673
        return isSubtypeUnchecked(t, s, noWarnings);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   674
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   675
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   676
     * Is t an unchecked subtype of s?
06bc494ca11e Initial load
duke
parents:
diff changeset
   677
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   678
    public boolean isSubtypeUnchecked(Type t, Type s, Warner warn) {
10810
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
   679
        boolean result = isSubtypeUncheckedInternal(t, s, warn);
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
   680
        if (result) {
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
   681
            checkUnsafeVarargsConversion(t, s, warn);
660
16c478b6af1d 6507024: casting an array to a generic type results in a 'capture#69 of ?' type error
mcimadamore
parents: 514
diff changeset
   682
        }
10810
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
   683
        return result;
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
   684
    }
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
   685
    //where
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
   686
        private boolean isSubtypeUncheckedInternal(Type t, Type s, Warner warn) {
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
   687
            if (t.hasTag(ARRAY) && s.hasTag(ARRAY)) {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
   688
                t = t.unannotatedType();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
   689
                s = s.unannotatedType();
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
   690
                if (((ArrayType)t).elemtype.isPrimitive()) {
10810
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
   691
                    return isSameType(elemtype(t), elemtype(s));
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
   692
                } else {
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
   693
                    return isSubtypeUnchecked(elemtype(t), elemtype(s), warn);
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
   694
                }
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
   695
            } else if (isSubtype(t, s)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   696
                return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   697
            }
10810
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
   698
            else if (t.tag == TYPEVAR) {
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
   699
                return isSubtypeUnchecked(t.getUpperBound(), s, warn);
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
   700
            }
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
   701
            else if (!s.isRaw()) {
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
   702
                Type t2 = asSuper(t, s.tsym);
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
   703
                if (t2 != null && t2.isRaw()) {
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
   704
                    if (isReifiable(s))
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
   705
                        warn.silentWarn(LintCategory.UNCHECKED);
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
   706
                    else
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
   707
                        warn.warn(LintCategory.UNCHECKED);
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
   708
                    return true;
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
   709
                }
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
   710
            }
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
   711
            return false;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   712
        }
10810
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
   713
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
   714
        private void checkUnsafeVarargsConversion(Type t, Type s, Warner warn) {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
   715
            if (t.tag != ARRAY || isReifiable(t))
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
   716
                return;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
   717
            t = t.unannotatedType();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
   718
            s = s.unannotatedType();
10810
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
   719
            ArrayType from = (ArrayType)t;
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
   720
            boolean shouldWarn = false;
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
   721
            switch (s.tag) {
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
   722
                case ARRAY:
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
   723
                    ArrayType to = (ArrayType)s;
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
   724
                    shouldWarn = from.isVarargs() &&
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
   725
                            !to.isVarargs() &&
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
   726
                            !isReifiable(from);
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
   727
                    break;
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
   728
                case CLASS:
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
   729
                    shouldWarn = from.isVarargs();
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
   730
                    break;
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
   731
            }
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
   732
            if (shouldWarn) {
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
   733
                warn.warn(LintCategory.VARARGS);
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
   734
            }
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
   735
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   736
06bc494ca11e Initial load
duke
parents:
diff changeset
   737
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   738
     * Is t a subtype of s?<br>
06bc494ca11e Initial load
duke
parents:
diff changeset
   739
     * (not defined for Method and ForAll types)
06bc494ca11e Initial load
duke
parents:
diff changeset
   740
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   741
    final public boolean isSubtype(Type t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   742
        return isSubtype(t, s, true);
06bc494ca11e Initial load
duke
parents:
diff changeset
   743
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   744
    final public boolean isSubtypeNoCapture(Type t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   745
        return isSubtype(t, s, false);
06bc494ca11e Initial load
duke
parents:
diff changeset
   746
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   747
    public boolean isSubtype(Type t, Type s, boolean capture) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   748
        if (t == s)
06bc494ca11e Initial load
duke
parents:
diff changeset
   749
            return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   750
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
   751
        t = t.unannotatedType();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
   752
        s = s.unannotatedType();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
   753
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
   754
        if (t == s)
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
   755
            return true;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
   756
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
   757
        if (s.isPartial())
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   758
            return isSuperType(s, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   759
3140
15a274b13051 6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents: 2223
diff changeset
   760
        if (s.isCompound()) {
15a274b13051 6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents: 2223
diff changeset
   761
            for (Type s2 : interfaces(s).prepend(supertype(s))) {
15a274b13051 6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents: 2223
diff changeset
   762
                if (!isSubtype(t, s2, capture))
15a274b13051 6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents: 2223
diff changeset
   763
                    return false;
15a274b13051 6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents: 2223
diff changeset
   764
            }
15a274b13051 6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents: 2223
diff changeset
   765
            return true;
15a274b13051 6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents: 2223
diff changeset
   766
        }
15a274b13051 6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents: 2223
diff changeset
   767
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   768
        Type lower = lowerBound(s);
06bc494ca11e Initial load
duke
parents:
diff changeset
   769
        if (s != lower)
06bc494ca11e Initial load
duke
parents:
diff changeset
   770
            return isSubtype(capture ? capture(t) : t, lower, false);
06bc494ca11e Initial load
duke
parents:
diff changeset
   771
06bc494ca11e Initial load
duke
parents:
diff changeset
   772
        return isSubtype.visit(capture ? capture(t) : t, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
   773
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   774
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
   775
        private TypeRelation isSubtype = new TypeRelation()
06bc494ca11e Initial load
duke
parents:
diff changeset
   776
        {
06bc494ca11e Initial load
duke
parents:
diff changeset
   777
            public Boolean visitType(Type t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   778
                switch (t.tag) {
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
   779
                 case BYTE:
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
   780
                     return (!s.hasTag(CHAR) && t.getTag().isSubRangeOf(s.getTag()));
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
   781
                 case CHAR:
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
   782
                     return (!s.hasTag(SHORT) && t.getTag().isSubRangeOf(s.getTag()));
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
   783
                 case SHORT: case INT: case LONG:
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
   784
                 case FLOAT: case DOUBLE:
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
   785
                     return t.getTag().isSubRangeOf(s.getTag());
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
   786
                 case BOOLEAN: case VOID:
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
   787
                     return t.hasTag(s.getTag());
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
   788
                 case TYPEVAR:
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
   789
                     return isSubtypeNoCapture(t.getUpperBound(), s);
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
   790
                 case BOT:
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
   791
                     return
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
   792
                         s.hasTag(BOT) || s.hasTag(CLASS) ||
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
   793
                         s.hasTag(ARRAY) || s.hasTag(TYPEVAR);
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
   794
                 case WILDCARD: //we shouldn't be here - avoids crash (see 7034495)
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
   795
                 case NONE:
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
   796
                     return false;
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
   797
                 default:
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
   798
                     throw new AssertionError("isSubtype " + t.tag);
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
   799
                 }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   800
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   801
06bc494ca11e Initial load
duke
parents:
diff changeset
   802
            private Set<TypePair> cache = new HashSet<TypePair>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   803
06bc494ca11e Initial load
duke
parents:
diff changeset
   804
            private boolean containsTypeRecursive(Type t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   805
                TypePair pair = new TypePair(t, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
   806
                if (cache.add(pair)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   807
                    try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   808
                        return containsType(t.getTypeArguments(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   809
                                            s.getTypeArguments());
06bc494ca11e Initial load
duke
parents:
diff changeset
   810
                    } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   811
                        cache.remove(pair);
06bc494ca11e Initial load
duke
parents:
diff changeset
   812
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   813
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   814
                    return containsType(t.getTypeArguments(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   815
                                        rewriteSupers(s).getTypeArguments());
06bc494ca11e Initial load
duke
parents:
diff changeset
   816
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   817
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   818
06bc494ca11e Initial load
duke
parents:
diff changeset
   819
            private Type rewriteSupers(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   820
                if (!t.isParameterized())
06bc494ca11e Initial load
duke
parents:
diff changeset
   821
                    return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   822
                ListBuffer<Type> from = lb();
06bc494ca11e Initial load
duke
parents:
diff changeset
   823
                ListBuffer<Type> to = lb();
06bc494ca11e Initial load
duke
parents:
diff changeset
   824
                adaptSelf(t, from, to);
06bc494ca11e Initial load
duke
parents:
diff changeset
   825
                if (from.isEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
   826
                    return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   827
                ListBuffer<Type> rewrite = lb();
06bc494ca11e Initial load
duke
parents:
diff changeset
   828
                boolean changed = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   829
                for (Type orig : to.toList()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   830
                    Type s = rewriteSupers(orig);
06bc494ca11e Initial load
duke
parents:
diff changeset
   831
                    if (s.isSuperBound() && !s.isExtendsBound()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   832
                        s = new WildcardType(syms.objectType,
06bc494ca11e Initial load
duke
parents:
diff changeset
   833
                                             BoundKind.UNBOUND,
06bc494ca11e Initial load
duke
parents:
diff changeset
   834
                                             syms.boundClass);
06bc494ca11e Initial load
duke
parents:
diff changeset
   835
                        changed = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   836
                    } else if (s != orig) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   837
                        s = new WildcardType(upperBound(s),
06bc494ca11e Initial load
duke
parents:
diff changeset
   838
                                             BoundKind.EXTENDS,
06bc494ca11e Initial load
duke
parents:
diff changeset
   839
                                             syms.boundClass);
06bc494ca11e Initial load
duke
parents:
diff changeset
   840
                        changed = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   841
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   842
                    rewrite.append(s);
06bc494ca11e Initial load
duke
parents:
diff changeset
   843
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   844
                if (changed)
06bc494ca11e Initial load
duke
parents:
diff changeset
   845
                    return subst(t.tsym.type, from.toList(), rewrite.toList());
06bc494ca11e Initial load
duke
parents:
diff changeset
   846
                else
06bc494ca11e Initial load
duke
parents:
diff changeset
   847
                    return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   848
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   849
06bc494ca11e Initial load
duke
parents:
diff changeset
   850
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   851
            public Boolean visitClassType(ClassType t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   852
                Type sup = asSuper(t, s.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   853
                return sup != null
06bc494ca11e Initial load
duke
parents:
diff changeset
   854
                    && sup.tsym == s.tsym
06bc494ca11e Initial load
duke
parents:
diff changeset
   855
                    // You're not allowed to write
06bc494ca11e Initial load
duke
parents:
diff changeset
   856
                    //     Vector<Object> vec = new Vector<String>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   857
                    // But with wildcards you can write
06bc494ca11e Initial load
duke
parents:
diff changeset
   858
                    //     Vector<? extends Object> vec = new Vector<String>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   859
                    // which means that subtype checking must be done
06bc494ca11e Initial load
duke
parents:
diff changeset
   860
                    // here instead of same-type checking (via containsType).
06bc494ca11e Initial load
duke
parents:
diff changeset
   861
                    && (!s.isParameterized() || containsTypeRecursive(s, sup))
06bc494ca11e Initial load
duke
parents:
diff changeset
   862
                    && isSubtypeNoCapture(sup.getEnclosingType(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   863
                                          s.getEnclosingType());
06bc494ca11e Initial load
duke
parents:
diff changeset
   864
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   865
06bc494ca11e Initial load
duke
parents:
diff changeset
   866
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   867
            public Boolean visitArrayType(ArrayType t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   868
                if (s.tag == ARRAY) {
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
   869
                    if (t.elemtype.isPrimitive())
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   870
                        return isSameType(t.elemtype, elemtype(s));
06bc494ca11e Initial load
duke
parents:
diff changeset
   871
                    else
06bc494ca11e Initial load
duke
parents:
diff changeset
   872
                        return isSubtypeNoCapture(t.elemtype, elemtype(s));
06bc494ca11e Initial load
duke
parents:
diff changeset
   873
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   874
06bc494ca11e Initial load
duke
parents:
diff changeset
   875
                if (s.tag == CLASS) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   876
                    Name sname = s.tsym.getQualifiedName();
06bc494ca11e Initial load
duke
parents:
diff changeset
   877
                    return sname == names.java_lang_Object
06bc494ca11e Initial load
duke
parents:
diff changeset
   878
                        || sname == names.java_lang_Cloneable
06bc494ca11e Initial load
duke
parents:
diff changeset
   879
                        || sname == names.java_io_Serializable;
06bc494ca11e Initial load
duke
parents:
diff changeset
   880
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   881
06bc494ca11e Initial load
duke
parents:
diff changeset
   882
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   883
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   884
06bc494ca11e Initial load
duke
parents:
diff changeset
   885
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   886
            public Boolean visitUndetVar(UndetVar t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   887
                //todo: test against origin needed? or replace with substitution?
10634
7f15f5a11ae9 7086586: Inference producing null type argument
mcimadamore
parents: 10453
diff changeset
   888
                if (t == s || t.qtype == s || s.tag == ERROR || s.tag == UNKNOWN) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   889
                    return true;
10634
7f15f5a11ae9 7086586: Inference producing null type argument
mcimadamore
parents: 10453
diff changeset
   890
                } else if (s.tag == BOT) {
7f15f5a11ae9 7086586: Inference producing null type argument
mcimadamore
parents: 10453
diff changeset
   891
                    //if 's' is 'null' there's no instantiated type U for which
7f15f5a11ae9 7086586: Inference producing null type argument
mcimadamore
parents: 10453
diff changeset
   892
                    //U <: s (but 'null' itself, which is not a valid type)
7f15f5a11ae9 7086586: Inference producing null type argument
mcimadamore
parents: 10453
diff changeset
   893
                    return false;
7f15f5a11ae9 7086586: Inference producing null type argument
mcimadamore
parents: 10453
diff changeset
   894
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   895
14048
308d1cf8fe46 7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents: 13689
diff changeset
   896
                t.addBound(InferenceBound.UPPER, s, Types.this);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   897
                return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   898
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   899
06bc494ca11e Initial load
duke
parents:
diff changeset
   900
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   901
            public Boolean visitErrorType(ErrorType t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   902
                return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   903
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   904
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
   905
06bc494ca11e Initial load
duke
parents:
diff changeset
   906
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   907
     * Is t a subtype of every type in given list `ts'?<br>
06bc494ca11e Initial load
duke
parents:
diff changeset
   908
     * (not defined for Method and ForAll types)<br>
06bc494ca11e Initial load
duke
parents:
diff changeset
   909
     * Allows unchecked conversions.
06bc494ca11e Initial load
duke
parents:
diff changeset
   910
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   911
    public boolean isSubtypeUnchecked(Type t, List<Type> ts, Warner warn) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   912
        for (List<Type> l = ts; l.nonEmpty(); l = l.tail)
06bc494ca11e Initial load
duke
parents:
diff changeset
   913
            if (!isSubtypeUnchecked(t, l.head, warn))
06bc494ca11e Initial load
duke
parents:
diff changeset
   914
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   915
        return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   916
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   917
06bc494ca11e Initial load
duke
parents:
diff changeset
   918
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   919
     * Are corresponding elements of ts subtypes of ss?  If lists are
06bc494ca11e Initial load
duke
parents:
diff changeset
   920
     * of different length, return false.
06bc494ca11e Initial load
duke
parents:
diff changeset
   921
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   922
    public boolean isSubtypes(List<Type> ts, List<Type> ss) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   923
        while (ts.tail != null && ss.tail != null
06bc494ca11e Initial load
duke
parents:
diff changeset
   924
               /*inlined: ts.nonEmpty() && ss.nonEmpty()*/ &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   925
               isSubtype(ts.head, ss.head)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   926
            ts = ts.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   927
            ss = ss.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   928
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   929
        return ts.tail == null && ss.tail == null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   930
        /*inlined: ts.isEmpty() && ss.isEmpty();*/
06bc494ca11e Initial load
duke
parents:
diff changeset
   931
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   932
06bc494ca11e Initial load
duke
parents:
diff changeset
   933
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   934
     * Are corresponding elements of ts subtypes of ss, allowing
06bc494ca11e Initial load
duke
parents:
diff changeset
   935
     * unchecked conversions?  If lists are of different length,
06bc494ca11e Initial load
duke
parents:
diff changeset
   936
     * return false.
06bc494ca11e Initial load
duke
parents:
diff changeset
   937
     **/
06bc494ca11e Initial load
duke
parents:
diff changeset
   938
    public boolean isSubtypesUnchecked(List<Type> ts, List<Type> ss, Warner warn) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   939
        while (ts.tail != null && ss.tail != null
06bc494ca11e Initial load
duke
parents:
diff changeset
   940
               /*inlined: ts.nonEmpty() && ss.nonEmpty()*/ &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   941
               isSubtypeUnchecked(ts.head, ss.head, warn)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   942
            ts = ts.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   943
            ss = ss.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   944
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   945
        return ts.tail == null && ss.tail == null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   946
        /*inlined: ts.isEmpty() && ss.isEmpty();*/
06bc494ca11e Initial load
duke
parents:
diff changeset
   947
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   948
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
   949
06bc494ca11e Initial load
duke
parents:
diff changeset
   950
    // <editor-fold defaultstate="collapsed" desc="isSuperType">
06bc494ca11e Initial load
duke
parents:
diff changeset
   951
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   952
     * Is t a supertype of s?
06bc494ca11e Initial load
duke
parents:
diff changeset
   953
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   954
    public boolean isSuperType(Type t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   955
        switch (t.tag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   956
        case ERROR:
06bc494ca11e Initial load
duke
parents:
diff changeset
   957
            return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   958
        case UNDETVAR: {
06bc494ca11e Initial load
duke
parents:
diff changeset
   959
            UndetVar undet = (UndetVar)t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   960
            if (t == s ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   961
                undet.qtype == s ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   962
                s.tag == ERROR ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   963
                s.tag == BOT) return true;
14048
308d1cf8fe46 7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents: 13689
diff changeset
   964
            undet.addBound(InferenceBound.LOWER, s, this);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   965
            return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   966
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   967
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   968
            return isSubtype(s, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   969
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   970
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   971
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
   972
06bc494ca11e Initial load
duke
parents:
diff changeset
   973
    // <editor-fold defaultstate="collapsed" desc="isSameType">
06bc494ca11e Initial load
duke
parents:
diff changeset
   974
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   975
     * Are corresponding elements of the lists the same type?  If
06bc494ca11e Initial load
duke
parents:
diff changeset
   976
     * lists are of different length, return false.
06bc494ca11e Initial load
duke
parents:
diff changeset
   977
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   978
    public boolean isSameTypes(List<Type> ts, List<Type> ss) {
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
   979
        return isSameTypes(ts, ss, false);
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
   980
    }
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
   981
    public boolean isSameTypes(List<Type> ts, List<Type> ss, boolean strict) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   982
        while (ts.tail != null && ss.tail != null
06bc494ca11e Initial load
duke
parents:
diff changeset
   983
               /*inlined: ts.nonEmpty() && ss.nonEmpty()*/ &&
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
   984
               isSameType(ts.head, ss.head, strict)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   985
            ts = ts.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   986
            ss = ss.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   987
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   988
        return ts.tail == null && ss.tail == null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   989
        /*inlined: ts.isEmpty() && ss.isEmpty();*/
06bc494ca11e Initial load
duke
parents:
diff changeset
   990
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   991
06bc494ca11e Initial load
duke
parents:
diff changeset
   992
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   993
     * Is t the same type as s?
06bc494ca11e Initial load
duke
parents:
diff changeset
   994
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   995
    public boolean isSameType(Type t, Type s) {
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
   996
        return isSameType(t, s, false);
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
   997
    }
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
   998
    public boolean isSameType(Type t, Type s, boolean strict) {
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
   999
        return strict ?
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1000
                isSameTypeStrict.visit(t, s) :
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1001
                isSameTypeLoose.visit(t, s);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1002
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1003
    // where
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1004
        abstract class SameTypeVisitor extends TypeRelation {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1005
06bc494ca11e Initial load
duke
parents:
diff changeset
  1006
            public Boolean visitType(Type t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1007
                if (t == s)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1008
                    return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1009
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
  1010
                if (s.isPartial())
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1011
                    return visit(s, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1012
06bc494ca11e Initial load
duke
parents:
diff changeset
  1013
                switch (t.tag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1014
                case BYTE: case CHAR: case SHORT: case INT: case LONG: case FLOAT:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1015
                case DOUBLE: case BOOLEAN: case VOID: case BOT: case NONE:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1016
                    return t.tag == s.tag;
5648
e8aa492874b5 6880344: Recursive type parameters do not compile
mcimadamore
parents: 5004
diff changeset
  1017
                case TYPEVAR: {
e8aa492874b5 6880344: Recursive type parameters do not compile
mcimadamore
parents: 5004
diff changeset
  1018
                    if (s.tag == TYPEVAR) {
e8aa492874b5 6880344: Recursive type parameters do not compile
mcimadamore
parents: 5004
diff changeset
  1019
                        //type-substitution does not preserve type-var types
e8aa492874b5 6880344: Recursive type parameters do not compile
mcimadamore
parents: 5004
diff changeset
  1020
                        //check that type var symbols and bounds are indeed the same
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1021
                        return sameTypeVars((TypeVar)t, (TypeVar)s);
5648
e8aa492874b5 6880344: Recursive type parameters do not compile
mcimadamore
parents: 5004
diff changeset
  1022
                    }
e8aa492874b5 6880344: Recursive type parameters do not compile
mcimadamore
parents: 5004
diff changeset
  1023
                    else {
e8aa492874b5 6880344: Recursive type parameters do not compile
mcimadamore
parents: 5004
diff changeset
  1024
                        //special case for s == ? super X, where upper(s) = u
e8aa492874b5 6880344: Recursive type parameters do not compile
mcimadamore
parents: 5004
diff changeset
  1025
                        //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
  1026
                        return s.isSuperBound() &&
e8aa492874b5 6880344: Recursive type parameters do not compile
mcimadamore
parents: 5004
diff changeset
  1027
                                !s.isExtendsBound() &&
e8aa492874b5 6880344: Recursive type parameters do not compile
mcimadamore
parents: 5004
diff changeset
  1028
                                visit(t, upperBound(s));
e8aa492874b5 6880344: Recursive type parameters do not compile
mcimadamore
parents: 5004
diff changeset
  1029
                    }
e8aa492874b5 6880344: Recursive type parameters do not compile
mcimadamore
parents: 5004
diff changeset
  1030
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1031
                default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1032
                    throw new AssertionError("isSameType " + t.tag);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1033
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1034
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1035
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1036
            abstract boolean sameTypeVars(TypeVar tv1, TypeVar tv2);
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1037
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1038
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1039
            public Boolean visitWildcardType(WildcardType t, Type s) {
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
  1040
                if (s.isPartial())
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1041
                    return visit(s, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1042
                else
06bc494ca11e Initial load
duke
parents:
diff changeset
  1043
                    return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1044
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1045
06bc494ca11e Initial load
duke
parents:
diff changeset
  1046
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1047
            public Boolean visitClassType(ClassType t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1048
                if (t == s)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1049
                    return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1050
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
  1051
                if (s.isPartial())
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1052
                    return visit(s, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1053
06bc494ca11e Initial load
duke
parents:
diff changeset
  1054
                if (s.isSuperBound() && !s.isExtendsBound())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1055
                    return visit(t, upperBound(s)) && visit(t, lowerBound(s));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1056
06bc494ca11e Initial load
duke
parents:
diff changeset
  1057
                if (t.isCompound() && s.isCompound()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1058
                    if (!visit(supertype(t), supertype(s)))
06bc494ca11e Initial load
duke
parents:
diff changeset
  1059
                        return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1060
14949
45f43822bbde 8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents: 14801
diff changeset
  1061
                    HashSet<UniqueType> set = new HashSet<UniqueType>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1062
                    for (Type x : interfaces(t))
14949
45f43822bbde 8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents: 14801
diff changeset
  1063
                        set.add(new UniqueType(x, Types.this));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1064
                    for (Type x : interfaces(s)) {
14949
45f43822bbde 8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents: 14801
diff changeset
  1065
                        if (!set.remove(new UniqueType(x, Types.this)))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1066
                            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1067
                    }
7637
467bc8fc514e 6990134: minor (but red) findbugs warnings
jjg
parents: 7635
diff changeset
  1068
                    return (set.isEmpty());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1069
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1070
                return t.tsym == s.tsym
06bc494ca11e Initial load
duke
parents:
diff changeset
  1071
                    && visit(t.getEnclosingType(), s.getEnclosingType())
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1072
                    && containsTypes(t.getTypeArguments(), s.getTypeArguments());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1073
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1074
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1075
            abstract protected boolean containsTypes(List<Type> ts1, List<Type> ts2);
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1076
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1077
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1078
            public Boolean visitArrayType(ArrayType t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1079
                if (t == s)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1080
                    return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1081
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
  1082
                if (s.isPartial())
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1083
                    return visit(s, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1084
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
  1085
                return s.hasTag(ARRAY)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1086
                    && containsTypeEquivalent(t.elemtype, elemtype(s));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1087
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1088
06bc494ca11e Initial load
duke
parents:
diff changeset
  1089
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1090
            public Boolean visitMethodType(MethodType t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1091
                // isSameType for methods does not take thrown
06bc494ca11e Initial load
duke
parents:
diff changeset
  1092
                // exceptions into account!
06bc494ca11e Initial load
duke
parents:
diff changeset
  1093
                return hasSameArgs(t, s) && visit(t.getReturnType(), s.getReturnType());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1094
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1095
06bc494ca11e Initial load
duke
parents:
diff changeset
  1096
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1097
            public Boolean visitPackageType(PackageType t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1098
                return t == s;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1099
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1100
06bc494ca11e Initial load
duke
parents:
diff changeset
  1101
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1102
            public Boolean visitForAll(ForAll t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1103
                if (s.tag != FORALL)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1104
                    return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1105
06bc494ca11e Initial load
duke
parents:
diff changeset
  1106
                ForAll forAll = (ForAll)s;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1107
                return hasSameBounds(t, forAll)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1108
                    && visit(t.qtype, subst(forAll.qtype, forAll.tvars, t.tvars));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1109
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1110
06bc494ca11e Initial load
duke
parents:
diff changeset
  1111
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1112
            public Boolean visitUndetVar(UndetVar t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1113
                if (s.tag == WILDCARD)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1114
                    // FIXME, this might be leftovers from before capture conversion
06bc494ca11e Initial load
duke
parents:
diff changeset
  1115
                    return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1116
06bc494ca11e Initial load
duke
parents:
diff changeset
  1117
                if (t == s || t.qtype == s || s.tag == ERROR || s.tag == UNKNOWN)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1118
                    return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1119
14048
308d1cf8fe46 7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents: 13689
diff changeset
  1120
                t.addBound(InferenceBound.EQ, s, Types.this);
12468
1100643c0209 7154127: Inference cleanup: remove bound check analysis from visitors in Types.java
mcimadamore
parents: 11549
diff changeset
  1121
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1122
                return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1123
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1124
06bc494ca11e Initial load
duke
parents:
diff changeset
  1125
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1126
            public Boolean visitErrorType(ErrorType t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1127
                return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1128
            }
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1129
        }
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1130
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1131
        /**
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1132
         * Standard type-equality relation - type variables are considered
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1133
         * equals if they share the same type symbol.
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1134
         */
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1135
        TypeRelation isSameTypeLoose = new SameTypeVisitor() {
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1136
            @Override
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1137
            boolean sameTypeVars(TypeVar tv1, TypeVar tv2) {
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1138
                return tv1.tsym == tv2.tsym && visit(tv1.getUpperBound(), tv2.getUpperBound());
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1139
            }
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1140
            @Override
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1141
            protected boolean containsTypes(List<Type> ts1, List<Type> ts2) {
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1142
                return containsTypeEquivalent(ts1, ts2);
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1143
            }
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1144
        };
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1145
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1146
        /**
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1147
         * Strict type-equality relation - type variables are considered
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1148
         * equals if they share the same object identity.
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1149
         */
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1150
        TypeRelation isSameTypeStrict = new SameTypeVisitor() {
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1151
            @Override
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1152
            boolean sameTypeVars(TypeVar tv1, TypeVar tv2) {
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1153
                return tv1 == tv2;
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1154
            }
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1155
            @Override
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1156
            protected boolean containsTypes(List<Type> ts1, List<Type> ts2) {
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1157
                return isSameTypes(ts1, ts2, true);
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1158
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1159
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
  1160
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  1161
06bc494ca11e Initial load
duke
parents:
diff changeset
  1162
    // <editor-fold defaultstate="collapsed" desc="Contains Type">
06bc494ca11e Initial load
duke
parents:
diff changeset
  1163
    public boolean containedBy(Type t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1164
        switch (t.tag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1165
        case UNDETVAR:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1166
            if (s.tag == WILDCARD) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1167
                UndetVar undetvar = (UndetVar)t;
1991
aafb4bf914ee 6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents: 1874
diff changeset
  1168
                WildcardType wt = (WildcardType)s;
aafb4bf914ee 6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents: 1874
diff changeset
  1169
                switch(wt.kind) {
aafb4bf914ee 6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents: 1874
diff changeset
  1170
                    case UNBOUND: //similar to ? extends Object
aafb4bf914ee 6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents: 1874
diff changeset
  1171
                    case EXTENDS: {
aafb4bf914ee 6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents: 1874
diff changeset
  1172
                        Type bound = upperBound(s);
14048
308d1cf8fe46 7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents: 13689
diff changeset
  1173
                        undetvar.addBound(InferenceBound.UPPER, bound, this);
1991
aafb4bf914ee 6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents: 1874
diff changeset
  1174
                        break;
aafb4bf914ee 6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents: 1874
diff changeset
  1175
                    }
aafb4bf914ee 6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents: 1874
diff changeset
  1176
                    case SUPER: {
aafb4bf914ee 6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents: 1874
diff changeset
  1177
                        Type bound = lowerBound(s);
14048
308d1cf8fe46 7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents: 13689
diff changeset
  1178
                        undetvar.addBound(InferenceBound.LOWER, bound, this);
1991
aafb4bf914ee 6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents: 1874
diff changeset
  1179
                        break;
aafb4bf914ee 6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents: 1874
diff changeset
  1180
                    }
1535
a64c289fe413 6762569: Javac crashes with AssertionError in Types.containedBy
mcimadamore
parents: 1530
diff changeset
  1181
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1182
                return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1183
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1184
                return isSameType(t, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1185
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1186
        case ERROR:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1187
            return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1188
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1189
            return containsType(s, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1190
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1191
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1192
06bc494ca11e Initial load
duke
parents:
diff changeset
  1193
    boolean containsType(List<Type> ts, List<Type> ss) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1194
        while (ts.nonEmpty() && ss.nonEmpty()
06bc494ca11e Initial load
duke
parents:
diff changeset
  1195
               && containsType(ts.head, ss.head)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1196
            ts = ts.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1197
            ss = ss.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1198
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1199
        return ts.isEmpty() && ss.isEmpty();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1200
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1201
06bc494ca11e Initial load
duke
parents:
diff changeset
  1202
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1203
     * Check if t contains s.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1204
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  1205
     * <p>T contains S if:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1206
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  1207
     * <p>{@code L(T) <: L(S) && U(S) <: U(T)}
06bc494ca11e Initial load
duke
parents:
diff changeset
  1208
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  1209
     * <p>This relation is only used by ClassType.isSubtype(), that
06bc494ca11e Initial load
duke
parents:
diff changeset
  1210
     * is,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1211
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  1212
     * <p>{@code C<S> <: C<T> if T contains S.}
06bc494ca11e Initial load
duke
parents:
diff changeset
  1213
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  1214
     * <p>Because of F-bounds, this relation can lead to infinite
06bc494ca11e Initial load
duke
parents:
diff changeset
  1215
     * recursion.  Thus we must somehow break that recursion.  Notice
06bc494ca11e Initial load
duke
parents:
diff changeset
  1216
     * that containsType() is only called from ClassType.isSubtype().
06bc494ca11e Initial load
duke
parents:
diff changeset
  1217
     * Since the arguments have already been checked against their
06bc494ca11e Initial load
duke
parents:
diff changeset
  1218
     * bounds, we know:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1219
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  1220
     * <p>{@code U(S) <: U(T) if T is "super" bound (U(T) *is* the bound)}
06bc494ca11e Initial load
duke
parents:
diff changeset
  1221
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  1222
     * <p>{@code L(T) <: L(S) if T is "extends" bound (L(T) is bottom)}
06bc494ca11e Initial load
duke
parents:
diff changeset
  1223
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  1224
     * @param t a type
06bc494ca11e Initial load
duke
parents:
diff changeset
  1225
     * @param s a type
06bc494ca11e Initial load
duke
parents:
diff changeset
  1226
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1227
    public boolean containsType(Type t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1228
        return containsType.visit(t, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1229
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1230
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
  1231
        private TypeRelation containsType = new TypeRelation() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1232
06bc494ca11e Initial load
duke
parents:
diff changeset
  1233
            private Type U(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1234
                while (t.tag == WILDCARD) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1235
                    WildcardType w = (WildcardType)t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1236
                    if (w.isSuperBound())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1237
                        return w.bound == null ? syms.objectType : w.bound.bound;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1238
                    else
06bc494ca11e Initial load
duke
parents:
diff changeset
  1239
                        t = w.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1240
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1241
                return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1242
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1243
06bc494ca11e Initial load
duke
parents:
diff changeset
  1244
            private Type L(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1245
                while (t.tag == WILDCARD) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1246
                    WildcardType w = (WildcardType)t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1247
                    if (w.isExtendsBound())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1248
                        return syms.botType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1249
                    else
06bc494ca11e Initial load
duke
parents:
diff changeset
  1250
                        t = w.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1251
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1252
                return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1253
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1254
06bc494ca11e Initial load
duke
parents:
diff changeset
  1255
            public Boolean visitType(Type t, Type s) {
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
  1256
                if (s.isPartial())
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1257
                    return containedBy(s, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1258
                else
06bc494ca11e Initial load
duke
parents:
diff changeset
  1259
                    return isSameType(t, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1260
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1261
7637
467bc8fc514e 6990134: minor (but red) findbugs warnings
jjg
parents: 7635
diff changeset
  1262
//            void debugContainsType(WildcardType t, Type s) {
467bc8fc514e 6990134: minor (but red) findbugs warnings
jjg
parents: 7635
diff changeset
  1263
//                System.err.println();
467bc8fc514e 6990134: minor (but red) findbugs warnings
jjg
parents: 7635
diff changeset
  1264
//                System.err.format(" does %s contain %s?%n", t, s);
467bc8fc514e 6990134: minor (but red) findbugs warnings
jjg
parents: 7635
diff changeset
  1265
//                System.err.format(" %s U(%s) <: U(%s) %s = %s%n",
467bc8fc514e 6990134: minor (but red) findbugs warnings
jjg
parents: 7635
diff changeset
  1266
//                                  upperBound(s), s, t, U(t),
467bc8fc514e 6990134: minor (but red) findbugs warnings
jjg
parents: 7635
diff changeset
  1267
//                                  t.isSuperBound()
467bc8fc514e 6990134: minor (but red) findbugs warnings
jjg
parents: 7635
diff changeset
  1268
//                                  || isSubtypeNoCapture(upperBound(s), U(t)));
467bc8fc514e 6990134: minor (but red) findbugs warnings
jjg
parents: 7635
diff changeset
  1269
//                System.err.format(" %s L(%s) <: L(%s) %s = %s%n",
467bc8fc514e 6990134: minor (but red) findbugs warnings
jjg
parents: 7635
diff changeset
  1270
//                                  L(t), t, s, lowerBound(s),
467bc8fc514e 6990134: minor (but red) findbugs warnings
jjg
parents: 7635
diff changeset
  1271
//                                  t.isExtendsBound()
467bc8fc514e 6990134: minor (but red) findbugs warnings
jjg
parents: 7635
diff changeset
  1272
//                                  || isSubtypeNoCapture(L(t), lowerBound(s)));
467bc8fc514e 6990134: minor (but red) findbugs warnings
jjg
parents: 7635
diff changeset
  1273
//                System.err.println();
467bc8fc514e 6990134: minor (but red) findbugs warnings
jjg
parents: 7635
diff changeset
  1274
//            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1275
06bc494ca11e Initial load
duke
parents:
diff changeset
  1276
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1277
            public Boolean visitWildcardType(WildcardType t, Type s) {
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
  1278
                if (s.isPartial())
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1279
                    return containedBy(s, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1280
                else {
7637
467bc8fc514e 6990134: minor (but red) findbugs warnings
jjg
parents: 7635
diff changeset
  1281
//                    debugContainsType(t, s);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1282
                    return isSameWildcard(t, s)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1283
                        || isCaptureOf(s, t)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1284
                        || ((t.isExtendsBound() || isSubtypeNoCapture(L(t), lowerBound(s))) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1285
                            (t.isSuperBound() || isSubtypeNoCapture(upperBound(s), U(t))));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1286
                }
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 visitUndetVar(UndetVar t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1291
                if (s.tag != WILDCARD)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1292
                    return isSameType(t, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1293
                else
06bc494ca11e Initial load
duke
parents:
diff changeset
  1294
                    return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1295
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1296
06bc494ca11e Initial load
duke
parents:
diff changeset
  1297
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1298
            public Boolean visitErrorType(ErrorType t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1299
                return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1300
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1301
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
  1302
06bc494ca11e Initial load
duke
parents:
diff changeset
  1303
    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
  1304
        if (s.tag != TYPEVAR || !((TypeVar)s).isCaptured())
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1305
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1306
        return isSameWildcard(t, ((CapturedType)s).wildcard);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1307
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1308
06bc494ca11e Initial load
duke
parents:
diff changeset
  1309
    public boolean isSameWildcard(WildcardType t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1310
        if (s.tag != WILDCARD)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1311
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1312
        WildcardType w = (WildcardType)s;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1313
        return w.kind == t.kind && w.type == t.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1314
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1315
06bc494ca11e Initial load
duke
parents:
diff changeset
  1316
    public boolean containsTypeEquivalent(List<Type> ts, List<Type> ss) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1317
        while (ts.nonEmpty() && ss.nonEmpty()
06bc494ca11e Initial load
duke
parents:
diff changeset
  1318
               && containsTypeEquivalent(ts.head, ss.head)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1319
            ts = ts.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1320
            ss = ss.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1321
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1322
        return ts.isEmpty() && ss.isEmpty();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1323
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1324
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  1325
06bc494ca11e Initial load
duke
parents:
diff changeset
  1326
    // <editor-fold defaultstate="collapsed" desc="isCastable">
06bc494ca11e Initial load
duke
parents:
diff changeset
  1327
    public boolean isCastable(Type t, Type s) {
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14443
diff changeset
  1328
        return isCastable(t, s, noWarnings);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1329
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1330
06bc494ca11e Initial load
duke
parents:
diff changeset
  1331
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1332
     * Is t is castable to s?<br>
06bc494ca11e Initial load
duke
parents:
diff changeset
  1333
     * s is assumed to be an erased type.<br>
06bc494ca11e Initial load
duke
parents:
diff changeset
  1334
     * (not defined for Method and ForAll types).
06bc494ca11e Initial load
duke
parents:
diff changeset
  1335
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1336
    public boolean isCastable(Type t, Type s, Warner warn) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1337
        if (t == s)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1338
            return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1339
06bc494ca11e Initial load
duke
parents:
diff changeset
  1340
        if (t.isPrimitive() != s.isPrimitive())
9595
a7dea6cbd5f7 7038363: cast from object to primitive should be for source >= 1.7
jjg
parents: 9303
diff changeset
  1341
            return allowBoxing && (
a7dea6cbd5f7 7038363: cast from object to primitive should be for source >= 1.7
jjg
parents: 9303
diff changeset
  1342
                    isConvertible(t, s, warn)
9721
da048be191c2 7041730: Regression: compiler accepts invalid cast from int to Byte
mcimadamore
parents: 9607
diff changeset
  1343
                    || (allowObjectToPrimitiveCast &&
da048be191c2 7041730: Regression: compiler accepts invalid cast from int to Byte
mcimadamore
parents: 9607
diff changeset
  1344
                        s.isPrimitive() &&
da048be191c2 7041730: Regression: compiler accepts invalid cast from int to Byte
mcimadamore
parents: 9607
diff changeset
  1345
                        isSubtype(boxedClass(s).type, t)));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1346
        if (warn != warnStack.head) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1347
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1348
                warnStack = warnStack.prepend(warn);
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 7637
diff changeset
  1349
                checkUnsafeVarargsConversion(t, s, warn);
1790
7182011ee8a6 6665356: Cast not allowed when both qualifying type and inner class are parameterized
mcimadamore
parents: 1535
diff changeset
  1350
                return isCastable.visit(t,s);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1351
            } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1352
                warnStack = warnStack.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1353
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1354
        } else {
1790
7182011ee8a6 6665356: Cast not allowed when both qualifying type and inner class are parameterized
mcimadamore
parents: 1535
diff changeset
  1355
            return isCastable.visit(t,s);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1356
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1357
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1358
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
  1359
        private TypeRelation isCastable = new TypeRelation() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1360
06bc494ca11e Initial load
duke
parents:
diff changeset
  1361
            public Boolean visitType(Type t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1362
                if (s.tag == ERROR)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1363
                    return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1364
06bc494ca11e Initial load
duke
parents:
diff changeset
  1365
                switch (t.tag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1366
                case BYTE: case CHAR: case SHORT: case INT: case LONG: case FLOAT:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1367
                case DOUBLE:
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
  1368
                    return s.isNumeric();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1369
                case BOOLEAN:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1370
                    return s.tag == BOOLEAN;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1371
                case VOID:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1372
                    return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1373
                case BOT:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1374
                    return isSubtype(t, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1375
                default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1376
                    throw new AssertionError();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1377
                }
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 Boolean visitWildcardType(WildcardType t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1382
                return isCastable(upperBound(t), s, warnStack.head);
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 Boolean visitClassType(ClassType t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1387
                if (s.tag == ERROR || s.tag == BOT)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1388
                    return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1389
06bc494ca11e Initial load
duke
parents:
diff changeset
  1390
                if (s.tag == TYPEVAR) {
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14443
diff changeset
  1391
                    if (isCastable(t, s.getUpperBound(), noWarnings)) {
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 7637
diff changeset
  1392
                        warnStack.head.warn(LintCategory.UNCHECKED);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1393
                        return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1394
                    } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1395
                        return false;
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
                if (t.isCompound()) {
1992
7be5dee022d5 6557182: Unchecked warning *and* inconvertible types
mcimadamore
parents: 1991
diff changeset
  1400
                    Warner oldWarner = warnStack.head;
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14443
diff changeset
  1401
                    warnStack.head = noWarnings;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1402
                    if (!visit(supertype(t), s))
06bc494ca11e Initial load
duke
parents:
diff changeset
  1403
                        return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1404
                    for (Type intf : interfaces(t)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1405
                        if (!visit(intf, s))
06bc494ca11e Initial load
duke
parents:
diff changeset
  1406
                            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1407
                    }
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 7637
diff changeset
  1408
                    if (warnStack.head.hasLint(LintCategory.UNCHECKED))
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 7637
diff changeset
  1409
                        oldWarner.warn(LintCategory.UNCHECKED);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1410
                    return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1411
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1412
06bc494ca11e Initial load
duke
parents:
diff changeset
  1413
                if (s.isCompound()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1414
                    // call recursively to reuse the above code
06bc494ca11e Initial load
duke
parents:
diff changeset
  1415
                    return visitClassType((ClassType)s, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1416
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1417
06bc494ca11e Initial load
duke
parents:
diff changeset
  1418
                if (s.tag == CLASS || s.tag == ARRAY) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1419
                    boolean upcast;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1420
                    if ((upcast = isSubtype(erasure(t), erasure(s)))
06bc494ca11e Initial load
duke
parents:
diff changeset
  1421
                        || isSubtype(erasure(s), erasure(t))) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1422
                        if (!upcast && s.tag == ARRAY) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1423
                            if (!isReifiable(s))
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 7637
diff changeset
  1424
                                warnStack.head.warn(LintCategory.UNCHECKED);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1425
                            return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1426
                        } else if (s.isRaw()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1427
                            return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1428
                        } else if (t.isRaw()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1429
                            if (!isUnbounded(s))
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 7637
diff changeset
  1430
                                warnStack.head.warn(LintCategory.UNCHECKED);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1431
                            return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1432
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1433
                        // Assume |a| <: |b|
06bc494ca11e Initial load
duke
parents:
diff changeset
  1434
                        final Type a = upcast ? t : s;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1435
                        final Type b = upcast ? s : t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1436
                        final boolean HIGH = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1437
                        final boolean LOW = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1438
                        final boolean DONT_REWRITE_TYPEVARS = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1439
                        Type aHigh = rewriteQuantifiers(a, HIGH, DONT_REWRITE_TYPEVARS);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1440
                        Type aLow  = rewriteQuantifiers(a, LOW,  DONT_REWRITE_TYPEVARS);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1441
                        Type bHigh = rewriteQuantifiers(b, HIGH, DONT_REWRITE_TYPEVARS);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1442
                        Type bLow  = rewriteQuantifiers(b, LOW,  DONT_REWRITE_TYPEVARS);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1443
                        Type lowSub = asSub(bLow, aLow.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1444
                        Type highSub = (lowSub == null) ? null : asSub(bHigh, aHigh.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1445
                        if (highSub == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1446
                            final boolean REWRITE_TYPEVARS = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1447
                            aHigh = rewriteQuantifiers(a, HIGH, REWRITE_TYPEVARS);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1448
                            aLow  = rewriteQuantifiers(a, LOW,  REWRITE_TYPEVARS);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1449
                            bHigh = rewriteQuantifiers(b, HIGH, REWRITE_TYPEVARS);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1450
                            bLow  = rewriteQuantifiers(b, LOW,  REWRITE_TYPEVARS);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1451
                            lowSub = asSub(bLow, aLow.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1452
                            highSub = (lowSub == null) ? null : asSub(bHigh, aHigh.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1453
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1454
                        if (highSub != null) {
8032
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents: 7681
diff changeset
  1455
                            if (!(a.tsym == highSub.tsym && a.tsym == lowSub.tsym)) {
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents: 7681
diff changeset
  1456
                                Assert.error(a.tsym + " != " + highSub.tsym + " != " + lowSub.tsym);
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents: 7681
diff changeset
  1457
                            }
1790
7182011ee8a6 6665356: Cast not allowed when both qualifying type and inner class are parameterized
mcimadamore
parents: 1535
diff changeset
  1458
                            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
  1459
                                && !disjointTypes(aHigh.allparams(), lowSub.allparams())
7182011ee8a6 6665356: Cast not allowed when both qualifying type and inner class are parameterized
mcimadamore
parents: 1535
diff changeset
  1460
                                && !disjointTypes(aLow.allparams(), highSub.allparams())
7182011ee8a6 6665356: Cast not allowed when both qualifying type and inner class are parameterized
mcimadamore
parents: 1535
diff changeset
  1461
                                && !disjointTypes(aLow.allparams(), lowSub.allparams())) {
7627
de2a86da165e 7005095: Cast: compile reject sensible cast from final class to interface
mcimadamore
parents: 7331
diff changeset
  1462
                                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
  1463
                                    giveWarning(b, a))
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 7637
diff changeset
  1464
                                    warnStack.head.warn(LintCategory.UNCHECKED);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1465
                                return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1466
                            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1467
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1468
                        if (isReifiable(s))
06bc494ca11e Initial load
duke
parents:
diff changeset
  1469
                            return isSubtypeUnchecked(a, b);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1470
                        else
06bc494ca11e Initial load
duke
parents:
diff changeset
  1471
                            return isSubtypeUnchecked(a, b, warnStack.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1472
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1473
06bc494ca11e Initial load
duke
parents:
diff changeset
  1474
                    // Sidecast
06bc494ca11e Initial load
duke
parents:
diff changeset
  1475
                    if (s.tag == CLASS) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1476
                        if ((s.tsym.flags() & INTERFACE) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1477
                            return ((t.tsym.flags() & FINAL) == 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1478
                                ? sideCast(t, s, warnStack.head)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1479
                                : sideCastFinal(t, s, warnStack.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1480
                        } else if ((t.tsym.flags() & INTERFACE) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1481
                            return ((s.tsym.flags() & FINAL) == 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1482
                                ? sideCast(t, s, warnStack.head)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1483
                                : sideCastFinal(t, s, warnStack.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1484
                        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1485
                            // unrelated class types
06bc494ca11e Initial load
duke
parents:
diff changeset
  1486
                            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1487
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1488
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1489
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1490
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1491
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1492
06bc494ca11e Initial load
duke
parents:
diff changeset
  1493
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1494
            public Boolean visitArrayType(ArrayType t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1495
                switch (s.tag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1496
                case ERROR:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1497
                case BOT:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1498
                    return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1499
                case TYPEVAR:
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14443
diff changeset
  1500
                    if (isCastable(s, t, noWarnings)) {
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 7637
diff changeset
  1501
                        warnStack.head.warn(LintCategory.UNCHECKED);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1502
                        return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1503
                    } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1504
                        return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1505
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1506
                case CLASS:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1507
                    return isSubtype(t, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1508
                case ARRAY:
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
  1509
                    if (elemtype(t).isPrimitive() || elemtype(s).isPrimitive()) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1510
                        return elemtype(t).tag == elemtype(s).tag;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1511
                    } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1512
                        return visit(elemtype(t), elemtype(s));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1513
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1514
                default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1515
                    return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1516
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1517
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1518
06bc494ca11e Initial load
duke
parents:
diff changeset
  1519
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1520
            public Boolean visitTypeVar(TypeVar t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1521
                switch (s.tag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1522
                case ERROR:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1523
                case BOT:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1524
                    return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1525
                case TYPEVAR:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1526
                    if (isSubtype(t, s)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1527
                        return true;
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14443
diff changeset
  1528
                    } else if (isCastable(t.bound, s, noWarnings)) {
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 7637
diff changeset
  1529
                        warnStack.head.warn(LintCategory.UNCHECKED);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1530
                        return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1531
                    } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1532
                        return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1533
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1534
                default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1535
                    return isCastable(t.bound, s, warnStack.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1536
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1537
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1538
06bc494ca11e Initial load
duke
parents:
diff changeset
  1539
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1540
            public Boolean visitErrorType(ErrorType t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1541
                return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1542
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1543
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
  1544
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  1545
06bc494ca11e Initial load
duke
parents:
diff changeset
  1546
    // <editor-fold defaultstate="collapsed" desc="disjointTypes">
06bc494ca11e Initial load
duke
parents:
diff changeset
  1547
    public boolean disjointTypes(List<Type> ts, List<Type> ss) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1548
        while (ts.tail != null && ss.tail != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1549
            if (disjointType(ts.head, ss.head)) return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1550
            ts = ts.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1551
            ss = ss.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1552
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1553
        return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1554
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1555
06bc494ca11e Initial load
duke
parents:
diff changeset
  1556
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1557
     * Two types or wildcards are considered disjoint if it can be
06bc494ca11e Initial load
duke
parents:
diff changeset
  1558
     * proven that no type can be contained in both. It is
06bc494ca11e Initial load
duke
parents:
diff changeset
  1559
     * conservative in that it is allowed to say that two types are
06bc494ca11e Initial load
duke
parents:
diff changeset
  1560
     * not disjoint, even though they actually are.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1561
     *
14259
fb94a1df0d53 8000208: fix langtools javadoc comment issues
jjg
parents: 14258
diff changeset
  1562
     * The type {@code C<X>} is castable to {@code C<Y>} exactly if
fb94a1df0d53 8000208: fix langtools javadoc comment issues
jjg
parents: 14258
diff changeset
  1563
     * {@code X} and {@code Y} are not disjoint.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1564
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1565
    public boolean disjointType(Type t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1566
        return disjointType.visit(t, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1567
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1568
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
  1569
        private TypeRelation disjointType = new TypeRelation() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1570
06bc494ca11e Initial load
duke
parents:
diff changeset
  1571
            private Set<TypePair> cache = new HashSet<TypePair>();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1572
06bc494ca11e Initial load
duke
parents:
diff changeset
  1573
            public Boolean visitType(Type t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1574
                if (s.tag == WILDCARD)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1575
                    return visit(s, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1576
                else
06bc494ca11e Initial load
duke
parents:
diff changeset
  1577
                    return notSoftSubtypeRecursive(t, s) || notSoftSubtypeRecursive(s, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1578
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1579
06bc494ca11e Initial load
duke
parents:
diff changeset
  1580
            private boolean isCastableRecursive(Type t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1581
                TypePair pair = new TypePair(t, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1582
                if (cache.add(pair)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1583
                    try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1584
                        return Types.this.isCastable(t, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1585
                    } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1586
                        cache.remove(pair);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1587
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1588
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1589
                    return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1590
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1591
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1592
06bc494ca11e Initial load
duke
parents:
diff changeset
  1593
            private boolean notSoftSubtypeRecursive(Type t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1594
                TypePair pair = new TypePair(t, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1595
                if (cache.add(pair)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1596
                    try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1597
                        return Types.this.notSoftSubtype(t, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1598
                    } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1599
                        cache.remove(pair);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1600
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1601
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1602
                    return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1603
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1604
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1605
06bc494ca11e Initial load
duke
parents:
diff changeset
  1606
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1607
            public Boolean visitWildcardType(WildcardType t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1608
                if (t.isUnbound())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1609
                    return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1610
06bc494ca11e Initial load
duke
parents:
diff changeset
  1611
                if (s.tag != WILDCARD) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1612
                    if (t.isExtendsBound())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1613
                        return notSoftSubtypeRecursive(s, t.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1614
                    else // isSuperBound()
06bc494ca11e Initial load
duke
parents:
diff changeset
  1615
                        return notSoftSubtypeRecursive(t.type, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1616
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1617
06bc494ca11e Initial load
duke
parents:
diff changeset
  1618
                if (s.isUnbound())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1619
                    return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1620
06bc494ca11e Initial load
duke
parents:
diff changeset
  1621
                if (t.isExtendsBound()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1622
                    if (s.isExtendsBound())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1623
                        return !isCastableRecursive(t.type, upperBound(s));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1624
                    else if (s.isSuperBound())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1625
                        return notSoftSubtypeRecursive(lowerBound(s), t.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1626
                } else if (t.isSuperBound()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1627
                    if (s.isExtendsBound())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1628
                        return notSoftSubtypeRecursive(t.type, upperBound(s));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1629
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1630
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1631
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1632
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
  1633
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  1634
06bc494ca11e Initial load
duke
parents:
diff changeset
  1635
    // <editor-fold defaultstate="collapsed" desc="lowerBoundArgtypes">
06bc494ca11e Initial load
duke
parents:
diff changeset
  1636
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1637
     * Returns the lower bounds of the formals of a method.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1638
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1639
    public List<Type> lowerBoundArgtypes(Type t) {
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1640
        return lowerBounds(t.getParameterTypes());
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1641
    }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1642
    public List<Type> lowerBounds(List<Type> ts) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1643
        return map(ts, lowerBoundMapping);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1644
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1645
    private final Mapping lowerBoundMapping = new Mapping("lowerBound") {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1646
            public Type apply(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1647
                return lowerBound(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1648
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1649
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
  1650
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  1651
06bc494ca11e Initial load
duke
parents:
diff changeset
  1652
    // <editor-fold defaultstate="collapsed" desc="notSoftSubtype">
06bc494ca11e Initial load
duke
parents:
diff changeset
  1653
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1654
     * This relation answers the question: is impossible that
06bc494ca11e Initial load
duke
parents:
diff changeset
  1655
     * something of type `t' can be a subtype of `s'? This is
06bc494ca11e Initial load
duke
parents:
diff changeset
  1656
     * different from the question "is `t' not a subtype of `s'?"
06bc494ca11e Initial load
duke
parents:
diff changeset
  1657
     * when type variables are involved: Integer is not a subtype of T
14259
fb94a1df0d53 8000208: fix langtools javadoc comment issues
jjg
parents: 14258
diff changeset
  1658
     * where {@code <T extends Number>} but it is not true that Integer cannot
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1659
     * possibly be a subtype of T.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1660
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1661
    public boolean notSoftSubtype(Type t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1662
        if (t == s) return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1663
        if (t.tag == TYPEVAR) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1664
            TypeVar tv = (TypeVar) t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1665
            return !isCastable(tv.bound,
6353
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  1666
                               relaxBound(s),
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14443
diff changeset
  1667
                               noWarnings);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1668
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1669
        if (s.tag != WILDCARD)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1670
            s = upperBound(s);
6353
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  1671
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  1672
        return !isSubtype(t, relaxBound(s));
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  1673
    }
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  1674
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  1675
    private Type relaxBound(Type t) {
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  1676
        if (t.tag == TYPEVAR) {
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  1677
            while (t.tag == TYPEVAR)
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  1678
                t = t.getUpperBound();
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  1679
            t = rewriteQuantifiers(t, true, true);
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  1680
        }
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  1681
        return t;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1682
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1683
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  1684
06bc494ca11e Initial load
duke
parents:
diff changeset
  1685
    // <editor-fold defaultstate="collapsed" desc="isReifiable">
06bc494ca11e Initial load
duke
parents:
diff changeset
  1686
    public boolean isReifiable(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1687
        return isReifiable.visit(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1688
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1689
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
  1690
        private UnaryVisitor<Boolean> isReifiable = new UnaryVisitor<Boolean>() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1691
06bc494ca11e Initial load
duke
parents:
diff changeset
  1692
            public Boolean visitType(Type t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1693
                return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1694
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1695
06bc494ca11e Initial load
duke
parents:
diff changeset
  1696
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1697
            public Boolean visitClassType(ClassType t, Void ignored) {
3554
a0a7f7f2a6e5 6806876: ClassCastException occurs in assignment expressions without any heap pollutions
mcimadamore
parents: 3539
diff changeset
  1698
                if (t.isCompound())
a0a7f7f2a6e5 6806876: ClassCastException occurs in assignment expressions without any heap pollutions
mcimadamore
parents: 3539
diff changeset
  1699
                    return false;
a0a7f7f2a6e5 6806876: ClassCastException occurs in assignment expressions without any heap pollutions
mcimadamore
parents: 3539
diff changeset
  1700
                else {
a0a7f7f2a6e5 6806876: ClassCastException occurs in assignment expressions without any heap pollutions
mcimadamore
parents: 3539
diff changeset
  1701
                    if (!t.isParameterized())
a0a7f7f2a6e5 6806876: ClassCastException occurs in assignment expressions without any heap pollutions
mcimadamore
parents: 3539
diff changeset
  1702
                        return true;
a0a7f7f2a6e5 6806876: ClassCastException occurs in assignment expressions without any heap pollutions
mcimadamore
parents: 3539
diff changeset
  1703
a0a7f7f2a6e5 6806876: ClassCastException occurs in assignment expressions without any heap pollutions
mcimadamore
parents: 3539
diff changeset
  1704
                    for (Type param : t.allparams()) {
a0a7f7f2a6e5 6806876: ClassCastException occurs in assignment expressions without any heap pollutions
mcimadamore
parents: 3539
diff changeset
  1705
                        if (!param.isUnbound())
a0a7f7f2a6e5 6806876: ClassCastException occurs in assignment expressions without any heap pollutions
mcimadamore
parents: 3539
diff changeset
  1706
                            return false;
a0a7f7f2a6e5 6806876: ClassCastException occurs in assignment expressions without any heap pollutions
mcimadamore
parents: 3539
diff changeset
  1707
                    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1708
                    return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1709
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1710
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1711
06bc494ca11e Initial load
duke
parents:
diff changeset
  1712
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1713
            public Boolean visitArrayType(ArrayType t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1714
                return visit(t.elemtype);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1715
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1716
06bc494ca11e Initial load
duke
parents:
diff changeset
  1717
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1718
            public Boolean visitTypeVar(TypeVar t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1719
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1720
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1721
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
  1722
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  1723
06bc494ca11e Initial load
duke
parents:
diff changeset
  1724
    // <editor-fold defaultstate="collapsed" desc="Array Utils">
06bc494ca11e Initial load
duke
parents:
diff changeset
  1725
    public boolean isArray(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1726
        while (t.tag == WILDCARD)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1727
            t = upperBound(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1728
        return t.tag == ARRAY;
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 element type of an array.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1733
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1734
    public Type elemtype(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1735
        switch (t.tag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1736
        case WILDCARD:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1737
            return elemtype(upperBound(t));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1738
        case ARRAY:
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
  1739
            t = t.unannotatedType();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1740
            return ((ArrayType)t).elemtype;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1741
        case FORALL:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1742
            return elemtype(((ForAll)t).qtype);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1743
        case ERROR:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1744
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1745
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1746
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1747
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1748
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1749
7635
e0a3aeefe4e7 6199075: Unambiguous varargs method calls flagged as ambiguous
mcimadamore
parents: 7634
diff changeset
  1750
    public Type elemtypeOrType(Type t) {
e0a3aeefe4e7 6199075: Unambiguous varargs method calls flagged as ambiguous
mcimadamore
parents: 7634
diff changeset
  1751
        Type elemtype = elemtype(t);
e0a3aeefe4e7 6199075: Unambiguous varargs method calls flagged as ambiguous
mcimadamore
parents: 7634
diff changeset
  1752
        return elemtype != null ?
e0a3aeefe4e7 6199075: Unambiguous varargs method calls flagged as ambiguous
mcimadamore
parents: 7634
diff changeset
  1753
            elemtype :
e0a3aeefe4e7 6199075: Unambiguous varargs method calls flagged as ambiguous
mcimadamore
parents: 7634
diff changeset
  1754
            t;
e0a3aeefe4e7 6199075: Unambiguous varargs method calls flagged as ambiguous
mcimadamore
parents: 7634
diff changeset
  1755
    }
e0a3aeefe4e7 6199075: Unambiguous varargs method calls flagged as ambiguous
mcimadamore
parents: 7634
diff changeset
  1756
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1757
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1758
     * Mapping to take element type of an arraytype
06bc494ca11e Initial load
duke
parents:
diff changeset
  1759
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1760
    private Mapping elemTypeFun = new Mapping ("elemTypeFun") {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1761
        public Type apply(Type t) { return elemtype(t); }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1762
    };
06bc494ca11e Initial load
duke
parents:
diff changeset
  1763
06bc494ca11e Initial load
duke
parents:
diff changeset
  1764
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1765
     * The number of dimensions of an array type.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1766
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1767
    public int dimensions(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1768
        int result = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1769
        while (t.tag == ARRAY) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1770
            result++;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1771
            t = elemtype(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1772
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1773
        return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1774
    }
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 13633
diff changeset
  1775
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 13633
diff changeset
  1776
    /**
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 13633
diff changeset
  1777
     * Returns an ArrayType with the component type t
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 13633
diff changeset
  1778
     *
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 13633
diff changeset
  1779
     * @param t The component type of the ArrayType
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 13633
diff changeset
  1780
     * @return the ArrayType for the given component
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 13633
diff changeset
  1781
     */
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 13633
diff changeset
  1782
    public ArrayType makeArrayType(Type t) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 13633
diff changeset
  1783
        if (t.tag == VOID ||
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
  1784
            t.tag == PACKAGE) {
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
  1785
            Assert.error("Type t must not be a VOID or PACKAGE type, " + t.toString());
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 13633
diff changeset
  1786
        }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 13633
diff changeset
  1787
        return new ArrayType(t, syms.arrayClass);
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 13633
diff changeset
  1788
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1789
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  1790
06bc494ca11e Initial load
duke
parents:
diff changeset
  1791
    // <editor-fold defaultstate="collapsed" desc="asSuper">
06bc494ca11e Initial load
duke
parents:
diff changeset
  1792
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1793
     * Return the (most specific) base type of t that starts with the
06bc494ca11e Initial load
duke
parents:
diff changeset
  1794
     * given symbol.  If none exists, return null.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1795
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  1796
     * @param t a type
06bc494ca11e Initial load
duke
parents:
diff changeset
  1797
     * @param sym a symbol
06bc494ca11e Initial load
duke
parents:
diff changeset
  1798
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1799
    public Type asSuper(Type t, Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1800
        return asSuper.visit(t, sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1801
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1802
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
  1803
        private SimpleVisitor<Type,Symbol> asSuper = new SimpleVisitor<Type,Symbol>() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1804
06bc494ca11e Initial load
duke
parents:
diff changeset
  1805
            public Type visitType(Type t, Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1806
                return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1807
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1808
06bc494ca11e Initial load
duke
parents:
diff changeset
  1809
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1810
            public Type visitClassType(ClassType t, Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1811
                if (t.tsym == sym)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1812
                    return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1813
06bc494ca11e Initial load
duke
parents:
diff changeset
  1814
                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
  1815
                if (st.tag == CLASS || st.tag == TYPEVAR || st.tag == ERROR) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1816
                    Type x = asSuper(st, sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1817
                    if (x != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1818
                        return x;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1819
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1820
                if ((sym.flags() & INTERFACE) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1821
                    for (List<Type> l = interfaces(t); l.nonEmpty(); l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1822
                        Type x = asSuper(l.head, sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1823
                        if (x != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1824
                            return x;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1825
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1826
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1827
                return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1828
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1829
06bc494ca11e Initial load
duke
parents:
diff changeset
  1830
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1831
            public Type visitArrayType(ArrayType t, Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1832
                return isSubtype(t, sym.type) ? sym.type : null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1833
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1834
06bc494ca11e Initial load
duke
parents:
diff changeset
  1835
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1836
            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
  1837
                if (t.tsym == sym)
d51f30ce6796 6531090: Cannot access methods/fields of a captured type belonging to an intersection type
mcimadamore
parents: 162
diff changeset
  1838
                    return t;
d51f30ce6796 6531090: Cannot access methods/fields of a captured type belonging to an intersection type
mcimadamore
parents: 162
diff changeset
  1839
                else
d51f30ce6796 6531090: Cannot access methods/fields of a captured type belonging to an intersection type
mcimadamore
parents: 162
diff changeset
  1840
                    return asSuper(t.bound, sym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1841
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1842
06bc494ca11e Initial load
duke
parents:
diff changeset
  1843
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1844
            public Type visitErrorType(ErrorType t, Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1845
                return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1846
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1847
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
  1848
06bc494ca11e Initial load
duke
parents:
diff changeset
  1849
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1850
     * Return the base type of t or any of its outer types that starts
06bc494ca11e Initial load
duke
parents:
diff changeset
  1851
     * with the given symbol.  If none exists, return null.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1852
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  1853
     * @param t a type
06bc494ca11e Initial load
duke
parents:
diff changeset
  1854
     * @param sym a symbol
06bc494ca11e Initial load
duke
parents:
diff changeset
  1855
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1856
    public Type asOuterSuper(Type t, Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1857
        switch (t.tag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1858
        case CLASS:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1859
            do {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1860
                Type s = asSuper(t, sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1861
                if (s != null) return s;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1862
                t = t.getEnclosingType();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1863
            } while (t.tag == CLASS);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1864
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1865
        case ARRAY:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1866
            return isSubtype(t, sym.type) ? sym.type : null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1867
        case TYPEVAR:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1868
            return asSuper(t, sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1869
        case ERROR:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1870
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1871
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1872
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1873
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1874
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1875
06bc494ca11e Initial load
duke
parents:
diff changeset
  1876
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1877
     * Return the base type of t or any of its enclosing types that
06bc494ca11e Initial load
duke
parents:
diff changeset
  1878
     * starts with the given symbol.  If none exists, return null.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1879
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  1880
     * @param t a type
06bc494ca11e Initial load
duke
parents:
diff changeset
  1881
     * @param sym a symbol
06bc494ca11e Initial load
duke
parents:
diff changeset
  1882
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1883
    public Type asEnclosingSuper(Type t, Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1884
        switch (t.tag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1885
        case CLASS:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1886
            do {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1887
                Type s = asSuper(t, sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1888
                if (s != null) return s;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1889
                Type outer = t.getEnclosingType();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1890
                t = (outer.tag == CLASS) ? outer :
06bc494ca11e Initial load
duke
parents:
diff changeset
  1891
                    (t.tsym.owner.enclClass() != null) ? t.tsym.owner.enclClass().type :
06bc494ca11e Initial load
duke
parents:
diff changeset
  1892
                    Type.noType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1893
            } while (t.tag == CLASS);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1894
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1895
        case ARRAY:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1896
            return isSubtype(t, sym.type) ? sym.type : null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1897
        case TYPEVAR:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1898
            return asSuper(t, sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1899
        case ERROR:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1900
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1901
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1902
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1903
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1904
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1905
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  1906
06bc494ca11e Initial load
duke
parents:
diff changeset
  1907
    // <editor-fold defaultstate="collapsed" desc="memberType">
06bc494ca11e Initial load
duke
parents:
diff changeset
  1908
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1909
     * The type of given symbol, seen as a member of t.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1910
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  1911
     * @param t a type
06bc494ca11e Initial load
duke
parents:
diff changeset
  1912
     * @param sym a symbol
06bc494ca11e Initial load
duke
parents:
diff changeset
  1913
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1914
    public Type memberType(Type t, Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1915
        return (sym.flags() & STATIC) != 0
06bc494ca11e Initial load
duke
parents:
diff changeset
  1916
            ? sym.type
06bc494ca11e Initial load
duke
parents:
diff changeset
  1917
            : memberType.visit(t, sym);
3539
3c265cb6f4e0 6827648: Extremely slow compilation time for visitor pattern code + generics
mcimadamore
parents: 3140
diff changeset
  1918
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1919
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
  1920
        private SimpleVisitor<Type,Symbol> memberType = new SimpleVisitor<Type,Symbol>() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1921
06bc494ca11e Initial load
duke
parents:
diff changeset
  1922
            public Type visitType(Type t, Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1923
                return sym.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1924
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1925
06bc494ca11e Initial load
duke
parents:
diff changeset
  1926
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1927
            public Type visitWildcardType(WildcardType t, Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1928
                return memberType(upperBound(t), sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1929
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1930
06bc494ca11e Initial load
duke
parents:
diff changeset
  1931
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1932
            public Type visitClassType(ClassType t, Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1933
                Symbol owner = sym.owner;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1934
                long flags = sym.flags();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1935
                if (((flags & STATIC) == 0) && owner.type.isParameterized()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1936
                    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
  1937
                    //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
  1938
                    //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
  1939
                    //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
  1940
                    base = t.isCompound() ? capture(base) : base;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1941
                    if (base != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1942
                        List<Type> ownerParams = owner.type.allparams();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1943
                        List<Type> baseParams = base.allparams();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1944
                        if (ownerParams.nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1945
                            if (baseParams.isEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1946
                                // then base is a raw type
06bc494ca11e Initial load
duke
parents:
diff changeset
  1947
                                return erasure(sym.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1948
                            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1949
                                return subst(sym.type, ownerParams, baseParams);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1950
                            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1951
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1952
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1953
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1954
                return sym.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1955
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1956
06bc494ca11e Initial load
duke
parents:
diff changeset
  1957
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1958
            public Type visitTypeVar(TypeVar t, Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1959
                return memberType(t.bound, sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1960
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1961
06bc494ca11e Initial load
duke
parents:
diff changeset
  1962
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1963
            public Type visitErrorType(ErrorType t, Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1964
                return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1965
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1966
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
  1967
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  1968
06bc494ca11e Initial load
duke
parents:
diff changeset
  1969
    // <editor-fold defaultstate="collapsed" desc="isAssignable">
06bc494ca11e Initial load
duke
parents:
diff changeset
  1970
    public boolean isAssignable(Type t, Type s) {
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14443
diff changeset
  1971
        return isAssignable(t, s, noWarnings);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1972
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1973
06bc494ca11e Initial load
duke
parents:
diff changeset
  1974
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1975
     * Is t assignable to s?<br>
06bc494ca11e Initial load
duke
parents:
diff changeset
  1976
     * Equivalent to subtype except for constant values and raw
06bc494ca11e Initial load
duke
parents:
diff changeset
  1977
     * types.<br>
06bc494ca11e Initial load
duke
parents:
diff changeset
  1978
     * (not defined for Method and ForAll types)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1979
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1980
    public boolean isAssignable(Type t, Type s, Warner warn) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1981
        if (t.tag == ERROR)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1982
            return true;
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
  1983
        if (t.tag.isSubRangeOf(INT) && t.constValue() != null) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1984
            int value = ((Number)t.constValue()).intValue();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1985
            switch (s.tag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1986
            case BYTE:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1987
                if (Byte.MIN_VALUE <= value && value <= Byte.MAX_VALUE)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1988
                    return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1989
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1990
            case CHAR:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1991
                if (Character.MIN_VALUE <= value && value <= Character.MAX_VALUE)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1992
                    return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1993
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1994
            case SHORT:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1995
                if (Short.MIN_VALUE <= value && value <= Short.MAX_VALUE)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1996
                    return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1997
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1998
            case INT:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1999
                return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2000
            case CLASS:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2001
                switch (unboxedType(s).tag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2002
                case BYTE:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2003
                case CHAR:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2004
                case SHORT:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2005
                    return isAssignable(t, unboxedType(s), warn);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2006
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2007
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2008
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2009
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2010
        return isConvertible(t, s, warn);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2011
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2012
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  2013
06bc494ca11e Initial load
duke
parents:
diff changeset
  2014
    // <editor-fold defaultstate="collapsed" desc="erasure">
06bc494ca11e Initial load
duke
parents:
diff changeset
  2015
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  2016
     * The erasure of t {@code |t|} -- the type that results when all
06bc494ca11e Initial load
duke
parents:
diff changeset
  2017
     * type parameters in t are deleted.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2018
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2019
    public Type erasure(Type t) {
13633
0ea4283af723 7181320: javac NullPointerException for switch labels with cast to String expressions
sundar
parents: 12468
diff changeset
  2020
        return eraseNotNeeded(t)? t : erasure(t, false);
514
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  2021
    }
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  2022
    //where
13633
0ea4283af723 7181320: javac NullPointerException for switch labels with cast to String expressions
sundar
parents: 12468
diff changeset
  2023
    private boolean eraseNotNeeded(Type t) {
0ea4283af723 7181320: javac NullPointerException for switch labels with cast to String expressions
sundar
parents: 12468
diff changeset
  2024
        // We don't want to erase primitive types and String type as that
0ea4283af723 7181320: javac NullPointerException for switch labels with cast to String expressions
sundar
parents: 12468
diff changeset
  2025
        // operation is idempotent. Also, erasing these could result in loss
0ea4283af723 7181320: javac NullPointerException for switch labels with cast to String expressions
sundar
parents: 12468
diff changeset
  2026
        // of information such as constant values attached to such types.
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
  2027
        return (t.isPrimitive()) || (syms.stringType.tsym == t.tsym);
13633
0ea4283af723 7181320: javac NullPointerException for switch labels with cast to String expressions
sundar
parents: 12468
diff changeset
  2028
    }
0ea4283af723 7181320: javac NullPointerException for switch labels with cast to String expressions
sundar
parents: 12468
diff changeset
  2029
514
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  2030
    private Type erasure(Type t, boolean recurse) {
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
  2031
        if (t.isPrimitive())
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2032
            return t; /* fast special case */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2033
        else
514
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  2034
            return erasure.visit(t, recurse);
3539
3c265cb6f4e0 6827648: Extremely slow compilation time for visitor pattern code + generics
mcimadamore
parents: 3140
diff changeset
  2035
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2036
    // where
514
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  2037
        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
  2038
            public Type visitType(Type t, Boolean recurse) {
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
  2039
                if (t.isPrimitive())
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2040
                    return t; /*fast special case*/
06bc494ca11e Initial load
duke
parents:
diff changeset
  2041
                else
514
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  2042
                    return t.map(recurse ? erasureRecFun : erasureFun);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2043
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2044
06bc494ca11e Initial load
duke
parents:
diff changeset
  2045
            @Override
514
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  2046
            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
  2047
                return erasure(upperBound(t), recurse);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2048
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2049
06bc494ca11e Initial load
duke
parents:
diff changeset
  2050
            @Override
514
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  2051
            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
  2052
                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
  2053
                if (recurse) {
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  2054
                    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
  2055
                }
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  2056
                return erased;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2057
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2058
06bc494ca11e Initial load
duke
parents:
diff changeset
  2059
            @Override
514
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  2060
            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
  2061
                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
  2062
            }
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  2063
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  2064
            @Override
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  2065
            public Type visitErrorType(ErrorType t, Boolean recurse) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2066
                return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2067
            }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
  2068
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
  2069
            @Override
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
  2070
            public Type visitAnnotatedType(AnnotatedType t, Boolean recurse) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
  2071
                return new AnnotatedType(t.typeAnnotations, erasure(t.underlyingType, recurse));
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
  2072
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2073
        };
514
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  2074
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2075
    private Mapping erasureFun = new Mapping ("erasure") {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2076
            public Type apply(Type t) { return erasure(t); }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2077
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
  2078
514
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  2079
    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
  2080
        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
  2081
    };
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  2082
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2083
    public List<Type> erasure(List<Type> ts) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2084
        return Type.map(ts, erasureFun);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2085
    }
514
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  2086
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  2087
    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
  2088
        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
  2089
    }
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  2090
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  2091
    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
  2092
        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
  2093
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2094
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  2095
06bc494ca11e Initial load
duke
parents:
diff changeset
  2096
    // <editor-fold defaultstate="collapsed" desc="makeCompoundType">
06bc494ca11e Initial load
duke
parents:
diff changeset
  2097
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  2098
     * Make a compound type from non-empty list of types
06bc494ca11e Initial load
duke
parents:
diff changeset
  2099
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  2100
     * @param bounds            the types from which the compound type is formed
06bc494ca11e Initial load
duke
parents:
diff changeset
  2101
     * @param supertype         is objectType if all bounds are interfaces,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2102
     *                          null otherwise.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2103
     */
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
  2104
    public Type makeCompoundType(List<Type> bounds) {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
  2105
        return makeCompoundType(bounds, bounds.head.tsym.isInterface());
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
  2106
    }
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
  2107
    public Type makeCompoundType(List<Type> bounds, boolean allInterfaces) {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
  2108
        Assert.check(bounds.nonEmpty());
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
  2109
        Type firstExplicitBound = bounds.head;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
  2110
        if (allInterfaces) {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
  2111
            bounds = bounds.prepend(syms.objectType);
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
  2112
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2113
        ClassSymbol bc =
06bc494ca11e Initial load
duke
parents:
diff changeset
  2114
            new ClassSymbol(ABSTRACT|PUBLIC|SYNTHETIC|COMPOUND|ACYCLIC,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2115
                            Type.moreInfo
06bc494ca11e Initial load
duke
parents:
diff changeset
  2116
                                ? names.fromString(bounds.toString())
06bc494ca11e Initial load
duke
parents:
diff changeset
  2117
                                : names.empty,
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
  2118
                            null,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2119
                            syms.noSymbol);
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
  2120
        bc.type = new IntersectionClassType(bounds, bc, allInterfaces);
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
  2121
        bc.erasure_field = (bounds.head.tag == TYPEVAR) ?
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
  2122
                syms.objectType : // error condition, recover
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
  2123
                erasure(firstExplicitBound);
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
  2124
        bc.members_field = new Scope(bc);
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
  2125
        return bc.type;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2126
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2127
06bc494ca11e Initial load
duke
parents:
diff changeset
  2128
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  2129
     * A convenience wrapper for {@link #makeCompoundType(List)}; the
06bc494ca11e Initial load
duke
parents:
diff changeset
  2130
     * arguments are converted to a list and passed to the other
06bc494ca11e Initial load
duke
parents:
diff changeset
  2131
     * method.  Note that this might cause a symbol completion.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2132
     * Hence, this version of makeCompoundType may not be called
06bc494ca11e Initial load
duke
parents:
diff changeset
  2133
     * during a classfile read.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2134
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2135
    public Type makeCompoundType(Type bound1, Type bound2) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2136
        return makeCompoundType(List.of(bound1, bound2));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2137
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2138
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  2139
06bc494ca11e Initial load
duke
parents:
diff changeset
  2140
    // <editor-fold defaultstate="collapsed" desc="supertype">
06bc494ca11e Initial load
duke
parents:
diff changeset
  2141
    public Type supertype(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2142
        return supertype.visit(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2143
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2144
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
  2145
        private UnaryVisitor<Type> supertype = new UnaryVisitor<Type>() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2146
06bc494ca11e Initial load
duke
parents:
diff changeset
  2147
            public Type visitType(Type t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2148
                // A note on wildcards: there is no good way to
06bc494ca11e Initial load
duke
parents:
diff changeset
  2149
                // determine a supertype for a super bounded wildcard.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2150
                return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2151
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2152
06bc494ca11e Initial load
duke
parents:
diff changeset
  2153
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  2154
            public Type visitClassType(ClassType t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2155
                if (t.supertype_field == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2156
                    Type supertype = ((ClassSymbol)t.tsym).getSuperclass();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2157
                    // An interface has no superclass; its supertype is Object.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2158
                    if (t.isInterface())
06bc494ca11e Initial load
duke
parents:
diff changeset
  2159
                        supertype = ((ClassType)t.tsym.type).supertype_field;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2160
                    if (t.supertype_field == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2161
                        List<Type> actuals = classBound(t).allparams();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2162
                        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
  2163
                        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
  2164
                            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
  2165
                        } else if (formals.nonEmpty()) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2166
                            t.supertype_field = subst(supertype, formals, actuals);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2167
                        }
514
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  2168
                        else {
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  2169
                            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
  2170
                        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2171
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2172
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2173
                return t.supertype_field;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2174
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2175
06bc494ca11e Initial load
duke
parents:
diff changeset
  2176
            /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  2177
             * The supertype is always a class type. If the type
06bc494ca11e Initial load
duke
parents:
diff changeset
  2178
             * variable's bounds start with a class type, this is also
06bc494ca11e Initial load
duke
parents:
diff changeset
  2179
             * the supertype.  Otherwise, the supertype is
06bc494ca11e Initial load
duke
parents:
diff changeset
  2180
             * java.lang.Object.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2181
             */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2182
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  2183
            public Type visitTypeVar(TypeVar t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2184
                if (t.bound.tag == TYPEVAR ||
06bc494ca11e Initial load
duke
parents:
diff changeset
  2185
                    (!t.bound.isCompound() && !t.bound.isInterface())) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2186
                    return t.bound;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2187
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2188
                    return supertype(t.bound);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2189
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2190
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2191
06bc494ca11e Initial load
duke
parents:
diff changeset
  2192
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  2193
            public Type visitArrayType(ArrayType t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2194
                if (t.elemtype.isPrimitive() || isSameType(t.elemtype, syms.objectType))
06bc494ca11e Initial load
duke
parents:
diff changeset
  2195
                    return arraySuperType();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2196
                else
06bc494ca11e Initial load
duke
parents:
diff changeset
  2197
                    return new ArrayType(supertype(t.elemtype), t.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2198
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2199
06bc494ca11e Initial load
duke
parents:
diff changeset
  2200
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  2201
            public Type visitErrorType(ErrorType t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2202
                return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2203
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2204
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
  2205
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  2206
06bc494ca11e Initial load
duke
parents:
diff changeset
  2207
    // <editor-fold defaultstate="collapsed" desc="interfaces">
06bc494ca11e Initial load
duke
parents:
diff changeset
  2208
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  2209
     * Return the interfaces implemented by this class.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2210
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2211
    public List<Type> interfaces(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2212
        return interfaces.visit(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2213
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2214
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
  2215
        private UnaryVisitor<List<Type>> interfaces = new UnaryVisitor<List<Type>>() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2216
06bc494ca11e Initial load
duke
parents:
diff changeset
  2217
            public List<Type> visitType(Type t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2218
                return List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2219
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2220
06bc494ca11e Initial load
duke
parents:
diff changeset
  2221
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  2222
            public List<Type> visitClassType(ClassType t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2223
                if (t.interfaces_field == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2224
                    List<Type> interfaces = ((ClassSymbol)t.tsym).getInterfaces();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2225
                    if (t.interfaces_field == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2226
                        // If t.interfaces_field is null, then t must
06bc494ca11e Initial load
duke
parents:
diff changeset
  2227
                        // be a parameterized type (not to be confused
06bc494ca11e Initial load
duke
parents:
diff changeset
  2228
                        // with a generic type declaration).
06bc494ca11e Initial load
duke
parents:
diff changeset
  2229
                        // Terminology:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2230
                        //    Parameterized type: List<String>
06bc494ca11e Initial load
duke
parents:
diff changeset
  2231
                        //    Generic type declaration: class List<E> { ... }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2232
                        // So t corresponds to List<String> and
06bc494ca11e Initial load
duke
parents:
diff changeset
  2233
                        // t.tsym.type corresponds to List<E>.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2234
                        // The reason t must be parameterized type is
06bc494ca11e Initial load
duke
parents:
diff changeset
  2235
                        // that completion will happen as a side
06bc494ca11e Initial load
duke
parents:
diff changeset
  2236
                        // effect of calling
06bc494ca11e Initial load
duke
parents:
diff changeset
  2237
                        // ClassSymbol.getInterfaces.  Since
06bc494ca11e Initial load
duke
parents:
diff changeset
  2238
                        // t.interfaces_field is null after
06bc494ca11e Initial load
duke
parents:
diff changeset
  2239
                        // completion, we can assume that t is not the
06bc494ca11e Initial load
duke
parents:
diff changeset
  2240
                        // type of a class/interface declaration.
8032
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents: 7681
diff changeset
  2241
                        Assert.check(t != t.tsym.type, t);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2242
                        List<Type> actuals = t.allparams();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2243
                        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
  2244
                        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
  2245
                            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
  2246
                        } else if (formals.nonEmpty()) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2247
                            t.interfaces_field =
06bc494ca11e Initial load
duke
parents:
diff changeset
  2248
                                upperBounds(subst(interfaces, formals, actuals));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2249
                        }
514
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  2250
                        else {
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  2251
                            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
  2252
                        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2253
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2254
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2255
                return t.interfaces_field;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2256
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2257
06bc494ca11e Initial load
duke
parents:
diff changeset
  2258
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  2259
            public List<Type> visitTypeVar(TypeVar t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2260
                if (t.bound.isCompound())
06bc494ca11e Initial load
duke
parents:
diff changeset
  2261
                    return interfaces(t.bound);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2262
06bc494ca11e Initial load
duke
parents:
diff changeset
  2263
                if (t.bound.isInterface())
06bc494ca11e Initial load
duke
parents:
diff changeset
  2264
                    return List.of(t.bound);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2265
06bc494ca11e Initial load
duke
parents:
diff changeset
  2266
                return List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2267
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2268
        };
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2269
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14443
diff changeset
  2270
    public boolean isDirectSuperInterface(TypeSymbol isym, TypeSymbol origin) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14443
diff changeset
  2271
        for (Type i2 : interfaces(origin.type)) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14443
diff changeset
  2272
            if (isym == i2.tsym) return true;
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2273
        }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2274
        return false;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2275
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2276
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  2277
06bc494ca11e Initial load
duke
parents:
diff changeset
  2278
    // <editor-fold defaultstate="collapsed" desc="isDerivedRaw">
06bc494ca11e Initial load
duke
parents:
diff changeset
  2279
    Map<Type,Boolean> isDerivedRawCache = new HashMap<Type,Boolean>();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2280
06bc494ca11e Initial load
duke
parents:
diff changeset
  2281
    public boolean isDerivedRaw(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2282
        Boolean result = isDerivedRawCache.get(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2283
        if (result == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2284
            result = isDerivedRawInternal(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2285
            isDerivedRawCache.put(t, result);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2286
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2287
        return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2288
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2289
06bc494ca11e Initial load
duke
parents:
diff changeset
  2290
    public boolean isDerivedRawInternal(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2291
        if (t.isErroneous())
06bc494ca11e Initial load
duke
parents:
diff changeset
  2292
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2293
        return
06bc494ca11e Initial load
duke
parents:
diff changeset
  2294
            t.isRaw() ||
06bc494ca11e Initial load
duke
parents:
diff changeset
  2295
            supertype(t) != null && isDerivedRaw(supertype(t)) ||
06bc494ca11e Initial load
duke
parents:
diff changeset
  2296
            isDerivedRaw(interfaces(t));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2297
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2298
06bc494ca11e Initial load
duke
parents:
diff changeset
  2299
    public boolean isDerivedRaw(List<Type> ts) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2300
        List<Type> l = ts;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2301
        while (l.nonEmpty() && !isDerivedRaw(l.head)) l = l.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2302
        return l.nonEmpty();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2303
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2304
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  2305
06bc494ca11e Initial load
duke
parents:
diff changeset
  2306
    // <editor-fold defaultstate="collapsed" desc="setBounds">
06bc494ca11e Initial load
duke
parents:
diff changeset
  2307
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  2308
     * Set the bounds field of the given type variable to reflect a
06bc494ca11e Initial load
duke
parents:
diff changeset
  2309
     * (possibly multiple) list of bounds.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2310
     * @param t                 a type variable
06bc494ca11e Initial load
duke
parents:
diff changeset
  2311
     * @param bounds            the bounds, must be nonempty
06bc494ca11e Initial load
duke
parents:
diff changeset
  2312
     * @param supertype         is objectType if all bounds are interfaces,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2313
     *                          null otherwise.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2314
     */
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
  2315
    public void setBounds(TypeVar t, List<Type> bounds) {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
  2316
        setBounds(t, bounds, bounds.head.tsym.isInterface());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2317
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2318
06bc494ca11e Initial load
duke
parents:
diff changeset
  2319
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  2320
     * 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
  2321
     * third parameter is computed directly, as follows: if all
313b819dbcc2 6948381: javac Null Pointer Exception in Types.makeCompoundType
mcimadamore
parents: 5648
diff changeset
  2322
     * all bounds are interface types, the computed supertype is Object,
313b819dbcc2 6948381: javac Null Pointer Exception in Types.makeCompoundType
mcimadamore
parents: 5648
diff changeset
  2323
     * 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
  2324
     * 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
  2325
     * Note that this check might cause a symbol completion. Hence, this version of
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2326
     * setBounds may not be called during a classfile read.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2327
     */
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
  2328
    public void setBounds(TypeVar t, List<Type> bounds, boolean allInterfaces) {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
  2329
        t.bound = bounds.tail.isEmpty() ?
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
  2330
                bounds.head :
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
  2331
                makeCompoundType(bounds, allInterfaces);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2332
        t.rank_field = -1;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2333
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2334
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  2335
06bc494ca11e Initial load
duke
parents:
diff changeset
  2336
    // <editor-fold defaultstate="collapsed" desc="getBounds">
06bc494ca11e Initial load
duke
parents:
diff changeset
  2337
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  2338
     * Return list of bounds of the given type variable.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2339
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2340
    public List<Type> getBounds(TypeVar t) {
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
  2341
        if (t.bound.hasTag(NONE))
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14443
diff changeset
  2342
            return List.nil();
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14443
diff changeset
  2343
        else if (t.bound.isErroneous() || !t.bound.isCompound())
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2344
            return List.of(t.bound);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2345
        else if ((erasure(t).tsym.flags() & INTERFACE) == 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2346
            return interfaces(t).prepend(supertype(t));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2347
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
  2348
            // No superclass was given in bounds.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2349
            // In this case, supertype is Object, erasure is first interface.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2350
            return interfaces(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2351
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2352
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  2353
06bc494ca11e Initial load
duke
parents:
diff changeset
  2354
    // <editor-fold defaultstate="collapsed" desc="classBound">
06bc494ca11e Initial load
duke
parents:
diff changeset
  2355
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  2356
     * If the given type is a (possibly selected) type variable,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2357
     * return the bounding class of this type, otherwise return the
06bc494ca11e Initial load
duke
parents:
diff changeset
  2358
     * type itself.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2359
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2360
    public Type classBound(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2361
        return classBound.visit(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2362
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2363
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
  2364
        private UnaryVisitor<Type> classBound = new UnaryVisitor<Type>() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2365
06bc494ca11e Initial load
duke
parents:
diff changeset
  2366
            public Type visitType(Type t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2367
                return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2368
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2369
06bc494ca11e Initial load
duke
parents:
diff changeset
  2370
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  2371
            public Type visitClassType(ClassType t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2372
                Type outer1 = classBound(t.getEnclosingType());
06bc494ca11e Initial load
duke
parents:
diff changeset
  2373
                if (outer1 != t.getEnclosingType())
06bc494ca11e Initial load
duke
parents:
diff changeset
  2374
                    return new ClassType(outer1, t.getTypeArguments(), t.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2375
                else
06bc494ca11e Initial load
duke
parents:
diff changeset
  2376
                    return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2377
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2378
06bc494ca11e Initial load
duke
parents:
diff changeset
  2379
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  2380
            public Type visitTypeVar(TypeVar t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2381
                return classBound(supertype(t));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2382
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2383
06bc494ca11e Initial load
duke
parents:
diff changeset
  2384
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  2385
            public Type visitErrorType(ErrorType t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2386
                return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2387
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2388
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
  2389
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  2390
06bc494ca11e Initial load
duke
parents:
diff changeset
  2391
    // <editor-fold defaultstate="collapsed" desc="sub signature / override equivalence">
06bc494ca11e Initial load
duke
parents:
diff changeset
  2392
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  2393
     * Returns true iff the first signature is a <em>sub
06bc494ca11e Initial load
duke
parents:
diff changeset
  2394
     * signature</em> of the other.  This is <b>not</b> an equivalence
06bc494ca11e Initial load
duke
parents:
diff changeset
  2395
     * relation.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2396
     *
9303
eae35c201e19 7032975: API files in javax.annotation.processing need to be updated for references to JLS
jjh
parents: 9301
diff changeset
  2397
     * @jls section 8.4.2.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2398
     * @see #overrideEquivalent(Type t, Type s)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2399
     * @param t first signature (possibly raw).
06bc494ca11e Initial load
duke
parents:
diff changeset
  2400
     * @param s second signature (could be subjected to erasure).
06bc494ca11e Initial load
duke
parents:
diff changeset
  2401
     * @return true if t is a sub signature of s.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2402
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2403
    public boolean isSubSignature(Type t, Type s) {
8628
9e31f1c01bd4 7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents: 8625
diff changeset
  2404
        return isSubSignature(t, s, true);
9e31f1c01bd4 7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents: 8625
diff changeset
  2405
    }
9e31f1c01bd4 7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents: 8625
diff changeset
  2406
9e31f1c01bd4 7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents: 8625
diff changeset
  2407
    public boolean isSubSignature(Type t, Type s, boolean strict) {
9e31f1c01bd4 7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents: 8625
diff changeset
  2408
        return hasSameArgs(t, s, strict) || hasSameArgs(t, erasure(s), strict);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2409
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2410
06bc494ca11e Initial load
duke
parents:
diff changeset
  2411
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  2412
     * Returns true iff these signatures are related by <em>override
06bc494ca11e Initial load
duke
parents:
diff changeset
  2413
     * equivalence</em>.  This is the natural extension of
06bc494ca11e Initial load
duke
parents:
diff changeset
  2414
     * isSubSignature to an equivalence relation.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2415
     *
9303
eae35c201e19 7032975: API files in javax.annotation.processing need to be updated for references to JLS
jjh
parents: 9301
diff changeset
  2416
     * @jls section 8.4.2.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2417
     * @see #isSubSignature(Type t, Type s)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2418
     * @param t a signature (possible raw, could be subjected to
06bc494ca11e Initial load
duke
parents:
diff changeset
  2419
     * erasure).
06bc494ca11e Initial load
duke
parents:
diff changeset
  2420
     * @param s a signature (possible raw, could be subjected to
06bc494ca11e Initial load
duke
parents:
diff changeset
  2421
     * erasure).
06bc494ca11e Initial load
duke
parents:
diff changeset
  2422
     * @return true if either argument is a sub signature of the other.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2423
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2424
    public boolean overrideEquivalent(Type t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2425
        return hasSameArgs(t, s) ||
06bc494ca11e Initial load
duke
parents:
diff changeset
  2426
            hasSameArgs(t, erasure(s)) || hasSameArgs(erasure(t), s);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2427
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2428
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2429
    public boolean overridesObjectMethod(TypeSymbol origin, Symbol msym) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2430
        for (Scope.Entry e = syms.objectType.tsym.members().lookup(msym.name) ; e.scope != null ; e = e.next()) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2431
            if (msym.overrides(e.sym, origin, Types.this, true)) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2432
                return true;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2433
            }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2434
        }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2435
        return false;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2436
    }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2437
6591
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2438
    // <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
  2439
    class ImplementationCache {
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2440
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2441
        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
  2442
                new WeakHashMap<MethodSymbol, SoftReference<Map<TypeSymbol, Entry>>>();
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2443
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2444
        class Entry {
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2445
            final MethodSymbol cachedImpl;
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2446
            final Filter<Symbol> implFilter;
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2447
            final boolean checkResult;
8427
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents: 8242
diff changeset
  2448
            final int prevMark;
6591
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2449
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2450
            public Entry(MethodSymbol cachedImpl,
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2451
                    Filter<Symbol> scopeFilter,
8427
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents: 8242
diff changeset
  2452
                    boolean checkResult,
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents: 8242
diff changeset
  2453
                    int prevMark) {
6591
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2454
                this.cachedImpl = cachedImpl;
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2455
                this.implFilter = scopeFilter;
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2456
                this.checkResult = checkResult;
8427
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents: 8242
diff changeset
  2457
                this.prevMark = prevMark;
6591
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2458
            }
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2459
8427
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents: 8242
diff changeset
  2460
            boolean matches(Filter<Symbol> scopeFilter, boolean checkResult, int mark) {
6591
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2461
                return this.implFilter == scopeFilter &&
8427
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents: 8242
diff changeset
  2462
                        this.checkResult == checkResult &&
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents: 8242
diff changeset
  2463
                        this.prevMark == mark;
6591
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2464
            }
3539
3c265cb6f4e0 6827648: Extremely slow compilation time for visitor pattern code + generics
mcimadamore
parents: 3140
diff changeset
  2465
        }
6591
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2466
8242
3873b4aaf4a8 7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents: 8230
diff changeset
  2467
        MethodSymbol get(MethodSymbol ms, TypeSymbol origin, boolean checkResult, Filter<Symbol> implFilter) {
6591
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2468
            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
  2469
            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
  2470
            if (cache == null) {
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2471
                cache = new HashMap<TypeSymbol, Entry>();
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2472
                _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
  2473
            }
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2474
            Entry e = cache.get(origin);
9812
f716e42cb230 7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents: 9721
diff changeset
  2475
            CompoundScope members = membersClosure(origin.type, true);
6591
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2476
            if (e == null ||
8427
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents: 8242
diff changeset
  2477
                    !e.matches(implFilter, checkResult, members.getMark())) {
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents: 8242
diff changeset
  2478
                MethodSymbol impl = implementationInternal(ms, origin, checkResult, implFilter);
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents: 8242
diff changeset
  2479
                cache.put(origin, new Entry(impl, implFilter, checkResult, members.getMark()));
6591
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2480
                return impl;
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2481
            }
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2482
            else {
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2483
                return e.cachedImpl;
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2484
            }
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2485
        }
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2486
8427
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents: 8242
diff changeset
  2487
        private MethodSymbol implementationInternal(MethodSymbol ms, TypeSymbol origin, boolean checkResult, Filter<Symbol> implFilter) {
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents: 8242
diff changeset
  2488
            for (Type t = origin.type; t.tag == CLASS || t.tag == TYPEVAR; t = supertype(t)) {
3539
3c265cb6f4e0 6827648: Extremely slow compilation time for visitor pattern code + generics
mcimadamore
parents: 3140
diff changeset
  2489
                while (t.tag == TYPEVAR)
3c265cb6f4e0 6827648: Extremely slow compilation time for visitor pattern code + generics
mcimadamore
parents: 3140
diff changeset
  2490
                    t = t.getUpperBound();
3c265cb6f4e0 6827648: Extremely slow compilation time for visitor pattern code + generics
mcimadamore
parents: 3140
diff changeset
  2491
                TypeSymbol c = t.tsym;
6591
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2492
                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
  2493
                     e.scope != null;
7628
e7baeb97d164 6476118: compiler bug causes runtime ClassCastException for generics overloading
mcimadamore
parents: 7627
diff changeset
  2494
                     e = e.next(implFilter)) {
6591
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2495
                    if (e.sym != null &&
8427
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents: 8242
diff changeset
  2496
                             e.sym.overrides(ms, origin, Types.this, checkResult))
6591
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2497
                        return (MethodSymbol)e.sym;
3539
3c265cb6f4e0 6827648: Extremely slow compilation time for visitor pattern code + generics
mcimadamore
parents: 3140
diff changeset
  2498
                }
3c265cb6f4e0 6827648: Extremely slow compilation time for visitor pattern code + generics
mcimadamore
parents: 3140
diff changeset
  2499
            }
6591
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2500
            return null;
3539
3c265cb6f4e0 6827648: Extremely slow compilation time for visitor pattern code + generics
mcimadamore
parents: 3140
diff changeset
  2501
        }
3c265cb6f4e0 6827648: Extremely slow compilation time for visitor pattern code + generics
mcimadamore
parents: 3140
diff changeset
  2502
    }
3c265cb6f4e0 6827648: Extremely slow compilation time for visitor pattern code + generics
mcimadamore
parents: 3140
diff changeset
  2503
6591
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2504
    private ImplementationCache implCache = new ImplementationCache();
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2505
8242
3873b4aaf4a8 7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents: 8230
diff changeset
  2506
    public MethodSymbol implementation(MethodSymbol ms, TypeSymbol origin, boolean checkResult, Filter<Symbol> implFilter) {
3873b4aaf4a8 7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents: 8230
diff changeset
  2507
        return implCache.get(ms, origin, checkResult, implFilter);
3873b4aaf4a8 7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents: 8230
diff changeset
  2508
    }
3873b4aaf4a8 7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents: 8230
diff changeset
  2509
    // </editor-fold>
3873b4aaf4a8 7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents: 8230
diff changeset
  2510
3873b4aaf4a8 7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents: 8230
diff changeset
  2511
    // <editor-fold defaultstate="collapsed" desc="compute transitive closure of all members in given site">
9812
f716e42cb230 7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents: 9721
diff changeset
  2512
    class MembersClosureCache extends SimpleVisitor<CompoundScope, Boolean> {
f716e42cb230 7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents: 9721
diff changeset
  2513
f716e42cb230 7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents: 9721
diff changeset
  2514
        private WeakHashMap<TypeSymbol, Entry> _map =
f716e42cb230 7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents: 9721
diff changeset
  2515
                new WeakHashMap<TypeSymbol, Entry>();
f716e42cb230 7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents: 9721
diff changeset
  2516
f716e42cb230 7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents: 9721
diff changeset
  2517
        class Entry {
f716e42cb230 7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents: 9721
diff changeset
  2518
            final boolean skipInterfaces;
f716e42cb230 7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents: 9721
diff changeset
  2519
            final CompoundScope compoundScope;
f716e42cb230 7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents: 9721
diff changeset
  2520
f716e42cb230 7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents: 9721
diff changeset
  2521
            public Entry(boolean skipInterfaces, CompoundScope compoundScope) {
f716e42cb230 7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents: 9721
diff changeset
  2522
                this.skipInterfaces = skipInterfaces;
f716e42cb230 7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents: 9721
diff changeset
  2523
                this.compoundScope = compoundScope;
f716e42cb230 7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents: 9721
diff changeset
  2524
            }
f716e42cb230 7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents: 9721
diff changeset
  2525
f716e42cb230 7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents: 9721
diff changeset
  2526
            boolean matches(boolean skipInterfaces) {
f716e42cb230 7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents: 9721
diff changeset
  2527
                return this.skipInterfaces == skipInterfaces;
f716e42cb230 7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents: 9721
diff changeset
  2528
            }
f716e42cb230 7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents: 9721
diff changeset
  2529
        }
f716e42cb230 7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents: 9721
diff changeset
  2530
10453
41fe3e16698a 7079713: javac hangs when compiling a class that references a cyclically inherited class
mcimadamore
parents: 10452
diff changeset
  2531
        List<TypeSymbol> seenTypes = List.nil();
41fe3e16698a 7079713: javac hangs when compiling a class that references a cyclically inherited class
mcimadamore
parents: 10452
diff changeset
  2532
9812
f716e42cb230 7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents: 9721
diff changeset
  2533
        /** members closure visitor methods **/
f716e42cb230 7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents: 9721
diff changeset
  2534
f716e42cb230 7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents: 9721
diff changeset
  2535
        public CompoundScope visitType(Type t, Boolean skipInterface) {
8242
3873b4aaf4a8 7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents: 8230
diff changeset
  2536
            return null;
3873b4aaf4a8 7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents: 8230
diff changeset
  2537
        }
3873b4aaf4a8 7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents: 8230
diff changeset
  2538
3873b4aaf4a8 7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents: 8230
diff changeset
  2539
        @Override
9812
f716e42cb230 7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents: 9721
diff changeset
  2540
        public CompoundScope visitClassType(ClassType t, Boolean skipInterface) {
10453
41fe3e16698a 7079713: javac hangs when compiling a class that references a cyclically inherited class
mcimadamore
parents: 10452
diff changeset
  2541
            if (seenTypes.contains(t.tsym)) {
41fe3e16698a 7079713: javac hangs when compiling a class that references a cyclically inherited class
mcimadamore
parents: 10452
diff changeset
  2542
                //this is possible when an interface is implemented in multiple
41fe3e16698a 7079713: javac hangs when compiling a class that references a cyclically inherited class
mcimadamore
parents: 10452
diff changeset
  2543
                //superclasses, or when a classs hierarchy is circular - in such
41fe3e16698a 7079713: javac hangs when compiling a class that references a cyclically inherited class
mcimadamore
parents: 10452
diff changeset
  2544
                //cases we don't need to recurse (empty scope is returned)
41fe3e16698a 7079713: javac hangs when compiling a class that references a cyclically inherited class
mcimadamore
parents: 10452
diff changeset
  2545
                return new CompoundScope(t.tsym);
41fe3e16698a 7079713: javac hangs when compiling a class that references a cyclically inherited class
mcimadamore
parents: 10452
diff changeset
  2546
            }
41fe3e16698a 7079713: javac hangs when compiling a class that references a cyclically inherited class
mcimadamore
parents: 10452
diff changeset
  2547
            try {
41fe3e16698a 7079713: javac hangs when compiling a class that references a cyclically inherited class
mcimadamore
parents: 10452
diff changeset
  2548
                seenTypes = seenTypes.prepend(t.tsym);
41fe3e16698a 7079713: javac hangs when compiling a class that references a cyclically inherited class
mcimadamore
parents: 10452
diff changeset
  2549
                ClassSymbol csym = (ClassSymbol)t.tsym;
41fe3e16698a 7079713: javac hangs when compiling a class that references a cyclically inherited class
mcimadamore
parents: 10452
diff changeset
  2550
                Entry e = _map.get(csym);
41fe3e16698a 7079713: javac hangs when compiling a class that references a cyclically inherited class
mcimadamore
parents: 10452
diff changeset
  2551
                if (e == null || !e.matches(skipInterface)) {
41fe3e16698a 7079713: javac hangs when compiling a class that references a cyclically inherited class
mcimadamore
parents: 10452
diff changeset
  2552
                    CompoundScope membersClosure = new CompoundScope(csym);
41fe3e16698a 7079713: javac hangs when compiling a class that references a cyclically inherited class
mcimadamore
parents: 10452
diff changeset
  2553
                    if (!skipInterface) {
41fe3e16698a 7079713: javac hangs when compiling a class that references a cyclically inherited class
mcimadamore
parents: 10452
diff changeset
  2554
                        for (Type i : interfaces(t)) {
41fe3e16698a 7079713: javac hangs when compiling a class that references a cyclically inherited class
mcimadamore
parents: 10452
diff changeset
  2555
                            membersClosure.addSubScope(visit(i, skipInterface));
41fe3e16698a 7079713: javac hangs when compiling a class that references a cyclically inherited class
mcimadamore
parents: 10452
diff changeset
  2556
                        }
9812
f716e42cb230 7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents: 9721
diff changeset
  2557
                    }
10453
41fe3e16698a 7079713: javac hangs when compiling a class that references a cyclically inherited class
mcimadamore
parents: 10452
diff changeset
  2558
                    membersClosure.addSubScope(visit(supertype(t), skipInterface));
41fe3e16698a 7079713: javac hangs when compiling a class that references a cyclically inherited class
mcimadamore
parents: 10452
diff changeset
  2559
                    membersClosure.addSubScope(csym.members());
41fe3e16698a 7079713: javac hangs when compiling a class that references a cyclically inherited class
mcimadamore
parents: 10452
diff changeset
  2560
                    e = new Entry(skipInterface, membersClosure);
41fe3e16698a 7079713: javac hangs when compiling a class that references a cyclically inherited class
mcimadamore
parents: 10452
diff changeset
  2561
                    _map.put(csym, e);
8242
3873b4aaf4a8 7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents: 8230
diff changeset
  2562
                }
10453
41fe3e16698a 7079713: javac hangs when compiling a class that references a cyclically inherited class
mcimadamore
parents: 10452
diff changeset
  2563
                return e.compoundScope;
8242
3873b4aaf4a8 7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents: 8230
diff changeset
  2564
            }
10453
41fe3e16698a 7079713: javac hangs when compiling a class that references a cyclically inherited class
mcimadamore
parents: 10452
diff changeset
  2565
            finally {
41fe3e16698a 7079713: javac hangs when compiling a class that references a cyclically inherited class
mcimadamore
parents: 10452
diff changeset
  2566
                seenTypes = seenTypes.tail;
41fe3e16698a 7079713: javac hangs when compiling a class that references a cyclically inherited class
mcimadamore
parents: 10452
diff changeset
  2567
            }
8242
3873b4aaf4a8 7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents: 8230
diff changeset
  2568
        }
3873b4aaf4a8 7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents: 8230
diff changeset
  2569
3873b4aaf4a8 7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents: 8230
diff changeset
  2570
        @Override
9812
f716e42cb230 7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents: 9721
diff changeset
  2571
        public CompoundScope visitTypeVar(TypeVar t, Boolean skipInterface) {
f716e42cb230 7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents: 9721
diff changeset
  2572
            return visit(t.getUpperBound(), skipInterface);
8242
3873b4aaf4a8 7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents: 8230
diff changeset
  2573
        }
9812
f716e42cb230 7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents: 9721
diff changeset
  2574
    }
f716e42cb230 7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents: 9721
diff changeset
  2575
f716e42cb230 7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents: 9721
diff changeset
  2576
    private MembersClosureCache membersCache = new MembersClosureCache();
f716e42cb230 7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents: 9721
diff changeset
  2577
f716e42cb230 7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents: 9721
diff changeset
  2578
    public CompoundScope membersClosure(Type site, boolean skipInterface) {
f716e42cb230 7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents: 9721
diff changeset
  2579
        return membersCache.visit(site, skipInterface);
f716e42cb230 7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents: 9721
diff changeset
  2580
    }
6591
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2581
    // </editor-fold>
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2582
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2583
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2584
    //where
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2585
    public List<MethodSymbol> interfaceCandidates(Type site, MethodSymbol ms) {
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14443
diff changeset
  2586
        Filter<Symbol> filter = new MethodFilter(ms, site);
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2587
        List<MethodSymbol> candidates = List.nil();
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2588
        for (Symbol s : membersClosure(site, false).getElements(filter)) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2589
            if (!site.tsym.isInterface() && !s.owner.isInterface()) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2590
                return List.of((MethodSymbol)s);
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2591
            } else if (!candidates.contains(s)) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2592
                candidates = candidates.prepend((MethodSymbol)s);
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2593
            }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2594
        }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2595
        return prune(candidates, ownerComparator);
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2596
    }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2597
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2598
    public List<MethodSymbol> prune(List<MethodSymbol> methods, Comparator<MethodSymbol> cmp) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2599
        ListBuffer<MethodSymbol> methodsMin = ListBuffer.lb();
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2600
        for (MethodSymbol m1 : methods) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2601
            boolean isMin_m1 = true;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2602
            for (MethodSymbol m2 : methods) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2603
                if (m1 == m2) continue;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2604
                if (cmp.compare(m2, m1) < 0) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2605
                    isMin_m1 = false;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2606
                    break;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2607
                }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2608
            }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2609
            if (isMin_m1)
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2610
                methodsMin.append(m1);
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2611
        }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2612
        return methodsMin.toList();
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2613
    }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2614
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2615
    Comparator<MethodSymbol> ownerComparator = new Comparator<MethodSymbol>() {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2616
        public int compare(MethodSymbol s1, MethodSymbol s2) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2617
            return s1.owner.isSubClass(s2.owner, Types.this) ? -1 : 1;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2618
        }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2619
    };
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2620
    // where
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2621
            private class MethodFilter implements Filter<Symbol> {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2622
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2623
                Symbol msym;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2624
                Type site;
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14443
diff changeset
  2625
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14443
diff changeset
  2626
                MethodFilter(Symbol msym, Type site) {
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2627
                    this.msym = msym;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2628
                    this.site = site;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2629
                }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2630
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2631
                public boolean accepts(Symbol s) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2632
                    return s.kind == Kinds.MTH &&
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2633
                            s.name == msym.name &&
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2634
                            s.isInheritedIn(site.tsym, Types.this) &&
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2635
                            overrideEquivalent(memberType(site, s), memberType(site, msym));
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2636
                }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2637
            };
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2638
    // </editor-fold>
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2639
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2640
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  2641
     * Does t have the same arguments as s?  It is assumed that both
06bc494ca11e Initial load
duke
parents:
diff changeset
  2642
     * types are (possibly polymorphic) method types.  Monomorphic
06bc494ca11e Initial load
duke
parents:
diff changeset
  2643
     * method types "have the same arguments", if their argument lists
06bc494ca11e Initial load
duke
parents:
diff changeset
  2644
     * are equal.  Polymorphic method types "have the same arguments",
06bc494ca11e Initial load
duke
parents:
diff changeset
  2645
     * if they have the same arguments after renaming all type
06bc494ca11e Initial load
duke
parents:
diff changeset
  2646
     * variables of one to corresponding type variables in the other,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2647
     * where correspondence is by position in the type parameter list.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2648
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2649
    public boolean hasSameArgs(Type t, Type s) {
8628
9e31f1c01bd4 7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents: 8625
diff changeset
  2650
        return hasSameArgs(t, s, true);
9e31f1c01bd4 7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents: 8625
diff changeset
  2651
    }
9e31f1c01bd4 7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents: 8625
diff changeset
  2652
9e31f1c01bd4 7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents: 8625
diff changeset
  2653
    public boolean hasSameArgs(Type t, Type s, boolean strict) {
9e31f1c01bd4 7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents: 8625
diff changeset
  2654
        return hasSameArgs(t, s, strict ? hasSameArgs_strict : hasSameArgs_nonstrict);
9e31f1c01bd4 7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents: 8625
diff changeset
  2655
    }
9e31f1c01bd4 7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents: 8625
diff changeset
  2656
9e31f1c01bd4 7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents: 8625
diff changeset
  2657
    private boolean hasSameArgs(Type t, Type s, TypeRelation hasSameArgs) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2658
        return hasSameArgs.visit(t, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2659
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2660
    // where
8628
9e31f1c01bd4 7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents: 8625
diff changeset
  2661
        private class HasSameArgs extends TypeRelation {
9e31f1c01bd4 7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents: 8625
diff changeset
  2662
9e31f1c01bd4 7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents: 8625
diff changeset
  2663
            boolean strict;
9e31f1c01bd4 7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents: 8625
diff changeset
  2664
9e31f1c01bd4 7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents: 8625
diff changeset
  2665
            public HasSameArgs(boolean strict) {
9e31f1c01bd4 7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents: 8625
diff changeset
  2666
                this.strict = strict;
9e31f1c01bd4 7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents: 8625
diff changeset
  2667
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2668
06bc494ca11e Initial load
duke
parents:
diff changeset
  2669
            public Boolean visitType(Type t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2670
                throw new AssertionError();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2671
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2672
06bc494ca11e Initial load
duke
parents:
diff changeset
  2673
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  2674
            public Boolean visitMethodType(MethodType t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2675
                return s.tag == METHOD
06bc494ca11e Initial load
duke
parents:
diff changeset
  2676
                    && containsTypeEquivalent(t.argtypes, s.getParameterTypes());
06bc494ca11e Initial load
duke
parents:
diff changeset
  2677
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2678
06bc494ca11e Initial load
duke
parents:
diff changeset
  2679
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  2680
            public Boolean visitForAll(ForAll t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2681
                if (s.tag != FORALL)
8628
9e31f1c01bd4 7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents: 8625
diff changeset
  2682
                    return strict ? false : visitMethodType(t.asMethodType(), s);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2683
06bc494ca11e Initial load
duke
parents:
diff changeset
  2684
                ForAll forAll = (ForAll)s;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2685
                return hasSameBounds(t, forAll)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2686
                    && visit(t.qtype, subst(forAll.qtype, forAll.tvars, t.tvars));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2687
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2688
06bc494ca11e Initial load
duke
parents:
diff changeset
  2689
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  2690
            public Boolean visitErrorType(ErrorType t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2691
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2692
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2693
        };
8628
9e31f1c01bd4 7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents: 8625
diff changeset
  2694
9e31f1c01bd4 7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents: 8625
diff changeset
  2695
        TypeRelation hasSameArgs_strict = new HasSameArgs(true);
9e31f1c01bd4 7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents: 8625
diff changeset
  2696
        TypeRelation hasSameArgs_nonstrict = new HasSameArgs(false);
9e31f1c01bd4 7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents: 8625
diff changeset
  2697
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2698
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  2699
06bc494ca11e Initial load
duke
parents:
diff changeset
  2700
    // <editor-fold defaultstate="collapsed" desc="subst">
06bc494ca11e Initial load
duke
parents:
diff changeset
  2701
    public List<Type> subst(List<Type> ts,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2702
                            List<Type> from,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2703
                            List<Type> to) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2704
        return new Subst(from, to).subst(ts);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2705
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2706
06bc494ca11e Initial load
duke
parents:
diff changeset
  2707
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  2708
     * Substitute all occurrences of a type in `from' with the
06bc494ca11e Initial load
duke
parents:
diff changeset
  2709
     * corresponding type in `to' in 't'. Match lists `from' and `to'
06bc494ca11e Initial load
duke
parents:
diff changeset
  2710
     * from the right: If lists have different length, discard leading
06bc494ca11e Initial load
duke
parents:
diff changeset
  2711
     * elements of the longer list.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2712
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2713
    public Type subst(Type t, List<Type> from, List<Type> to) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2714
        return new Subst(from, to).subst(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2715
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2716
06bc494ca11e Initial load
duke
parents:
diff changeset
  2717
    private class Subst extends UnaryVisitor<Type> {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2718
        List<Type> from;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2719
        List<Type> to;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2720
06bc494ca11e Initial load
duke
parents:
diff changeset
  2721
        public Subst(List<Type> from, List<Type> to) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2722
            int fromLength = from.length();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2723
            int toLength = to.length();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2724
            while (fromLength > toLength) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2725
                fromLength--;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2726
                from = from.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2727
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2728
            while (fromLength < toLength) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2729
                toLength--;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2730
                to = to.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2731
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2732
            this.from = from;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2733
            this.to = to;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2734
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2735
06bc494ca11e Initial load
duke
parents:
diff changeset
  2736
        Type subst(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2737
            if (from.tail == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2738
                return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2739
            else
06bc494ca11e Initial load
duke
parents:
diff changeset
  2740
                return visit(t);
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents: 2218
diff changeset
  2741
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2742
06bc494ca11e Initial load
duke
parents:
diff changeset
  2743
        List<Type> subst(List<Type> ts) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2744
            if (from.tail == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2745
                return ts;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2746
            boolean wild = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2747
            if (ts.nonEmpty() && from.nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2748
                Type head1 = subst(ts.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2749
                List<Type> tail1 = subst(ts.tail);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2750
                if (head1 != ts.head || tail1 != ts.tail)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2751
                    return tail1.prepend(head1);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2752
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2753
            return ts;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2754
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2755
06bc494ca11e Initial load
duke
parents:
diff changeset
  2756
        public Type visitType(Type t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2757
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2758
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2759
06bc494ca11e Initial load
duke
parents:
diff changeset
  2760
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  2761
        public Type visitMethodType(MethodType t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2762
            List<Type> argtypes = subst(t.argtypes);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2763
            Type restype = subst(t.restype);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2764
            List<Type> thrown = subst(t.thrown);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2765
            if (argtypes == t.argtypes &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  2766
                restype == t.restype &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  2767
                thrown == t.thrown)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2768
                return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2769
            else
06bc494ca11e Initial load
duke
parents:
diff changeset
  2770
                return new MethodType(argtypes, restype, thrown, t.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2771
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2772
06bc494ca11e Initial load
duke
parents:
diff changeset
  2773
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  2774
        public Type visitTypeVar(TypeVar t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2775
            for (List<Type> from = this.from, to = this.to;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2776
                 from.nonEmpty();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2777
                 from = from.tail, to = to.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2778
                if (t == from.head) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2779
                    return to.head.withTypeVar(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2780
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2781
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2782
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2783
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2784
06bc494ca11e Initial load
duke
parents:
diff changeset
  2785
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  2786
        public Type visitClassType(ClassType t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2787
            if (!t.isCompound()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2788
                List<Type> typarams = t.getTypeArguments();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2789
                List<Type> typarams1 = subst(typarams);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2790
                Type outer = t.getEnclosingType();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2791
                Type outer1 = subst(outer);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2792
                if (typarams1 == typarams && outer1 == outer)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2793
                    return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2794
                else
06bc494ca11e Initial load
duke
parents:
diff changeset
  2795
                    return new ClassType(outer1, typarams1, t.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2796
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2797
                Type st = subst(supertype(t));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2798
                List<Type> is = upperBounds(subst(interfaces(t)));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2799
                if (st == supertype(t) && is == interfaces(t))
06bc494ca11e Initial load
duke
parents:
diff changeset
  2800
                    return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2801
                else
06bc494ca11e Initial load
duke
parents:
diff changeset
  2802
                    return makeCompoundType(is.prepend(st));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2803
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2804
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2805
06bc494ca11e Initial load
duke
parents:
diff changeset
  2806
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  2807
        public Type visitWildcardType(WildcardType t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2808
            Type bound = t.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2809
            if (t.kind != BoundKind.UNBOUND)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2810
                bound = subst(bound);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2811
            if (bound == t.type) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2812
                return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2813
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2814
                if (t.isExtendsBound() && bound.isExtendsBound())
06bc494ca11e Initial load
duke
parents:
diff changeset
  2815
                    bound = upperBound(bound);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2816
                return new WildcardType(bound, t.kind, syms.boundClass, t.bound);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2817
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2818
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2819
06bc494ca11e Initial load
duke
parents:
diff changeset
  2820
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  2821
        public Type visitArrayType(ArrayType t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2822
            Type elemtype = subst(t.elemtype);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2823
            if (elemtype == t.elemtype)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2824
                return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2825
            else
9607
94b7adcaccad 7040883: Compilation error: "length in Array is defined in an inaccessible class or interface"
mcimadamore
parents: 9602
diff changeset
  2826
                return new ArrayType(upperBound(elemtype), t.tsym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2827
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2828
06bc494ca11e Initial load
duke
parents:
diff changeset
  2829
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  2830
        public Type visitForAll(ForAll t, Void ignored) {
8230
b761a23e78d8 6838943: inference: javac is not handling type-variable substitution properly
mcimadamore
parents: 8032
diff changeset
  2831
            if (Type.containsAny(to, t.tvars)) {
b761a23e78d8 6838943: inference: javac is not handling type-variable substitution properly
mcimadamore
parents: 8032
diff changeset
  2832
                //perform alpha-renaming of free-variables in 't'
b761a23e78d8 6838943: inference: javac is not handling type-variable substitution properly
mcimadamore
parents: 8032
diff changeset
  2833
                //if 'to' types contain variables that are free in 't'
b761a23e78d8 6838943: inference: javac is not handling type-variable substitution properly
mcimadamore
parents: 8032
diff changeset
  2834
                List<Type> freevars = newInstances(t.tvars);
b761a23e78d8 6838943: inference: javac is not handling type-variable substitution properly
mcimadamore
parents: 8032
diff changeset
  2835
                t = new ForAll(freevars,
b761a23e78d8 6838943: inference: javac is not handling type-variable substitution properly
mcimadamore
parents: 8032
diff changeset
  2836
                        Types.this.subst(t.qtype, t.tvars, freevars));
b761a23e78d8 6838943: inference: javac is not handling type-variable substitution properly
mcimadamore
parents: 8032
diff changeset
  2837
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2838
            List<Type> tvars1 = substBounds(t.tvars, from, to);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2839
            Type qtype1 = subst(t.qtype);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2840
            if (tvars1 == t.tvars && qtype1 == t.qtype) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2841
                return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2842
            } else if (tvars1 == t.tvars) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2843
                return new ForAll(tvars1, qtype1);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2844
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2845
                return new ForAll(tvars1, Types.this.subst(qtype1, t.tvars, tvars1));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2846
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2847
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2848
06bc494ca11e Initial load
duke
parents:
diff changeset
  2849
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  2850
        public Type visitErrorType(ErrorType t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2851
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2852
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2853
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2854
06bc494ca11e Initial load
duke
parents:
diff changeset
  2855
    public List<Type> substBounds(List<Type> tvars,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2856
                                  List<Type> from,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2857
                                  List<Type> to) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2858
        if (tvars.isEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
  2859
            return tvars;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2860
        ListBuffer<Type> newBoundsBuf = lb();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2861
        boolean changed = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2862
        // calculate new bounds
06bc494ca11e Initial load
duke
parents:
diff changeset
  2863
        for (Type t : tvars) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2864
            TypeVar tv = (TypeVar) t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2865
            Type bound = subst(tv.bound, from, to);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2866
            if (bound != tv.bound)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2867
                changed = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2868
            newBoundsBuf.append(bound);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2869
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2870
        if (!changed)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2871
            return tvars;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2872
        ListBuffer<Type> newTvars = lb();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2873
        // create new type variables without bounds
06bc494ca11e Initial load
duke
parents:
diff changeset
  2874
        for (Type t : tvars) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2875
            newTvars.append(new TypeVar(t.tsym, null, syms.botType));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2876
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2877
        // the new bounds should use the new type variables in place
06bc494ca11e Initial load
duke
parents:
diff changeset
  2878
        // of the old
06bc494ca11e Initial load
duke
parents:
diff changeset
  2879
        List<Type> newBounds = newBoundsBuf.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2880
        from = tvars;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2881
        to = newTvars.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2882
        for (; !newBounds.isEmpty(); newBounds = newBounds.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2883
            newBounds.head = subst(newBounds.head, from, to);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2884
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2885
        newBounds = newBoundsBuf.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2886
        // set the bounds of new type variables to the new bounds
06bc494ca11e Initial load
duke
parents:
diff changeset
  2887
        for (Type t : newTvars.toList()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2888
            TypeVar tv = (TypeVar) t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2889
            tv.bound = newBounds.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2890
            newBounds = newBounds.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2891
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2892
        return newTvars.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2893
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2894
06bc494ca11e Initial load
duke
parents:
diff changeset
  2895
    public TypeVar substBound(TypeVar t, List<Type> from, List<Type> to) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2896
        Type bound1 = subst(t.bound, from, to);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2897
        if (bound1 == t.bound)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2898
            return t;
1993
9b37ef07ba64 6729401: Compiler error when using F-bounded generics with free type variables
mcimadamore
parents: 1992
diff changeset
  2899
        else {
9b37ef07ba64 6729401: Compiler error when using F-bounded generics with free type variables
mcimadamore
parents: 1992
diff changeset
  2900
            // create new type variable without bounds
9b37ef07ba64 6729401: Compiler error when using F-bounded generics with free type variables
mcimadamore
parents: 1992
diff changeset
  2901
            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
  2902
            // 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
  2903
            // of the old
9b37ef07ba64 6729401: Compiler error when using F-bounded generics with free type variables
mcimadamore
parents: 1992
diff changeset
  2904
            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
  2905
            return tv;
9b37ef07ba64 6729401: Compiler error when using F-bounded generics with free type variables
mcimadamore
parents: 1992
diff changeset
  2906
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2907
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2908
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  2909
06bc494ca11e Initial load
duke
parents:
diff changeset
  2910
    // <editor-fold defaultstate="collapsed" desc="hasSameBounds">
06bc494ca11e Initial load
duke
parents:
diff changeset
  2911
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  2912
     * Does t have the same bounds for quantified variables as s?
06bc494ca11e Initial load
duke
parents:
diff changeset
  2913
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2914
    boolean hasSameBounds(ForAll t, ForAll s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2915
        List<Type> l1 = t.tvars;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2916
        List<Type> l2 = s.tvars;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2917
        while (l1.nonEmpty() && l2.nonEmpty() &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  2918
               isSameType(l1.head.getUpperBound(),
06bc494ca11e Initial load
duke
parents:
diff changeset
  2919
                          subst(l2.head.getUpperBound(),
06bc494ca11e Initial load
duke
parents:
diff changeset
  2920
                                s.tvars,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2921
                                t.tvars))) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2922
            l1 = l1.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2923
            l2 = l2.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2924
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2925
        return l1.isEmpty() && l2.isEmpty();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2926
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2927
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  2928
06bc494ca11e Initial load
duke
parents:
diff changeset
  2929
    // <editor-fold defaultstate="collapsed" desc="newInstances">
06bc494ca11e Initial load
duke
parents:
diff changeset
  2930
    /** Create new vector of type variables from list of variables
06bc494ca11e Initial load
duke
parents:
diff changeset
  2931
     *  changing all recursive bounds from old to new list.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2932
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2933
    public List<Type> newInstances(List<Type> tvars) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2934
        List<Type> tvars1 = Type.map(tvars, newInstanceFun);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2935
        for (List<Type> l = tvars1; l.nonEmpty(); l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2936
            TypeVar tv = (TypeVar) l.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2937
            tv.bound = subst(tv.bound, tvars, tvars1);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2938
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2939
        return tvars1;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2940
    }
14801
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents: 14725
diff changeset
  2941
    private static final Mapping newInstanceFun = new Mapping("newInstanceFun") {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2942
            public Type apply(Type t) { return new TypeVar(t.tsym, t.getUpperBound(), t.getLowerBound()); }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2943
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
  2944
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  2945
8430
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  2946
    public Type createMethodTypeWithParameters(Type original, List<Type> newParams) {
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  2947
        return original.accept(methodWithParameters, newParams);
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  2948
    }
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  2949
    // where
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  2950
        private final MapVisitor<List<Type>> methodWithParameters = new MapVisitor<List<Type>>() {
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  2951
            public Type visitType(Type t, List<Type> newParams) {
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  2952
                throw new IllegalArgumentException("Not a method type: " + t);
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  2953
            }
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  2954
            public Type visitMethodType(MethodType t, List<Type> newParams) {
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  2955
                return new MethodType(newParams, t.restype, t.thrown, t.tsym);
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  2956
            }
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  2957
            public Type visitForAll(ForAll t, List<Type> newParams) {
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  2958
                return new ForAll(t.tvars, t.qtype.accept(this, newParams));
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  2959
            }
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  2960
        };
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  2961
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  2962
    public Type createMethodTypeWithThrown(Type original, List<Type> newThrown) {
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  2963
        return original.accept(methodWithThrown, newThrown);
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  2964
    }
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  2965
    // where
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  2966
        private final MapVisitor<List<Type>> methodWithThrown = new MapVisitor<List<Type>>() {
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  2967
            public Type visitType(Type t, List<Type> newThrown) {
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  2968
                throw new IllegalArgumentException("Not a method type: " + t);
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  2969
            }
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  2970
            public Type visitMethodType(MethodType t, List<Type> newThrown) {
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  2971
                return new MethodType(t.argtypes, t.restype, newThrown, t.tsym);
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  2972
            }
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  2973
            public Type visitForAll(ForAll t, List<Type> newThrown) {
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  2974
                return new ForAll(t.tvars, t.qtype.accept(this, newThrown));
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  2975
            }
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  2976
        };
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  2977
9075
cba34854a40e 7030150: Type inference for generic instance creation failed for formal type parameter
mcimadamore
parents: 8628
diff changeset
  2978
    public Type createMethodTypeWithReturn(Type original, Type newReturn) {
cba34854a40e 7030150: Type inference for generic instance creation failed for formal type parameter
mcimadamore
parents: 8628
diff changeset
  2979
        return original.accept(methodWithReturn, newReturn);
cba34854a40e 7030150: Type inference for generic instance creation failed for formal type parameter
mcimadamore
parents: 8628
diff changeset
  2980
    }
cba34854a40e 7030150: Type inference for generic instance creation failed for formal type parameter
mcimadamore
parents: 8628
diff changeset
  2981
    // where
cba34854a40e 7030150: Type inference for generic instance creation failed for formal type parameter
mcimadamore
parents: 8628
diff changeset
  2982
        private final MapVisitor<Type> methodWithReturn = new MapVisitor<Type>() {
cba34854a40e 7030150: Type inference for generic instance creation failed for formal type parameter
mcimadamore
parents: 8628
diff changeset
  2983
            public Type visitType(Type t, Type newReturn) {
cba34854a40e 7030150: Type inference for generic instance creation failed for formal type parameter
mcimadamore
parents: 8628
diff changeset
  2984
                throw new IllegalArgumentException("Not a method type: " + t);
cba34854a40e 7030150: Type inference for generic instance creation failed for formal type parameter
mcimadamore
parents: 8628
diff changeset
  2985
            }
cba34854a40e 7030150: Type inference for generic instance creation failed for formal type parameter
mcimadamore
parents: 8628
diff changeset
  2986
            public Type visitMethodType(MethodType t, Type newReturn) {
cba34854a40e 7030150: Type inference for generic instance creation failed for formal type parameter
mcimadamore
parents: 8628
diff changeset
  2987
                return new MethodType(t.argtypes, newReturn, t.thrown, t.tsym);
cba34854a40e 7030150: Type inference for generic instance creation failed for formal type parameter
mcimadamore
parents: 8628
diff changeset
  2988
            }
cba34854a40e 7030150: Type inference for generic instance creation failed for formal type parameter
mcimadamore
parents: 8628
diff changeset
  2989
            public Type visitForAll(ForAll t, Type newReturn) {
cba34854a40e 7030150: Type inference for generic instance creation failed for formal type parameter
mcimadamore
parents: 8628
diff changeset
  2990
                return new ForAll(t.tvars, t.qtype.accept(this, newReturn));
cba34854a40e 7030150: Type inference for generic instance creation failed for formal type parameter
mcimadamore
parents: 8628
diff changeset
  2991
            }
cba34854a40e 7030150: Type inference for generic instance creation failed for formal type parameter
mcimadamore
parents: 8628
diff changeset
  2992
        };
cba34854a40e 7030150: Type inference for generic instance creation failed for formal type parameter
mcimadamore
parents: 8628
diff changeset
  2993
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
  2994
    // <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
  2995
    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
  2996
        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
  2997
    }
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 1206
diff changeset
  2998
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 1206
diff changeset
  2999
    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
  3000
        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
  3001
    }
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 1206
diff changeset
  3002
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 1206
diff changeset
  3003
    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
  3004
        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
  3005
    }
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 1206
diff changeset
  3006
    // </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
  3007
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3008
    // <editor-fold defaultstate="collapsed" desc="rank">
06bc494ca11e Initial load
duke
parents:
diff changeset
  3009
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  3010
     * The rank of a class is the length of the longest path between
06bc494ca11e Initial load
duke
parents:
diff changeset
  3011
     * the class and java.lang.Object in the class inheritance
06bc494ca11e Initial load
duke
parents:
diff changeset
  3012
     * graph. Undefined for all but reference types.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3013
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3014
    public int rank(Type t) {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
  3015
        t = t.unannotatedType();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3016
        switch(t.tag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3017
        case CLASS: {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3018
            ClassType cls = (ClassType)t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3019
            if (cls.rank_field < 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3020
                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
  3021
                if (fullname == names.java_lang_Object)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3022
                    cls.rank_field = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3023
                else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3024
                    int r = rank(supertype(cls));
06bc494ca11e Initial load
duke
parents:
diff changeset
  3025
                    for (List<Type> l = interfaces(cls);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3026
                         l.nonEmpty();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3027
                         l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3028
                        if (rank(l.head) > r)
06bc494ca11e Initial load
duke
parents:
diff changeset
  3029
                            r = rank(l.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3030
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3031
                    cls.rank_field = r + 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3032
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3033
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3034
            return cls.rank_field;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3035
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3036
        case TYPEVAR: {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3037
            TypeVar tvar = (TypeVar)t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3038
            if (tvar.rank_field < 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3039
                int r = rank(supertype(tvar));
06bc494ca11e Initial load
duke
parents:
diff changeset
  3040
                for (List<Type> l = interfaces(tvar);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3041
                     l.nonEmpty();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3042
                     l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3043
                    if (rank(l.head) > r) r = rank(l.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3044
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3045
                tvar.rank_field = r + 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3046
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3047
            return tvar.rank_field;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3048
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3049
        case ERROR:
06bc494ca11e Initial load
duke
parents:
diff changeset
  3050
            return 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3051
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  3052
            throw new AssertionError();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3053
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3054
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3055
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  3056
1357
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  3057
    /**
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents: 2218
diff changeset
  3058
     * 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
  3059
     * accordingly to a given locale
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  3060
     */
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  3061
    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
  3062
        return Printer.createStandardPrinter(messages).visit(t, locale);
1357
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  3063
    }
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  3064
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  3065
    /**
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents: 2218
diff changeset
  3066
     * 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
  3067
     * accordingly to a given locale
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  3068
     */
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  3069
    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
  3070
        return Printer.createStandardPrinter(messages).visit(t, locale);
1357
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  3071
    }
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  3072
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3073
    // <editor-fold defaultstate="collapsed" desc="toString">
06bc494ca11e Initial load
duke
parents:
diff changeset
  3074
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  3075
     * 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
  3076
     *
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  3077
     * @deprecated Types.toString(Type t, Locale l) provides better support
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  3078
     * for localization
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3079
     */
1357
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  3080
    @Deprecated
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3081
    public String toString(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3082
        if (t.tag == FORALL) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3083
            ForAll forAll = (ForAll)t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3084
            return typaramsString(forAll.tvars) + forAll.qtype;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3085
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3086
        return "" + t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3087
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3088
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
  3089
        private String typaramsString(List<Type> tvars) {
8625
6b51ef804d49 6639645: Modeling type implementing missing interfaces
jjg
parents: 8617
diff changeset
  3090
            StringBuilder s = new StringBuilder();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3091
            s.append('<');
06bc494ca11e Initial load
duke
parents:
diff changeset
  3092
            boolean first = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3093
            for (Type t : tvars) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3094
                if (!first) s.append(", ");
06bc494ca11e Initial load
duke
parents:
diff changeset
  3095
                first = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3096
                appendTyparamString(((TypeVar)t), s);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3097
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3098
            s.append('>');
06bc494ca11e Initial load
duke
parents:
diff changeset
  3099
            return s.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3100
        }
8625
6b51ef804d49 6639645: Modeling type implementing missing interfaces
jjg
parents: 8617
diff changeset
  3101
        private void appendTyparamString(TypeVar t, StringBuilder buf) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3102
            buf.append(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3103
            if (t.bound == null ||
06bc494ca11e Initial load
duke
parents:
diff changeset
  3104
                t.bound.tsym.getQualifiedName() == names.java_lang_Object)
06bc494ca11e Initial load
duke
parents:
diff changeset
  3105
                return;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3106
            buf.append(" extends "); // Java syntax; no need for i18n
06bc494ca11e Initial load
duke
parents:
diff changeset
  3107
            Type bound = t.bound;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3108
            if (!bound.isCompound()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3109
                buf.append(bound);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3110
            } else if ((erasure(t).tsym.flags() & INTERFACE) == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3111
                buf.append(supertype(t));
06bc494ca11e Initial load
duke
parents:
diff changeset
  3112
                for (Type intf : interfaces(t)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3113
                    buf.append('&');
06bc494ca11e Initial load
duke
parents:
diff changeset
  3114
                    buf.append(intf);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3115
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3116
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3117
                // No superclass was given in bounds.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3118
                // In this case, supertype is Object, erasure is first interface.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3119
                boolean first = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3120
                for (Type intf : interfaces(t)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3121
                    if (!first) buf.append('&');
06bc494ca11e Initial load
duke
parents:
diff changeset
  3122
                    first = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3123
                    buf.append(intf);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3124
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3125
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3126
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3127
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  3128
06bc494ca11e Initial load
duke
parents:
diff changeset
  3129
    // <editor-fold defaultstate="collapsed" desc="Determining least upper bounds of types">
06bc494ca11e Initial load
duke
parents:
diff changeset
  3130
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  3131
     * A cache for closures.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3132
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  3133
     * <p>A closure is a list of all the supertypes and interfaces of
06bc494ca11e Initial load
duke
parents:
diff changeset
  3134
     * a class or interface type, ordered by ClassSymbol.precedes
06bc494ca11e Initial load
duke
parents:
diff changeset
  3135
     * (that is, subclasses come first, arbitrary but fixed
06bc494ca11e Initial load
duke
parents:
diff changeset
  3136
     * otherwise).
06bc494ca11e Initial load
duke
parents:
diff changeset
  3137
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3138
    private Map<Type,List<Type>> closureCache = new HashMap<Type,List<Type>>();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3139
06bc494ca11e Initial load
duke
parents:
diff changeset
  3140
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  3141
     * Returns the closure of a class or interface type.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3142
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3143
    public List<Type> closure(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3144
        List<Type> cl = closureCache.get(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3145
        if (cl == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3146
            Type st = supertype(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3147
            if (!t.isCompound()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3148
                if (st.tag == CLASS) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3149
                    cl = insert(closure(st), t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3150
                } else if (st.tag == TYPEVAR) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3151
                    cl = closure(st).prepend(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3152
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3153
                    cl = List.of(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3154
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3155
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3156
                cl = closure(supertype(t));
06bc494ca11e Initial load
duke
parents:
diff changeset
  3157
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3158
            for (List<Type> l = interfaces(t); l.nonEmpty(); l = l.tail)
06bc494ca11e Initial load
duke
parents:
diff changeset
  3159
                cl = union(cl, closure(l.head));
06bc494ca11e Initial load
duke
parents:
diff changeset
  3160
            closureCache.put(t, cl);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3161
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3162
        return cl;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3163
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3164
06bc494ca11e Initial load
duke
parents:
diff changeset
  3165
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  3166
     * Insert a type in a closure
06bc494ca11e Initial load
duke
parents:
diff changeset
  3167
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3168
    public List<Type> insert(List<Type> cl, Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3169
        if (cl.isEmpty() || t.tsym.precedes(cl.head.tsym, this)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3170
            return cl.prepend(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3171
        } else if (cl.head.tsym.precedes(t.tsym, this)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3172
            return insert(cl.tail, t).prepend(cl.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3173
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3174
            return cl;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3175
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3176
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3177
06bc494ca11e Initial load
duke
parents:
diff changeset
  3178
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  3179
     * Form the union of two closures
06bc494ca11e Initial load
duke
parents:
diff changeset
  3180
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3181
    public List<Type> union(List<Type> cl1, List<Type> cl2) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3182
        if (cl1.isEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3183
            return cl2;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3184
        } else if (cl2.isEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3185
            return cl1;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3186
        } else if (cl1.head.tsym.precedes(cl2.head.tsym, this)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3187
            return union(cl1.tail, cl2).prepend(cl1.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3188
        } else if (cl2.head.tsym.precedes(cl1.head.tsym, this)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3189
            return union(cl1, cl2.tail).prepend(cl2.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3190
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3191
            return union(cl1.tail, cl2.tail).prepend(cl1.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3192
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3193
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3194
06bc494ca11e Initial load
duke
parents:
diff changeset
  3195
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  3196
     * Intersect two closures
06bc494ca11e Initial load
duke
parents:
diff changeset
  3197
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3198
    public List<Type> intersect(List<Type> cl1, List<Type> cl2) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3199
        if (cl1 == cl2)
06bc494ca11e Initial load
duke
parents:
diff changeset
  3200
            return cl1;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3201
        if (cl1.isEmpty() || cl2.isEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
  3202
            return List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3203
        if (cl1.head.tsym.precedes(cl2.head.tsym, this))
06bc494ca11e Initial load
duke
parents:
diff changeset
  3204
            return intersect(cl1.tail, cl2);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3205
        if (cl2.head.tsym.precedes(cl1.head.tsym, this))
06bc494ca11e Initial load
duke
parents:
diff changeset
  3206
            return intersect(cl1, cl2.tail);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3207
        if (isSameType(cl1.head, cl2.head))
06bc494ca11e Initial load
duke
parents:
diff changeset
  3208
            return intersect(cl1.tail, cl2.tail).prepend(cl1.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3209
        if (cl1.head.tsym == cl2.head.tsym &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  3210
            cl1.head.tag == CLASS && cl2.head.tag == CLASS) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3211
            if (cl1.head.isParameterized() && cl2.head.isParameterized()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3212
                Type merge = merge(cl1.head,cl2.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3213
                return intersect(cl1.tail, cl2.tail).prepend(merge);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3214
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3215
            if (cl1.head.isRaw() || cl2.head.isRaw())
06bc494ca11e Initial load
duke
parents:
diff changeset
  3216
                return intersect(cl1.tail, cl2.tail).prepend(erasure(cl1.head));
06bc494ca11e Initial load
duke
parents:
diff changeset
  3217
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3218
        return intersect(cl1.tail, cl2.tail);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3219
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3220
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
  3221
        class TypePair {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3222
            final Type t1;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3223
            final Type t2;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3224
            TypePair(Type t1, Type t2) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3225
                this.t1 = t1;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3226
                this.t2 = t2;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3227
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3228
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  3229
            public int hashCode() {
14949
45f43822bbde 8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents: 14801
diff changeset
  3230
                return 127 * Types.this.hashCode(t1) + Types.this.hashCode(t2);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3231
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3232
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  3233
            public boolean equals(Object obj) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3234
                if (!(obj instanceof TypePair))
06bc494ca11e Initial load
duke
parents:
diff changeset
  3235
                    return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3236
                TypePair typePair = (TypePair)obj;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3237
                return isSameType(t1, typePair.t1)
06bc494ca11e Initial load
duke
parents:
diff changeset
  3238
                    && isSameType(t2, typePair.t2);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3239
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3240
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3241
        Set<TypePair> mergeCache = new HashSet<TypePair>();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3242
        private Type merge(Type c1, Type c2) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3243
            ClassType class1 = (ClassType) c1;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3244
            List<Type> act1 = class1.getTypeArguments();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3245
            ClassType class2 = (ClassType) c2;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3246
            List<Type> act2 = class2.getTypeArguments();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3247
            ListBuffer<Type> merged = new ListBuffer<Type>();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3248
            List<Type> typarams = class1.tsym.type.getTypeArguments();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3249
06bc494ca11e Initial load
duke
parents:
diff changeset
  3250
            while (act1.nonEmpty() && act2.nonEmpty() && typarams.nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3251
                if (containsType(act1.head, act2.head)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3252
                    merged.append(act1.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3253
                } else if (containsType(act2.head, act1.head)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3254
                    merged.append(act2.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3255
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3256
                    TypePair pair = new TypePair(c1, c2);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3257
                    Type m;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3258
                    if (mergeCache.add(pair)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3259
                        m = new WildcardType(lub(upperBound(act1.head),
06bc494ca11e Initial load
duke
parents:
diff changeset
  3260
                                                 upperBound(act2.head)),
06bc494ca11e Initial load
duke
parents:
diff changeset
  3261
                                             BoundKind.EXTENDS,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3262
                                             syms.boundClass);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3263
                        mergeCache.remove(pair);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3264
                    } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3265
                        m = new WildcardType(syms.objectType,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3266
                                             BoundKind.UNBOUND,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3267
                                             syms.boundClass);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3268
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3269
                    merged.append(m.withTypeVar(typarams.head));
06bc494ca11e Initial load
duke
parents:
diff changeset
  3270
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3271
                act1 = act1.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3272
                act2 = act2.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3273
                typarams = typarams.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3274
            }
8032
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents: 7681
diff changeset
  3275
            Assert.check(act1.isEmpty() && act2.isEmpty() && typarams.isEmpty());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3276
            return new ClassType(class1.getEnclosingType(), merged.toList(), class1.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3277
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3278
06bc494ca11e Initial load
duke
parents:
diff changeset
  3279
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  3280
     * Return the minimum type of a closure, a compound type if no
06bc494ca11e Initial load
duke
parents:
diff changeset
  3281
     * unique minimum exists.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3282
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3283
    private Type compoundMin(List<Type> cl) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3284
        if (cl.isEmpty()) return syms.objectType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3285
        List<Type> compound = closureMin(cl);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3286
        if (compound.isEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
  3287
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3288
        else if (compound.tail.isEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
  3289
            return compound.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3290
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
  3291
            return makeCompoundType(compound);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3292
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3293
06bc494ca11e Initial load
duke
parents:
diff changeset
  3294
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  3295
     * Return the minimum types of a closure, suitable for computing
06bc494ca11e Initial load
duke
parents:
diff changeset
  3296
     * compoundMin or glb.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3297
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3298
    private List<Type> closureMin(List<Type> cl) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3299
        ListBuffer<Type> classes = lb();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3300
        ListBuffer<Type> interfaces = lb();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3301
        while (!cl.isEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3302
            Type current = cl.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3303
            if (current.isInterface())
06bc494ca11e Initial load
duke
parents:
diff changeset
  3304
                interfaces.append(current);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3305
            else
06bc494ca11e Initial load
duke
parents:
diff changeset
  3306
                classes.append(current);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3307
            ListBuffer<Type> candidates = lb();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3308
            for (Type t : cl.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3309
                if (!isSubtypeNoCapture(current, t))
06bc494ca11e Initial load
duke
parents:
diff changeset
  3310
                    candidates.append(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3311
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3312
            cl = candidates.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3313
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3314
        return classes.appendList(interfaces).toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3315
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3316
06bc494ca11e Initial load
duke
parents:
diff changeset
  3317
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  3318
     * Return the least upper bound of pair of types.  if the lub does
06bc494ca11e Initial load
duke
parents:
diff changeset
  3319
     * not exist return null.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3320
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3321
    public Type lub(Type t1, Type t2) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3322
        return lub(List.of(t1, t2));
06bc494ca11e Initial load
duke
parents:
diff changeset
  3323
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3324
06bc494ca11e Initial load
duke
parents:
diff changeset
  3325
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  3326
     * Return the least upper bound (lub) of set of types.  If the lub
06bc494ca11e Initial load
duke
parents:
diff changeset
  3327
     * does not exist return the type of null (bottom).
06bc494ca11e Initial load
duke
parents:
diff changeset
  3328
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3329
    public Type lub(List<Type> ts) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3330
        final int ARRAY_BOUND = 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3331
        final int CLASS_BOUND = 2;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3332
        int boundkind = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3333
        for (Type t : ts) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3334
            switch (t.tag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3335
            case CLASS:
06bc494ca11e Initial load
duke
parents:
diff changeset
  3336
                boundkind |= CLASS_BOUND;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3337
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3338
            case ARRAY:
06bc494ca11e Initial load
duke
parents:
diff changeset
  3339
                boundkind |= ARRAY_BOUND;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3340
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3341
            case  TYPEVAR:
06bc494ca11e Initial load
duke
parents:
diff changeset
  3342
                do {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3343
                    t = t.getUpperBound();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3344
                } while (t.tag == TYPEVAR);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3345
                if (t.tag == ARRAY) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3346
                    boundkind |= ARRAY_BOUND;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3347
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3348
                    boundkind |= CLASS_BOUND;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3349
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3350
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3351
            default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  3352
                if (t.isPrimitive())
162
6620f2a8e265 6611449: Internal Error thrown during generic method/constructor invocation
mcimadamore
parents: 10
diff changeset
  3353
                    return syms.errType;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3354
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3355
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3356
        switch (boundkind) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3357
        case 0:
06bc494ca11e Initial load
duke
parents:
diff changeset
  3358
            return syms.botType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3359
06bc494ca11e Initial load
duke
parents:
diff changeset
  3360
        case ARRAY_BOUND:
06bc494ca11e Initial load
duke
parents:
diff changeset
  3361
            // calculate lub(A[], B[])
06bc494ca11e Initial load
duke
parents:
diff changeset
  3362
            List<Type> elements = Type.map(ts, elemTypeFun);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3363
            for (Type t : elements) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3364
                if (t.isPrimitive()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3365
                    // if a primitive type is found, then return
06bc494ca11e Initial load
duke
parents:
diff changeset
  3366
                    // arraySuperType unless all the types are the
06bc494ca11e Initial load
duke
parents:
diff changeset
  3367
                    // same
06bc494ca11e Initial load
duke
parents:
diff changeset
  3368
                    Type first = ts.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3369
                    for (Type s : ts.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3370
                        if (!isSameType(first, s)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3371
                             // lub(int[], B[]) is Cloneable & Serializable
06bc494ca11e Initial load
duke
parents:
diff changeset
  3372
                            return arraySuperType();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3373
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3374
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3375
                    // all the array types are the same, return one
06bc494ca11e Initial load
duke
parents:
diff changeset
  3376
                    // lub(int[], int[]) is int[]
06bc494ca11e Initial load
duke
parents:
diff changeset
  3377
                    return first;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3378
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3379
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3380
            // lub(A[], B[]) is lub(A, B)[]
06bc494ca11e Initial load
duke
parents:
diff changeset
  3381
            return new ArrayType(lub(elements), syms.arrayClass);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3382
06bc494ca11e Initial load
duke
parents:
diff changeset
  3383
        case CLASS_BOUND:
06bc494ca11e Initial load
duke
parents:
diff changeset
  3384
            // calculate lub(A, B)
06bc494ca11e Initial load
duke
parents:
diff changeset
  3385
            while (ts.head.tag != CLASS && ts.head.tag != TYPEVAR)
06bc494ca11e Initial load
duke
parents:
diff changeset
  3386
                ts = ts.tail;
8032
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents: 7681
diff changeset
  3387
            Assert.check(!ts.isEmpty());
8617
6a05d1669247 7015715: lub gets stuck on type with complex supertype
mcimadamore
parents: 8430
diff changeset
  3388
            //step 1 - compute erased candidate set (EC)
6a05d1669247 7015715: lub gets stuck on type with complex supertype
mcimadamore
parents: 8430
diff changeset
  3389
            List<Type> cl = erasedSupertypes(ts.head);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3390
            for (Type t : ts.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3391
                if (t.tag == CLASS || t.tag == TYPEVAR)
8617
6a05d1669247 7015715: lub gets stuck on type with complex supertype
mcimadamore
parents: 8430
diff changeset
  3392
                    cl = intersect(cl, erasedSupertypes(t));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3393
            }
8617
6a05d1669247 7015715: lub gets stuck on type with complex supertype
mcimadamore
parents: 8430
diff changeset
  3394
            //step 2 - compute minimal erased candidate set (MEC)
6a05d1669247 7015715: lub gets stuck on type with complex supertype
mcimadamore
parents: 8430
diff changeset
  3395
            List<Type> mec = closureMin(cl);
6a05d1669247 7015715: lub gets stuck on type with complex supertype
mcimadamore
parents: 8430
diff changeset
  3396
            //step 3 - for each element G in MEC, compute lci(Inv(G))
6a05d1669247 7015715: lub gets stuck on type with complex supertype
mcimadamore
parents: 8430
diff changeset
  3397
            List<Type> candidates = List.nil();
6a05d1669247 7015715: lub gets stuck on type with complex supertype
mcimadamore
parents: 8430
diff changeset
  3398
            for (Type erasedSupertype : mec) {
6a05d1669247 7015715: lub gets stuck on type with complex supertype
mcimadamore
parents: 8430
diff changeset
  3399
                List<Type> lci = List.of(asSuper(ts.head, erasedSupertype.tsym));
6a05d1669247 7015715: lub gets stuck on type with complex supertype
mcimadamore
parents: 8430
diff changeset
  3400
                for (Type t : ts) {
6a05d1669247 7015715: lub gets stuck on type with complex supertype
mcimadamore
parents: 8430
diff changeset
  3401
                    lci = intersect(lci, List.of(asSuper(t, erasedSupertype.tsym)));
6a05d1669247 7015715: lub gets stuck on type with complex supertype
mcimadamore
parents: 8430
diff changeset
  3402
                }
6a05d1669247 7015715: lub gets stuck on type with complex supertype
mcimadamore
parents: 8430
diff changeset
  3403
                candidates = candidates.appendList(lci);
6a05d1669247 7015715: lub gets stuck on type with complex supertype
mcimadamore
parents: 8430
diff changeset
  3404
            }
6a05d1669247 7015715: lub gets stuck on type with complex supertype
mcimadamore
parents: 8430
diff changeset
  3405
            //step 4 - let MEC be { G1, G2 ... Gn }, then we have that
6a05d1669247 7015715: lub gets stuck on type with complex supertype
mcimadamore
parents: 8430
diff changeset
  3406
            //lub = lci(Inv(G1)) & lci(Inv(G2)) & ... & lci(Inv(Gn))
6a05d1669247 7015715: lub gets stuck on type with complex supertype
mcimadamore
parents: 8430
diff changeset
  3407
            return compoundMin(candidates);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3408
06bc494ca11e Initial load
duke
parents:
diff changeset
  3409
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  3410
            // calculate lub(A, B[])
06bc494ca11e Initial load
duke
parents:
diff changeset
  3411
            List<Type> classes = List.of(arraySuperType());
06bc494ca11e Initial load
duke
parents:
diff changeset
  3412
            for (Type t : ts) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3413
                if (t.tag != ARRAY) // Filter out any arrays
06bc494ca11e Initial load
duke
parents:
diff changeset
  3414
                    classes = classes.prepend(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3415
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3416
            // lub(A, B[]) is lub(A, arraySuperType)
06bc494ca11e Initial load
duke
parents:
diff changeset
  3417
            return lub(classes);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3418
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3419
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3420
    // where
8617
6a05d1669247 7015715: lub gets stuck on type with complex supertype
mcimadamore
parents: 8430
diff changeset
  3421
        List<Type> erasedSupertypes(Type t) {
6a05d1669247 7015715: lub gets stuck on type with complex supertype
mcimadamore
parents: 8430
diff changeset
  3422
            ListBuffer<Type> buf = lb();
6a05d1669247 7015715: lub gets stuck on type with complex supertype
mcimadamore
parents: 8430
diff changeset
  3423
            for (Type sup : closure(t)) {
6a05d1669247 7015715: lub gets stuck on type with complex supertype
mcimadamore
parents: 8430
diff changeset
  3424
                if (sup.tag == TYPEVAR) {
6a05d1669247 7015715: lub gets stuck on type with complex supertype
mcimadamore
parents: 8430
diff changeset
  3425
                    buf.append(sup);
6a05d1669247 7015715: lub gets stuck on type with complex supertype
mcimadamore
parents: 8430
diff changeset
  3426
                } else {
6a05d1669247 7015715: lub gets stuck on type with complex supertype
mcimadamore
parents: 8430
diff changeset
  3427
                    buf.append(erasure(sup));
6a05d1669247 7015715: lub gets stuck on type with complex supertype
mcimadamore
parents: 8430
diff changeset
  3428
                }
6a05d1669247 7015715: lub gets stuck on type with complex supertype
mcimadamore
parents: 8430
diff changeset
  3429
            }
6a05d1669247 7015715: lub gets stuck on type with complex supertype
mcimadamore
parents: 8430
diff changeset
  3430
            return buf.toList();
6a05d1669247 7015715: lub gets stuck on type with complex supertype
mcimadamore
parents: 8430
diff changeset
  3431
        }
6a05d1669247 7015715: lub gets stuck on type with complex supertype
mcimadamore
parents: 8430
diff changeset
  3432
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3433
        private Type arraySuperType = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3434
        private Type arraySuperType() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3435
            // initialized lazily to avoid problems during compiler startup
06bc494ca11e Initial load
duke
parents:
diff changeset
  3436
            if (arraySuperType == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3437
                synchronized (this) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3438
                    if (arraySuperType == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3439
                        // JLS 10.8: all arrays implement Cloneable and Serializable.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3440
                        arraySuperType = makeCompoundType(List.of(syms.serializableType,
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
  3441
                                                                  syms.cloneableType), true);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3442
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3443
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3444
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3445
            return arraySuperType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3446
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3447
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  3448
06bc494ca11e Initial load
duke
parents:
diff changeset
  3449
    // <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
  3450
    public Type glb(List<Type> ts) {
aafb4bf914ee 6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents: 1874
diff changeset
  3451
        Type t1 = ts.head;
aafb4bf914ee 6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents: 1874
diff changeset
  3452
        for (Type t2 : ts.tail) {
aafb4bf914ee 6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents: 1874
diff changeset
  3453
            if (t1.isErroneous())
aafb4bf914ee 6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents: 1874
diff changeset
  3454
                return t1;
aafb4bf914ee 6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents: 1874
diff changeset
  3455
            t1 = glb(t1, t2);
aafb4bf914ee 6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents: 1874
diff changeset
  3456
        }
aafb4bf914ee 6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents: 1874
diff changeset
  3457
        return t1;
aafb4bf914ee 6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents: 1874
diff changeset
  3458
    }
aafb4bf914ee 6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents: 1874
diff changeset
  3459
    //where
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3460
    public Type glb(Type t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3461
        if (s == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  3462
            return t;
7331
02ffc087c654 6995200: JDK 7 compiler crashes when type-variable is inferred from expected primitive type
mcimadamore
parents: 7204
diff changeset
  3463
        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
  3464
            return syms.errType;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3465
        else if (isSubtypeNoCapture(t, s))
06bc494ca11e Initial load
duke
parents:
diff changeset
  3466
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3467
        else if (isSubtypeNoCapture(s, t))
06bc494ca11e Initial load
duke
parents:
diff changeset
  3468
            return s;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3469
06bc494ca11e Initial load
duke
parents:
diff changeset
  3470
        List<Type> closure = union(closure(t), closure(s));
06bc494ca11e Initial load
duke
parents:
diff changeset
  3471
        List<Type> bounds = closureMin(closure);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3472
06bc494ca11e Initial load
duke
parents:
diff changeset
  3473
        if (bounds.isEmpty()) {             // length == 0
06bc494ca11e Initial load
duke
parents:
diff changeset
  3474
            return syms.objectType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3475
        } else if (bounds.tail.isEmpty()) { // length == 1
06bc494ca11e Initial load
duke
parents:
diff changeset
  3476
            return bounds.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3477
        } else {                            // length > 1
06bc494ca11e Initial load
duke
parents:
diff changeset
  3478
            int classCount = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3479
            for (Type bound : bounds)
06bc494ca11e Initial load
duke
parents:
diff changeset
  3480
                if (!bound.isInterface())
06bc494ca11e Initial load
duke
parents:
diff changeset
  3481
                    classCount++;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3482
            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
  3483
                return createErrorType(t);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3484
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3485
        return makeCompoundType(bounds);
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="hashCode">
06bc494ca11e Initial load
duke
parents:
diff changeset
  3490
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  3491
     * Compute a hash code on a type.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3492
     */
14949
45f43822bbde 8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents: 14801
diff changeset
  3493
    public int hashCode(Type t) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3494
        return hashCode.visit(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3495
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3496
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
  3497
        private static final UnaryVisitor<Integer> hashCode = new UnaryVisitor<Integer>() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3498
06bc494ca11e Initial load
duke
parents:
diff changeset
  3499
            public Integer visitType(Type t, Void ignored) {
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
  3500
                return t.tag.ordinal();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3501
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3502
06bc494ca11e Initial load
duke
parents:
diff changeset
  3503
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  3504
            public Integer visitClassType(ClassType t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3505
                int result = visit(t.getEnclosingType());
06bc494ca11e Initial load
duke
parents:
diff changeset
  3506
                result *= 127;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3507
                result += t.tsym.flatName().hashCode();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3508
                for (Type s : t.getTypeArguments()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3509
                    result *= 127;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3510
                    result += visit(s);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3511
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3512
                return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3513
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3514
06bc494ca11e Initial load
duke
parents:
diff changeset
  3515
            @Override
14949
45f43822bbde 8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents: 14801
diff changeset
  3516
            public Integer visitMethodType(MethodType t, Void ignored) {
45f43822bbde 8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents: 14801
diff changeset
  3517
                int h = METHOD.ordinal();
45f43822bbde 8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents: 14801
diff changeset
  3518
                for (List<Type> thisargs = t.argtypes;
45f43822bbde 8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents: 14801
diff changeset
  3519
                     thisargs.tail != null;
45f43822bbde 8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents: 14801
diff changeset
  3520
                     thisargs = thisargs.tail)
45f43822bbde 8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents: 14801
diff changeset
  3521
                    h = (h << 5) + visit(thisargs.head);
45f43822bbde 8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents: 14801
diff changeset
  3522
                return (h << 5) + visit(t.restype);
45f43822bbde 8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents: 14801
diff changeset
  3523
            }
45f43822bbde 8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents: 14801
diff changeset
  3524
45f43822bbde 8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents: 14801
diff changeset
  3525
            @Override
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3526
            public Integer visitWildcardType(WildcardType t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3527
                int result = t.kind.hashCode();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3528
                if (t.type != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3529
                    result *= 127;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3530
                    result += visit(t.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3531
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3532
                return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3533
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3534
06bc494ca11e Initial load
duke
parents:
diff changeset
  3535
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  3536
            public Integer visitArrayType(ArrayType t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3537
                return visit(t.elemtype) + 12;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3538
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3539
06bc494ca11e Initial load
duke
parents:
diff changeset
  3540
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  3541
            public Integer visitTypeVar(TypeVar t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3542
                return System.identityHashCode(t.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3543
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3544
06bc494ca11e Initial load
duke
parents:
diff changeset
  3545
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  3546
            public Integer visitUndetVar(UndetVar t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3547
                return System.identityHashCode(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3548
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3549
06bc494ca11e Initial load
duke
parents:
diff changeset
  3550
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  3551
            public Integer visitErrorType(ErrorType t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3552
                return 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3553
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3554
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
  3555
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  3556
06bc494ca11e Initial load
duke
parents:
diff changeset
  3557
    // <editor-fold defaultstate="collapsed" desc="Return-Type-Substitutable">
06bc494ca11e Initial load
duke
parents:
diff changeset
  3558
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  3559
     * Does t have a result that is a subtype of the result type of s,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3560
     * suitable for covariant returns?  It is assumed that both types
06bc494ca11e Initial load
duke
parents:
diff changeset
  3561
     * are (possibly polymorphic) method types.  Monomorphic method
06bc494ca11e Initial load
duke
parents:
diff changeset
  3562
     * types are handled in the obvious way.  Polymorphic method types
06bc494ca11e Initial load
duke
parents:
diff changeset
  3563
     * require renaming all type variables of one to corresponding
06bc494ca11e Initial load
duke
parents:
diff changeset
  3564
     * type variables in the other, where correspondence is by
06bc494ca11e Initial load
duke
parents:
diff changeset
  3565
     * position in the type parameter list. */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3566
    public boolean resultSubtype(Type t, Type s, Warner warner) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3567
        List<Type> tvars = t.getTypeArguments();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3568
        List<Type> svars = s.getTypeArguments();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3569
        Type tres = t.getReturnType();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3570
        Type sres = subst(s.getReturnType(), svars, tvars);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3571
        return covariantReturnType(tres, sres, warner);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3572
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3573
06bc494ca11e Initial load
duke
parents:
diff changeset
  3574
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  3575
     * Return-Type-Substitutable.
9303
eae35c201e19 7032975: API files in javax.annotation.processing need to be updated for references to JLS
jjh
parents: 9301
diff changeset
  3576
     * @jls section 8.4.5
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3577
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3578
    public boolean returnTypeSubstitutable(Type r1, Type r2) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3579
        if (hasSameArgs(r1, r2))
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14443
diff changeset
  3580
            return resultSubtype(r1, r2, noWarnings);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3581
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
  3582
            return covariantReturnType(r1.getReturnType(),
1874
891c2d6823bb 6797463: 6557199 breaks the jax-ws workspace
tbell
parents: 1868
diff changeset
  3583
                                       erasure(r2.getReturnType()),
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14443
diff changeset
  3584
                                       noWarnings);
1874
891c2d6823bb 6797463: 6557199 breaks the jax-ws workspace
tbell
parents: 1868
diff changeset
  3585
    }
891c2d6823bb 6797463: 6557199 breaks the jax-ws workspace
tbell
parents: 1868
diff changeset
  3586
891c2d6823bb 6797463: 6557199 breaks the jax-ws workspace
tbell
parents: 1868
diff changeset
  3587
    public boolean returnTypeSubstitutable(Type r1,
891c2d6823bb 6797463: 6557199 breaks the jax-ws workspace
tbell
parents: 1868
diff changeset
  3588
                                           Type r2, Type r2res,
891c2d6823bb 6797463: 6557199 breaks the jax-ws workspace
tbell
parents: 1868
diff changeset
  3589
                                           Warner warner) {
891c2d6823bb 6797463: 6557199 breaks the jax-ws workspace
tbell
parents: 1868
diff changeset
  3590
        if (isSameType(r1.getReturnType(), r2res))
891c2d6823bb 6797463: 6557199 breaks the jax-ws workspace
tbell
parents: 1868
diff changeset
  3591
            return true;
891c2d6823bb 6797463: 6557199 breaks the jax-ws workspace
tbell
parents: 1868
diff changeset
  3592
        if (r1.getReturnType().isPrimitive() || r2res.isPrimitive())
891c2d6823bb 6797463: 6557199 breaks the jax-ws workspace
tbell
parents: 1868
diff changeset
  3593
            return false;
891c2d6823bb 6797463: 6557199 breaks the jax-ws workspace
tbell
parents: 1868
diff changeset
  3594
891c2d6823bb 6797463: 6557199 breaks the jax-ws workspace
tbell
parents: 1868
diff changeset
  3595
        if (hasSameArgs(r1, r2))
891c2d6823bb 6797463: 6557199 breaks the jax-ws workspace
tbell
parents: 1868
diff changeset
  3596
            return covariantReturnType(r1.getReturnType(), r2res, warner);
9595
a7dea6cbd5f7 7038363: cast from object to primitive should be for source >= 1.7
jjg
parents: 9303
diff changeset
  3597
        if (!allowCovariantReturns)
1874
891c2d6823bb 6797463: 6557199 breaks the jax-ws workspace
tbell
parents: 1868
diff changeset
  3598
            return false;
891c2d6823bb 6797463: 6557199 breaks the jax-ws workspace
tbell
parents: 1868
diff changeset
  3599
        if (isSubtypeUnchecked(r1.getReturnType(), r2res, warner))
891c2d6823bb 6797463: 6557199 breaks the jax-ws workspace
tbell
parents: 1868
diff changeset
  3600
            return true;
891c2d6823bb 6797463: 6557199 breaks the jax-ws workspace
tbell
parents: 1868
diff changeset
  3601
        if (!isSubtype(r1.getReturnType(), erasure(r2res)))
891c2d6823bb 6797463: 6557199 breaks the jax-ws workspace
tbell
parents: 1868
diff changeset
  3602
            return false;
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 7637
diff changeset
  3603
        warner.warn(LintCategory.UNCHECKED);
1874
891c2d6823bb 6797463: 6557199 breaks the jax-ws workspace
tbell
parents: 1868
diff changeset
  3604
        return true;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3605
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3606
06bc494ca11e Initial load
duke
parents:
diff changeset
  3607
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  3608
     * Is t an appropriate return type in an overrider for a
06bc494ca11e Initial load
duke
parents:
diff changeset
  3609
     * method that returns s?
06bc494ca11e Initial load
duke
parents:
diff changeset
  3610
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3611
    public boolean covariantReturnType(Type t, Type s, Warner warner) {
1874
891c2d6823bb 6797463: 6557199 breaks the jax-ws workspace
tbell
parents: 1868
diff changeset
  3612
        return
891c2d6823bb 6797463: 6557199 breaks the jax-ws workspace
tbell
parents: 1868
diff changeset
  3613
            isSameType(t, s) ||
9595
a7dea6cbd5f7 7038363: cast from object to primitive should be for source >= 1.7
jjg
parents: 9303
diff changeset
  3614
            allowCovariantReturns &&
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3615
            !t.isPrimitive() &&
1874
891c2d6823bb 6797463: 6557199 breaks the jax-ws workspace
tbell
parents: 1868
diff changeset
  3616
            !s.isPrimitive() &&
891c2d6823bb 6797463: 6557199 breaks the jax-ws workspace
tbell
parents: 1868
diff changeset
  3617
            isAssignable(t, s, warner);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3618
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3619
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  3620
06bc494ca11e Initial load
duke
parents:
diff changeset
  3621
    // <editor-fold defaultstate="collapsed" desc="Box/unbox support">
06bc494ca11e Initial load
duke
parents:
diff changeset
  3622
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  3623
     * Return the class that boxes the given primitive.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3624
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3625
    public ClassSymbol boxedClass(Type t) {
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
  3626
        return reader.enterClass(syms.boxedName[t.tag.ordinal()]);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3627
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3628
06bc494ca11e Initial load
duke
parents:
diff changeset
  3629
    /**
7331
02ffc087c654 6995200: JDK 7 compiler crashes when type-variable is inferred from expected primitive type
mcimadamore
parents: 7204
diff changeset
  3630
     * 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
  3631
     */
02ffc087c654 6995200: JDK 7 compiler crashes when type-variable is inferred from expected primitive type
mcimadamore
parents: 7204
diff changeset
  3632
    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
  3633
        return t.isPrimitive() ?
02ffc087c654 6995200: JDK 7 compiler crashes when type-variable is inferred from expected primitive type
mcimadamore
parents: 7204
diff changeset
  3634
            boxedClass(t).type :
02ffc087c654 6995200: JDK 7 compiler crashes when type-variable is inferred from expected primitive type
mcimadamore
parents: 7204
diff changeset
  3635
            t;
02ffc087c654 6995200: JDK 7 compiler crashes when type-variable is inferred from expected primitive type
mcimadamore
parents: 7204
diff changeset
  3636
    }
02ffc087c654 6995200: JDK 7 compiler crashes when type-variable is inferred from expected primitive type
mcimadamore
parents: 7204
diff changeset
  3637
02ffc087c654 6995200: JDK 7 compiler crashes when type-variable is inferred from expected primitive type
mcimadamore
parents: 7204
diff changeset
  3638
    /**
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3639
     * Return the primitive type corresponding to a boxed type.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3640
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3641
    public Type unboxedType(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3642
        if (allowBoxing) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3643
            for (int i=0; i<syms.boxedName.length; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3644
                Name box = syms.boxedName[i];
06bc494ca11e Initial load
duke
parents:
diff changeset
  3645
                if (box != null &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  3646
                    asSuper(t, reader.enterClass(box)) != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  3647
                    return syms.typeOfTag[i];
06bc494ca11e Initial load
duke
parents:
diff changeset
  3648
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3649
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3650
        return Type.noType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3651
    }
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
  3652
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
  3653
    /**
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
  3654
     * Return the unboxed type if 't' is a boxed class, otherwise return 't' itself.
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
  3655
     */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
  3656
    public Type unboxedTypeOrType(Type t) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
  3657
        Type unboxedType = unboxedType(t);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
  3658
        return unboxedType.tag == NONE ? t : unboxedType;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
  3659
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3660
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  3661
06bc494ca11e Initial load
duke
parents:
diff changeset
  3662
    // <editor-fold defaultstate="collapsed" desc="Capture conversion">
06bc494ca11e Initial load
duke
parents:
diff changeset
  3663
    /*
9303
eae35c201e19 7032975: API files in javax.annotation.processing need to be updated for references to JLS
jjh
parents: 9301
diff changeset
  3664
     * JLS 5.1.10 Capture Conversion:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3665
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  3666
     * Let G name a generic type declaration with n formal type
06bc494ca11e Initial load
duke
parents:
diff changeset
  3667
     * parameters A1 ... An with corresponding bounds U1 ... Un. There
06bc494ca11e Initial load
duke
parents:
diff changeset
  3668
     * exists a capture conversion from G<T1 ... Tn> to G<S1 ... Sn>,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3669
     * where, for 1 <= i <= n:
06bc494ca11e Initial load
duke
parents:
diff changeset
  3670
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  3671
     * + If Ti is a wildcard type argument (4.5.1) of the form ? then
06bc494ca11e Initial load
duke
parents:
diff changeset
  3672
     *   Si is a fresh type variable whose upper bound is
06bc494ca11e Initial load
duke
parents:
diff changeset
  3673
     *   Ui[A1 := S1, ..., An := Sn] and whose lower bound is the null
06bc494ca11e Initial load
duke
parents:
diff changeset
  3674
     *   type.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3675
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  3676
     * + If Ti is a wildcard type argument of the form ? extends Bi,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3677
     *   then Si is a fresh type variable whose upper bound is
06bc494ca11e Initial load
duke
parents:
diff changeset
  3678
     *   glb(Bi, Ui[A1 := S1, ..., An := Sn]) and whose lower bound is
06bc494ca11e Initial load
duke
parents:
diff changeset
  3679
     *   the null type, where glb(V1,... ,Vm) is V1 & ... & Vm. It is
06bc494ca11e Initial load
duke
parents:
diff changeset
  3680
     *   a compile-time error if for any two classes (not interfaces)
06bc494ca11e Initial load
duke
parents:
diff changeset
  3681
     *   Vi and Vj,Vi is not a subclass of Vj or vice versa.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3682
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  3683
     * + If Ti is a wildcard type argument of the form ? super Bi,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3684
     *   then Si is a fresh type variable whose upper bound is
06bc494ca11e Initial load
duke
parents:
diff changeset
  3685
     *   Ui[A1 := S1, ..., An := Sn] and whose lower bound is Bi.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3686
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  3687
     * + Otherwise, Si = Ti.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3688
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  3689
     * Capture conversion on any type other than a parameterized type
06bc494ca11e Initial load
duke
parents:
diff changeset
  3690
     * (4.5) acts as an identity conversion (5.1.1). Capture
06bc494ca11e Initial load
duke
parents:
diff changeset
  3691
     * conversions never require a special action at run time and
06bc494ca11e Initial load
duke
parents:
diff changeset
  3692
     * therefore never throw an exception at run time.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3693
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  3694
     * Capture conversion is not applied recursively.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3695
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3696
    /**
9303
eae35c201e19 7032975: API files in javax.annotation.processing need to be updated for references to JLS
jjh
parents: 9301
diff changeset
  3697
     * Capture conversion as specified by the JLS.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3698
     */
3140
15a274b13051 6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents: 2223
diff changeset
  3699
15a274b13051 6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents: 2223
diff changeset
  3700
    public List<Type> capture(List<Type> ts) {
15a274b13051 6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents: 2223
diff changeset
  3701
        List<Type> buf = List.nil();
15a274b13051 6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents: 2223
diff changeset
  3702
        for (Type t : ts) {
15a274b13051 6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents: 2223
diff changeset
  3703
            buf = buf.prepend(capture(t));
15a274b13051 6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents: 2223
diff changeset
  3704
        }
15a274b13051 6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents: 2223
diff changeset
  3705
        return buf.reverse();
15a274b13051 6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents: 2223
diff changeset
  3706
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3707
    public Type capture(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3708
        if (t.tag != CLASS)
06bc494ca11e Initial load
duke
parents:
diff changeset
  3709
            return t;
6350
7070b0722ebb 6886247: regression: javac crashes with an assertion error in Attr.java
mcimadamore
parents: 5847
diff changeset
  3710
        if (t.getEnclosingType() != Type.noType) {
7070b0722ebb 6886247: regression: javac crashes with an assertion error in Attr.java
mcimadamore
parents: 5847
diff changeset
  3711
            Type capturedEncl = capture(t.getEnclosingType());
7070b0722ebb 6886247: regression: javac crashes with an assertion error in Attr.java
mcimadamore
parents: 5847
diff changeset
  3712
            if (capturedEncl != t.getEnclosingType()) {
7070b0722ebb 6886247: regression: javac crashes with an assertion error in Attr.java
mcimadamore
parents: 5847
diff changeset
  3713
                Type type1 = memberType(capturedEncl, t.tsym);
7070b0722ebb 6886247: regression: javac crashes with an assertion error in Attr.java
mcimadamore
parents: 5847
diff changeset
  3714
                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
  3715
            }
7070b0722ebb 6886247: regression: javac crashes with an assertion error in Attr.java
mcimadamore
parents: 5847
diff changeset
  3716
        }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
  3717
        t = t.unannotatedType();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3718
        ClassType cls = (ClassType)t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3719
        if (cls.isRaw() || !cls.isParameterized())
06bc494ca11e Initial load
duke
parents:
diff changeset
  3720
            return cls;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3721
06bc494ca11e Initial load
duke
parents:
diff changeset
  3722
        ClassType G = (ClassType)cls.asElement().asType();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3723
        List<Type> A = G.getTypeArguments();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3724
        List<Type> T = cls.getTypeArguments();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3725
        List<Type> S = freshTypeVariables(T);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3726
06bc494ca11e Initial load
duke
parents:
diff changeset
  3727
        List<Type> currentA = A;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3728
        List<Type> currentT = T;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3729
        List<Type> currentS = S;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3730
        boolean captured = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3731
        while (!currentA.isEmpty() &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  3732
               !currentT.isEmpty() &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  3733
               !currentS.isEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3734
            if (currentS.head != currentT.head) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3735
                captured = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3736
                WildcardType Ti = (WildcardType)currentT.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3737
                Type Ui = currentA.head.getUpperBound();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3738
                CapturedType Si = (CapturedType)currentS.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3739
                if (Ui == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  3740
                    Ui = syms.objectType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3741
                switch (Ti.kind) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3742
                case UNBOUND:
06bc494ca11e Initial load
duke
parents:
diff changeset
  3743
                    Si.bound = subst(Ui, A, S);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3744
                    Si.lower = syms.botType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3745
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3746
                case EXTENDS:
06bc494ca11e Initial load
duke
parents:
diff changeset
  3747
                    Si.bound = glb(Ti.getExtendsBound(), subst(Ui, A, S));
06bc494ca11e Initial load
duke
parents:
diff changeset
  3748
                    Si.lower = syms.botType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3749
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3750
                case SUPER:
06bc494ca11e Initial load
duke
parents:
diff changeset
  3751
                    Si.bound = subst(Ui, A, S);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3752
                    Si.lower = Ti.getSuperBound();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3753
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3754
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3755
                if (Si.bound == Si.lower)
06bc494ca11e Initial load
duke
parents:
diff changeset
  3756
                    currentS.head = Si.bound;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3757
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3758
            currentA = currentA.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3759
            currentT = currentT.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3760
            currentS = currentS.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3761
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3762
        if (!currentA.isEmpty() || !currentT.isEmpty() || !currentS.isEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
  3763
            return erasure(t); // some "rare" type involved
06bc494ca11e Initial load
duke
parents:
diff changeset
  3764
06bc494ca11e Initial load
duke
parents:
diff changeset
  3765
        if (captured)
06bc494ca11e Initial load
duke
parents:
diff changeset
  3766
            return new ClassType(cls.getEnclosingType(), S, cls.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3767
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
  3768
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3769
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3770
    // where
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents: 2218
diff changeset
  3771
        public List<Type> freshTypeVariables(List<Type> types) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3772
            ListBuffer<Type> result = lb();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3773
            for (Type t : types) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3774
                if (t.tag == WILDCARD) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3775
                    Type bound = ((WildcardType)t).getExtendsBound();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3776
                    if (bound == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  3777
                        bound = syms.objectType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3778
                    result.append(new CapturedType(capturedName,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3779
                                                   syms.noSymbol,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3780
                                                   bound,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3781
                                                   syms.botType,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3782
                                                   (WildcardType)t));
06bc494ca11e Initial load
duke
parents:
diff changeset
  3783
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3784
                    result.append(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3785
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3786
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3787
            return result.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3788
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3789
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  3790
06bc494ca11e Initial load
duke
parents:
diff changeset
  3791
    // <editor-fold defaultstate="collapsed" desc="Internal utility methods">
06bc494ca11e Initial load
duke
parents:
diff changeset
  3792
    private List<Type> upperBounds(List<Type> ss) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3793
        if (ss.isEmpty()) return ss;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3794
        Type head = upperBound(ss.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3795
        List<Type> tail = upperBounds(ss.tail);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3796
        if (head != ss.head || tail != ss.tail)
06bc494ca11e Initial load
duke
parents:
diff changeset
  3797
            return tail.prepend(head);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3798
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
  3799
            return ss;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3800
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3801
06bc494ca11e Initial load
duke
parents:
diff changeset
  3802
    private boolean sideCast(Type from, Type to, Warner warn) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3803
        // We are casting from type $from$ to type $to$, which are
06bc494ca11e Initial load
duke
parents:
diff changeset
  3804
        // non-final unrelated types.  This method
06bc494ca11e Initial load
duke
parents:
diff changeset
  3805
        // tries to reject a cast by transferring type parameters
06bc494ca11e Initial load
duke
parents:
diff changeset
  3806
        // from $to$ to $from$ by common superinterfaces.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3807
        boolean reverse = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3808
        Type target = to;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3809
        if ((to.tsym.flags() & INTERFACE) == 0) {
8032
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents: 7681
diff changeset
  3810
            Assert.check((from.tsym.flags() & INTERFACE) != 0);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3811
            reverse = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3812
            to = from;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3813
            from = target;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3814
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3815
        List<Type> commonSupers = superClosure(to, erasure(from));
06bc494ca11e Initial load
duke
parents:
diff changeset
  3816
        boolean giveWarning = commonSupers.isEmpty();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3817
        // The arguments to the supers could be unified here to
06bc494ca11e Initial load
duke
parents:
diff changeset
  3818
        // get a more accurate analysis
06bc494ca11e Initial load
duke
parents:
diff changeset
  3819
        while (commonSupers.nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3820
            Type t1 = asSuper(from, commonSupers.head.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3821
            Type t2 = commonSupers.head; // same as asSuper(to, commonSupers.head.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3822
            if (disjointTypes(t1.getTypeArguments(), t2.getTypeArguments()))
06bc494ca11e Initial load
duke
parents:
diff changeset
  3823
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3824
            giveWarning = giveWarning || (reverse ? giveWarning(t2, t1) : giveWarning(t1, t2));
06bc494ca11e Initial load
duke
parents:
diff changeset
  3825
            commonSupers = commonSupers.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3826
        }
1792
985e9406d42a 6558559: Extra "unchecked" diagnostic
mcimadamore
parents: 1790
diff changeset
  3827
        if (giveWarning && !isReifiable(reverse ? from : to))
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 7637
diff changeset
  3828
            warn.warn(LintCategory.UNCHECKED);
9595
a7dea6cbd5f7 7038363: cast from object to primitive should be for source >= 1.7
jjg
parents: 9303
diff changeset
  3829
        if (!allowCovariantReturns)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3830
            // reject if there is a common method signature with
06bc494ca11e Initial load
duke
parents:
diff changeset
  3831
            // incompatible return types.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3832
            chk.checkCompatibleAbstracts(warn.pos(), from, to);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3833
        return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3834
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3835
06bc494ca11e Initial load
duke
parents:
diff changeset
  3836
    private boolean sideCastFinal(Type from, Type to, Warner warn) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3837
        // We are casting from type $from$ to type $to$, which are
06bc494ca11e Initial load
duke
parents:
diff changeset
  3838
        // unrelated types one of which is final and the other of
06bc494ca11e Initial load
duke
parents:
diff changeset
  3839
        // which is an interface.  This method
06bc494ca11e Initial load
duke
parents:
diff changeset
  3840
        // tries to reject a cast by transferring type parameters
06bc494ca11e Initial load
duke
parents:
diff changeset
  3841
        // from the final class to the interface.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3842
        boolean reverse = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3843
        Type target = to;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3844
        if ((to.tsym.flags() & INTERFACE) == 0) {
8032
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents: 7681
diff changeset
  3845
            Assert.check((from.tsym.flags() & INTERFACE) != 0);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3846
            reverse = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3847
            to = from;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3848
            from = target;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3849
        }
8032
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents: 7681
diff changeset
  3850
        Assert.check((from.tsym.flags() & FINAL) != 0);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3851
        Type t1 = asSuper(from, to.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3852
        if (t1 == null) return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3853
        Type t2 = to;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3854
        if (disjointTypes(t1.getTypeArguments(), t2.getTypeArguments()))
06bc494ca11e Initial load
duke
parents:
diff changeset
  3855
            return false;
9595
a7dea6cbd5f7 7038363: cast from object to primitive should be for source >= 1.7
jjg
parents: 9303
diff changeset
  3856
        if (!allowCovariantReturns)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3857
            // reject if there is a common method signature with
06bc494ca11e Initial load
duke
parents:
diff changeset
  3858
            // incompatible return types.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3859
            chk.checkCompatibleAbstracts(warn.pos(), from, to);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3860
        if (!isReifiable(target) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  3861
            (reverse ? giveWarning(t2, t1) : giveWarning(t1, t2)))
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 7637
diff changeset
  3862
            warn.warn(LintCategory.UNCHECKED);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3863
        return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3864
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3865
06bc494ca11e Initial load
duke
parents:
diff changeset
  3866
    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
  3867
        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
  3868
        return to.isParameterized() &&
a8ec0baae870 6467183: javac fails to raise unchecked warning on cast of parameterized generic subclass
mcimadamore
parents: 1993
diff changeset
  3869
                (!(isUnbounded(to) ||
a8ec0baae870 6467183: javac fails to raise unchecked warning on cast of parameterized generic subclass
mcimadamore
parents: 1993
diff changeset
  3870
                isSubtype(from, to) ||
7204
0960f7e5e366 6714835: Safe cast is rejected (with warning) by javac
mcimadamore
parents: 6709
diff changeset
  3871
                ((subFrom != null) && containsType(to.allparams(), subFrom.allparams()))));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3872
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3873
06bc494ca11e Initial load
duke
parents:
diff changeset
  3874
    private List<Type> superClosure(Type t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3875
        List<Type> cl = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3876
        for (List<Type> l = interfaces(t); l.nonEmpty(); l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3877
            if (isSubtype(s, erasure(l.head))) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3878
                cl = insert(cl, l.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3879
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3880
                cl = union(cl, superClosure(l.head, s));
06bc494ca11e Initial load
duke
parents:
diff changeset
  3881
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3882
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3883
        return cl;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3884
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3885
06bc494ca11e Initial load
duke
parents:
diff changeset
  3886
    private boolean containsTypeEquivalent(Type t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3887
        return
06bc494ca11e Initial load
duke
parents:
diff changeset
  3888
            isSameType(t, s) || // shortcut
06bc494ca11e Initial load
duke
parents:
diff changeset
  3889
            containsType(t, s) && containsType(s, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3890
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3891
1473
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3892
    // <editor-fold defaultstate="collapsed" desc="adapt">
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3893
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  3894
     * Adapt a type by computing a substitution which maps a source
06bc494ca11e Initial load
duke
parents:
diff changeset
  3895
     * type to a target type.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3896
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  3897
     * @param source    the source type
06bc494ca11e Initial load
duke
parents:
diff changeset
  3898
     * @param target    the target type
06bc494ca11e Initial load
duke
parents:
diff changeset
  3899
     * @param from      the type variables of the computed substitution
06bc494ca11e Initial load
duke
parents:
diff changeset
  3900
     * @param to        the types of the computed substitution.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3901
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3902
    public void adapt(Type source,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3903
                       Type target,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3904
                       ListBuffer<Type> from,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3905
                       ListBuffer<Type> to) throws AdaptFailure {
1473
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3906
        new Adapter(from, to).adapt(source, target);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3907
    }
1473
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3908
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3909
    class Adapter extends SimpleVisitor<Void, Type> {
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3910
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3911
        ListBuffer<Type> from;
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3912
        ListBuffer<Type> to;
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3913
        Map<Symbol,Type> mapping;
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3914
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3915
        Adapter(ListBuffer<Type> from, ListBuffer<Type> to) {
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3916
            this.from = from;
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3917
            this.to = to;
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3918
            mapping = new HashMap<Symbol,Type>();
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3919
        }
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3920
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3921
        public void adapt(Type source, Type target) throws AdaptFailure {
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3922
            visit(source, target);
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3923
            List<Type> fromList = from.toList();
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3924
            List<Type> toList = to.toList();
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3925
            while (!fromList.isEmpty()) {
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3926
                Type val = mapping.get(fromList.head.tsym);
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3927
                if (toList.head != val)
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3928
                    toList.head = val;
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3929
                fromList = fromList.tail;
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3930
                toList = toList.tail;
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3931
            }
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3932
        }
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3933
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3934
        @Override
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3935
        public Void visitClassType(ClassType source, Type target) throws AdaptFailure {
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3936
            if (target.tag == CLASS)
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3937
                adaptRecursive(source.allparams(), target.allparams());
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3938
            return null;
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3939
        }
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3940
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3941
        @Override
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3942
        public Void visitArrayType(ArrayType source, Type target) throws AdaptFailure {
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3943
            if (target.tag == ARRAY)
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3944
                adaptRecursive(elemtype(source), elemtype(target));
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3945
            return null;
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3946
        }
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3947
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3948
        @Override
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3949
        public Void visitWildcardType(WildcardType source, Type target) throws AdaptFailure {
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3950
            if (source.isExtendsBound())
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3951
                adaptRecursive(upperBound(source), upperBound(target));
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3952
            else if (source.isSuperBound())
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3953
                adaptRecursive(lowerBound(source), lowerBound(target));
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3954
            return null;
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3955
        }
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3956
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3957
        @Override
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3958
        public Void visitTypeVar(TypeVar source, Type target) throws AdaptFailure {
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3959
            // Check to see if there is
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3960
            // already a mapping for $source$, in which case
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3961
            // the old mapping will be merged with the new
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3962
            Type val = mapping.get(source.tsym);
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3963
            if (val != null) {
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3964
                if (val.isSuperBound() && target.isSuperBound()) {
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3965
                    val = isSubtype(lowerBound(val), lowerBound(target))
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3966
                        ? target : val;
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3967
                } else if (val.isExtendsBound() && target.isExtendsBound()) {
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3968
                    val = isSubtype(upperBound(val), upperBound(target))
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3969
                        ? val : target;
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3970
                } else if (!isSameType(val, target)) {
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3971
                    throw new AdaptFailure();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3972
                }
1473
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3973
            } else {
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3974
                val = target;
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3975
                from.append(source);
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3976
                to.append(target);
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3977
            }
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3978
            mapping.put(source.tsym, val);
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3979
            return null;
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3980
        }
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3981
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3982
        @Override
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3983
        public Void visitType(Type source, Type target) {
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3984
            return null;
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3985
        }
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3986
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3987
        private Set<TypePair> cache = new HashSet<TypePair>();
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3988
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3989
        private void adaptRecursive(Type source, Type target) {
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3990
            TypePair pair = new TypePair(source, target);
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3991
            if (cache.add(pair)) {
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3992
                try {
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3993
                    visit(source, target);
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3994
                } finally {
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3995
                    cache.remove(pair);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3996
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3997
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3998
        }
1473
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  3999
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4000
        private void adaptRecursive(List<Type> source, List<Type> target) {
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4001
            if (source.length() == target.length()) {
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4002
                while (source.nonEmpty()) {
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4003
                    adaptRecursive(source.head, target.head);
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4004
                    source = source.tail;
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4005
                    target = target.tail;
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4006
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4007
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4008
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4009
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4010
1473
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4011
    public static class AdaptFailure extends RuntimeException {
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4012
        static final long serialVersionUID = -7490231548272701566L;
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4013
    }
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4014
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4015
    private void adaptSelf(Type t,
06bc494ca11e Initial load
duke
parents:
diff changeset
  4016
                           ListBuffer<Type> from,
06bc494ca11e Initial load
duke
parents:
diff changeset
  4017
                           ListBuffer<Type> to) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4018
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4019
            //if (t.tsym.type != t)
06bc494ca11e Initial load
duke
parents:
diff changeset
  4020
                adapt(t.tsym.type, t, from, to);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4021
        } catch (AdaptFailure ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4022
            // Adapt should never fail calculating a mapping from
06bc494ca11e Initial load
duke
parents:
diff changeset
  4023
            // t.tsym.type to t as there can be no merge problem.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4024
            throw new AssertionError(ex);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4025
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4026
    }
1473
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4027
    // </editor-fold>
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4028
06bc494ca11e Initial load
duke
parents:
diff changeset
  4029
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  4030
     * Rewrite all type variables (universal quantifiers) in the given
06bc494ca11e Initial load
duke
parents:
diff changeset
  4031
     * type to wildcards (existential quantifiers).  This is used to
06bc494ca11e Initial load
duke
parents:
diff changeset
  4032
     * determine if a cast is allowed.  For example, if high is true
06bc494ca11e Initial load
duke
parents:
diff changeset
  4033
     * and {@code T <: Number}, then {@code List<T>} is rewritten to
06bc494ca11e Initial load
duke
parents:
diff changeset
  4034
     * {@code List<?  extends Number>}.  Since {@code List<Integer> <:
06bc494ca11e Initial load
duke
parents:
diff changeset
  4035
     * List<? extends Number>} a {@code List<T>} can be cast to {@code
06bc494ca11e Initial load
duke
parents:
diff changeset
  4036
     * List<Integer>} with a warning.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4037
     * @param t a type
06bc494ca11e Initial load
duke
parents:
diff changeset
  4038
     * @param high if true return an upper bound; otherwise a lower
06bc494ca11e Initial load
duke
parents:
diff changeset
  4039
     * bound
06bc494ca11e Initial load
duke
parents:
diff changeset
  4040
     * @param rewriteTypeVars only rewrite captured wildcards if false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4041
     * otherwise rewrite all type variables
06bc494ca11e Initial load
duke
parents:
diff changeset
  4042
     * @return the type rewritten with wildcards (existential
06bc494ca11e Initial load
duke
parents:
diff changeset
  4043
     * quantifiers) only
06bc494ca11e Initial load
duke
parents:
diff changeset
  4044
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  4045
    private Type rewriteQuantifiers(Type t, boolean high, boolean rewriteTypeVars) {
6353
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  4046
        return new Rewriter(high, rewriteTypeVars).visit(t);
1530
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4047
    }
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4048
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4049
    class Rewriter extends UnaryVisitor<Type> {
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4050
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4051
        boolean high;
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4052
        boolean rewriteTypeVars;
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4053
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4054
        Rewriter(boolean high, boolean rewriteTypeVars) {
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4055
            this.high = high;
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4056
            this.rewriteTypeVars = rewriteTypeVars;
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4057
        }
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4058
6353
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  4059
        @Override
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  4060
        public Type visitClassType(ClassType t, Void s) {
1530
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4061
            ListBuffer<Type> rewritten = new ListBuffer<Type>();
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4062
            boolean changed = false;
6353
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  4063
            for (Type arg : t.allparams()) {
1530
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4064
                Type bound = visit(arg);
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4065
                if (arg != bound) {
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4066
                    changed = true;
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4067
                }
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4068
                rewritten.append(bound);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4069
            }
1530
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4070
            if (changed)
6353
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  4071
                return subst(t.tsym.type,
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  4072
                        t.tsym.type.allparams(),
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  4073
                        rewritten.toList());
1530
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4074
            else
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4075
                return t;
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4076
        }
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4077
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4078
        public Type visitType(Type t, Void s) {
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4079
            return high ? upperBound(t) : lowerBound(t);
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4080
        }
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4081
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4082
        @Override
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4083
        public Type visitCapturedType(CapturedType t, Void s) {
11549
cc901a28adc3 7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents: 10810
diff changeset
  4084
            Type w_bound = t.wildcard.type;
cc901a28adc3 7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents: 10810
diff changeset
  4085
            Type bound = w_bound.contains(t) ?
cc901a28adc3 7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents: 10810
diff changeset
  4086
                        erasure(w_bound) :
cc901a28adc3 7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents: 10810
diff changeset
  4087
                        visit(w_bound);
cc901a28adc3 7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents: 10810
diff changeset
  4088
            return rewriteAsWildcardType(visit(bound), t.wildcard.bound, t.wildcard.kind);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4089
        }
1530
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4090
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4091
        @Override
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4092
        public Type visitTypeVar(TypeVar t, Void s) {
6353
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  4093
            if (rewriteTypeVars) {
11549
cc901a28adc3 7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents: 10810
diff changeset
  4094
                Type bound = t.bound.contains(t) ?
7627
de2a86da165e 7005095: Cast: compile reject sensible cast from final class to interface
mcimadamore
parents: 7331
diff changeset
  4095
                        erasure(t.bound) :
11549
cc901a28adc3 7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents: 10810
diff changeset
  4096
                        visit(t.bound);
cc901a28adc3 7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents: 10810
diff changeset
  4097
                return rewriteAsWildcardType(bound, t, EXTENDS);
cc901a28adc3 7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents: 10810
diff changeset
  4098
            } else {
cc901a28adc3 7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents: 10810
diff changeset
  4099
                return t;
6353
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  4100
            }
1530
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4101
        }
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4102
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4103
        @Override
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4104
        public Type visitWildcardType(WildcardType t, Void s) {
11549
cc901a28adc3 7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents: 10810
diff changeset
  4105
            Type bound2 = visit(t.type);
cc901a28adc3 7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents: 10810
diff changeset
  4106
            return t.type == bound2 ? t : rewriteAsWildcardType(bound2, t.bound, t.kind);
6353
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  4107
        }
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  4108
11549
cc901a28adc3 7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents: 10810
diff changeset
  4109
        private Type rewriteAsWildcardType(Type bound, TypeVar formal, BoundKind bk) {
cc901a28adc3 7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents: 10810
diff changeset
  4110
            switch (bk) {
cc901a28adc3 7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents: 10810
diff changeset
  4111
               case EXTENDS: return high ?
cc901a28adc3 7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents: 10810
diff changeset
  4112
                       makeExtendsWildcard(B(bound), formal) :
cc901a28adc3 7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents: 10810
diff changeset
  4113
                       makeExtendsWildcard(syms.objectType, formal);
cc901a28adc3 7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents: 10810
diff changeset
  4114
               case SUPER: return high ?
cc901a28adc3 7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents: 10810
diff changeset
  4115
                       makeSuperWildcard(syms.botType, formal) :
cc901a28adc3 7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents: 10810
diff changeset
  4116
                       makeSuperWildcard(B(bound), formal);
cc901a28adc3 7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents: 10810
diff changeset
  4117
               case UNBOUND: return makeExtendsWildcard(syms.objectType, formal);
cc901a28adc3 7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents: 10810
diff changeset
  4118
               default:
cc901a28adc3 7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents: 10810
diff changeset
  4119
                   Assert.error("Invalid bound kind " + bk);
cc901a28adc3 7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents: 10810
diff changeset
  4120
                   return null;
cc901a28adc3 7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents: 10810
diff changeset
  4121
            }
6353
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  4122
        }
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  4123
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  4124
        Type B(Type t) {
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  4125
            while (t.tag == WILDCARD) {
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  4126
                WildcardType w = (WildcardType)t;
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  4127
                t = high ?
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  4128
                    w.getExtendsBound() :
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  4129
                    w.getSuperBound();
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  4130
                if (t == null) {
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  4131
                    t = high ? syms.objectType : syms.botType;
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  4132
                }
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  4133
            }
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  4134
            return t;
1530
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4135
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4136
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4137
6353
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  4138
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4139
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  4140
     * Create a wildcard with the given upper (extends) bound; create
06bc494ca11e Initial load
duke
parents:
diff changeset
  4141
     * an unbounded wildcard if bound is Object.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4142
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  4143
     * @param bound the upper bound
06bc494ca11e Initial load
duke
parents:
diff changeset
  4144
     * @param formal the formal type parameter that will be
06bc494ca11e Initial load
duke
parents:
diff changeset
  4145
     * substituted by the wildcard
06bc494ca11e Initial load
duke
parents:
diff changeset
  4146
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  4147
    private WildcardType makeExtendsWildcard(Type bound, TypeVar formal) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4148
        if (bound == syms.objectType) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4149
            return new WildcardType(syms.objectType,
06bc494ca11e Initial load
duke
parents:
diff changeset
  4150
                                    BoundKind.UNBOUND,
06bc494ca11e Initial load
duke
parents:
diff changeset
  4151
                                    syms.boundClass,
06bc494ca11e Initial load
duke
parents:
diff changeset
  4152
                                    formal);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4153
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4154
            return new WildcardType(bound,
06bc494ca11e Initial load
duke
parents:
diff changeset
  4155
                                    BoundKind.EXTENDS,
06bc494ca11e Initial load
duke
parents:
diff changeset
  4156
                                    syms.boundClass,
06bc494ca11e Initial load
duke
parents:
diff changeset
  4157
                                    formal);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4158
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4159
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4160
06bc494ca11e Initial load
duke
parents:
diff changeset
  4161
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  4162
     * Create a wildcard with the given lower (super) bound; create an
06bc494ca11e Initial load
duke
parents:
diff changeset
  4163
     * unbounded wildcard if bound is bottom (type of {@code null}).
06bc494ca11e Initial load
duke
parents:
diff changeset
  4164
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  4165
     * @param bound the lower bound
06bc494ca11e Initial load
duke
parents:
diff changeset
  4166
     * @param formal the formal type parameter that will be
06bc494ca11e Initial load
duke
parents:
diff changeset
  4167
     * substituted by the wildcard
06bc494ca11e Initial load
duke
parents:
diff changeset
  4168
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  4169
    private WildcardType makeSuperWildcard(Type bound, TypeVar formal) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4170
        if (bound.tag == BOT) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4171
            return new WildcardType(syms.objectType,
06bc494ca11e Initial load
duke
parents:
diff changeset
  4172
                                    BoundKind.UNBOUND,
06bc494ca11e Initial load
duke
parents:
diff changeset
  4173
                                    syms.boundClass,
06bc494ca11e Initial load
duke
parents:
diff changeset
  4174
                                    formal);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4175
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4176
            return new WildcardType(bound,
06bc494ca11e Initial load
duke
parents:
diff changeset
  4177
                                    BoundKind.SUPER,
06bc494ca11e Initial load
duke
parents:
diff changeset
  4178
                                    syms.boundClass,
06bc494ca11e Initial load
duke
parents:
diff changeset
  4179
                                    formal);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4180
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4181
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4182
06bc494ca11e Initial load
duke
parents:
diff changeset
  4183
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  4184
     * A wrapper for a type that allows use in sets.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4185
     */
14949
45f43822bbde 8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents: 14801
diff changeset
  4186
    public static class UniqueType {
45f43822bbde 8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents: 14801
diff changeset
  4187
        public final Type type;
45f43822bbde 8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents: 14801
diff changeset
  4188
        final Types types;
45f43822bbde 8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents: 14801
diff changeset
  4189
45f43822bbde 8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents: 14801
diff changeset
  4190
        public UniqueType(Type type, Types types) {
45f43822bbde 8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents: 14801
diff changeset
  4191
            this.type = type;
45f43822bbde 8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents: 14801
diff changeset
  4192
            this.types = types;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4193
        }
14949
45f43822bbde 8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents: 14801
diff changeset
  4194
45f43822bbde 8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents: 14801
diff changeset
  4195
        public int hashCode() {
45f43822bbde 8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents: 14801
diff changeset
  4196
            return types.hashCode(type);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4197
        }
14949
45f43822bbde 8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents: 14801
diff changeset
  4198
45f43822bbde 8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents: 14801
diff changeset
  4199
        public boolean equals(Object obj) {
45f43822bbde 8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents: 14801
diff changeset
  4200
            return (obj instanceof UniqueType) &&
45f43822bbde 8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents: 14801
diff changeset
  4201
                types.isSameType(type, ((UniqueType)obj).type);
45f43822bbde 8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents: 14801
diff changeset
  4202
        }
45f43822bbde 8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents: 14801
diff changeset
  4203
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4204
        public String toString() {
14949
45f43822bbde 8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents: 14801
diff changeset
  4205
            return type.toString();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4206
        }
14949
45f43822bbde 8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents: 14801
diff changeset
  4207
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4208
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4209
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  4210
06bc494ca11e Initial load
duke
parents:
diff changeset
  4211
    // <editor-fold defaultstate="collapsed" desc="Visitors">
06bc494ca11e Initial load
duke
parents:
diff changeset
  4212
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  4213
     * A default visitor for types.  All visitor methods except
06bc494ca11e Initial load
duke
parents:
diff changeset
  4214
     * visitType are implemented by delegating to visitType.  Concrete
06bc494ca11e Initial load
duke
parents:
diff changeset
  4215
     * subclasses must provide an implementation of visitType and can
06bc494ca11e Initial load
duke
parents:
diff changeset
  4216
     * override other methods as needed.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4217
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  4218
     * @param <R> the return type of the operation implemented by this
06bc494ca11e Initial load
duke
parents:
diff changeset
  4219
     * visitor; use Void if no return type is needed.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4220
     * @param <S> the type of the second argument (the first being the
06bc494ca11e Initial load
duke
parents:
diff changeset
  4221
     * type itself) of the operation implemented by this visitor; use
06bc494ca11e Initial load
duke
parents:
diff changeset
  4222
     * Void if a second argument is not needed.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4223
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  4224
    public static abstract class DefaultTypeVisitor<R,S> implements Type.Visitor<R,S> {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4225
        final public R visit(Type t, S s)               { return t.accept(this, s); }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4226
        public R visitClassType(ClassType t, S s)       { return visitType(t, s); }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4227
        public R visitWildcardType(WildcardType t, S s) { return visitType(t, s); }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4228
        public R visitArrayType(ArrayType t, S s)       { return visitType(t, s); }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4229
        public R visitMethodType(MethodType t, S s)     { return visitType(t, s); }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4230
        public R visitPackageType(PackageType t, S s)   { return visitType(t, s); }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4231
        public R visitTypeVar(TypeVar t, S s)           { return visitType(t, s); }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4232
        public R visitCapturedType(CapturedType t, S s) { return visitType(t, s); }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4233
        public R visitForAll(ForAll t, S s)             { return visitType(t, s); }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4234
        public R visitUndetVar(UndetVar t, S s)         { return visitType(t, s); }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4235
        public R visitErrorType(ErrorType t, S s)       { return visitType(t, s); }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
  4236
        // Pretend annotations don't exist
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
  4237
        public R visitAnnotatedType(AnnotatedType t, S s) { return visit(t.underlyingType, s); }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4238
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4239
06bc494ca11e Initial load
duke
parents:
diff changeset
  4240
    /**
1357
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  4241
     * A default visitor for symbols.  All visitor methods except
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  4242
     * visitSymbol are implemented by delegating to visitSymbol.  Concrete
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  4243
     * subclasses must provide an implementation of visitSymbol and can
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  4244
     * override other methods as needed.
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  4245
     *
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  4246
     * @param <R> the return type of the operation implemented by this
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  4247
     * visitor; use Void if no return type is needed.
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  4248
     * @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
  4249
     * symbol itself) of the operation implemented by this visitor; use
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  4250
     * Void if a second argument is not needed.
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  4251
     */
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  4252
    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
  4253
        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
  4254
        public R visitClassSymbol(ClassSymbol s, S arg)         { return visitSymbol(s, arg); }
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  4255
        public R visitMethodSymbol(MethodSymbol s, S arg)       { return visitSymbol(s, arg); }
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  4256
        public R visitOperatorSymbol(OperatorSymbol s, S arg)   { return visitSymbol(s, arg); }
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  4257
        public R visitPackageSymbol(PackageSymbol s, S arg)     { return visitSymbol(s, arg); }
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  4258
        public R visitTypeSymbol(TypeSymbol s, S arg)           { return visitSymbol(s, arg); }
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  4259
        public R visitVarSymbol(VarSymbol s, S arg)             { return visitSymbol(s, arg); }
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  4260
    }
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  4261
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  4262
    /**
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4263
     * A <em>simple</em> visitor for types.  This visitor is simple as
06bc494ca11e Initial load
duke
parents:
diff changeset
  4264
     * captured wildcards, for-all types (generic methods), and
06bc494ca11e Initial load
duke
parents:
diff changeset
  4265
     * undetermined type variables (part of inference) are hidden.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4266
     * Captured wildcards are hidden by treating them as type
06bc494ca11e Initial load
duke
parents:
diff changeset
  4267
     * variables and the rest are hidden by visiting their qtypes.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4268
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  4269
     * @param <R> the return type of the operation implemented by this
06bc494ca11e Initial load
duke
parents:
diff changeset
  4270
     * visitor; use Void if no return type is needed.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4271
     * @param <S> the type of the second argument (the first being the
06bc494ca11e Initial load
duke
parents:
diff changeset
  4272
     * type itself) of the operation implemented by this visitor; use
06bc494ca11e Initial load
duke
parents:
diff changeset
  4273
     * Void if a second argument is not needed.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4274
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  4275
    public static abstract class SimpleVisitor<R,S> extends DefaultTypeVisitor<R,S> {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4276
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  4277
        public R visitCapturedType(CapturedType t, S s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4278
            return visitTypeVar(t, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4279
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4280
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  4281
        public R visitForAll(ForAll t, S s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4282
            return visit(t.qtype, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4283
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4284
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  4285
        public R visitUndetVar(UndetVar t, S s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4286
            return visit(t.qtype, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4287
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4288
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4289
06bc494ca11e Initial load
duke
parents:
diff changeset
  4290
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  4291
     * A plain relation on types.  That is a 2-ary function on the
06bc494ca11e Initial load
duke
parents:
diff changeset
  4292
     * form Type&nbsp;&times;&nbsp;Type&nbsp;&rarr;&nbsp;Boolean.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4293
     * <!-- In plain text: Type x Type -> Boolean -->
06bc494ca11e Initial load
duke
parents:
diff changeset
  4294
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  4295
    public static abstract class TypeRelation extends SimpleVisitor<Boolean,Type> {}
06bc494ca11e Initial load
duke
parents:
diff changeset
  4296
06bc494ca11e Initial load
duke
parents:
diff changeset
  4297
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  4298
     * A convenience visitor for implementing operations that only
06bc494ca11e Initial load
duke
parents:
diff changeset
  4299
     * require one argument (the type itself), that is, unary
06bc494ca11e Initial load
duke
parents:
diff changeset
  4300
     * operations.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4301
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  4302
     * @param <R> the return type of the operation implemented by this
06bc494ca11e Initial load
duke
parents:
diff changeset
  4303
     * visitor; use Void if no return type is needed.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4304
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  4305
    public static abstract class UnaryVisitor<R> extends SimpleVisitor<R,Void> {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4306
        final public R visit(Type t) { return t.accept(this, null); }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4307
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4308
06bc494ca11e Initial load
duke
parents:
diff changeset
  4309
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  4310
     * A visitor for implementing a mapping from types to types.  The
06bc494ca11e Initial load
duke
parents:
diff changeset
  4311
     * default behavior of this class is to implement the identity
06bc494ca11e Initial load
duke
parents:
diff changeset
  4312
     * mapping (mapping a type to itself).  This can be overridden in
06bc494ca11e Initial load
duke
parents:
diff changeset
  4313
     * subclasses.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4314
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  4315
     * @param <S> the type of the second argument (the first being the
06bc494ca11e Initial load
duke
parents:
diff changeset
  4316
     * type itself) of this mapping; use Void if a second argument is
06bc494ca11e Initial load
duke
parents:
diff changeset
  4317
     * not needed.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4318
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  4319
    public static class MapVisitor<S> extends DefaultTypeVisitor<Type,S> {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4320
        final public Type visit(Type t) { return t.accept(this, null); }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4321
        public Type visitType(Type t, S s) { return t; }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4322
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4323
    // </editor-fold>
6575
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents: 6353
diff changeset
  4324
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents: 6353
diff changeset
  4325
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents: 6353
diff changeset
  4326
    // <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
  4327
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents: 6353
diff changeset
  4328
    public RetentionPolicy getRetention(Attribute.Compound a) {
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 13633
diff changeset
  4329
        return getRetention(a.type.tsym);
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 13633
diff changeset
  4330
    }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 13633
diff changeset
  4331
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 13633
diff changeset
  4332
    public RetentionPolicy getRetention(Symbol sym) {
6575
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents: 6353
diff changeset
  4333
        RetentionPolicy vis = RetentionPolicy.CLASS; // the default
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 13633
diff changeset
  4334
        Attribute.Compound c = sym.attribute(syms.retentionType.tsym);
6575
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents: 6353
diff changeset
  4335
        if (c != null) {
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents: 6353
diff changeset
  4336
            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
  4337
            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
  4338
                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
  4339
                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
  4340
                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
  4341
                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
  4342
                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
  4343
            }
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents: 6353
diff changeset
  4344
        }
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents: 6353
diff changeset
  4345
        return vis;
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents: 6353
diff changeset
  4346
    }
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents: 6353
diff changeset
  4347
    // </editor-fold>
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4348
}