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