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