src/jdk.compiler/share/classes/com/sun/tools/javac/code/Types.java
author mcimadamore
Mon, 04 Dec 2017 17:54:49 +0000
changeset 48054 702043a4cdeb
parent 47959 5dd899009525
child 48661 19173eb3358b
permissions -rw-r--r--
8189749: Devise strategy for making source level checks more uniform Summary: Create a 'feature' enum which is responsible for handling source version checks and related diagnostic generation Reviewed-by: jjg, jlahoda
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
45754
740593904730 8182649: Unable to integrate due to compilation error
vromero
parents: 45504
diff changeset
     2
 * Copyright (c) 2003, 2017, 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;
41526
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
    33
import java.util.Optional;
14719
cd65a651beb3 8004068: Fix build problems caused by on-demand imports
jjg
parents: 14547
diff changeset
    34
import java.util.Set;
cd65a651beb3 8004068: Fix build problems caused by on-demand imports
jjg
parents: 14547
diff changeset
    35
import java.util.WeakHashMap;
29555
71f15ff4b409 8048838: type inference performance regression
mcimadamore
parents: 29554
diff changeset
    36
import java.util.function.BiPredicate;
44186
fe848a208b20 8176265: Method overload resolution on a covariant base type doesn't work in 9
mcimadamore
parents: 43025
diff changeset
    37
import java.util.function.Function;
29555
71f15ff4b409 8048838: type inference performance regression
mcimadamore
parents: 29554
diff changeset
    38
import java.util.stream.Collector;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
18730
95354d510139 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18662
diff changeset
    40
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
    41
6575
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents: 6353
diff changeset
    42
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
    43
import com.sun.tools.javac.code.Lint.LintCategory;
48054
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47959
diff changeset
    44
import com.sun.tools.javac.code.Source.Feature;
14048
308d1cf8fe46 7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents: 13689
diff changeset
    45
import com.sun.tools.javac.code.Type.UndetVar.InferenceBound;
29842
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29555
diff changeset
    46
import com.sun.tools.javac.code.TypeMetadata.Entry.Kind;
18730
95354d510139 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18662
diff changeset
    47
import com.sun.tools.javac.comp.AttrContext;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
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
    49
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
    50
import com.sun.tools.javac.comp.Env;
14258
8d2148961366 8000663: clean up langtools imports
jjg
parents: 14058
diff changeset
    51
import com.sun.tools.javac.util.*;
24604
7f68545b5128 8041422: Split javac ClassReader into ClassReader+ClassFinder
jjg
parents: 24397
diff changeset
    52
14258
8d2148961366 8000663: clean up langtools imports
jjg
parents: 14058
diff changeset
    53
import static com.sun.tools.javac.code.BoundKind.*;
8d2148961366 8000663: clean up langtools imports
jjg
parents: 14058
diff changeset
    54
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
    55
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
    56
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
    57
import static com.sun.tools.javac.code.Scope.LookupKind.NON_RECURSIVE;
14258
8d2148961366 8000663: clean up langtools imports
jjg
parents: 14058
diff changeset
    58
import static com.sun.tools.javac.code.Symbol.*;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
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
    60
import static com.sun.tools.javac.code.TypeTag.*;
16300
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
    61
import static com.sun.tools.javac.jvm.ClassFile.externalize;
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44186
diff changeset
    62
import com.sun.tools.javac.resources.CompilerProperties.Fragments;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
 * Utility class containing various operations on types.
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
 * <p>Unless other names are more illustrative, the following naming
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
 * conventions should be observed in this file:
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
 * <dl>
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
 * <dt>t</dt>
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
 * <dd>If the first argument to an operation is a type, it should be named t.</dd>
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
 * <dt>s</dt>
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
 * <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
    75
 * <dt>ts</dt>
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
 * <dd>If an operations takes a list of types, the first should be named ts.</dd>
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
 * <dt>ss</dt>
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
 * <dd>A second list of types should be named ss.</dd>
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
 * </dl>
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5654
diff changeset
    81
 * <p><b>This is NOT part of any supported API.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
 * If you write code that depends on this, you do so at your own risk.
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
 * This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
 * deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
public class Types {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21712
diff changeset
    87
    protected static final Context.Key<Types> typesKey = new Context.Key<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
    final Symtab syms;
1471
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1469
diff changeset
    90
    final JavacMessages messages;
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 1257
diff changeset
    91
    final Names names;
9595
a7dea6cbd5f7 7038363: cast from object to primitive should be for source >= 1.7
jjg
parents: 9303
diff changeset
    92
    final boolean allowObjectToPrimitiveCast;
28459
48a68a485760 8068517: Compiler may generate wrong InnerClasses attribute for static enum reference
jlahoda
parents: 28451
diff changeset
    93
    final boolean allowDefaultMethods;
42819
4ce83e629dc1 8075793: Source incompatibility for inference using -source 7
dlsmith
parents: 41862
diff changeset
    94
    final boolean mapCapturesToBounds;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
    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
    96
    final Enter enter;
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
    97
    JCDiagnostic.Factory diags;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
    List<Warner> warnStack = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
    final Name capturedName;
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   100
    private final FunctionDescriptorLookupError functionDescriptorLookupError;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14443
diff changeset
   102
    public final Warner noWarnings;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14443
diff changeset
   103
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
    // <editor-fold defaultstate="collapsed" desc="Instantiating">
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
    public static Types instance(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
        Types instance = context.get(typesKey);
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
        if (instance == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
            instance = new Types(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
        return instance;
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
    protected Types(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
        context.put(typesKey, this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
        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
   115
        names = Names.instance(context);
9595
a7dea6cbd5f7 7038363: cast from object to primitive should be for source >= 1.7
jjg
parents: 9303
diff changeset
   116
        Source source = Source.instance(context);
48054
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47959
diff changeset
   117
        allowObjectToPrimitiveCast = Feature.OBJECT_TO_PRIMITIVE_CAST.allowedInSource(source);
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47959
diff changeset
   118
        allowDefaultMethods = Feature.DEFAULT_METHODS.allowedInSource(source);
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47959
diff changeset
   119
        mapCapturesToBounds = Feature.MAP_CAPTURES_TO_BOUNDS.allowedInSource(source);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
        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
   121
        enter = Enter.instance(context);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
        capturedName = names.fromString("<captured wildcard>");
1471
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1469
diff changeset
   123
        messages = JavacMessages.instance(context);
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   124
        diags = JCDiagnostic.Factory.instance(context);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   125
        functionDescriptorLookupError = new FunctionDescriptorLookupError();
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14443
diff changeset
   126
        noWarnings = new Warner(null);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
24612
75dc732b45af 8042338: Refactor Types.upperBound to treat wildcards and variables separately
dlsmith
parents: 24604
diff changeset
   130
    // <editor-fold defaultstate="collapsed" desc="bounds">
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
    /**
24612
75dc732b45af 8042338: Refactor Types.upperBound to treat wildcards and variables separately
dlsmith
parents: 24604
diff changeset
   132
     * 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
   133
     * @param t a type argument, either a wildcard or a type
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
     */
24612
75dc732b45af 8042338: Refactor Types.upperBound to treat wildcards and variables separately
dlsmith
parents: 24604
diff changeset
   135
    public Type wildUpperBound(Type t) {
75dc732b45af 8042338: Refactor Types.upperBound to treat wildcards and variables separately
dlsmith
parents: 24604
diff changeset
   136
        if (t.hasTag(WILDCARD)) {
75dc732b45af 8042338: Refactor Types.upperBound to treat wildcards and variables separately
dlsmith
parents: 24604
diff changeset
   137
            WildcardType w = (WildcardType) t;
75dc732b45af 8042338: Refactor Types.upperBound to treat wildcards and variables separately
dlsmith
parents: 24604
diff changeset
   138
            if (w.isSuperBound())
75dc732b45af 8042338: Refactor Types.upperBound to treat wildcards and variables separately
dlsmith
parents: 24604
diff changeset
   139
                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
   140
            else
75dc732b45af 8042338: Refactor Types.upperBound to treat wildcards and variables separately
dlsmith
parents: 24604
diff changeset
   141
                return wildUpperBound(w.type);
75dc732b45af 8042338: Refactor Types.upperBound to treat wildcards and variables separately
dlsmith
parents: 24604
diff changeset
   142
        }
75dc732b45af 8042338: Refactor Types.upperBound to treat wildcards and variables separately
dlsmith
parents: 24604
diff changeset
   143
        else return t;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
    }
24612
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
    /**
75dc732b45af 8042338: Refactor Types.upperBound to treat wildcards and variables separately
dlsmith
parents: 24604
diff changeset
   147
     * 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
   148
     * @param t a type
75dc732b45af 8042338: Refactor Types.upperBound to treat wildcards and variables separately
dlsmith
parents: 24604
diff changeset
   149
     */
75dc732b45af 8042338: Refactor Types.upperBound to treat wildcards and variables separately
dlsmith
parents: 24604
diff changeset
   150
    public Type cvarUpperBound(Type t) {
75dc732b45af 8042338: Refactor Types.upperBound to treat wildcards and variables separately
dlsmith
parents: 24604
diff changeset
   151
        if (t.hasTag(TYPEVAR)) {
75dc732b45af 8042338: Refactor Types.upperBound to treat wildcards and variables separately
dlsmith
parents: 24604
diff changeset
   152
            TypeVar v = (TypeVar) t;
75dc732b45af 8042338: Refactor Types.upperBound to treat wildcards and variables separately
dlsmith
parents: 24604
diff changeset
   153
            return v.isCaptured() ? cvarUpperBound(v.bound) : v;
75dc732b45af 8042338: Refactor Types.upperBound to treat wildcards and variables separately
dlsmith
parents: 24604
diff changeset
   154
        }
75dc732b45af 8042338: Refactor Types.upperBound to treat wildcards and variables separately
dlsmith
parents: 24604
diff changeset
   155
        else return t;
75dc732b45af 8042338: Refactor Types.upperBound to treat wildcards and variables separately
dlsmith
parents: 24604
diff changeset
   156
    }
75dc732b45af 8042338: Refactor Types.upperBound to treat wildcards and variables separately
dlsmith
parents: 24604
diff changeset
   157
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
    /**
24293
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
   159
     * Get a wildcard's lower bound, returning non-wildcards unchanged.
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
   160
     * @param t a type argument, either a wildcard or a type
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
     */
24293
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
   162
    public Type wildLowerBound(Type t) {
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
   163
        if (t.hasTag(WILDCARD)) {
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
   164
            WildcardType w = (WildcardType) t;
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
   165
            return w.isExtendsBound() ? syms.botType : wildLowerBound(w.type);
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
   166
        }
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
   167
        else return t;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
    }
24612
75dc732b45af 8042338: Refactor Types.upperBound to treat wildcards and variables separately
dlsmith
parents: 24604
diff changeset
   169
24293
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
   170
    /**
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
   171
     * 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
   172
     * @param t a type
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
   173
     */
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
   174
    public Type cvarLowerBound(Type t) {
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
   175
        if (t.hasTag(TYPEVAR) && ((TypeVar) t).isCaptured()) {
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
   176
            return cvarLowerBound(t.getLowerBound());
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
   177
        }
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
   178
        else return t;
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
   179
    }
29292
c10d63c667cd 8073842: Invalid method reference when referencing a method on a wildcard type
mcimadamore
parents: 29147
diff changeset
   180
c10d63c667cd 8073842: Invalid method reference when referencing a method on a wildcard type
mcimadamore
parents: 29147
diff changeset
   181
    /**
c10d63c667cd 8073842: Invalid method reference when referencing a method on a wildcard type
mcimadamore
parents: 29147
diff changeset
   182
     * Recursively skip type-variables until a class/array type is found; capture conversion is then
c10d63c667cd 8073842: Invalid method reference when referencing a method on a wildcard type
mcimadamore
parents: 29147
diff changeset
   183
     * (optionally) applied to the resulting type. This is useful for i.e. computing a site that is
c10d63c667cd 8073842: Invalid method reference when referencing a method on a wildcard type
mcimadamore
parents: 29147
diff changeset
   184
     * suitable for a method lookup.
c10d63c667cd 8073842: Invalid method reference when referencing a method on a wildcard type
mcimadamore
parents: 29147
diff changeset
   185
     */
c10d63c667cd 8073842: Invalid method reference when referencing a method on a wildcard type
mcimadamore
parents: 29147
diff changeset
   186
    public Type skipTypeVars(Type site, boolean capture) {
c10d63c667cd 8073842: Invalid method reference when referencing a method on a wildcard type
mcimadamore
parents: 29147
diff changeset
   187
        while (site.hasTag(TYPEVAR)) {
c10d63c667cd 8073842: Invalid method reference when referencing a method on a wildcard type
mcimadamore
parents: 29147
diff changeset
   188
            site = site.getUpperBound();
c10d63c667cd 8073842: Invalid method reference when referencing a method on a wildcard type
mcimadamore
parents: 29147
diff changeset
   189
        }
c10d63c667cd 8073842: Invalid method reference when referencing a method on a wildcard type
mcimadamore
parents: 29147
diff changeset
   190
        return capture ? capture(site) : site;
c10d63c667cd 8073842: Invalid method reference when referencing a method on a wildcard type
mcimadamore
parents: 29147
diff changeset
   191
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   194
    // <editor-fold defaultstate="collapsed" desc="projections">
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   195
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   196
    /**
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   197
     * A projection kind. See {@link TypeProjection}
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   198
     */
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   199
    enum ProjectionKind {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   200
        UPWARDS() {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   201
            @Override
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   202
            ProjectionKind complement() {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   203
                return DOWNWARDS;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   204
            }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   205
        },
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   206
        DOWNWARDS() {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   207
            @Override
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   208
            ProjectionKind complement() {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   209
                return UPWARDS;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   210
            }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   211
        };
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   212
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   213
        abstract ProjectionKind complement();
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   214
    }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   215
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   216
    /**
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   217
     * This visitor performs upwards and downwards projections on types.
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   218
     *
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   219
     * A projection is defined as a function that takes a type T, a set of type variables V and that
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   220
     * produces another type S.
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   221
     *
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   222
     * An upwards projection maps a type T into a type S such that (i) T has no variables in V,
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   223
     * and (ii) S is an upper bound of T.
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   224
     *
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   225
     * A downwards projection maps a type T into a type S such that (i) T has no variables in V,
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   226
     * and (ii) S is a lower bound of T.
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   227
     *
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   228
     * Note that projections are only allowed to touch variables in V. Theferore it is possible for
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   229
     * a projection to leave its input type unchanged if it does not contain any variables in V.
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   230
     *
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   231
     * Moreover, note that while an upwards projection is always defined (every type as an upper bound),
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   232
     * a downwards projection is not always defined.
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   233
     *
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   234
     * Examples:
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   235
     *
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   236
     * {@code upwards(List<#CAP1>, [#CAP1]) = List<? extends String>, where #CAP1 <: String }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   237
     * {@code downwards(List<#CAP2>, [#CAP2]) = List<? super String>, where #CAP2 :> String }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   238
     * {@code upwards(List<#CAP1>, [#CAP2]) = List<#CAP1> }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   239
     * {@code downwards(List<#CAP1>, [#CAP1]) = not defined }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   240
     */
47959
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   241
    class TypeProjection extends TypeMapping<ProjectionKind> {
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   242
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   243
        List<Type> vars;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   244
        Set<Type> seen = new HashSet<>();
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   245
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   246
        public TypeProjection(List<Type> vars) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   247
            this.vars = vars;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   248
        }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   249
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   250
        @Override
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   251
        public Type visitClassType(ClassType t, ProjectionKind pkind) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   252
            if (t.isCompound()) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   253
                List<Type> components = directSupertypes(t);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   254
                List<Type> components1 = components.map(c -> c.map(this, pkind));
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   255
                if (components == components1) return t;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   256
                else return makeIntersectionType(components1);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   257
            } else {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   258
                Type outer = t.getEnclosingType();
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   259
                Type outer1 = visit(outer, pkind);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   260
                List<Type> typarams = t.getTypeArguments();
47959
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   261
                List<Type> formals = t.tsym.type.getTypeArguments();
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   262
                ListBuffer<Type> typarams1 = new ListBuffer<>();
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   263
                boolean changed = false;
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   264
                for (Type actual : typarams) {
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   265
                    Type t2 = mapTypeArgument(t, formals.head.getUpperBound(), actual, pkind);
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   266
                    if (t2.hasTag(BOT)) {
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   267
                        //not defined
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   268
                        return syms.botType;
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   269
                    }
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   270
                    typarams1.add(t2);
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   271
                    changed |= actual != t2;
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   272
                    formals = formals.tail;
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   273
                }
47959
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   274
                if (outer1 == outer && !changed) return t;
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   275
                else return new ClassType(outer1, typarams1.toList(), t.tsym, t.getMetadata()) {
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   276
                    @Override
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   277
                    protected boolean needsStripping() {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   278
                        return true;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   279
                    }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   280
                };
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   281
            }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   282
        }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   283
47959
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   284
        @Override
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   285
        public Type visitArrayType(ArrayType t, ProjectionKind s) {
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   286
            Type elemtype = t.elemtype;
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   287
            Type elemtype1 = visit(elemtype, s);
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   288
            if (elemtype1 == elemtype) {
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   289
                return t;
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   290
            } else if (elemtype1.hasTag(BOT)) {
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   291
                //undefined
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   292
                return syms.botType;
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   293
            } else {
47959
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   294
                return new ArrayType(elemtype1, t.tsym, t.metadata) {
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   295
                    @Override
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   296
                    protected boolean needsStripping() {
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   297
                        return true;
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   298
                    }
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   299
                };
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   300
            }
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   301
        }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   302
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   303
        @Override
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   304
        public Type visitTypeVar(TypeVar t, ProjectionKind pkind) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   305
            if (vars.contains(t)) {
47458
945935416d7c 8189838: Stack overflow when computing upward projection of an intersection type with fbounds
mcimadamore
parents: 47268
diff changeset
   306
                if (seen.add(t)) {
945935416d7c 8189838: Stack overflow when computing upward projection of an intersection type with fbounds
mcimadamore
parents: 47268
diff changeset
   307
                    try {
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   308
                        final Type bound;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   309
                        switch (pkind) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   310
                            case UPWARDS:
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   311
                                bound = t.getUpperBound();
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   312
                                break;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   313
                            case DOWNWARDS:
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   314
                                bound = (t.getLowerBound() == null) ?
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   315
                                        syms.botType :
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   316
                                        t.getLowerBound();
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   317
                                break;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   318
                            default:
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   319
                                Assert.error();
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   320
                                return null;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   321
                        }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   322
                        return bound.map(this, pkind);
47458
945935416d7c 8189838: Stack overflow when computing upward projection of an intersection type with fbounds
mcimadamore
parents: 47268
diff changeset
   323
                    } finally {
945935416d7c 8189838: Stack overflow when computing upward projection of an intersection type with fbounds
mcimadamore
parents: 47268
diff changeset
   324
                        seen.remove(t);
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   325
                    }
47458
945935416d7c 8189838: Stack overflow when computing upward projection of an intersection type with fbounds
mcimadamore
parents: 47268
diff changeset
   326
                } else {
945935416d7c 8189838: Stack overflow when computing upward projection of an intersection type with fbounds
mcimadamore
parents: 47268
diff changeset
   327
                    //cycle
945935416d7c 8189838: Stack overflow when computing upward projection of an intersection type with fbounds
mcimadamore
parents: 47268
diff changeset
   328
                    return pkind == ProjectionKind.UPWARDS ?
945935416d7c 8189838: Stack overflow when computing upward projection of an intersection type with fbounds
mcimadamore
parents: 47268
diff changeset
   329
                            syms.objectType : syms.botType;
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   330
                }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   331
            } else {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   332
                return t;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   333
            }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   334
        }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   335
47959
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   336
        private Type mapTypeArgument(Type site, Type declaredBound, Type t, ProjectionKind pkind) {
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   337
            return t.containsAny(vars) ?
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   338
                    t.map(new TypeArgumentProjection(site, declaredBound), pkind) :
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   339
                    t;
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   340
        }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   341
47959
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   342
        class TypeArgumentProjection extends TypeMapping<ProjectionKind> {
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   343
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   344
            Type site;
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   345
            Type declaredBound;
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   346
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   347
            TypeArgumentProjection(Type site, Type declaredBound) {
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   348
                this.site = site;
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   349
                this.declaredBound = declaredBound;
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   350
            }
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   351
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   352
            @Override
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   353
            public Type visitType(Type t, ProjectionKind pkind) {
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   354
                //type argument is some type containing restricted vars
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   355
                if (pkind == ProjectionKind.DOWNWARDS) {
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   356
                    //not defined
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   357
                    return syms.botType;
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   358
                }
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   359
                Type upper = t.map(TypeProjection.this, ProjectionKind.UPWARDS);
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   360
                Type lower = t.map(TypeProjection.this, ProjectionKind.DOWNWARDS);
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   361
                List<Type> formals = site.tsym.type.getTypeArguments();
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   362
                BoundKind bk;
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   363
                Type bound;
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   364
                if (!isSameType(upper, syms.objectType) &&
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   365
                        (declaredBound.containsAny(formals) ||
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   366
                         !isSubtype(declaredBound, upper))) {
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   367
                    bound = upper;
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   368
                    bk = EXTENDS;
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   369
                } else if (!lower.hasTag(BOT)) {
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   370
                    bound = lower;
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   371
                    bk = SUPER;
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   372
                } else {
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   373
                    bound = syms.objectType;
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   374
                    bk = UNBOUND;
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   375
                }
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   376
                return makeWildcard(bound, bk);
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   377
            }
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   378
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   379
            @Override
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   380
            public Type visitWildcardType(WildcardType wt, ProjectionKind pkind) {
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   381
                //type argument is some wildcard whose bound contains restricted vars
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   382
                Type bound = syms.botType;
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   383
                BoundKind bk = wt.kind;
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   384
                switch (wt.kind) {
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   385
                    case EXTENDS:
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   386
                        bound = wt.type.map(TypeProjection.this, pkind);
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   387
                        if (bound.hasTag(BOT)) {
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   388
                            return syms.botType;
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   389
                        }
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   390
                        break;
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   391
                    case SUPER:
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   392
                        bound = wt.type.map(TypeProjection.this, pkind.complement());
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   393
                        if (bound.hasTag(BOT)) {
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   394
                            bound = syms.objectType;
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   395
                            bk = UNBOUND;
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   396
                        }
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   397
                        break;
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   398
                }
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   399
                return makeWildcard(bound, bk);
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   400
            }
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   401
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   402
            private Type makeWildcard(Type bound, BoundKind bk) {
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   403
                return new WildcardType(bound, bk, syms.boundClass) {
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   404
                    @Override
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   405
                    protected boolean needsStripping() {
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   406
                        return true;
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   407
                    }
5dd899009525 8191802: Upward projection result is A<? extends Number> instead of A<? super Integer>
mcimadamore
parents: 47458
diff changeset
   408
                };
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   409
            }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   410
        }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   411
    }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   412
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   413
    /**
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   414
     * Computes an upward projection of given type, and vars. See {@link TypeProjection}.
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   415
     *
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   416
     * @param t the type to be projected
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   417
     * @param vars the set of type variables to be mapped
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   418
     * @return the type obtained as result of the projection
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   419
     */
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   420
    public Type upward(Type t, List<Type> vars) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   421
        return t.map(new TypeProjection(vars), ProjectionKind.UPWARDS);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   422
    }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   423
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   424
    /**
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   425
     * Computes the set of captured variables mentioned in a given type. See {@link CaptureScanner}.
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   426
     * This routine is typically used to computed the input set of variables to be used during
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   427
     * an upwards projection (see {@link Types#upward(Type, List)}).
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   428
     *
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   429
     * @param t the type where occurrences of captured variables have to be found
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   430
     * @return the set of captured variables found in t
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   431
     */
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   432
    public List<Type> captures(Type t) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   433
        CaptureScanner cs = new CaptureScanner();
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   434
        Set<Type> captures = new HashSet<>();
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   435
        cs.visit(t, captures);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   436
        return List.from(captures);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   437
    }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   438
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   439
    /**
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   440
     * This visitor scans a type recursively looking for occurrences of captured type variables.
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   441
     */
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   442
    class CaptureScanner extends SimpleVisitor<Void, Set<Type>> {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   443
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   444
        @Override
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   445
        public Void visitType(Type t, Set<Type> types) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   446
            return null;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   447
        }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   448
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   449
        @Override
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   450
        public Void visitClassType(ClassType t, Set<Type> seen) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   451
            if (t.isCompound()) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   452
                directSupertypes(t).forEach(s -> visit(s, seen));
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   453
            } else {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   454
                t.allparams().forEach(ta -> visit(ta, seen));
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   455
            }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   456
            return null;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   457
        }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   458
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   459
        @Override
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   460
        public Void visitArrayType(ArrayType t, Set<Type> seen) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   461
            return visit(t.elemtype, seen);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   462
        }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   463
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   464
        @Override
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   465
        public Void visitWildcardType(WildcardType t, Set<Type> seen) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   466
            visit(t.type, seen);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   467
            return null;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   468
        }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   469
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   470
        @Override
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   471
        public Void visitTypeVar(TypeVar t, Set<Type> seen) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   472
            if ((t.tsym.flags() & Flags.SYNTHETIC) != 0 && seen.add(t)) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   473
                visit(t.getUpperBound(), seen);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   474
            }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   475
            return null;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   476
        }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   477
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   478
        @Override
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   479
        public Void visitCapturedType(CapturedType t, Set<Type> seen) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   480
            if (seen.add(t)) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   481
                visit(t.getUpperBound(), seen);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   482
                visit(t.getLowerBound(), seen);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   483
            }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   484
            return null;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   485
        }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   486
    }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   487
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   488
    // </editor-fold>
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   489
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   490
    // <editor-fold defaultstate="collapsed" desc="isUnbounded">
06bc494ca11e Initial load
duke
parents:
diff changeset
   491
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   492
     * Checks that all the arguments to a class are unbounded
06bc494ca11e Initial load
duke
parents:
diff changeset
   493
     * wildcards or something else that doesn't make any restrictions
06bc494ca11e Initial load
duke
parents:
diff changeset
   494
     * on the arguments. If a class isUnbounded, a raw super- or
06bc494ca11e Initial load
duke
parents:
diff changeset
   495
     * subclass can be cast to it without a warning.
06bc494ca11e Initial load
duke
parents:
diff changeset
   496
     * @param t a type
06bc494ca11e Initial load
duke
parents:
diff changeset
   497
     * @return true iff the given type is unbounded or raw
06bc494ca11e Initial load
duke
parents:
diff changeset
   498
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   499
    public boolean isUnbounded(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   500
        return isUnbounded.visit(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   501
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   502
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
   503
        private final UnaryVisitor<Boolean> isUnbounded = new UnaryVisitor<Boolean>() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   504
06bc494ca11e Initial load
duke
parents:
diff changeset
   505
            public Boolean visitType(Type t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   506
                return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   507
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   508
06bc494ca11e Initial load
duke
parents:
diff changeset
   509
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   510
            public Boolean visitClassType(ClassType t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   511
                List<Type> parms = t.tsym.type.allparams();
06bc494ca11e Initial load
duke
parents:
diff changeset
   512
                List<Type> args = t.allparams();
06bc494ca11e Initial load
duke
parents:
diff changeset
   513
                while (parms.nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   514
                    WildcardType unb = new WildcardType(syms.objectType,
06bc494ca11e Initial load
duke
parents:
diff changeset
   515
                                                        BoundKind.UNBOUND,
06bc494ca11e Initial load
duke
parents:
diff changeset
   516
                                                        syms.boundClass,
24396
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 24293
diff changeset
   517
                                                        (TypeVar)parms.head);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   518
                    if (!containsType(args.head, unb))
06bc494ca11e Initial load
duke
parents:
diff changeset
   519
                        return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   520
                    parms = parms.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   521
                    args = args.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   522
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   523
                return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   524
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   525
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
   526
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
   527
06bc494ca11e Initial load
duke
parents:
diff changeset
   528
    // <editor-fold defaultstate="collapsed" desc="asSub">
06bc494ca11e Initial load
duke
parents:
diff changeset
   529
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   530
     * Return the least specific subtype of t that starts with symbol
06bc494ca11e Initial load
duke
parents:
diff changeset
   531
     * sym.  If none exists, return null.  The least specific subtype
06bc494ca11e Initial load
duke
parents:
diff changeset
   532
     * is determined as follows:
06bc494ca11e Initial load
duke
parents:
diff changeset
   533
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   534
     * <p>If there is exactly one parameterized instance of sym that is a
06bc494ca11e Initial load
duke
parents:
diff changeset
   535
     * subtype of t, that parameterized instance is returned.<br>
06bc494ca11e Initial load
duke
parents:
diff changeset
   536
     * Otherwise, if the plain type or raw type `sym' is a subtype of
06bc494ca11e Initial load
duke
parents:
diff changeset
   537
     * type t, the type `sym' itself is returned.  Otherwise, null is
06bc494ca11e Initial load
duke
parents:
diff changeset
   538
     * returned.
06bc494ca11e Initial load
duke
parents:
diff changeset
   539
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   540
    public Type asSub(Type t, Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   541
        return asSub.visit(t, sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   542
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   543
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
   544
        private final SimpleVisitor<Type,Symbol> asSub = new SimpleVisitor<Type,Symbol>() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   545
06bc494ca11e Initial load
duke
parents:
diff changeset
   546
            public Type visitType(Type t, Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   547
                return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   548
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   549
06bc494ca11e Initial load
duke
parents:
diff changeset
   550
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   551
            public Type visitClassType(ClassType t, Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   552
                if (t.tsym == sym)
06bc494ca11e Initial load
duke
parents:
diff changeset
   553
                    return t;
21712
d363f367d4c0 8027439: Compile-time error in the case of ((Integer[] & Serializable)new Integer[1]).getClass()
emc
parents: 21041
diff changeset
   554
                Type base = asSuper(sym.type, t.tsym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   555
                if (base == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   556
                    return null;
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21712
diff changeset
   557
                ListBuffer<Type> from = new ListBuffer<>();
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21712
diff changeset
   558
                ListBuffer<Type> to = new ListBuffer<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   559
                try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   560
                    adapt(base, t, from, to);
06bc494ca11e Initial load
duke
parents:
diff changeset
   561
                } catch (AdaptFailure ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   562
                    return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   563
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   564
                Type res = subst(sym.type, from.toList(), to.toList());
06bc494ca11e Initial load
duke
parents:
diff changeset
   565
                if (!isSubtype(res, t))
06bc494ca11e Initial load
duke
parents:
diff changeset
   566
                    return null;
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21712
diff changeset
   567
                ListBuffer<Type> openVars = new ListBuffer<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   568
                for (List<Type> l = sym.type.allparams();
06bc494ca11e Initial load
duke
parents:
diff changeset
   569
                     l.nonEmpty(); l = l.tail)
06bc494ca11e Initial load
duke
parents:
diff changeset
   570
                    if (res.contains(l.head) && !t.contains(l.head))
06bc494ca11e Initial load
duke
parents:
diff changeset
   571
                        openVars.append(l.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
   572
                if (openVars.nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   573
                    if (t.isRaw()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   574
                        // The subtype of a raw type is raw
06bc494ca11e Initial load
duke
parents:
diff changeset
   575
                        res = erasure(res);
06bc494ca11e Initial load
duke
parents:
diff changeset
   576
                    } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   577
                        // Unbound type arguments default to ?
06bc494ca11e Initial load
duke
parents:
diff changeset
   578
                        List<Type> opens = openVars.toList();
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21712
diff changeset
   579
                        ListBuffer<Type> qs = new ListBuffer<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   580
                        for (List<Type> iter = opens; iter.nonEmpty(); iter = iter.tail) {
24396
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 24293
diff changeset
   581
                            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
   582
                                                       syms.boundClass, (TypeVar) iter.head));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   583
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   584
                        res = subst(res, opens, qs.toList());
06bc494ca11e Initial load
duke
parents:
diff changeset
   585
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   586
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   587
                return res;
06bc494ca11e Initial load
duke
parents:
diff changeset
   588
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   589
06bc494ca11e Initial load
duke
parents:
diff changeset
   590
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   591
            public Type visitErrorType(ErrorType t, Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   592
                return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   593
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   594
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
   595
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
   596
06bc494ca11e Initial load
duke
parents:
diff changeset
   597
    // <editor-fold defaultstate="collapsed" desc="isConvertible">
06bc494ca11e Initial load
duke
parents:
diff changeset
   598
    /**
10452
c8f32009b413 7085024: internal error; cannot instantiate Foo
mcimadamore
parents: 9812
diff changeset
   599
     * Is t a subtype of or convertible via boxing/unboxing
c8f32009b413 7085024: internal error; cannot instantiate Foo
mcimadamore
parents: 9812
diff changeset
   600
     * conversion to s?
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   601
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   602
    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
   603
        if (t.hasTag(ERROR)) {
10452
c8f32009b413 7085024: internal error; cannot instantiate Foo
mcimadamore
parents: 9812
diff changeset
   604
            return true;
18646
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18412
diff changeset
   605
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   606
        boolean tPrimitive = t.isPrimitive();
06bc494ca11e Initial load
duke
parents:
diff changeset
   607
        boolean sPrimitive = s.isPrimitive();
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 7637
diff changeset
   608
        if (tPrimitive == sPrimitive) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   609
            return isSubtypeUnchecked(t, s, warn);
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 7637
diff changeset
   610
        }
45754
740593904730 8182649: Unable to integrate due to compilation error
vromero
parents: 45504
diff changeset
   611
        boolean tUndet = t.hasTag(UNDETVAR);
740593904730 8182649: Unable to integrate due to compilation error
vromero
parents: 45504
diff changeset
   612
        boolean sUndet = s.hasTag(UNDETVAR);
740593904730 8182649: Unable to integrate due to compilation error
vromero
parents: 45504
diff changeset
   613
740593904730 8182649: Unable to integrate due to compilation error
vromero
parents: 45504
diff changeset
   614
        if (tUndet || sUndet) {
740593904730 8182649: Unable to integrate due to compilation error
vromero
parents: 45504
diff changeset
   615
            return tUndet ?
740593904730 8182649: Unable to integrate due to compilation error
vromero
parents: 45504
diff changeset
   616
                    isSubtype(t, boxedTypeOrType(s)) :
740593904730 8182649: Unable to integrate due to compilation error
vromero
parents: 45504
diff changeset
   617
                    isSubtype(boxedTypeOrType(t), s);
740593904730 8182649: Unable to integrate due to compilation error
vromero
parents: 45504
diff changeset
   618
        }
740593904730 8182649: Unable to integrate due to compilation error
vromero
parents: 45504
diff changeset
   619
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   620
        return tPrimitive
06bc494ca11e Initial load
duke
parents:
diff changeset
   621
            ? isSubtype(boxedClass(t).type, s)
06bc494ca11e Initial load
duke
parents:
diff changeset
   622
            : isSubtype(unboxedType(t), s);
06bc494ca11e Initial load
duke
parents:
diff changeset
   623
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   624
06bc494ca11e Initial load
duke
parents:
diff changeset
   625
    /**
24226
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23810
diff changeset
   626
     * 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
   627
     * conversions to s?
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   628
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   629
    public boolean isConvertible(Type t, Type s) {
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14443
diff changeset
   630
        return isConvertible(t, s, noWarnings);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   631
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   632
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
   633
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   634
    // <editor-fold defaultstate="collapsed" desc="findSam">
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   635
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   636
    /**
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   637
     * Exception used to report a function descriptor lookup failure. The exception
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   638
     * 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
   639
     * messages.
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   640
     */
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   641
    public static class FunctionDescriptorLookupError extends RuntimeException {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   642
        private static final long serialVersionUID = 0;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   643
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   644
        JCDiagnostic diagnostic;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   645
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   646
        FunctionDescriptorLookupError() {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   647
            this.diagnostic = null;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   648
        }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   649
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   650
        FunctionDescriptorLookupError setMessage(JCDiagnostic diag) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   651
            this.diagnostic = diag;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   652
            return this;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   653
        }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   654
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   655
        public JCDiagnostic getDiagnostic() {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   656
            return diagnostic;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   657
        }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   658
    }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   659
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   660
    /**
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   661
     * A cache that keeps track of function descriptors associated with given
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   662
     * functional interfaces.
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   663
     */
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   664
    class DescriptorCache {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   665
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21712
diff changeset
   666
        private WeakHashMap<TypeSymbol, Entry> _map = new WeakHashMap<>();
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   667
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   668
        class FunctionDescriptor {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   669
            Symbol descSym;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   670
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   671
            FunctionDescriptor(Symbol descSym) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   672
                this.descSym = descSym;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   673
            }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   674
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   675
            public Symbol getSymbol() {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   676
                return descSym;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   677
            }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   678
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15361
diff changeset
   679
            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
   680
                site = removeWildcards(site);
3be2cdacb3b3 8006749: compiler does not allow Object protected methods to be used in lambda
mcimadamore
parents: 15718
diff changeset
   681
                if (!chk.checkValidGenericType(site)) {
3be2cdacb3b3 8006749: compiler does not allow Object protected methods to be used in lambda
mcimadamore
parents: 15718
diff changeset
   682
                    //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
   683
                    //or if it's not a subtype of the original target, issue an error
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44186
diff changeset
   684
                    throw failure(diags.fragment(Fragments.NoSuitableFunctionalIntfInst(site)));
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15361
diff changeset
   685
                }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15361
diff changeset
   686
                return memberType(site, descSym);
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   687
            }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   688
        }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   689
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   690
        class Entry {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   691
            final FunctionDescriptor cachedDescRes;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   692
            final int prevMark;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   693
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   694
            public Entry(FunctionDescriptor cachedDescRes,
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   695
                    int prevMark) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   696
                this.cachedDescRes = cachedDescRes;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   697
                this.prevMark = prevMark;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   698
            }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   699
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   700
            boolean matches(int mark) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   701
                return  this.prevMark == mark;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   702
            }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   703
        }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   704
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   705
        FunctionDescriptor get(TypeSymbol origin) throws FunctionDescriptorLookupError {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   706
            Entry e = _map.get(origin);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   707
            CompoundScope members = membersClosure(origin.type, false);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   708
            if (e == null ||
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   709
                    !e.matches(members.getMark())) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   710
                FunctionDescriptor descRes = findDescriptorInternal(origin, members);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   711
                _map.put(origin, new Entry(descRes, members.getMark()));
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   712
                return descRes;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   713
            }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   714
            else {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   715
                return e.cachedDescRes;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   716
            }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   717
        }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   718
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   719
        /**
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   720
         * Compute the function descriptor associated with a given functional interface
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   721
         */
18646
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18412
diff changeset
   722
        public FunctionDescriptor findDescriptorInternal(TypeSymbol origin,
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18412
diff changeset
   723
                CompoundScope membersCache) throws FunctionDescriptorLookupError {
15361
01f1828683e6 8005299: Add FunctionalInterface checking to javac
mcimadamore
parents: 14949
diff changeset
   724
            if (!origin.isInterface() || (origin.flags() & ANNOTATION) != 0) {
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   725
                //t must be an interface
15361
01f1828683e6 8005299: Add FunctionalInterface checking to javac
mcimadamore
parents: 14949
diff changeset
   726
                throw failure("not.a.functional.intf", origin);
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   727
            }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   728
20249
93f8eae31092 6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents: 19914
diff changeset
   729
            final ListBuffer<Symbol> abstracts = new ListBuffer<>();
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25441
diff changeset
   730
            for (Symbol sym : membersCache.getSymbols(new DescriptorFilter(origin))) {
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   731
                Type mtype = memberType(origin.type, sym);
40591
8843f886a89f 8047338: javac is not correctly filtering non-members methods to obtain the function descriptor
vromero
parents: 39363
diff changeset
   732
                if (abstracts.isEmpty()) {
8843f886a89f 8047338: javac is not correctly filtering non-members methods to obtain the function descriptor
vromero
parents: 39363
diff changeset
   733
                    abstracts.append(sym);
8843f886a89f 8047338: javac is not correctly filtering non-members methods to obtain the function descriptor
vromero
parents: 39363
diff changeset
   734
                } else if ((sym.name == abstracts.first().name &&
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   735
                        overrideEquivalent(mtype, memberType(origin.type, abstracts.first())))) {
40591
8843f886a89f 8047338: javac is not correctly filtering non-members methods to obtain the function descriptor
vromero
parents: 39363
diff changeset
   736
                    if (!abstracts.stream().filter(msym -> msym.owner.isSubClass(sym.enclClass(), Types.this))
8843f886a89f 8047338: javac is not correctly filtering non-members methods to obtain the function descriptor
vromero
parents: 39363
diff changeset
   737
                            .map(msym -> memberType(origin.type, msym))
8843f886a89f 8047338: javac is not correctly filtering non-members methods to obtain the function descriptor
vromero
parents: 39363
diff changeset
   738
                            .anyMatch(abstractMType -> isSubSignature(abstractMType, mtype))) {
8843f886a89f 8047338: javac is not correctly filtering non-members methods to obtain the function descriptor
vromero
parents: 39363
diff changeset
   739
                        abstracts.append(sym);
8843f886a89f 8047338: javac is not correctly filtering non-members methods to obtain the function descriptor
vromero
parents: 39363
diff changeset
   740
                    }
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   741
                } else {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   742
                    //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
   743
                    throw failure("not.a.functional.intf.1",  origin,
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44186
diff changeset
   744
                            diags.fragment(Fragments.IncompatibleAbstracts(Kinds.kindName(origin), origin)));
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   745
                }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   746
            }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   747
            if (abstracts.isEmpty()) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   748
                //t must define a suitable non-generic method
15361
01f1828683e6 8005299: Add FunctionalInterface checking to javac
mcimadamore
parents: 14949
diff changeset
   749
                throw failure("not.a.functional.intf.1", origin,
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44186
diff changeset
   750
                            diags.fragment(Fragments.NoAbstracts(Kinds.kindName(origin), origin)));
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   751
            } else if (abstracts.size() == 1) {
14723
46aa71a5e4e0 8004102: Add support for generic functional descriptors
mcimadamore
parents: 14719
diff changeset
   752
                return new FunctionDescriptor(abstracts.first());
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   753
            } else { // size > 1
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   754
                FunctionDescriptor descRes = mergeDescriptors(origin, abstracts.toList());
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   755
                if (descRes == null) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   756
                    //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
   757
                    ListBuffer<JCDiagnostic> descriptors = new ListBuffer<>();
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   758
                    for (Symbol desc : abstracts) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   759
                        String key = desc.type.getThrownTypes().nonEmpty() ?
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   760
                                "descriptor.throws" : "descriptor";
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   761
                        descriptors.append(diags.fragment(key, desc.name,
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   762
                                desc.type.getParameterTypes(),
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   763
                                desc.type.getReturnType(),
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   764
                                desc.type.getThrownTypes()));
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   765
                    }
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44186
diff changeset
   766
                    JCDiagnostic msg =
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44186
diff changeset
   767
                            diags.fragment(Fragments.IncompatibleDescsInFunctionalIntf(Kinds.kindName(origin),
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44186
diff changeset
   768
                                                                                       origin));
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   769
                    JCDiagnostic.MultilineDiagnostic incompatibleDescriptors =
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44186
diff changeset
   770
                            new JCDiagnostic.MultilineDiagnostic(msg, descriptors.toList());
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   771
                    throw failure(incompatibleDescriptors);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   772
                }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   773
                return descRes;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   774
            }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   775
        }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   776
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   777
        /**
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   778
         * Compute a synthetic type for the target descriptor given a list
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   779
         * of override-equivalent methods in the functional interface type.
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   780
         * 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
   781
         * and return-type substitutable with each method in the original list.
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   782
         */
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   783
        private FunctionDescriptor mergeDescriptors(TypeSymbol origin, List<Symbol> methodSyms) {
41526
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
   784
            return mergeAbstracts(methodSyms, origin.type, false)
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
   785
                    .map(bestSoFar -> new FunctionDescriptor(bestSoFar.baseSymbol()) {
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
   786
                        @Override
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
   787
                        public Type getType(Type origin) {
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
   788
                            Type mt = memberType(origin, getSymbol());
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
   789
                            return createMethodTypeWithThrown(mt, bestSoFar.type.getThrownTypes());
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   790
                        }
41526
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
   791
                    }).orElse(null);
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   792
        }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   793
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   794
        FunctionDescriptorLookupError failure(String msg, Object... args) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   795
            return failure(diags.fragment(msg, args));
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   796
        }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   797
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   798
        FunctionDescriptorLookupError failure(JCDiagnostic diag) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   799
            return functionDescriptorLookupError.setMessage(diag);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   800
        }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   801
    }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   802
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   803
    private DescriptorCache descCache = new DescriptorCache();
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   804
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   805
    /**
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   806
     * Find the method descriptor associated to this class symbol - if the
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   807
     * symbol 'origin' is not a functional interface, an exception is thrown.
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   808
     */
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   809
    public Symbol findDescriptorSymbol(TypeSymbol origin) throws FunctionDescriptorLookupError {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   810
        return descCache.get(origin).getSymbol();
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   811
    }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   812
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   813
    /**
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   814
     * 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
   815
     * 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
   816
     */
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   817
    public Type findDescriptorType(Type origin) throws FunctionDescriptorLookupError {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   818
        return descCache.get(origin.tsym).getType(origin);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   819
    }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   820
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   821
    /**
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   822
     * Is given type a functional interface?
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   823
     */
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   824
    public boolean isFunctionalInterface(TypeSymbol tsym) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   825
        try {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   826
            findDescriptorSymbol(tsym);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   827
            return true;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   828
        } catch (FunctionDescriptorLookupError ex) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   829
            return false;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   830
        }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   831
    }
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15361
diff changeset
   832
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15361
diff changeset
   833
    public boolean isFunctionalInterface(Type site) {
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15361
diff changeset
   834
        try {
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15361
diff changeset
   835
            findDescriptorType(site);
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15361
diff changeset
   836
            return true;
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15361
diff changeset
   837
        } catch (FunctionDescriptorLookupError ex) {
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15361
diff changeset
   838
            return false;
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15361
diff changeset
   839
        }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15361
diff changeset
   840
    }
16292
3be2cdacb3b3 8006749: compiler does not allow Object protected methods to be used in lambda
mcimadamore
parents: 15718
diff changeset
   841
3be2cdacb3b3 8006749: compiler does not allow Object protected methods to be used in lambda
mcimadamore
parents: 15718
diff changeset
   842
    public Type removeWildcards(Type site) {
34757
85ceb9850b1d 8142876: Javac does not correctly implement wildcards removal from functional interfaces
mcimadamore
parents: 34756
diff changeset
   843
        if (site.getTypeArguments().stream().anyMatch(t -> t.hasTag(WILDCARD))) {
85ceb9850b1d 8142876: Javac does not correctly implement wildcards removal from functional interfaces
mcimadamore
parents: 34756
diff changeset
   844
            //compute non-wildcard parameterization - JLS 9.9
85ceb9850b1d 8142876: Javac does not correctly implement wildcards removal from functional interfaces
mcimadamore
parents: 34756
diff changeset
   845
            List<Type> actuals = site.getTypeArguments();
85ceb9850b1d 8142876: Javac does not correctly implement wildcards removal from functional interfaces
mcimadamore
parents: 34756
diff changeset
   846
            List<Type> formals = site.tsym.type.getTypeArguments();
85ceb9850b1d 8142876: Javac does not correctly implement wildcards removal from functional interfaces
mcimadamore
parents: 34756
diff changeset
   847
            ListBuffer<Type> targs = new ListBuffer<>();
85ceb9850b1d 8142876: Javac does not correctly implement wildcards removal from functional interfaces
mcimadamore
parents: 34756
diff changeset
   848
            for (Type formal : formals) {
85ceb9850b1d 8142876: Javac does not correctly implement wildcards removal from functional interfaces
mcimadamore
parents: 34756
diff changeset
   849
                Type actual = actuals.head;
85ceb9850b1d 8142876: Javac does not correctly implement wildcards removal from functional interfaces
mcimadamore
parents: 34756
diff changeset
   850
                Type bound = formal.getUpperBound();
85ceb9850b1d 8142876: Javac does not correctly implement wildcards removal from functional interfaces
mcimadamore
parents: 34756
diff changeset
   851
                if (actuals.head.hasTag(WILDCARD)) {
85ceb9850b1d 8142876: Javac does not correctly implement wildcards removal from functional interfaces
mcimadamore
parents: 34756
diff changeset
   852
                    WildcardType wt = (WildcardType)actual;
85ceb9850b1d 8142876: Javac does not correctly implement wildcards removal from functional interfaces
mcimadamore
parents: 34756
diff changeset
   853
                    //check that bound does not contain other formals
85ceb9850b1d 8142876: Javac does not correctly implement wildcards removal from functional interfaces
mcimadamore
parents: 34756
diff changeset
   854
                    if (bound.containsAny(formals)) {
85ceb9850b1d 8142876: Javac does not correctly implement wildcards removal from functional interfaces
mcimadamore
parents: 34756
diff changeset
   855
                        targs.add(wt.type);
85ceb9850b1d 8142876: Javac does not correctly implement wildcards removal from functional interfaces
mcimadamore
parents: 34756
diff changeset
   856
                    } else {
85ceb9850b1d 8142876: Javac does not correctly implement wildcards removal from functional interfaces
mcimadamore
parents: 34756
diff changeset
   857
                        //compute new type-argument based on declared bound and wildcard bound
85ceb9850b1d 8142876: Javac does not correctly implement wildcards removal from functional interfaces
mcimadamore
parents: 34756
diff changeset
   858
                        switch (wt.kind) {
85ceb9850b1d 8142876: Javac does not correctly implement wildcards removal from functional interfaces
mcimadamore
parents: 34756
diff changeset
   859
                            case UNBOUND:
85ceb9850b1d 8142876: Javac does not correctly implement wildcards removal from functional interfaces
mcimadamore
parents: 34756
diff changeset
   860
                                targs.add(bound);
85ceb9850b1d 8142876: Javac does not correctly implement wildcards removal from functional interfaces
mcimadamore
parents: 34756
diff changeset
   861
                                break;
85ceb9850b1d 8142876: Javac does not correctly implement wildcards removal from functional interfaces
mcimadamore
parents: 34756
diff changeset
   862
                            case EXTENDS:
85ceb9850b1d 8142876: Javac does not correctly implement wildcards removal from functional interfaces
mcimadamore
parents: 34756
diff changeset
   863
                                targs.add(glb(bound, wt.type));
85ceb9850b1d 8142876: Javac does not correctly implement wildcards removal from functional interfaces
mcimadamore
parents: 34756
diff changeset
   864
                                break;
85ceb9850b1d 8142876: Javac does not correctly implement wildcards removal from functional interfaces
mcimadamore
parents: 34756
diff changeset
   865
                            case SUPER:
85ceb9850b1d 8142876: Javac does not correctly implement wildcards removal from functional interfaces
mcimadamore
parents: 34756
diff changeset
   866
                                targs.add(wt.type);
85ceb9850b1d 8142876: Javac does not correctly implement wildcards removal from functional interfaces
mcimadamore
parents: 34756
diff changeset
   867
                                break;
85ceb9850b1d 8142876: Javac does not correctly implement wildcards removal from functional interfaces
mcimadamore
parents: 34756
diff changeset
   868
                            default:
85ceb9850b1d 8142876: Javac does not correctly implement wildcards removal from functional interfaces
mcimadamore
parents: 34756
diff changeset
   869
                                Assert.error("Cannot get here!");
85ceb9850b1d 8142876: Javac does not correctly implement wildcards removal from functional interfaces
mcimadamore
parents: 34756
diff changeset
   870
                        }
16311
1090c7ac6ae2 8008293: Declared bounds not considered when functional interface containing unbound wildcards is instantiated
mcimadamore
parents: 16300
diff changeset
   871
                    }
16292
3be2cdacb3b3 8006749: compiler does not allow Object protected methods to be used in lambda
mcimadamore
parents: 15718
diff changeset
   872
                } else {
34757
85ceb9850b1d 8142876: Javac does not correctly implement wildcards removal from functional interfaces
mcimadamore
parents: 34756
diff changeset
   873
                    //not a wildcard - the new type argument remains unchanged
85ceb9850b1d 8142876: Javac does not correctly implement wildcards removal from functional interfaces
mcimadamore
parents: 34756
diff changeset
   874
                    targs.add(actual);
16292
3be2cdacb3b3 8006749: compiler does not allow Object protected methods to be used in lambda
mcimadamore
parents: 15718
diff changeset
   875
                }
34757
85ceb9850b1d 8142876: Javac does not correctly implement wildcards removal from functional interfaces
mcimadamore
parents: 34756
diff changeset
   876
                actuals = actuals.tail;
16292
3be2cdacb3b3 8006749: compiler does not allow Object protected methods to be used in lambda
mcimadamore
parents: 15718
diff changeset
   877
            }
34757
85ceb9850b1d 8142876: Javac does not correctly implement wildcards removal from functional interfaces
mcimadamore
parents: 34756
diff changeset
   878
            return subst(site.tsym.type, formals, targs.toList());
16292
3be2cdacb3b3 8006749: compiler does not allow Object protected methods to be used in lambda
mcimadamore
parents: 15718
diff changeset
   879
        } else {
3be2cdacb3b3 8006749: compiler does not allow Object protected methods to be used in lambda
mcimadamore
parents: 15718
diff changeset
   880
            return site;
3be2cdacb3b3 8006749: compiler does not allow Object protected methods to be used in lambda
mcimadamore
parents: 15718
diff changeset
   881
        }
3be2cdacb3b3 8006749: compiler does not allow Object protected methods to be used in lambda
mcimadamore
parents: 15718
diff changeset
   882
    }
18730
95354d510139 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18662
diff changeset
   883
95354d510139 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18662
diff changeset
   884
    /**
95354d510139 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18662
diff changeset
   885
     * 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
   886
     * 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
   887
     * 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
   888
     * (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
   889
     */
95354d510139 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18662
diff changeset
   890
    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
   891
        if (targets.isEmpty()) {
19127
8a0cbd5cb055 8020804: javac crashes when speculative attribution infers intersection type with array component
mcimadamore
parents: 19124
diff changeset
   892
            return null;
8a0cbd5cb055 8020804: javac crashes when speculative attribution infers intersection type with array component
mcimadamore
parents: 19124
diff changeset
   893
        }
18730
95354d510139 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18662
diff changeset
   894
        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
   895
        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
   896
        ClassSymbol csym = new ClassSymbol(cflags, name, env.enclClass.sym.outermostClass());
30066
d74c06a92bd8 8078600: Infinite loop when compiling annotations with -XDcompletionDeps
alundblad
parents: 29842
diff changeset
   897
        csym.completer = Completer.NULL_COMPLETER;
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25441
diff changeset
   898
        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
   899
        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
   900
        csym.members_field.enter(instDescSym);
42828
cce89649f958 8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents: 42819
diff changeset
   901
        Type.ClassType ctype = new Type.ClassType(Type.noType, List.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
   902
        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
   903
        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
   904
        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
   905
        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
   906
        return csym;
95354d510139 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18662
diff changeset
   907
    }
95354d510139 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18662
diff changeset
   908
95354d510139 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18662
diff changeset
   909
    /**
95354d510139 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18662
diff changeset
   910
     * 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
   911
     * 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
   912
     * erased signatures.
95354d510139 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18662
diff changeset
   913
     */
95354d510139 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18662
diff changeset
   914
    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
   915
        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
   916
        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
   917
        CompoundScope members = membersClosure(origin.type, false);
20249
93f8eae31092 6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents: 19914
diff changeset
   918
        ListBuffer<Symbol> overridden = new ListBuffer<>();
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25441
diff changeset
   919
        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
   920
            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
   921
            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
   922
                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
   923
                    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
   924
                            (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
   925
                            (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
   926
                            (((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
   927
                        continue outer;
95354d510139 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18662
diff changeset
   928
                    }
95354d510139 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18662
diff changeset
   929
                }
95354d510139 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18662
diff changeset
   930
                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
   931
            }
95354d510139 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18662
diff changeset
   932
        }
95354d510139 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18662
diff changeset
   933
        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
   934
    }
95354d510139 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18662
diff changeset
   935
    //where
95354d510139 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18662
diff changeset
   936
        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
   937
            public boolean accepts(Symbol t) {
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 27125
diff changeset
   938
                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
   939
                        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
   940
                        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
   941
                        (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
   942
            }
95354d510139 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18662
diff changeset
   943
        };
21712
d363f367d4c0 8027439: Compile-time error in the case of ((Integer[] & Serializable)new Integer[1]).getClass()
emc
parents: 21041
diff changeset
   944
        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
   945
            //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
   946
            //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
   947
            //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
   948
            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
   949
                    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
   950
                    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
   951
                return false;
95354d510139 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18662
diff changeset
   952
            }
21712
d363f367d4c0 8027439: Compile-time error in the case of ((Integer[] & Serializable)new Integer[1]).getClass()
emc
parents: 21041
diff changeset
   953
            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
   954
                return true;
95354d510139 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18662
diff changeset
   955
            }
95354d510139 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18662
diff changeset
   956
            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
   957
                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
   958
                    return true;
95354d510139 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18662
diff changeset
   959
                }
95354d510139 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18662
diff changeset
   960
            }
95354d510139 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18662
diff changeset
   961
            return false;
95354d510139 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18662
diff changeset
   962
        }
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   963
    // </editor-fold>
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   964
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   965
   /**
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   966
    * 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
   967
    * 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
   968
    */
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   969
    class DescriptorFilter implements Filter<Symbol> {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   970
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   971
       TypeSymbol origin;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   972
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   973
       DescriptorFilter(TypeSymbol origin) {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   974
           this.origin = origin;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   975
       }
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   976
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   977
       @Override
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   978
       public boolean accepts(Symbol sym) {
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 27125
diff changeset
   979
           return sym.kind == MTH &&
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   980
                   (sym.flags() & (ABSTRACT | DEFAULT)) == ABSTRACT &&
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   981
                   !overridesObjectMethod(origin, sym) &&
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   982
                   (interfaceCandidates(origin.type, (MethodSymbol)sym).head.flags() & DEFAULT) == 0;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   983
       }
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21712
diff changeset
   984
    }
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
   985
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   986
    // <editor-fold defaultstate="collapsed" desc="isSubtype">
06bc494ca11e Initial load
duke
parents:
diff changeset
   987
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   988
     * Is t an unchecked subtype of s?
06bc494ca11e Initial load
duke
parents:
diff changeset
   989
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   990
    public boolean isSubtypeUnchecked(Type t, Type s) {
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14443
diff changeset
   991
        return isSubtypeUnchecked(t, s, noWarnings);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   992
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   993
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   994
     * Is t an unchecked subtype of s?
06bc494ca11e Initial load
duke
parents:
diff changeset
   995
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   996
    public boolean isSubtypeUnchecked(Type t, Type s, Warner warn) {
39100
5dc24593e3ae 8159680: Inference failure with unchecked subtyping and arrays
mcimadamore
parents: 38834
diff changeset
   997
        boolean result = isSubtypeUncheckedInternal(t, s, true, warn);
10810
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
   998
        if (result) {
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
   999
            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
  1000
        }
10810
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
  1001
        return result;
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
  1002
    }
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
  1003
    //where
39100
5dc24593e3ae 8159680: Inference failure with unchecked subtyping and arrays
mcimadamore
parents: 38834
diff changeset
  1004
        private boolean isSubtypeUncheckedInternal(Type t, Type s, boolean capture, Warner warn) {
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
  1005
            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
  1006
                if (((ArrayType)t).elemtype.isPrimitive()) {
10810
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
  1007
                    return isSameType(elemtype(t), elemtype(s));
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
  1008
                } else {
39100
5dc24593e3ae 8159680: Inference failure with unchecked subtyping and arrays
mcimadamore
parents: 38834
diff changeset
  1009
                    return isSubtypeUncheckedInternal(elemtype(t), elemtype(s), false, warn);
10810
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
  1010
                }
39100
5dc24593e3ae 8159680: Inference failure with unchecked subtyping and arrays
mcimadamore
parents: 38834
diff changeset
  1011
            } else if (isSubtype(t, s, capture)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1012
                return true;
18646
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18412
diff changeset
  1013
            } else if (t.hasTag(TYPEVAR)) {
39100
5dc24593e3ae 8159680: Inference failure with unchecked subtyping and arrays
mcimadamore
parents: 38834
diff changeset
  1014
                return isSubtypeUncheckedInternal(t.getUpperBound(), s, false, warn);
18646
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18412
diff changeset
  1015
            } 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
  1016
                Type t2 = asSuper(t, s.tsym);
10810
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
  1017
                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
  1018
                    if (isReifiable(s)) {
10810
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
  1019
                        warn.silentWarn(LintCategory.UNCHECKED);
18646
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18412
diff changeset
  1020
                    } else {
10810
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
  1021
                        warn.warn(LintCategory.UNCHECKED);
18646
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18412
diff changeset
  1022
                    }
10810
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
  1023
                    return true;
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
  1024
                }
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
  1025
            }
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
  1026
            return false;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1027
        }
10810
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
  1028
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
  1029
        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
  1030
            if (!t.hasTag(ARRAY) || isReifiable(t)) {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
  1031
                return;
18646
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18412
diff changeset
  1032
            }
10810
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
  1033
            ArrayType from = (ArrayType)t;
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
  1034
            boolean shouldWarn = false;
18646
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18412
diff changeset
  1035
            switch (s.getTag()) {
10810
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
  1036
                case ARRAY:
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
  1037
                    ArrayType to = (ArrayType)s;
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
  1038
                    shouldWarn = from.isVarargs() &&
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
  1039
                            !to.isVarargs() &&
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
  1040
                            !isReifiable(from);
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
  1041
                    break;
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
  1042
                case CLASS:
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
  1043
                    shouldWarn = from.isVarargs();
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
  1044
                    break;
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
  1045
            }
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
  1046
            if (shouldWarn) {
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
  1047
                warn.warn(LintCategory.VARARGS);
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
  1048
            }
9444a714fdaf 7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents: 10634
diff changeset
  1049
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1050
06bc494ca11e Initial load
duke
parents:
diff changeset
  1051
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1052
     * Is t a subtype of s?<br>
06bc494ca11e Initial load
duke
parents:
diff changeset
  1053
     * (not defined for Method and ForAll types)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1054
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1055
    final public boolean isSubtype(Type t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1056
        return isSubtype(t, s, true);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1057
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1058
    final public boolean isSubtypeNoCapture(Type t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1059
        return isSubtype(t, s, false);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1060
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1061
    public boolean isSubtype(Type t, Type s, boolean capture) {
29842
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29555
diff changeset
  1062
        if (t.equalsIgnoreMetadata(s))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1063
            return true;
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
  1064
        if (s.isPartial())
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1065
            return isSuperType(s, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1066
3140
15a274b13051 6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents: 2223
diff changeset
  1067
        if (s.isCompound()) {
15a274b13051 6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents: 2223
diff changeset
  1068
            for (Type s2 : interfaces(s).prepend(supertype(s))) {
15a274b13051 6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents: 2223
diff changeset
  1069
                if (!isSubtype(t, s2, capture))
15a274b13051 6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents: 2223
diff changeset
  1070
                    return false;
15a274b13051 6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents: 2223
diff changeset
  1071
            }
15a274b13051 6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents: 2223
diff changeset
  1072
            return true;
15a274b13051 6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents: 2223
diff changeset
  1073
        }
15a274b13051 6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents: 2223
diff changeset
  1074
28451
d4fdab318a53 8062358: ClassCastException in TransTypes.visitApply
jlahoda
parents: 27390
diff changeset
  1075
        // 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
  1076
        // 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
  1077
        // (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
  1078
        if (!t.hasTag(UNDETVAR) && !t.isCompound()) {
24293
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
  1079
            // 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
  1080
            Type lower = cvarLowerBound(wildLowerBound(s));
28451
d4fdab318a53 8062358: ClassCastException in TransTypes.visitApply
jlahoda
parents: 27390
diff changeset
  1081
            if (s != lower && !lower.hasTag(BOT))
24293
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
  1082
                return isSubtype(capture ? capture(t) : t, lower, false);
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
  1083
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1084
06bc494ca11e Initial load
duke
parents:
diff changeset
  1085
        return isSubtype.visit(capture ? capture(t) : t, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1086
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1087
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
  1088
        private TypeRelation isSubtype = new TypeRelation()
06bc494ca11e Initial load
duke
parents:
diff changeset
  1089
        {
18646
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18412
diff changeset
  1090
            @Override
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1091
            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
  1092
                switch (t.getTag()) {
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
  1093
                 case BYTE:
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
  1094
                     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
  1095
                 case CHAR:
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
  1096
                     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
  1097
                 case SHORT: case INT: case LONG:
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
  1098
                 case FLOAT: case DOUBLE:
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
  1099
                     return t.getTag().isSubRangeOf(s.getTag());
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
  1100
                 case BOOLEAN: case VOID:
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
  1101
                     return t.hasTag(s.getTag());
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
  1102
                 case TYPEVAR:
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
  1103
                     return isSubtypeNoCapture(t.getUpperBound(), s);
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
  1104
                 case BOT:
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
  1105
                     return
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
  1106
                         s.hasTag(BOT) || s.hasTag(CLASS) ||
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
  1107
                         s.hasTag(ARRAY) || s.hasTag(TYPEVAR);
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
  1108
                 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
  1109
                 case NONE:
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
  1110
                     return false;
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
  1111
                 default:
18646
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18412
diff changeset
  1112
                     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
  1113
                 }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1114
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1115
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21712
diff changeset
  1116
            private Set<TypePair> cache = new HashSet<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1117
06bc494ca11e Initial load
duke
parents:
diff changeset
  1118
            private boolean containsTypeRecursive(Type t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1119
                TypePair pair = new TypePair(t, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1120
                if (cache.add(pair)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1121
                    try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1122
                        return containsType(t.getTypeArguments(),
06bc494ca11e Initial load
duke
parents:
diff changeset
  1123
                                            s.getTypeArguments());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1124
                    } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1125
                        cache.remove(pair);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1126
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1127
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1128
                    return containsType(t.getTypeArguments(),
06bc494ca11e Initial load
duke
parents:
diff changeset
  1129
                                        rewriteSupers(s).getTypeArguments());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1130
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1131
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1132
06bc494ca11e Initial load
duke
parents:
diff changeset
  1133
            private Type rewriteSupers(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1134
                if (!t.isParameterized())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1135
                    return t;
20249
93f8eae31092 6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents: 19914
diff changeset
  1136
                ListBuffer<Type> from = new ListBuffer<>();
93f8eae31092 6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents: 19914
diff changeset
  1137
                ListBuffer<Type> to = new ListBuffer<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1138
                adaptSelf(t, from, to);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1139
                if (from.isEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1140
                    return t;
20249
93f8eae31092 6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents: 19914
diff changeset
  1141
                ListBuffer<Type> rewrite = new ListBuffer<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1142
                boolean changed = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1143
                for (Type orig : to.toList()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1144
                    Type s = rewriteSupers(orig);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1145
                    if (s.isSuperBound() && !s.isExtendsBound()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1146
                        s = new WildcardType(syms.objectType,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1147
                                             BoundKind.UNBOUND,
24396
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 24293
diff changeset
  1148
                                             syms.boundClass,
26781
a786b07c7b91 8048614: Add TypeMetadata to contain type annotations and other type information
emc
parents: 26107
diff changeset
  1149
                                             s.getMetadata());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1150
                        changed = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1151
                    } else if (s != orig) {
24612
75dc732b45af 8042338: Refactor Types.upperBound to treat wildcards and variables separately
dlsmith
parents: 24604
diff changeset
  1152
                        s = new WildcardType(wildUpperBound(s),
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1153
                                             BoundKind.EXTENDS,
24396
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 24293
diff changeset
  1154
                                             syms.boundClass,
26781
a786b07c7b91 8048614: Add TypeMetadata to contain type annotations and other type information
emc
parents: 26107
diff changeset
  1155
                                             s.getMetadata());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1156
                        changed = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1157
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1158
                    rewrite.append(s);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1159
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1160
                if (changed)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1161
                    return subst(t.tsym.type, from.toList(), rewrite.toList());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1162
                else
06bc494ca11e Initial load
duke
parents:
diff changeset
  1163
                    return t;
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 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
  1168
                Type sup = asSuper(t, s.tsym);
24397
32aef69ee177 8042656: Inference produces spurious error for intersection type bounds check
dlsmith
parents: 24396
diff changeset
  1169
                if (sup == null) return false;
32aef69ee177 8042656: Inference produces spurious error for intersection type bounds check
dlsmith
parents: 24396
diff changeset
  1170
                // 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
  1171
                if (!sup.hasTag(CLASS)) return isSubtypeNoCapture(sup, s);
32aef69ee177 8042656: Inference produces spurious error for intersection type bounds check
dlsmith
parents: 24396
diff changeset
  1172
                return sup.tsym == s.tsym
32aef69ee177 8042656: Inference produces spurious error for intersection type bounds check
dlsmith
parents: 24396
diff changeset
  1173
                     // Check type variable containment
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1174
                    && (!s.isParameterized() || containsTypeRecursive(s, sup))
06bc494ca11e Initial load
duke
parents:
diff changeset
  1175
                    && isSubtypeNoCapture(sup.getEnclosingType(),
06bc494ca11e Initial load
duke
parents:
diff changeset
  1176
                                          s.getEnclosingType());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1177
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1178
06bc494ca11e Initial load
duke
parents:
diff changeset
  1179
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1180
            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
  1181
                if (s.hasTag(ARRAY)) {
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
  1182
                    if (t.elemtype.isPrimitive())
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1183
                        return isSameType(t.elemtype, elemtype(s));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1184
                    else
06bc494ca11e Initial load
duke
parents:
diff changeset
  1185
                        return isSubtypeNoCapture(t.elemtype, elemtype(s));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1186
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1187
18646
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18412
diff changeset
  1188
                if (s.hasTag(CLASS)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1189
                    Name sname = s.tsym.getQualifiedName();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1190
                    return sname == names.java_lang_Object
06bc494ca11e Initial load
duke
parents:
diff changeset
  1191
                        || sname == names.java_lang_Cloneable
06bc494ca11e Initial load
duke
parents:
diff changeset
  1192
                        || sname == names.java_io_Serializable;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1193
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1194
06bc494ca11e Initial load
duke
parents:
diff changeset
  1195
                return false;
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 visitUndetVar(UndetVar t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1200
                //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
  1201
                if (t == s || t.qtype == s || s.hasTag(ERROR) || s.hasTag(UNKNOWN)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1202
                    return true;
18646
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18412
diff changeset
  1203
                } else if (s.hasTag(BOT)) {
10634
7f15f5a11ae9 7086586: Inference producing null type argument
mcimadamore
parents: 10453
diff changeset
  1204
                    //if 's' is 'null' there's no instantiated type U for which
7f15f5a11ae9 7086586: Inference producing null type argument
mcimadamore
parents: 10453
diff changeset
  1205
                    //U <: s (but 'null' itself, which is not a valid type)
7f15f5a11ae9 7086586: Inference producing null type argument
mcimadamore
parents: 10453
diff changeset
  1206
                    return false;
7f15f5a11ae9 7086586: Inference producing null type argument
mcimadamore
parents: 10453
diff changeset
  1207
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1208
14048
308d1cf8fe46 7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents: 13689
diff changeset
  1209
                t.addBound(InferenceBound.UPPER, s, Types.this);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1210
                return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1211
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1212
06bc494ca11e Initial load
duke
parents:
diff changeset
  1213
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1214
            public Boolean visitErrorType(ErrorType t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1215
                return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1216
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1217
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
  1218
06bc494ca11e Initial load
duke
parents:
diff changeset
  1219
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1220
     * Is t a subtype of every type in given list `ts'?<br>
06bc494ca11e Initial load
duke
parents:
diff changeset
  1221
     * (not defined for Method and ForAll types)<br>
06bc494ca11e Initial load
duke
parents:
diff changeset
  1222
     * Allows unchecked conversions.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1223
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1224
    public boolean isSubtypeUnchecked(Type t, List<Type> ts, Warner warn) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1225
        for (List<Type> l = ts; l.nonEmpty(); l = l.tail)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1226
            if (!isSubtypeUnchecked(t, l.head, warn))
06bc494ca11e Initial load
duke
parents:
diff changeset
  1227
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1228
        return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1229
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1230
06bc494ca11e Initial load
duke
parents:
diff changeset
  1231
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1232
     * Are corresponding elements of ts subtypes of ss?  If lists are
06bc494ca11e Initial load
duke
parents:
diff changeset
  1233
     * of different length, return false.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1234
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1235
    public boolean isSubtypes(List<Type> ts, List<Type> ss) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1236
        while (ts.tail != null && ss.tail != null
06bc494ca11e Initial load
duke
parents:
diff changeset
  1237
               /*inlined: ts.nonEmpty() && ss.nonEmpty()*/ &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1238
               isSubtype(ts.head, ss.head)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1239
            ts = ts.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1240
            ss = ss.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1241
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1242
        return ts.tail == null && ss.tail == null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1243
        /*inlined: ts.isEmpty() && ss.isEmpty();*/
06bc494ca11e Initial load
duke
parents:
diff changeset
  1244
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1245
06bc494ca11e Initial load
duke
parents:
diff changeset
  1246
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1247
     * Are corresponding elements of ts subtypes of ss, allowing
06bc494ca11e Initial load
duke
parents:
diff changeset
  1248
     * unchecked conversions?  If lists are of different length,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1249
     * return false.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1250
     **/
06bc494ca11e Initial load
duke
parents:
diff changeset
  1251
    public boolean isSubtypesUnchecked(List<Type> ts, List<Type> ss, Warner warn) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1252
        while (ts.tail != null && ss.tail != null
06bc494ca11e Initial load
duke
parents:
diff changeset
  1253
               /*inlined: ts.nonEmpty() && ss.nonEmpty()*/ &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1254
               isSubtypeUnchecked(ts.head, ss.head, warn)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1255
            ts = ts.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1256
            ss = ss.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1257
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1258
        return ts.tail == null && ss.tail == null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1259
        /*inlined: ts.isEmpty() && ss.isEmpty();*/
06bc494ca11e Initial load
duke
parents:
diff changeset
  1260
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1261
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  1262
06bc494ca11e Initial load
duke
parents:
diff changeset
  1263
    // <editor-fold defaultstate="collapsed" desc="isSuperType">
06bc494ca11e Initial load
duke
parents:
diff changeset
  1264
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1265
     * Is t a supertype of s?
06bc494ca11e Initial load
duke
parents:
diff changeset
  1266
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1267
    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
  1268
        switch (t.getTag()) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1269
        case ERROR:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1270
            return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1271
        case UNDETVAR: {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1272
            UndetVar undet = (UndetVar)t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1273
            if (t == s ||
06bc494ca11e Initial load
duke
parents:
diff changeset
  1274
                undet.qtype == s ||
18646
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18412
diff changeset
  1275
                s.hasTag(ERROR) ||
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18412
diff changeset
  1276
                s.hasTag(BOT)) {
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18412
diff changeset
  1277
                return true;
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18412
diff changeset
  1278
            }
14048
308d1cf8fe46 7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents: 13689
diff changeset
  1279
            undet.addBound(InferenceBound.LOWER, s, this);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1280
            return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1281
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1282
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1283
            return isSubtype(s, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1284
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1285
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1286
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  1287
06bc494ca11e Initial load
duke
parents:
diff changeset
  1288
    // <editor-fold defaultstate="collapsed" desc="isSameType">
06bc494ca11e Initial load
duke
parents:
diff changeset
  1289
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1290
     * Are corresponding elements of the lists the same type?  If
06bc494ca11e Initial load
duke
parents:
diff changeset
  1291
     * lists are of different length, return false.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1292
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1293
    public boolean isSameTypes(List<Type> ts, List<Type> ss) {
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1294
        return isSameTypes(ts, ss, false);
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1295
    }
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1296
    public boolean isSameTypes(List<Type> ts, List<Type> ss, boolean strict) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1297
        while (ts.tail != null && ss.tail != null
06bc494ca11e Initial load
duke
parents:
diff changeset
  1298
               /*inlined: ts.nonEmpty() && ss.nonEmpty()*/ &&
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1299
               isSameType(ts.head, ss.head, strict)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1300
            ts = ts.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1301
            ss = ss.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1302
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1303
        return ts.tail == null && ss.tail == null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1304
        /*inlined: ts.isEmpty() && ss.isEmpty();*/
06bc494ca11e Initial load
duke
parents:
diff changeset
  1305
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1306
06bc494ca11e Initial load
duke
parents:
diff changeset
  1307
    /**
36995
e19153419efd 8149644: Integrate VarHandles
psandoz
parents: 34995
diff changeset
  1308
     * A polymorphic signature method (JLS 15.12.3) is a method that
e19153419efd 8149644: Integrate VarHandles
psandoz
parents: 34995
diff changeset
  1309
     *   (i) is declared in the java.lang.invoke.MethodHandle/VarHandle classes;
e19153419efd 8149644: Integrate VarHandles
psandoz
parents: 34995
diff changeset
  1310
     *  (ii) takes a single variable arity parameter;
e19153419efd 8149644: Integrate VarHandles
psandoz
parents: 34995
diff changeset
  1311
     * (iii) whose declared type is Object[];
e19153419efd 8149644: Integrate VarHandles
psandoz
parents: 34995
diff changeset
  1312
     *  (iv) has any return type, Object signifying a polymorphic return type; and
e19153419efd 8149644: Integrate VarHandles
psandoz
parents: 34995
diff changeset
  1313
     *   (v) is native.
18389
a425d0819f36 8016569: javac, add new flag for polymorphic method signatures
vromero
parents: 17578
diff changeset
  1314
    */
a425d0819f36 8016569: javac, add new flag for polymorphic method signatures
vromero
parents: 17578
diff changeset
  1315
   public boolean isSignaturePolymorphic(MethodSymbol msym) {
a425d0819f36 8016569: javac, add new flag for polymorphic method signatures
vromero
parents: 17578
diff changeset
  1316
       List<Type> argtypes = msym.type.getParameterTypes();
a425d0819f36 8016569: javac, add new flag for polymorphic method signatures
vromero
parents: 17578
diff changeset
  1317
       return (msym.flags_field & NATIVE) != 0 &&
36995
e19153419efd 8149644: Integrate VarHandles
psandoz
parents: 34995
diff changeset
  1318
              (msym.owner == syms.methodHandleType.tsym || msym.owner == syms.varHandleType.tsym) &&
41856
13a056e8f16e 8168774: Polymorhic signature method check crashes javac
mcimadamore
parents: 41526
diff changeset
  1319
               argtypes.length() == 1 &&
18389
a425d0819f36 8016569: javac, add new flag for polymorphic method signatures
vromero
parents: 17578
diff changeset
  1320
               argtypes.head.hasTag(TypeTag.ARRAY) &&
a425d0819f36 8016569: javac, add new flag for polymorphic method signatures
vromero
parents: 17578
diff changeset
  1321
               ((ArrayType)argtypes.head).elemtype.tsym == syms.objectType.tsym;
a425d0819f36 8016569: javac, add new flag for polymorphic method signatures
vromero
parents: 17578
diff changeset
  1322
   }
a425d0819f36 8016569: javac, add new flag for polymorphic method signatures
vromero
parents: 17578
diff changeset
  1323
a425d0819f36 8016569: javac, add new flag for polymorphic method signatures
vromero
parents: 17578
diff changeset
  1324
    /**
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1325
     * Is t the same type as s?
06bc494ca11e Initial load
duke
parents:
diff changeset
  1326
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1327
    public boolean isSameType(Type t, Type s) {
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1328
        return isSameType(t, s, false);
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1329
    }
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1330
    public boolean isSameType(Type t, Type s, boolean strict) {
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1331
        return strict ?
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1332
                isSameTypeStrict.visit(t, s) :
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1333
                isSameTypeLoose.visit(t, s);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1334
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1335
    // where
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1336
        abstract class SameTypeVisitor extends TypeRelation {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1337
06bc494ca11e Initial load
duke
parents:
diff changeset
  1338
            public Boolean visitType(Type t, Type s) {
29842
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29555
diff changeset
  1339
                if (t.equalsIgnoreMetadata(s))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1340
                    return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1341
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
  1342
                if (s.isPartial())
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1343
                    return visit(s, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1344
18646
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18412
diff changeset
  1345
                switch (t.getTag()) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1346
                case BYTE: case CHAR: case SHORT: case INT: case LONG: case FLOAT:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1347
                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
  1348
                    return t.hasTag(s.getTag());
5648
e8aa492874b5 6880344: Recursive type parameters do not compile
mcimadamore
parents: 5004
diff changeset
  1349
                case TYPEVAR: {
18646
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18412
diff changeset
  1350
                    if (s.hasTag(TYPEVAR)) {
5648
e8aa492874b5 6880344: Recursive type parameters do not compile
mcimadamore
parents: 5004
diff changeset
  1351
                        //type-substitution does not preserve type-var types
e8aa492874b5 6880344: Recursive type parameters do not compile
mcimadamore
parents: 5004
diff changeset
  1352
                        //check that type var symbols and bounds are indeed the same
24396
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 24293
diff changeset
  1353
                        return sameTypeVars((TypeVar)t, (TypeVar)s);
5648
e8aa492874b5 6880344: Recursive type parameters do not compile
mcimadamore
parents: 5004
diff changeset
  1354
                    }
e8aa492874b5 6880344: Recursive type parameters do not compile
mcimadamore
parents: 5004
diff changeset
  1355
                    else {
e8aa492874b5 6880344: Recursive type parameters do not compile
mcimadamore
parents: 5004
diff changeset
  1356
                        //special case for s == ? super X, where upper(s) = u
e8aa492874b5 6880344: Recursive type parameters do not compile
mcimadamore
parents: 5004
diff changeset
  1357
                        //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
  1358
                        return s.isSuperBound() &&
e8aa492874b5 6880344: Recursive type parameters do not compile
mcimadamore
parents: 5004
diff changeset
  1359
                                !s.isExtendsBound() &&
24612
75dc732b45af 8042338: Refactor Types.upperBound to treat wildcards and variables separately
dlsmith
parents: 24604
diff changeset
  1360
                                visit(t, wildUpperBound(s));
5648
e8aa492874b5 6880344: Recursive type parameters do not compile
mcimadamore
parents: 5004
diff changeset
  1361
                    }
e8aa492874b5 6880344: Recursive type parameters do not compile
mcimadamore
parents: 5004
diff changeset
  1362
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1363
                default:
18646
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18412
diff changeset
  1364
                    throw new AssertionError("isSameType " + t.getTag());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1365
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1366
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1367
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1368
            abstract boolean sameTypeVars(TypeVar tv1, TypeVar tv2);
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1369
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1370
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1371
            public Boolean visitWildcardType(WildcardType t, Type s) {
39363
ebaee646b121 8159970: javac, JLS8 18.2.4 is not completely implemented by the compiler
vromero
parents: 39100
diff changeset
  1372
                if (!s.hasTag(WILDCARD)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1373
                    return false;
39363
ebaee646b121 8159970: javac, JLS8 18.2.4 is not completely implemented by the compiler
vromero
parents: 39100
diff changeset
  1374
                } else {
ebaee646b121 8159970: javac, JLS8 18.2.4 is not completely implemented by the compiler
vromero
parents: 39100
diff changeset
  1375
                    WildcardType t2 = (WildcardType)s;
ebaee646b121 8159970: javac, JLS8 18.2.4 is not completely implemented by the compiler
vromero
parents: 39100
diff changeset
  1376
                    return (t.kind == t2.kind || (t.isExtendsBound() && s.isExtendsBound())) &&
ebaee646b121 8159970: javac, JLS8 18.2.4 is not completely implemented by the compiler
vromero
parents: 39100
diff changeset
  1377
                            isSameType(t.type, t2.type, true);
ebaee646b121 8159970: javac, JLS8 18.2.4 is not completely implemented by the compiler
vromero
parents: 39100
diff changeset
  1378
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1379
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1380
06bc494ca11e Initial load
duke
parents:
diff changeset
  1381
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1382
            public Boolean visitClassType(ClassType t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1383
                if (t == s)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1384
                    return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1385
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
  1386
                if (s.isPartial())
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1387
                    return visit(s, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1388
06bc494ca11e Initial load
duke
parents:
diff changeset
  1389
                if (s.isSuperBound() && !s.isExtendsBound())
24612
75dc732b45af 8042338: Refactor Types.upperBound to treat wildcards and variables separately
dlsmith
parents: 24604
diff changeset
  1390
                    return visit(t, wildUpperBound(s)) && visit(t, wildLowerBound(s));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1391
06bc494ca11e Initial load
duke
parents:
diff changeset
  1392
                if (t.isCompound() && s.isCompound()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1393
                    if (!visit(supertype(t), supertype(s)))
06bc494ca11e Initial load
duke
parents:
diff changeset
  1394
                        return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1395
34995
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34757
diff changeset
  1396
                    Map<Symbol,Type> tMap = new HashMap<>();
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34757
diff changeset
  1397
                    for (Type ti : interfaces(t)) {
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34757
diff changeset
  1398
                        if (tMap.containsKey(ti)) {
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34757
diff changeset
  1399
                            throw new AssertionError("Malformed intersection");
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34757
diff changeset
  1400
                        }
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34757
diff changeset
  1401
                        tMap.put(ti.tsym, ti);
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34757
diff changeset
  1402
                    }
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34757
diff changeset
  1403
                    for (Type si : interfaces(s)) {
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34757
diff changeset
  1404
                        if (!tMap.containsKey(si.tsym))
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34757
diff changeset
  1405
                            return false;
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34757
diff changeset
  1406
                        Type ti = tMap.remove(si.tsym);
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34757
diff changeset
  1407
                        if (!visit(ti, si))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1408
                            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1409
                    }
34995
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34757
diff changeset
  1410
                    return tMap.isEmpty();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1411
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1412
                return t.tsym == s.tsym
06bc494ca11e Initial load
duke
parents:
diff changeset
  1413
                    && visit(t.getEnclosingType(), s.getEnclosingType())
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1414
                    && containsTypes(t.getTypeArguments(), s.getTypeArguments());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1415
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1416
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1417
            abstract protected boolean containsTypes(List<Type> ts1, List<Type> ts2);
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1418
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1419
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1420
            public Boolean visitArrayType(ArrayType t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1421
                if (t == s)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1422
                    return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1423
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
  1424
                if (s.isPartial())
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1425
                    return visit(s, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1426
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
  1427
                return s.hasTag(ARRAY)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1428
                    && containsTypeEquivalent(t.elemtype, elemtype(s));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1429
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1430
06bc494ca11e Initial load
duke
parents:
diff changeset
  1431
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1432
            public Boolean visitMethodType(MethodType t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1433
                // isSameType for methods does not take thrown
06bc494ca11e Initial load
duke
parents:
diff changeset
  1434
                // exceptions into account!
06bc494ca11e Initial load
duke
parents:
diff changeset
  1435
                return hasSameArgs(t, s) && visit(t.getReturnType(), s.getReturnType());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1436
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1437
06bc494ca11e Initial load
duke
parents:
diff changeset
  1438
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1439
            public Boolean visitPackageType(PackageType t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1440
                return t == s;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1441
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1442
06bc494ca11e Initial load
duke
parents:
diff changeset
  1443
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1444
            public 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
  1445
                if (!s.hasTag(FORALL)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1446
                    return false;
18646
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18412
diff changeset
  1447
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1448
06bc494ca11e Initial load
duke
parents:
diff changeset
  1449
                ForAll forAll = (ForAll)s;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1450
                return hasSameBounds(t, forAll)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1451
                    && visit(t.qtype, subst(forAll.qtype, forAll.tvars, t.tvars));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1452
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1453
06bc494ca11e Initial load
duke
parents:
diff changeset
  1454
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1455
            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
  1456
                if (s.hasTag(WILDCARD)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1457
                    // FIXME, this might be leftovers from before capture conversion
06bc494ca11e Initial load
duke
parents:
diff changeset
  1458
                    return false;
18646
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18412
diff changeset
  1459
                }
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18412
diff changeset
  1460
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18412
diff changeset
  1461
                if (t == s || t.qtype == s || s.hasTag(ERROR) || s.hasTag(UNKNOWN)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1462
                    return true;
18646
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18412
diff changeset
  1463
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1464
14048
308d1cf8fe46 7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents: 13689
diff changeset
  1465
                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
  1466
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1467
                return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1468
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1469
06bc494ca11e Initial load
duke
parents:
diff changeset
  1470
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1471
            public Boolean visitErrorType(ErrorType t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1472
                return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1473
            }
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1474
        }
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1475
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1476
        /**
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1477
         * Standard type-equality relation - type variables are considered
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1478
         * equals if they share the same type symbol.
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1479
         */
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16973
diff changeset
  1480
        TypeRelation isSameTypeLoose = new LooseSameTypeVisitor();
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16973
diff changeset
  1481
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16973
diff changeset
  1482
        private class LooseSameTypeVisitor extends SameTypeVisitor {
24899
ead62a035849 8043725: javac fails with StackOverflowException
mcimadamore
parents: 24647
diff changeset
  1483
ead62a035849 8043725: javac fails with StackOverflowException
mcimadamore
parents: 24647
diff changeset
  1484
            /** cache of the type-variable pairs being (recursively) tested. */
ead62a035849 8043725: javac fails with StackOverflowException
mcimadamore
parents: 24647
diff changeset
  1485
            private Set<TypePair> cache = new HashSet<>();
ead62a035849 8043725: javac fails with StackOverflowException
mcimadamore
parents: 24647
diff changeset
  1486
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1487
            @Override
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1488
            boolean sameTypeVars(TypeVar tv1, TypeVar tv2) {
24899
ead62a035849 8043725: javac fails with StackOverflowException
mcimadamore
parents: 24647
diff changeset
  1489
                return tv1.tsym == tv2.tsym && checkSameBounds(tv1, tv2);
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1490
            }
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1491
            @Override
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1492
            protected boolean containsTypes(List<Type> ts1, List<Type> ts2) {
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1493
                return containsTypeEquivalent(ts1, ts2);
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1494
            }
24899
ead62a035849 8043725: javac fails with StackOverflowException
mcimadamore
parents: 24647
diff changeset
  1495
ead62a035849 8043725: javac fails with StackOverflowException
mcimadamore
parents: 24647
diff changeset
  1496
            /**
ead62a035849 8043725: javac fails with StackOverflowException
mcimadamore
parents: 24647
diff changeset
  1497
             * Since type-variable bounds can be recursive, we need to protect against
ead62a035849 8043725: javac fails with StackOverflowException
mcimadamore
parents: 24647
diff changeset
  1498
             * infinite loops - where the same bounds are checked over and over recursively.
ead62a035849 8043725: javac fails with StackOverflowException
mcimadamore
parents: 24647
diff changeset
  1499
             */
ead62a035849 8043725: javac fails with StackOverflowException
mcimadamore
parents: 24647
diff changeset
  1500
            private boolean checkSameBounds(TypeVar tv1, TypeVar tv2) {
ead62a035849 8043725: javac fails with StackOverflowException
mcimadamore
parents: 24647
diff changeset
  1501
                TypePair p = new TypePair(tv1, tv2, true);
ead62a035849 8043725: javac fails with StackOverflowException
mcimadamore
parents: 24647
diff changeset
  1502
                if (cache.add(p)) {
ead62a035849 8043725: javac fails with StackOverflowException
mcimadamore
parents: 24647
diff changeset
  1503
                    try {
ead62a035849 8043725: javac fails with StackOverflowException
mcimadamore
parents: 24647
diff changeset
  1504
                        return visit(tv1.getUpperBound(), tv2.getUpperBound());
ead62a035849 8043725: javac fails with StackOverflowException
mcimadamore
parents: 24647
diff changeset
  1505
                    } finally {
ead62a035849 8043725: javac fails with StackOverflowException
mcimadamore
parents: 24647
diff changeset
  1506
                        cache.remove(p);
ead62a035849 8043725: javac fails with StackOverflowException
mcimadamore
parents: 24647
diff changeset
  1507
                    }
ead62a035849 8043725: javac fails with StackOverflowException
mcimadamore
parents: 24647
diff changeset
  1508
                } else {
ead62a035849 8043725: javac fails with StackOverflowException
mcimadamore
parents: 24647
diff changeset
  1509
                    return false;
ead62a035849 8043725: javac fails with StackOverflowException
mcimadamore
parents: 24647
diff changeset
  1510
                }
ead62a035849 8043725: javac fails with StackOverflowException
mcimadamore
parents: 24647
diff changeset
  1511
            }
ead62a035849 8043725: javac fails with StackOverflowException
mcimadamore
parents: 24647
diff changeset
  1512
        };
ead62a035849 8043725: javac fails with StackOverflowException
mcimadamore
parents: 24647
diff changeset
  1513
ead62a035849 8043725: javac fails with StackOverflowException
mcimadamore
parents: 24647
diff changeset
  1514
        /**
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1515
         * Strict type-equality relation - type variables are considered
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1516
         * equals if they share the same object identity.
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1517
         */
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1518
        TypeRelation isSameTypeStrict = new SameTypeVisitor() {
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1519
            @Override
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1520
            boolean sameTypeVars(TypeVar tv1, TypeVar tv2) {
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1521
                return tv1 == tv2;
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1522
            }
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1523
            @Override
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1524
            protected boolean containsTypes(List<Type> ts1, List<Type> ts2) {
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1525
                return isSameTypes(ts1, ts2, true);
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  1526
            }
16567
946441667c02 8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents: 16565
diff changeset
  1527
946441667c02 8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents: 16565
diff changeset
  1528
            @Override
946441667c02 8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents: 16565
diff changeset
  1529
            public Boolean visitWildcardType(WildcardType t, Type s) {
946441667c02 8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents: 16565
diff changeset
  1530
                if (!s.hasTag(WILDCARD)) {
946441667c02 8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents: 16565
diff changeset
  1531
                    return false;
946441667c02 8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents: 16565
diff changeset
  1532
                } else {
24396
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 24293
diff changeset
  1533
                    WildcardType t2 = (WildcardType)s;
16567
946441667c02 8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents: 16565
diff changeset
  1534
                    return t.kind == t2.kind &&
946441667c02 8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents: 16565
diff changeset
  1535
                            isSameType(t.type, t2.type, true);
946441667c02 8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents: 16565
diff changeset
  1536
                }
946441667c02 8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents: 16565
diff changeset
  1537
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1538
        };
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16973
diff changeset
  1539
24396
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 24293
diff changeset
  1540
    // </editor-fold>
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 24293
diff changeset
  1541
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1542
    // <editor-fold defaultstate="collapsed" desc="Contains Type">
06bc494ca11e Initial load
duke
parents:
diff changeset
  1543
    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
  1544
        switch (t.getTag()) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1545
        case UNDETVAR:
18646
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18412
diff changeset
  1546
            if (s.hasTag(WILDCARD)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1547
                UndetVar undetvar = (UndetVar)t;
24396
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 24293
diff changeset
  1548
                WildcardType wt = (WildcardType)s;
1991
aafb4bf914ee 6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents: 1874
diff changeset
  1549
                switch(wt.kind) {
25441
92dcee5a4741 8043926: javac, code valid in 7 is not compiling for 8
mcimadamore
parents: 25279
diff changeset
  1550
                    case UNBOUND:
92dcee5a4741 8043926: javac, code valid in 7 is not compiling for 8
mcimadamore
parents: 25279
diff changeset
  1551
                        break;
1991
aafb4bf914ee 6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents: 1874
diff changeset
  1552
                    case EXTENDS: {
24612
75dc732b45af 8042338: Refactor Types.upperBound to treat wildcards and variables separately
dlsmith
parents: 24604
diff changeset
  1553
                        Type bound = wildUpperBound(s);
14048
308d1cf8fe46 7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents: 13689
diff changeset
  1554
                        undetvar.addBound(InferenceBound.UPPER, bound, this);
1991
aafb4bf914ee 6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents: 1874
diff changeset
  1555
                        break;
aafb4bf914ee 6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents: 1874
diff changeset
  1556
                    }
aafb4bf914ee 6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents: 1874
diff changeset
  1557
                    case SUPER: {
24293
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
  1558
                        Type bound = wildLowerBound(s);
14048
308d1cf8fe46 7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents: 13689
diff changeset
  1559
                        undetvar.addBound(InferenceBound.LOWER, bound, this);
1991
aafb4bf914ee 6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents: 1874
diff changeset
  1560
                        break;
aafb4bf914ee 6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents: 1874
diff changeset
  1561
                    }
1535
a64c289fe413 6762569: Javac crashes with AssertionError in Types.containedBy
mcimadamore
parents: 1530
diff changeset
  1562
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1563
                return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1564
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1565
                return isSameType(t, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1566
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1567
        case ERROR:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1568
            return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1569
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1570
            return containsType(s, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1571
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1572
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1573
06bc494ca11e Initial load
duke
parents:
diff changeset
  1574
    boolean containsType(List<Type> ts, List<Type> ss) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1575
        while (ts.nonEmpty() && ss.nonEmpty()
06bc494ca11e Initial load
duke
parents:
diff changeset
  1576
               && containsType(ts.head, ss.head)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1577
            ts = ts.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1578
            ss = ss.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1579
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1580
        return ts.isEmpty() && ss.isEmpty();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1581
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1582
06bc494ca11e Initial load
duke
parents:
diff changeset
  1583
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1584
     * Check if t contains s.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1585
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  1586
     * <p>T contains S if:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1587
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  1588
     * <p>{@code L(T) <: L(S) && U(S) <: U(T)}
06bc494ca11e Initial load
duke
parents:
diff changeset
  1589
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  1590
     * <p>This relation is only used by ClassType.isSubtype(), that
06bc494ca11e Initial load
duke
parents:
diff changeset
  1591
     * is,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1592
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  1593
     * <p>{@code C<S> <: C<T> if T contains S.}
06bc494ca11e Initial load
duke
parents:
diff changeset
  1594
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  1595
     * <p>Because of F-bounds, this relation can lead to infinite
06bc494ca11e Initial load
duke
parents:
diff changeset
  1596
     * recursion.  Thus we must somehow break that recursion.  Notice
06bc494ca11e Initial load
duke
parents:
diff changeset
  1597
     * that containsType() is only called from ClassType.isSubtype().
06bc494ca11e Initial load
duke
parents:
diff changeset
  1598
     * Since the arguments have already been checked against their
06bc494ca11e Initial load
duke
parents:
diff changeset
  1599
     * bounds, we know:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1600
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  1601
     * <p>{@code U(S) <: U(T) if T is "super" bound (U(T) *is* the bound)}
06bc494ca11e Initial load
duke
parents:
diff changeset
  1602
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  1603
     * <p>{@code L(T) <: L(S) if T is "extends" bound (L(T) is bottom)}
06bc494ca11e Initial load
duke
parents:
diff changeset
  1604
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  1605
     * @param t a type
06bc494ca11e Initial load
duke
parents:
diff changeset
  1606
     * @param s a type
06bc494ca11e Initial load
duke
parents:
diff changeset
  1607
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1608
    public boolean containsType(Type t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1609
        return containsType.visit(t, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1610
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1611
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
  1612
        private TypeRelation containsType = new TypeRelation() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1613
06bc494ca11e Initial load
duke
parents:
diff changeset
  1614
            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
  1615
                if (s.isPartial())
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1616
                    return containedBy(s, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1617
                else
06bc494ca11e Initial load
duke
parents:
diff changeset
  1618
                    return isSameType(t, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1619
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1620
7637
467bc8fc514e 6990134: minor (but red) findbugs warnings
jjg
parents: 7635
diff changeset
  1621
//            void debugContainsType(WildcardType t, Type s) {
467bc8fc514e 6990134: minor (but red) findbugs warnings
jjg
parents: 7635
diff changeset
  1622
//                System.err.println();
467bc8fc514e 6990134: minor (but red) findbugs warnings
jjg
parents: 7635
diff changeset
  1623
//                System.err.format(" does %s contain %s?%n", t, s);
467bc8fc514e 6990134: minor (but red) findbugs warnings
jjg
parents: 7635
diff changeset
  1624
//                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
  1625
//                                  wildUpperBound(s), s, t, wildUpperBound(t),
7637
467bc8fc514e 6990134: minor (but red) findbugs warnings
jjg
parents: 7635
diff changeset
  1626
//                                  t.isSuperBound()
24612
75dc732b45af 8042338: Refactor Types.upperBound to treat wildcards and variables separately
dlsmith
parents: 24604
diff changeset
  1627
//                                  || isSubtypeNoCapture(wildUpperBound(s), wildUpperBound(t)));
7637
467bc8fc514e 6990134: minor (but red) findbugs warnings
jjg
parents: 7635
diff changeset
  1628
//                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
  1629
//                                  wildLowerBound(t), t, s, wildLowerBound(s),
7637
467bc8fc514e 6990134: minor (but red) findbugs warnings
jjg
parents: 7635
diff changeset
  1630
//                                  t.isExtendsBound()
24612
75dc732b45af 8042338: Refactor Types.upperBound to treat wildcards and variables separately
dlsmith
parents: 24604
diff changeset
  1631
//                                  || isSubtypeNoCapture(wildLowerBound(t), wildLowerBound(s)));
7637
467bc8fc514e 6990134: minor (but red) findbugs warnings
jjg
parents: 7635
diff changeset
  1632
//                System.err.println();
467bc8fc514e 6990134: minor (but red) findbugs warnings
jjg
parents: 7635
diff changeset
  1633
//            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1634
06bc494ca11e Initial load
duke
parents:
diff changeset
  1635
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1636
            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
  1637
                if (s.isPartial())
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1638
                    return containedBy(s, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1639
                else {
7637
467bc8fc514e 6990134: minor (but red) findbugs warnings
jjg
parents: 7635
diff changeset
  1640
//                    debugContainsType(t, s);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1641
                    return isSameWildcard(t, s)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1642
                        || isCaptureOf(s, t)
24612
75dc732b45af 8042338: Refactor Types.upperBound to treat wildcards and variables separately
dlsmith
parents: 24604
diff changeset
  1643
                        || ((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
  1644
                            (t.isSuperBound() || isSubtypeNoCapture(wildUpperBound(s), wildUpperBound(t))));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1645
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1646
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1647
06bc494ca11e Initial load
duke
parents:
diff changeset
  1648
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1649
            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
  1650
                if (!s.hasTag(WILDCARD)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1651
                    return isSameType(t, s);
18646
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18412
diff changeset
  1652
                } else {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1653
                    return false;
18646
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18412
diff changeset
  1654
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1655
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1656
06bc494ca11e Initial load
duke
parents:
diff changeset
  1657
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1658
            public Boolean visitErrorType(ErrorType t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1659
                return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1660
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1661
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
  1662
06bc494ca11e Initial load
duke
parents:
diff changeset
  1663
    public boolean isCaptureOf(Type s, WildcardType t) {
24396
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 24293
diff changeset
  1664
        if (!s.hasTag(TYPEVAR) || !((TypeVar)s).isCaptured())
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1665
            return false;
24396
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 24293
diff changeset
  1666
        return isSameWildcard(t, ((CapturedType)s).wildcard);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1667
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1668
06bc494ca11e Initial load
duke
parents:
diff changeset
  1669
    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
  1670
        if (!s.hasTag(WILDCARD))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1671
            return false;
24396
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 24293
diff changeset
  1672
        WildcardType w = (WildcardType)s;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1673
        return w.kind == t.kind && w.type == t.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1674
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1675
06bc494ca11e Initial load
duke
parents:
diff changeset
  1676
    public boolean containsTypeEquivalent(List<Type> ts, List<Type> ss) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1677
        while (ts.nonEmpty() && ss.nonEmpty()
06bc494ca11e Initial load
duke
parents:
diff changeset
  1678
               && containsTypeEquivalent(ts.head, ss.head)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1679
            ts = ts.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1680
            ss = ss.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1681
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1682
        return ts.isEmpty() && ss.isEmpty();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1683
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1684
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  1685
06bc494ca11e Initial load
duke
parents:
diff changeset
  1686
    // <editor-fold defaultstate="collapsed" desc="isCastable">
06bc494ca11e Initial load
duke
parents:
diff changeset
  1687
    public boolean isCastable(Type t, Type s) {
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14443
diff changeset
  1688
        return isCastable(t, s, noWarnings);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1689
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1690
06bc494ca11e Initial load
duke
parents:
diff changeset
  1691
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1692
     * Is t is castable to s?<br>
06bc494ca11e Initial load
duke
parents:
diff changeset
  1693
     * s is assumed to be an erased type.<br>
06bc494ca11e Initial load
duke
parents:
diff changeset
  1694
     * (not defined for Method and ForAll types).
06bc494ca11e Initial load
duke
parents:
diff changeset
  1695
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1696
    public boolean isCastable(Type t, Type s, Warner warn) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1697
        if (t == s)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1698
            return true;
34756
d31f11c4cc75 8144832: cast conversion fails when converting a type-variable to primitive type
vromero
parents: 34563
diff changeset
  1699
        if (t.isPrimitive() != s.isPrimitive()) {
d31f11c4cc75 8144832: cast conversion fails when converting a type-variable to primitive type
vromero
parents: 34563
diff changeset
  1700
            t = skipTypeVars(t, false);
25445
603f0c93d5c9 8011044: Remove support for 1.5 and earlier source and target options
ntoda
parents: 25443
diff changeset
  1701
            return (isConvertible(t, s, warn)
9721
da048be191c2 7041730: Regression: compiler accepts invalid cast from int to Byte
mcimadamore
parents: 9607
diff changeset
  1702
                    || (allowObjectToPrimitiveCast &&
da048be191c2 7041730: Regression: compiler accepts invalid cast from int to Byte
mcimadamore
parents: 9607
diff changeset
  1703
                        s.isPrimitive() &&
da048be191c2 7041730: Regression: compiler accepts invalid cast from int to Byte
mcimadamore
parents: 9607
diff changeset
  1704
                        isSubtype(boxedClass(s).type, t)));
34756
d31f11c4cc75 8144832: cast conversion fails when converting a type-variable to primitive type
vromero
parents: 34563
diff changeset
  1705
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1706
        if (warn != warnStack.head) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1707
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1708
                warnStack = warnStack.prepend(warn);
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 7637
diff changeset
  1709
                checkUnsafeVarargsConversion(t, s, warn);
1790
7182011ee8a6 6665356: Cast not allowed when both qualifying type and inner class are parameterized
mcimadamore
parents: 1535
diff changeset
  1710
                return isCastable.visit(t,s);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1711
            } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1712
                warnStack = warnStack.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1713
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1714
        } else {
1790
7182011ee8a6 6665356: Cast not allowed when both qualifying type and inner class are parameterized
mcimadamore
parents: 1535
diff changeset
  1715
            return isCastable.visit(t,s);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1716
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1717
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1718
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
  1719
        private TypeRelation isCastable = new TypeRelation() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1720
06bc494ca11e Initial load
duke
parents:
diff changeset
  1721
            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
  1722
                if (s.hasTag(ERROR))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1723
                    return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1724
18646
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18412
diff changeset
  1725
                switch (t.getTag()) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1726
                case BYTE: case CHAR: case SHORT: case INT: case LONG: case FLOAT:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1727
                case DOUBLE:
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
  1728
                    return s.isNumeric();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1729
                case BOOLEAN:
18646
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18412
diff changeset
  1730
                    return s.hasTag(BOOLEAN);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1731
                case VOID:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1732
                    return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1733
                case BOT:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1734
                    return isSubtype(t, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1735
                default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1736
                    throw new AssertionError();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1737
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1738
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1739
06bc494ca11e Initial load
duke
parents:
diff changeset
  1740
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1741
            public Boolean visitWildcardType(WildcardType t, Type s) {
24612
75dc732b45af 8042338: Refactor Types.upperBound to treat wildcards and variables separately
dlsmith
parents: 24604
diff changeset
  1742
                return isCastable(wildUpperBound(t), s, warnStack.head);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1743
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1744
06bc494ca11e Initial load
duke
parents:
diff changeset
  1745
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1746
            public Boolean 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
  1747
                if (s.hasTag(ERROR) || s.hasTag(BOT))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1748
                    return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1749
18646
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18412
diff changeset
  1750
                if (s.hasTag(TYPEVAR)) {
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14443
diff changeset
  1751
                    if (isCastable(t, s.getUpperBound(), noWarnings)) {
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 7637
diff changeset
  1752
                        warnStack.head.warn(LintCategory.UNCHECKED);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1753
                        return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1754
                    } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1755
                        return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1756
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1757
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1758
33556
a14a556cf2c9 8141343: Subtle semantics changes for union types in cast conversion
mcimadamore
parents: 32915
diff changeset
  1759
                if (t.isCompound() || s.isCompound()) {
a14a556cf2c9 8141343: Subtle semantics changes for union types in cast conversion
mcimadamore
parents: 32915
diff changeset
  1760
                    return !t.isCompound() ?
a14a556cf2c9 8141343: Subtle semantics changes for union types in cast conversion
mcimadamore
parents: 32915
diff changeset
  1761
                            visitCompoundType((ClassType)s, t, true) :
a14a556cf2c9 8141343: Subtle semantics changes for union types in cast conversion
mcimadamore
parents: 32915
diff changeset
  1762
                            visitCompoundType(t, s, false);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1763
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1764
18646
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18412
diff changeset
  1765
                if (s.hasTag(CLASS) || s.hasTag(ARRAY)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1766
                    boolean upcast;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1767
                    if ((upcast = isSubtype(erasure(t), erasure(s)))
06bc494ca11e Initial load
duke
parents:
diff changeset
  1768
                        || 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
  1769
                        if (!upcast && s.hasTag(ARRAY)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1770
                            if (!isReifiable(s))
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 7637
diff changeset
  1771
                                warnStack.head.warn(LintCategory.UNCHECKED);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1772
                            return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1773
                        } else if (s.isRaw()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1774
                            return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1775
                        } else if (t.isRaw()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1776
                            if (!isUnbounded(s))
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 7637
diff changeset
  1777
                                warnStack.head.warn(LintCategory.UNCHECKED);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1778
                            return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1779
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1780
                        // Assume |a| <: |b|
06bc494ca11e Initial load
duke
parents:
diff changeset
  1781
                        final Type a = upcast ? t : s;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1782
                        final Type b = upcast ? s : t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1783
                        final boolean HIGH = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1784
                        final boolean LOW = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1785
                        final boolean DONT_REWRITE_TYPEVARS = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1786
                        Type aHigh = rewriteQuantifiers(a, HIGH, DONT_REWRITE_TYPEVARS);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1787
                        Type aLow  = rewriteQuantifiers(a, LOW,  DONT_REWRITE_TYPEVARS);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1788
                        Type bHigh = rewriteQuantifiers(b, HIGH, DONT_REWRITE_TYPEVARS);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1789
                        Type bLow  = rewriteQuantifiers(b, LOW,  DONT_REWRITE_TYPEVARS);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1790
                        Type lowSub = asSub(bLow, aLow.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1791
                        Type highSub = (lowSub == null) ? null : asSub(bHigh, aHigh.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1792
                        if (highSub == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1793
                            final boolean REWRITE_TYPEVARS = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1794
                            aHigh = rewriteQuantifiers(a, HIGH, REWRITE_TYPEVARS);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1795
                            aLow  = rewriteQuantifiers(a, LOW,  REWRITE_TYPEVARS);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1796
                            bHigh = rewriteQuantifiers(b, HIGH, REWRITE_TYPEVARS);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1797
                            bLow  = rewriteQuantifiers(b, LOW,  REWRITE_TYPEVARS);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1798
                            lowSub = asSub(bLow, aLow.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1799
                            highSub = (lowSub == null) ? null : asSub(bHigh, aHigh.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1800
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1801
                        if (highSub != null) {
8032
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents: 7681
diff changeset
  1802
                            if (!(a.tsym == highSub.tsym && a.tsym == lowSub.tsym)) {
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents: 7681
diff changeset
  1803
                                Assert.error(a.tsym + " != " + highSub.tsym + " != " + lowSub.tsym);
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents: 7681
diff changeset
  1804
                            }
1790
7182011ee8a6 6665356: Cast not allowed when both qualifying type and inner class are parameterized
mcimadamore
parents: 1535
diff changeset
  1805
                            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
  1806
                                && !disjointTypes(aHigh.allparams(), lowSub.allparams())
7182011ee8a6 6665356: Cast not allowed when both qualifying type and inner class are parameterized
mcimadamore
parents: 1535
diff changeset
  1807
                                && !disjointTypes(aLow.allparams(), highSub.allparams())
7182011ee8a6 6665356: Cast not allowed when both qualifying type and inner class are parameterized
mcimadamore
parents: 1535
diff changeset
  1808
                                && !disjointTypes(aLow.allparams(), lowSub.allparams())) {
7627
de2a86da165e 7005095: Cast: compile reject sensible cast from final class to interface
mcimadamore
parents: 7331
diff changeset
  1809
                                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
  1810
                                    giveWarning(b, a))
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 7637
diff changeset
  1811
                                    warnStack.head.warn(LintCategory.UNCHECKED);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1812
                                return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1813
                            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1814
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1815
                        if (isReifiable(s))
06bc494ca11e Initial load
duke
parents:
diff changeset
  1816
                            return isSubtypeUnchecked(a, b);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1817
                        else
06bc494ca11e Initial load
duke
parents:
diff changeset
  1818
                            return isSubtypeUnchecked(a, b, warnStack.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1819
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1820
06bc494ca11e Initial load
duke
parents:
diff changeset
  1821
                    // Sidecast
18646
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18412
diff changeset
  1822
                    if (s.hasTag(CLASS)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1823
                        if ((s.tsym.flags() & INTERFACE) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1824
                            return ((t.tsym.flags() & FINAL) == 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1825
                                ? sideCast(t, s, warnStack.head)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1826
                                : sideCastFinal(t, s, warnStack.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1827
                        } else if ((t.tsym.flags() & INTERFACE) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1828
                            return ((s.tsym.flags() & FINAL) == 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1829
                                ? sideCast(t, s, warnStack.head)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1830
                                : sideCastFinal(t, s, warnStack.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1831
                        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1832
                            // unrelated class types
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
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1837
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1838
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1839
33556
a14a556cf2c9 8141343: Subtle semantics changes for union types in cast conversion
mcimadamore
parents: 32915
diff changeset
  1840
            boolean visitCompoundType(ClassType ct, Type s, boolean reverse) {
16565
aee0d0aaec44 8010101: Intersection type cast issues redundant unchecked warning
mcimadamore
parents: 16556
diff changeset
  1841
                Warner warn = noWarnings;
33556
a14a556cf2c9 8141343: Subtle semantics changes for union types in cast conversion
mcimadamore
parents: 32915
diff changeset
  1842
                for (Type c : directSupertypes(ct)) {
16565
aee0d0aaec44 8010101: Intersection type cast issues redundant unchecked warning
mcimadamore
parents: 16556
diff changeset
  1843
                    warn.clear();
aee0d0aaec44 8010101: Intersection type cast issues redundant unchecked warning
mcimadamore
parents: 16556
diff changeset
  1844
                    if (reverse ? !isCastable(s, c, warn) : !isCastable(c, s, warn))
aee0d0aaec44 8010101: Intersection type cast issues redundant unchecked warning
mcimadamore
parents: 16556
diff changeset
  1845
                        return false;
aee0d0aaec44 8010101: Intersection type cast issues redundant unchecked warning
mcimadamore
parents: 16556
diff changeset
  1846
                }
aee0d0aaec44 8010101: Intersection type cast issues redundant unchecked warning
mcimadamore
parents: 16556
diff changeset
  1847
                if (warn.hasLint(LintCategory.UNCHECKED))
aee0d0aaec44 8010101: Intersection type cast issues redundant unchecked warning
mcimadamore
parents: 16556
diff changeset
  1848
                    warnStack.head.warn(LintCategory.UNCHECKED);
aee0d0aaec44 8010101: Intersection type cast issues redundant unchecked warning
mcimadamore
parents: 16556
diff changeset
  1849
                return true;
aee0d0aaec44 8010101: Intersection type cast issues redundant unchecked warning
mcimadamore
parents: 16556
diff changeset
  1850
            }
aee0d0aaec44 8010101: Intersection type cast issues redundant unchecked warning
mcimadamore
parents: 16556
diff changeset
  1851
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1852
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1853
            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
  1854
                switch (s.getTag()) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1855
                case ERROR:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1856
                case BOT:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1857
                    return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1858
                case TYPEVAR:
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14443
diff changeset
  1859
                    if (isCastable(s, t, noWarnings)) {
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 7637
diff changeset
  1860
                        warnStack.head.warn(LintCategory.UNCHECKED);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1861
                        return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1862
                    } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1863
                        return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1864
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1865
                case CLASS:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1866
                    return isSubtype(t, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1867
                case ARRAY:
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
  1868
                    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
  1869
                        return elemtype(t).hasTag(elemtype(s).getTag());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1870
                    } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1871
                        return visit(elemtype(t), elemtype(s));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1872
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1873
                default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1874
                    return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1875
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1876
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1877
06bc494ca11e Initial load
duke
parents:
diff changeset
  1878
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1879
            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
  1880
                switch (s.getTag()) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1881
                case ERROR:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1882
                case BOT:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1883
                    return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1884
                case TYPEVAR:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1885
                    if (isSubtype(t, s)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1886
                        return true;
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14443
diff changeset
  1887
                    } 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
  1888
                        warnStack.head.warn(LintCategory.UNCHECKED);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1889
                        return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1890
                    } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1891
                        return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1892
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1893
                default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1894
                    return isCastable(t.bound, s, warnStack.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1895
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1896
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1897
06bc494ca11e Initial load
duke
parents:
diff changeset
  1898
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1899
            public Boolean visitErrorType(ErrorType t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1900
                return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1901
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1902
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
  1903
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  1904
06bc494ca11e Initial load
duke
parents:
diff changeset
  1905
    // <editor-fold defaultstate="collapsed" desc="disjointTypes">
06bc494ca11e Initial load
duke
parents:
diff changeset
  1906
    public boolean disjointTypes(List<Type> ts, List<Type> ss) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1907
        while (ts.tail != null && ss.tail != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1908
            if (disjointType(ts.head, ss.head)) return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1909
            ts = ts.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1910
            ss = ss.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1911
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1912
        return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1913
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1914
06bc494ca11e Initial load
duke
parents:
diff changeset
  1915
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1916
     * Two types or wildcards are considered disjoint if it can be
06bc494ca11e Initial load
duke
parents:
diff changeset
  1917
     * proven that no type can be contained in both. It is
06bc494ca11e Initial load
duke
parents:
diff changeset
  1918
     * conservative in that it is allowed to say that two types are
06bc494ca11e Initial load
duke
parents:
diff changeset
  1919
     * not disjoint, even though they actually are.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1920
     *
14259
fb94a1df0d53 8000208: fix langtools javadoc comment issues
jjg
parents: 14258
diff changeset
  1921
     * 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
  1922
     * {@code X} and {@code Y} are not disjoint.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1923
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1924
    public boolean disjointType(Type t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1925
        return disjointType.visit(t, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1926
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1927
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
  1928
        private TypeRelation disjointType = new TypeRelation() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1929
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21712
diff changeset
  1930
            private Set<TypePair> cache = new HashSet<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1931
18646
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18412
diff changeset
  1932
            @Override
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1933
            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
  1934
                if (s.hasTag(WILDCARD))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1935
                    return visit(s, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1936
                else
06bc494ca11e Initial load
duke
parents:
diff changeset
  1937
                    return notSoftSubtypeRecursive(t, s) || notSoftSubtypeRecursive(s, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1938
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1939
06bc494ca11e Initial load
duke
parents:
diff changeset
  1940
            private boolean isCastableRecursive(Type t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1941
                TypePair pair = new TypePair(t, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1942
                if (cache.add(pair)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1943
                    try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1944
                        return Types.this.isCastable(t, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1945
                    } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1946
                        cache.remove(pair);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1947
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1948
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1949
                    return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1950
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1951
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1952
06bc494ca11e Initial load
duke
parents:
diff changeset
  1953
            private boolean notSoftSubtypeRecursive(Type t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1954
                TypePair pair = new TypePair(t, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1955
                if (cache.add(pair)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1956
                    try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1957
                        return Types.this.notSoftSubtype(t, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1958
                    } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1959
                        cache.remove(pair);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1960
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1961
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1962
                    return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1963
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1964
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1965
06bc494ca11e Initial load
duke
parents:
diff changeset
  1966
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1967
            public Boolean visitWildcardType(WildcardType t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1968
                if (t.isUnbound())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1969
                    return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1970
18646
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18412
diff changeset
  1971
                if (!s.hasTag(WILDCARD)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1972
                    if (t.isExtendsBound())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1973
                        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
  1974
                    else
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1975
                        return notSoftSubtypeRecursive(t.type, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1976
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1977
06bc494ca11e Initial load
duke
parents:
diff changeset
  1978
                if (s.isUnbound())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1979
                    return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1980
06bc494ca11e Initial load
duke
parents:
diff changeset
  1981
                if (t.isExtendsBound()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1982
                    if (s.isExtendsBound())
24612
75dc732b45af 8042338: Refactor Types.upperBound to treat wildcards and variables separately
dlsmith
parents: 24604
diff changeset
  1983
                        return !isCastableRecursive(t.type, wildUpperBound(s));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1984
                    else if (s.isSuperBound())
24293
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
  1985
                        return notSoftSubtypeRecursive(wildLowerBound(s), t.type);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1986
                } else if (t.isSuperBound()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1987
                    if (s.isExtendsBound())
24612
75dc732b45af 8042338: Refactor Types.upperBound to treat wildcards and variables separately
dlsmith
parents: 24604
diff changeset
  1988
                        return notSoftSubtypeRecursive(t.type, wildUpperBound(s));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1989
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1990
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1991
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1992
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
  1993
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  1994
24293
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
  1995
    // <editor-fold defaultstate="collapsed" desc="cvarLowerBounds">
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
  1996
    public List<Type> cvarLowerBounds(List<Type> ts) {
29554
6d7957bd6866 8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents: 29292
diff changeset
  1997
        return ts.map(cvarLowerBoundMapping);
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1998
    }
29554
6d7957bd6866 8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents: 29292
diff changeset
  1999
        private final TypeMapping<Void> cvarLowerBoundMapping = new TypeMapping<Void>() {
6d7957bd6866 8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents: 29292
diff changeset
  2000
            @Override
6d7957bd6866 8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents: 29292
diff changeset
  2001
            public Type visitCapturedType(CapturedType t, Void _unused) {
24293
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
  2002
                return cvarLowerBound(t);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2003
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2004
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
  2005
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  2006
06bc494ca11e Initial load
duke
parents:
diff changeset
  2007
    // <editor-fold defaultstate="collapsed" desc="notSoftSubtype">
06bc494ca11e Initial load
duke
parents:
diff changeset
  2008
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  2009
     * This relation answers the question: is impossible that
06bc494ca11e Initial load
duke
parents:
diff changeset
  2010
     * something of type `t' can be a subtype of `s'? This is
06bc494ca11e Initial load
duke
parents:
diff changeset
  2011
     * different from the question "is `t' not a subtype of `s'?"
06bc494ca11e Initial load
duke
parents:
diff changeset
  2012
     * 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
  2013
     * where {@code <T extends Number>} but it is not true that Integer cannot
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2014
     * possibly be a subtype of T.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2015
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2016
    public boolean notSoftSubtype(Type t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2017
        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
  2018
        if (t.hasTag(TYPEVAR)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2019
            TypeVar tv = (TypeVar) t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2020
            return !isCastable(tv.bound,
6353
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  2021
                               relaxBound(s),
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14443
diff changeset
  2022
                               noWarnings);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2023
        }
18646
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18412
diff changeset
  2024
        if (!s.hasTag(WILDCARD))
24612
75dc732b45af 8042338: Refactor Types.upperBound to treat wildcards and variables separately
dlsmith
parents: 24604
diff changeset
  2025
            s = cvarUpperBound(s);
6353
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  2026
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  2027
        return !isSubtype(t, relaxBound(s));
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  2028
    }
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  2029
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  2030
    private Type relaxBound(Type t) {
29292
c10d63c667cd 8073842: Invalid method reference when referencing a method on a wildcard type
mcimadamore
parents: 29147
diff changeset
  2031
        return (t.hasTag(TYPEVAR)) ?
c10d63c667cd 8073842: Invalid method reference when referencing a method on a wildcard type
mcimadamore
parents: 29147
diff changeset
  2032
                rewriteQuantifiers(skipTypeVars(t, false), true, true) :
c10d63c667cd 8073842: Invalid method reference when referencing a method on a wildcard type
mcimadamore
parents: 29147
diff changeset
  2033
                t;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2034
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2035
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  2036
06bc494ca11e Initial load
duke
parents:
diff changeset
  2037
    // <editor-fold defaultstate="collapsed" desc="isReifiable">
06bc494ca11e Initial load
duke
parents:
diff changeset
  2038
    public boolean isReifiable(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2039
        return isReifiable.visit(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2040
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2041
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
  2042
        private UnaryVisitor<Boolean> isReifiable = new UnaryVisitor<Boolean>() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2043
06bc494ca11e Initial load
duke
parents:
diff changeset
  2044
            public Boolean visitType(Type t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2045
                return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2046
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2047
06bc494ca11e Initial load
duke
parents:
diff changeset
  2048
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  2049
            public Boolean visitClassType(ClassType t, Void ignored) {
3554
a0a7f7f2a6e5 6806876: ClassCastException occurs in assignment expressions without any heap pollutions
mcimadamore
parents: 3539
diff changeset
  2050
                if (t.isCompound())
a0a7f7f2a6e5 6806876: ClassCastException occurs in assignment expressions without any heap pollutions
mcimadamore
parents: 3539
diff changeset
  2051
                    return false;
a0a7f7f2a6e5 6806876: ClassCastException occurs in assignment expressions without any heap pollutions
mcimadamore
parents: 3539
diff changeset
  2052
                else {
a0a7f7f2a6e5 6806876: ClassCastException occurs in assignment expressions without any heap pollutions
mcimadamore
parents: 3539
diff changeset
  2053
                    if (!t.isParameterized())
a0a7f7f2a6e5 6806876: ClassCastException occurs in assignment expressions without any heap pollutions
mcimadamore
parents: 3539
diff changeset
  2054
                        return true;
a0a7f7f2a6e5 6806876: ClassCastException occurs in assignment expressions without any heap pollutions
mcimadamore
parents: 3539
diff changeset
  2055
a0a7f7f2a6e5 6806876: ClassCastException occurs in assignment expressions without any heap pollutions
mcimadamore
parents: 3539
diff changeset
  2056
                    for (Type param : t.allparams()) {
a0a7f7f2a6e5 6806876: ClassCastException occurs in assignment expressions without any heap pollutions
mcimadamore
parents: 3539
diff changeset
  2057
                        if (!param.isUnbound())
a0a7f7f2a6e5 6806876: ClassCastException occurs in assignment expressions without any heap pollutions
mcimadamore
parents: 3539
diff changeset
  2058
                            return false;
a0a7f7f2a6e5 6806876: ClassCastException occurs in assignment expressions without any heap pollutions
mcimadamore
parents: 3539
diff changeset
  2059
                    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2060
                    return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2061
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2062
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2063
06bc494ca11e Initial load
duke
parents:
diff changeset
  2064
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  2065
            public Boolean visitArrayType(ArrayType t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2066
                return visit(t.elemtype);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2067
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2068
06bc494ca11e Initial load
duke
parents:
diff changeset
  2069
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  2070
            public Boolean visitTypeVar(TypeVar t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2071
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2072
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2073
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
  2074
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  2075
06bc494ca11e Initial load
duke
parents:
diff changeset
  2076
    // <editor-fold defaultstate="collapsed" desc="Array Utils">
06bc494ca11e Initial load
duke
parents:
diff changeset
  2077
    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
  2078
        while (t.hasTag(WILDCARD))
24612
75dc732b45af 8042338: Refactor Types.upperBound to treat wildcards and variables separately
dlsmith
parents: 24604
diff changeset
  2079
            t = wildUpperBound(t);
18646
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18412
diff changeset
  2080
        return t.hasTag(ARRAY);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2081
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2082
06bc494ca11e Initial load
duke
parents:
diff changeset
  2083
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  2084
     * The element type of an array.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2085
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2086
    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
  2087
        switch (t.getTag()) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2088
        case WILDCARD:
24612
75dc732b45af 8042338: Refactor Types.upperBound to treat wildcards and variables separately
dlsmith
parents: 24604
diff changeset
  2089
            return elemtype(wildUpperBound(t));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2090
        case ARRAY:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2091
            return ((ArrayType)t).elemtype;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2092
        case FORALL:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2093
            return elemtype(((ForAll)t).qtype);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2094
        case ERROR:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2095
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2096
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2097
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2098
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2099
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2100
7635
e0a3aeefe4e7 6199075: Unambiguous varargs method calls flagged as ambiguous
mcimadamore
parents: 7634
diff changeset
  2101
    public Type elemtypeOrType(Type t) {
e0a3aeefe4e7 6199075: Unambiguous varargs method calls flagged as ambiguous
mcimadamore
parents: 7634
diff changeset
  2102
        Type elemtype = elemtype(t);
e0a3aeefe4e7 6199075: Unambiguous varargs method calls flagged as ambiguous
mcimadamore
parents: 7634
diff changeset
  2103
        return elemtype != null ?
e0a3aeefe4e7 6199075: Unambiguous varargs method calls flagged as ambiguous
mcimadamore
parents: 7634
diff changeset
  2104
            elemtype :
e0a3aeefe4e7 6199075: Unambiguous varargs method calls flagged as ambiguous
mcimadamore
parents: 7634
diff changeset
  2105
            t;
e0a3aeefe4e7 6199075: Unambiguous varargs method calls flagged as ambiguous
mcimadamore
parents: 7634
diff changeset
  2106
    }
e0a3aeefe4e7 6199075: Unambiguous varargs method calls flagged as ambiguous
mcimadamore
parents: 7634
diff changeset
  2107
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2108
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  2109
     * Mapping to take element type of an arraytype
06bc494ca11e Initial load
duke
parents:
diff changeset
  2110
     */
29554
6d7957bd6866 8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents: 29292
diff changeset
  2111
    private TypeMapping<Void> elemTypeFun = new TypeMapping<Void>() {
6d7957bd6866 8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents: 29292
diff changeset
  2112
        @Override
6d7957bd6866 8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents: 29292
diff changeset
  2113
        public Type visitArrayType(ArrayType t, Void _unused) {
6d7957bd6866 8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents: 29292
diff changeset
  2114
            return t.elemtype;
6d7957bd6866 8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents: 29292
diff changeset
  2115
        }
6d7957bd6866 8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents: 29292
diff changeset
  2116
6d7957bd6866 8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents: 29292
diff changeset
  2117
        @Override
6d7957bd6866 8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents: 29292
diff changeset
  2118
        public Type visitTypeVar(TypeVar t, Void _unused) {
6d7957bd6866 8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents: 29292
diff changeset
  2119
            return visit(skipTypeVars(t, false));
27125
6c718c5086b3 8058511: StackOverflowError at com.sun.tools.javac.code.Types.lub
mcimadamore
parents: 26781
diff changeset
  2120
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2121
    };
06bc494ca11e Initial load
duke
parents:
diff changeset
  2122
06bc494ca11e Initial load
duke
parents:
diff changeset
  2123
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  2124
     * The number of dimensions of an array type.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2125
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2126
    public int dimensions(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2127
        int result = 0;
18646
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18412
diff changeset
  2128
        while (t.hasTag(ARRAY)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2129
            result++;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2130
            t = elemtype(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2131
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2132
        return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2133
    }
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 13633
diff changeset
  2134
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 13633
diff changeset
  2135
    /**
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 13633
diff changeset
  2136
     * Returns an ArrayType with the component type t
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 13633
diff changeset
  2137
     *
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 13633
diff changeset
  2138
     * @param t The component type of the ArrayType
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 13633
diff changeset
  2139
     * @return the ArrayType for the given component
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 13633
diff changeset
  2140
     */
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 13633
diff changeset
  2141
    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
  2142
        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
  2143
            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
  2144
        }
26781
a786b07c7b91 8048614: Add TypeMetadata to contain type annotations and other type information
emc
parents: 26107
diff changeset
  2145
        return new ArrayType(t, syms.arrayClass);
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 13633
diff changeset
  2146
    }
10
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="asSuper">
06bc494ca11e Initial load
duke
parents:
diff changeset
  2150
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  2151
     * Return the (most specific) base type of t that starts with the
06bc494ca11e Initial load
duke
parents:
diff changeset
  2152
     * given symbol.  If none exists, return null.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2153
     *
28800
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents: 28459
diff changeset
  2154
     * 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
  2155
     * 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
  2156
     * 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
  2157
     * 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
  2158
     *
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2159
     * @param t a type
06bc494ca11e Initial load
duke
parents:
diff changeset
  2160
     * @param sym a symbol
06bc494ca11e Initial load
duke
parents:
diff changeset
  2161
     */
21712
d363f367d4c0 8027439: Compile-time error in the case of ((Integer[] & Serializable)new Integer[1]).getClass()
emc
parents: 21041
diff changeset
  2162
    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
  2163
        /* Some examples:
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23810
diff changeset
  2164
         *
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23810
diff changeset
  2165
         * (Enum<E>, Comparable) => Comparable<E>
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23810
diff changeset
  2166
         * (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
  2167
         * (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
  2168
         * (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
  2169
         *     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
  2170
         */
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23810
diff changeset
  2171
        if (sym.type == syms.objectType) { //optimization
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23810
diff changeset
  2172
            return syms.objectType;
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23810
diff changeset
  2173
        }
21712
d363f367d4c0 8027439: Compile-time error in the case of ((Integer[] & Serializable)new Integer[1]).getClass()
emc
parents: 21041
diff changeset
  2174
        return asSuper.visit(t, sym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2175
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2176
    // where
21712
d363f367d4c0 8027439: Compile-time error in the case of ((Integer[] & Serializable)new Integer[1]).getClass()
emc
parents: 21041
diff changeset
  2177
        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
  2178
d363f367d4c0 8027439: Compile-time error in the case of ((Integer[] & Serializable)new Integer[1]).getClass()
emc
parents: 21041
diff changeset
  2179
            public Type visitType(Type t, Symbol sym) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2180
                return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2181
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2182
06bc494ca11e Initial load
duke
parents:
diff changeset
  2183
            @Override
21712
d363f367d4c0 8027439: Compile-time error in the case of ((Integer[] & Serializable)new Integer[1]).getClass()
emc
parents: 21041
diff changeset
  2184
            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
  2185
                if (t.tsym == sym)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2186
                    return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2187
06bc494ca11e Initial load
duke
parents:
diff changeset
  2188
                Type st = supertype(t);
24397
32aef69ee177 8042656: Inference produces spurious error for intersection type bounds check
dlsmith
parents: 24396
diff changeset
  2189
                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
  2190
                    Type x = asSuper(st, sym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2191
                    if (x != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2192
                        return x;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2193
                }
21712
d363f367d4c0 8027439: Compile-time error in the case of ((Integer[] & Serializable)new Integer[1]).getClass()
emc
parents: 21041
diff changeset
  2194
                if ((sym.flags() & INTERFACE) != 0) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2195
                    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
  2196
                        if (!l.head.hasTag(ERROR)) {
32aef69ee177 8042656: Inference produces spurious error for intersection type bounds check
dlsmith
parents: 24396
diff changeset
  2197
                            Type x = asSuper(l.head, sym);
32aef69ee177 8042656: Inference produces spurious error for intersection type bounds check
dlsmith
parents: 24396
diff changeset
  2198
                            if (x != null)
32aef69ee177 8042656: Inference produces spurious error for intersection type bounds check
dlsmith
parents: 24396
diff changeset
  2199
                                return x;
32aef69ee177 8042656: Inference produces spurious error for intersection type bounds check
dlsmith
parents: 24396
diff changeset
  2200
                        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2201
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2202
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2203
                return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2204
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2205
06bc494ca11e Initial load
duke
parents:
diff changeset
  2206
            @Override
21712
d363f367d4c0 8027439: Compile-time error in the case of ((Integer[] & Serializable)new Integer[1]).getClass()
emc
parents: 21041
diff changeset
  2207
            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
  2208
                return isSubtype(t, sym.type) ? sym.type : null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2209
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2210
06bc494ca11e Initial load
duke
parents:
diff changeset
  2211
            @Override
21712
d363f367d4c0 8027439: Compile-time error in the case of ((Integer[] & Serializable)new Integer[1]).getClass()
emc
parents: 21041
diff changeset
  2212
            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
  2213
                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
  2214
                    return t;
d51f30ce6796 6531090: Cannot access methods/fields of a captured type belonging to an intersection type
mcimadamore
parents: 162
diff changeset
  2215
                else
21712
d363f367d4c0 8027439: Compile-time error in the case of ((Integer[] & Serializable)new Integer[1]).getClass()
emc
parents: 21041
diff changeset
  2216
                    return asSuper(t.bound, sym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2217
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2218
06bc494ca11e Initial load
duke
parents:
diff changeset
  2219
            @Override
21712
d363f367d4c0 8027439: Compile-time error in the case of ((Integer[] & Serializable)new Integer[1]).getClass()
emc
parents: 21041
diff changeset
  2220
            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
  2221
                return t;
d363f367d4c0 8027439: Compile-time error in the case of ((Integer[] & Serializable)new Integer[1]).getClass()
emc
parents: 21041
diff changeset
  2222
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2223
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
  2224
06bc494ca11e Initial load
duke
parents:
diff changeset
  2225
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  2226
     * Return the base type of t or any of its outer types that starts
06bc494ca11e Initial load
duke
parents:
diff changeset
  2227
     * with the given symbol.  If none exists, return null.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2228
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  2229
     * @param t a type
06bc494ca11e Initial load
duke
parents:
diff changeset
  2230
     * @param sym a symbol
06bc494ca11e Initial load
duke
parents:
diff changeset
  2231
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2232
    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
  2233
        switch (t.getTag()) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2234
        case CLASS:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2235
            do {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2236
                Type s = asSuper(t, sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2237
                if (s != null) return s;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2238
                t = t.getEnclosingType();
18646
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18412
diff changeset
  2239
            } while (t.hasTag(CLASS));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2240
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2241
        case ARRAY:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2242
            return isSubtype(t, sym.type) ? sym.type : null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2243
        case TYPEVAR:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2244
            return asSuper(t, sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2245
        case ERROR:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2246
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2247
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2248
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2249
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2250
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2251
06bc494ca11e Initial load
duke
parents:
diff changeset
  2252
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  2253
     * Return the base type of t or any of its enclosing types that
06bc494ca11e Initial load
duke
parents:
diff changeset
  2254
     * starts with the given symbol.  If none exists, return null.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2255
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  2256
     * @param t a type
06bc494ca11e Initial load
duke
parents:
diff changeset
  2257
     * @param sym a symbol
06bc494ca11e Initial load
duke
parents:
diff changeset
  2258
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2259
    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
  2260
        switch (t.getTag()) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2261
        case CLASS:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2262
            do {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2263
                Type s = asSuper(t, sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2264
                if (s != null) return s;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2265
                Type outer = t.getEnclosingType();
18646
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18412
diff changeset
  2266
                t = (outer.hasTag(CLASS)) ? outer :
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2267
                    (t.tsym.owner.enclClass() != null) ? t.tsym.owner.enclClass().type :
06bc494ca11e Initial load
duke
parents:
diff changeset
  2268
                    Type.noType;
18646
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18412
diff changeset
  2269
            } while (t.hasTag(CLASS));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2270
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2271
        case ARRAY:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2272
            return isSubtype(t, sym.type) ? sym.type : null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2273
        case TYPEVAR:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2274
            return asSuper(t, sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2275
        case ERROR:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2276
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2277
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2278
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2279
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2280
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2281
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  2282
06bc494ca11e Initial load
duke
parents:
diff changeset
  2283
    // <editor-fold defaultstate="collapsed" desc="memberType">
06bc494ca11e Initial load
duke
parents:
diff changeset
  2284
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  2285
     * The type of given symbol, seen as a member of t.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2286
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  2287
     * @param t a type
06bc494ca11e Initial load
duke
parents:
diff changeset
  2288
     * @param sym a symbol
06bc494ca11e Initial load
duke
parents:
diff changeset
  2289
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2290
    public Type memberType(Type t, Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2291
        return (sym.flags() & STATIC) != 0
06bc494ca11e Initial load
duke
parents:
diff changeset
  2292
            ? sym.type
06bc494ca11e Initial load
duke
parents:
diff changeset
  2293
            : memberType.visit(t, sym);
3539
3c265cb6f4e0 6827648: Extremely slow compilation time for visitor pattern code + generics
mcimadamore
parents: 3140
diff changeset
  2294
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2295
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
  2296
        private SimpleVisitor<Type,Symbol> memberType = new SimpleVisitor<Type,Symbol>() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2297
06bc494ca11e Initial load
duke
parents:
diff changeset
  2298
            public Type visitType(Type t, Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2299
                return sym.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2300
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2301
06bc494ca11e Initial load
duke
parents:
diff changeset
  2302
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  2303
            public Type visitWildcardType(WildcardType t, Symbol sym) {
24612
75dc732b45af 8042338: Refactor Types.upperBound to treat wildcards and variables separately
dlsmith
parents: 24604
diff changeset
  2304
                return memberType(wildUpperBound(t), sym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2305
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2306
06bc494ca11e Initial load
duke
parents:
diff changeset
  2307
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  2308
            public Type visitClassType(ClassType t, Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2309
                Symbol owner = sym.owner;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2310
                long flags = sym.flags();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2311
                if (((flags & STATIC) == 0) && owner.type.isParameterized()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2312
                    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
  2313
                    //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
  2314
                    //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
  2315
                    //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
  2316
                    base = t.isCompound() ? capture(base) : base;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2317
                    if (base != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2318
                        List<Type> ownerParams = owner.type.allparams();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2319
                        List<Type> baseParams = base.allparams();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2320
                        if (ownerParams.nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2321
                            if (baseParams.isEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2322
                                // then base is a raw type
06bc494ca11e Initial load
duke
parents:
diff changeset
  2323
                                return erasure(sym.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2324
                            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2325
                                return subst(sym.type, ownerParams, baseParams);
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
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2330
                return sym.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2331
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2332
06bc494ca11e Initial load
duke
parents:
diff changeset
  2333
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  2334
            public Type visitTypeVar(TypeVar t, Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2335
                return memberType(t.bound, sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2336
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2337
06bc494ca11e Initial load
duke
parents:
diff changeset
  2338
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  2339
            public Type visitErrorType(ErrorType t, Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2340
                return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2341
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2342
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
  2343
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  2344
06bc494ca11e Initial load
duke
parents:
diff changeset
  2345
    // <editor-fold defaultstate="collapsed" desc="isAssignable">
06bc494ca11e Initial load
duke
parents:
diff changeset
  2346
    public boolean isAssignable(Type t, Type s) {
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14443
diff changeset
  2347
        return isAssignable(t, s, noWarnings);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2348
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2349
06bc494ca11e Initial load
duke
parents:
diff changeset
  2350
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  2351
     * Is t assignable to s?<br>
06bc494ca11e Initial load
duke
parents:
diff changeset
  2352
     * Equivalent to subtype except for constant values and raw
06bc494ca11e Initial load
duke
parents:
diff changeset
  2353
     * types.<br>
06bc494ca11e Initial load
duke
parents:
diff changeset
  2354
     * (not defined for Method and ForAll types)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2355
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2356
    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
  2357
        if (t.hasTag(ERROR))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2358
            return true;
18646
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18412
diff changeset
  2359
        if (t.getTag().isSubRangeOf(INT) && t.constValue() != null) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2360
            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
  2361
            switch (s.getTag()) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2362
            case BYTE:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2363
            case CHAR:
42973
3795332b55c7 8171132: Improve class reading of invalid or out-of-range ConstantValue attributes
cushon
parents: 42828
diff changeset
  2364
            case SHORT:
3795332b55c7 8171132: Improve class reading of invalid or out-of-range ConstantValue attributes
cushon
parents: 42828
diff changeset
  2365
            case INT:
3795332b55c7 8171132: Improve class reading of invalid or out-of-range ConstantValue attributes
cushon
parents: 42828
diff changeset
  2366
                if (s.getTag().checkRange(value))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2367
                    return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2368
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2369
            case CLASS:
18646
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18412
diff changeset
  2370
                switch (unboxedType(s).getTag()) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2371
                case BYTE:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2372
                case CHAR:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2373
                case SHORT:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2374
                    return isAssignable(t, unboxedType(s), warn);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2375
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2376
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2377
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2378
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2379
        return isConvertible(t, s, warn);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2380
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2381
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  2382
06bc494ca11e Initial load
duke
parents:
diff changeset
  2383
    // <editor-fold defaultstate="collapsed" desc="erasure">
06bc494ca11e Initial load
duke
parents:
diff changeset
  2384
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  2385
     * The erasure of t {@code |t|} -- the type that results when all
06bc494ca11e Initial load
duke
parents:
diff changeset
  2386
     * type parameters in t are deleted.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2387
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2388
    public Type erasure(Type t) {
29842
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29555
diff changeset
  2389
        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
  2390
    }
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  2391
    //where
13633
0ea4283af723 7181320: javac NullPointerException for switch labels with cast to String expressions
sundar
parents: 12468
diff changeset
  2392
    private boolean eraseNotNeeded(Type t) {
0ea4283af723 7181320: javac NullPointerException for switch labels with cast to String expressions
sundar
parents: 12468
diff changeset
  2393
        // 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
  2394
        // 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
  2395
        // 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
  2396
        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
  2397
    }
0ea4283af723 7181320: javac NullPointerException for switch labels with cast to String expressions
sundar
parents: 12468
diff changeset
  2398
514
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  2399
    private Type erasure(Type t, boolean recurse) {
24396
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 24293
diff changeset
  2400
        if (t.isPrimitive()) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2401
            return t; /* fast special case */
24396
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 24293
diff changeset
  2402
        } else {
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 24293
diff changeset
  2403
            Type out = erasure.visit(t, recurse);
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 24293
diff changeset
  2404
            return out;
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 24293
diff changeset
  2405
        }
45756
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45754
diff changeset
  2406
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2407
    // where
44186
fe848a208b20 8176265: Method overload resolution on a covariant base type doesn't work in 9
mcimadamore
parents: 43025
diff changeset
  2408
        private TypeMapping<Boolean> erasure = new StructuralTypeMapping<Boolean>() {
29842
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29555
diff changeset
  2409
            private Type combineMetadata(final Type s,
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29555
diff changeset
  2410
                                         final Type t) {
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29555
diff changeset
  2411
                if (t.getMetadata() != TypeMetadata.EMPTY) {
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29555
diff changeset
  2412
                    switch (s.getKind()) {
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29555
diff changeset
  2413
                        case OTHER:
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29555
diff changeset
  2414
                        case UNION:
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29555
diff changeset
  2415
                        case INTERSECTION:
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29555
diff changeset
  2416
                        case PACKAGE:
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29555
diff changeset
  2417
                        case EXECUTABLE:
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29555
diff changeset
  2418
                        case NONE:
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29555
diff changeset
  2419
                        case VOID:
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29555
diff changeset
  2420
                        case ERROR:
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29555
diff changeset
  2421
                            return s;
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29555
diff changeset
  2422
                        default: return s.cloneWithMetadata(s.getMetadata().without(Kind.ANNOTATIONS));
26781
a786b07c7b91 8048614: Add TypeMetadata to contain type annotations and other type information
emc
parents: 26107
diff changeset
  2423
                    }
a786b07c7b91 8048614: Add TypeMetadata to contain type annotations and other type information
emc
parents: 26107
diff changeset
  2424
                } else {
29842
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29555
diff changeset
  2425
                    return s;
26781
a786b07c7b91 8048614: Add TypeMetadata to contain type annotations and other type information
emc
parents: 26107
diff changeset
  2426
                }
a786b07c7b91 8048614: Add TypeMetadata to contain type annotations and other type information
emc
parents: 26107
diff changeset
  2427
            }
a786b07c7b91 8048614: Add TypeMetadata to contain type annotations and other type information
emc
parents: 26107
diff changeset
  2428
514
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  2429
            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
  2430
                if (t.isPrimitive())
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2431
                    return t; /*fast special case*/
24396
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 24293
diff changeset
  2432
                else {
29554
6d7957bd6866 8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents: 29292
diff changeset
  2433
                    //other cases already handled
29842
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29555
diff changeset
  2434
                    return combineMetadata(t, t);
24396
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 24293
diff changeset
  2435
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2436
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2437
06bc494ca11e Initial load
duke
parents:
diff changeset
  2438
            @Override
33915
66f0ce11ae22 8142931: java compiler: type erasure doesn't work since 9-b28
sadayapalam
parents: 33709
diff changeset
  2439
            public Type visitWildcardType(WildcardType t, Boolean recurse) {
66f0ce11ae22 8142931: java compiler: type erasure doesn't work since 9-b28
sadayapalam
parents: 33709
diff changeset
  2440
                Type erased = erasure(wildUpperBound(t), recurse);
66f0ce11ae22 8142931: java compiler: type erasure doesn't work since 9-b28
sadayapalam
parents: 33709
diff changeset
  2441
                return combineMetadata(erased, t);
66f0ce11ae22 8142931: java compiler: type erasure doesn't work since 9-b28
sadayapalam
parents: 33709
diff changeset
  2442
            }
66f0ce11ae22 8142931: java compiler: type erasure doesn't work since 9-b28
sadayapalam
parents: 33709
diff changeset
  2443
66f0ce11ae22 8142931: java compiler: type erasure doesn't work since 9-b28
sadayapalam
parents: 33709
diff changeset
  2444
            @Override
514
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  2445
            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
  2446
                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
  2447
                if (recurse) {
29842
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29555
diff changeset
  2448
                    erased = new ErasedClassType(erased.getEnclosingType(),erased.tsym,
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29555
diff changeset
  2449
                            t.getMetadata().without(Kind.ANNOTATIONS));
26781
a786b07c7b91 8048614: Add TypeMetadata to contain type annotations and other type information
emc
parents: 26107
diff changeset
  2450
                    return erased;
a786b07c7b91 8048614: Add TypeMetadata to contain type annotations and other type information
emc
parents: 26107
diff changeset
  2451
                } else {
29842
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29555
diff changeset
  2452
                    return combineMetadata(erased, t);
514
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  2453
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2454
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2455
06bc494ca11e Initial load
duke
parents:
diff changeset
  2456
            @Override
514
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  2457
            public Type visitTypeVar(TypeVar t, Boolean recurse) {
24396
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 24293
diff changeset
  2458
                Type erased = erasure(t.bound, recurse);
29842
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29555
diff changeset
  2459
                return combineMetadata(erased, t);
514
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  2460
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2461
        };
514
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  2462
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2463
    public List<Type> erasure(List<Type> ts) {
29554
6d7957bd6866 8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents: 29292
diff changeset
  2464
        return erasure.visit(ts, false);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2465
    }
514
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  2466
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  2467
    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
  2468
        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
  2469
    }
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  2470
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  2471
    public List<Type> erasureRecursive(List<Type> ts) {
29554
6d7957bd6866 8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents: 29292
diff changeset
  2472
        return erasure.visit(ts, true);
514
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  2473
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2474
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  2475
29147
4cba0458106b 8071291: Compiler crashes trying to cast UnionType to IntersectionClassType
mcimadamore
parents: 29146
diff changeset
  2476
    // <editor-fold defaultstate="collapsed" desc="makeIntersectionType">
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2477
    /**
29147
4cba0458106b 8071291: Compiler crashes trying to cast UnionType to IntersectionClassType
mcimadamore
parents: 29146
diff changeset
  2478
     * 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
  2479
     * {@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
  2480
     * Hence, this version of makeIntersectionType may not be called during a classfile read.
10
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
     * @param bounds    the types from which the intersection type is formed
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2483
     */
29147
4cba0458106b 8071291: Compiler crashes trying to cast UnionType to IntersectionClassType
mcimadamore
parents: 29146
diff changeset
  2484
    public IntersectionClassType makeIntersectionType(List<Type> bounds) {
4cba0458106b 8071291: Compiler crashes trying to cast UnionType to IntersectionClassType
mcimadamore
parents: 29146
diff changeset
  2485
        return makeIntersectionType(bounds, bounds.head.tsym.isInterface());
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
  2486
    }
29147
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
    /**
4cba0458106b 8071291: Compiler crashes trying to cast UnionType to IntersectionClassType
mcimadamore
parents: 29146
diff changeset
  2489
     * 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
  2490
     * {@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
  2491
     * 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
  2492
     * supertype is implicitly assumed to be 'Object'.
4cba0458106b 8071291: Compiler crashes trying to cast UnionType to IntersectionClassType
mcimadamore
parents: 29146
diff changeset
  2493
     *
4cba0458106b 8071291: Compiler crashes trying to cast UnionType to IntersectionClassType
mcimadamore
parents: 29146
diff changeset
  2494
     * @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
  2495
     * @param allInterfaces are all bounds interface types?
4cba0458106b 8071291: Compiler crashes trying to cast UnionType to IntersectionClassType
mcimadamore
parents: 29146
diff changeset
  2496
     */
4cba0458106b 8071291: Compiler crashes trying to cast UnionType to IntersectionClassType
mcimadamore
parents: 29146
diff changeset
  2497
    public IntersectionClassType makeIntersectionType(List<Type> bounds, boolean allInterfaces) {
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
  2498
        Assert.check(bounds.nonEmpty());
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
  2499
        Type firstExplicitBound = bounds.head;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
  2500
        if (allInterfaces) {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
  2501
            bounds = bounds.prepend(syms.objectType);
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
  2502
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2503
        ClassSymbol bc =
06bc494ca11e Initial load
duke
parents:
diff changeset
  2504
            new ClassSymbol(ABSTRACT|PUBLIC|SYNTHETIC|COMPOUND|ACYCLIC,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2505
                            Type.moreInfo
06bc494ca11e Initial load
duke
parents:
diff changeset
  2506
                                ? names.fromString(bounds.toString())
06bc494ca11e Initial load
duke
parents:
diff changeset
  2507
                                : names.empty,
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
  2508
                            null,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2509
                            syms.noSymbol);
29147
4cba0458106b 8071291: Compiler crashes trying to cast UnionType to IntersectionClassType
mcimadamore
parents: 29146
diff changeset
  2510
        IntersectionClassType intersectionType = new IntersectionClassType(bounds, bc, allInterfaces);
4cba0458106b 8071291: Compiler crashes trying to cast UnionType to IntersectionClassType
mcimadamore
parents: 29146
diff changeset
  2511
        bc.type = intersectionType;
18646
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18412
diff changeset
  2512
        bc.erasure_field = (bounds.head.hasTag(TYPEVAR)) ?
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
  2513
                syms.objectType : // error condition, recover
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
  2514
                erasure(firstExplicitBound);
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25441
diff changeset
  2515
        bc.members_field = WriteableScope.create(bc);
29147
4cba0458106b 8071291: Compiler crashes trying to cast UnionType to IntersectionClassType
mcimadamore
parents: 29146
diff changeset
  2516
        return intersectionType;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2517
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2518
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  2519
06bc494ca11e Initial load
duke
parents:
diff changeset
  2520
    // <editor-fold defaultstate="collapsed" desc="supertype">
06bc494ca11e Initial load
duke
parents:
diff changeset
  2521
    public Type supertype(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2522
        return supertype.visit(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2523
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2524
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
  2525
        private UnaryVisitor<Type> supertype = new UnaryVisitor<Type>() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2526
06bc494ca11e Initial load
duke
parents:
diff changeset
  2527
            public Type visitType(Type t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2528
                // A note on wildcards: there is no good way to
06bc494ca11e Initial load
duke
parents:
diff changeset
  2529
                // determine a supertype for a super bounded wildcard.
25276
59c17f73fee3 8034147: javac crashes with a NullPointerException during bounds checking
dlsmith
parents: 25273
diff changeset
  2530
                return Type.noType;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2531
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2532
06bc494ca11e Initial load
duke
parents:
diff changeset
  2533
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  2534
            public Type visitClassType(ClassType t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2535
                if (t.supertype_field == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2536
                    Type supertype = ((ClassSymbol)t.tsym).getSuperclass();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2537
                    // An interface has no superclass; its supertype is Object.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2538
                    if (t.isInterface())
06bc494ca11e Initial load
duke
parents:
diff changeset
  2539
                        supertype = ((ClassType)t.tsym.type).supertype_field;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2540
                    if (t.supertype_field == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2541
                        List<Type> actuals = classBound(t).allparams();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2542
                        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
  2543
                        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
  2544
                            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
  2545
                        } else if (formals.nonEmpty()) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2546
                            t.supertype_field = subst(supertype, formals, actuals);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2547
                        }
514
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  2548
                        else {
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  2549
                            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
  2550
                        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2551
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2552
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2553
                return t.supertype_field;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2554
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2555
06bc494ca11e Initial load
duke
parents:
diff changeset
  2556
            /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  2557
             * The supertype is always a class type. If the type
06bc494ca11e Initial load
duke
parents:
diff changeset
  2558
             * variable's bounds start with a class type, this is also
06bc494ca11e Initial load
duke
parents:
diff changeset
  2559
             * the supertype.  Otherwise, the supertype is
06bc494ca11e Initial load
duke
parents:
diff changeset
  2560
             * java.lang.Object.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2561
             */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2562
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  2563
            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
  2564
                if (t.bound.hasTag(TYPEVAR) ||
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2565
                    (!t.bound.isCompound() && !t.bound.isInterface())) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2566
                    return t.bound;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2567
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2568
                    return supertype(t.bound);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2569
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2570
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2571
06bc494ca11e Initial load
duke
parents:
diff changeset
  2572
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  2573
            public Type visitArrayType(ArrayType t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2574
                if (t.elemtype.isPrimitive() || isSameType(t.elemtype, syms.objectType))
06bc494ca11e Initial load
duke
parents:
diff changeset
  2575
                    return arraySuperType();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2576
                else
06bc494ca11e Initial load
duke
parents:
diff changeset
  2577
                    return new ArrayType(supertype(t.elemtype), t.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2578
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2579
06bc494ca11e Initial load
duke
parents:
diff changeset
  2580
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  2581
            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
  2582
                return Type.noType;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2583
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2584
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
  2585
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  2586
06bc494ca11e Initial load
duke
parents:
diff changeset
  2587
    // <editor-fold defaultstate="collapsed" desc="interfaces">
06bc494ca11e Initial load
duke
parents:
diff changeset
  2588
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  2589
     * Return the interfaces implemented by this class.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2590
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2591
    public List<Type> interfaces(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2592
        return interfaces.visit(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2593
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2594
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
  2595
        private UnaryVisitor<List<Type>> interfaces = new UnaryVisitor<List<Type>>() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2596
06bc494ca11e Initial load
duke
parents:
diff changeset
  2597
            public List<Type> visitType(Type t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2598
                return List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2599
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2600
06bc494ca11e Initial load
duke
parents:
diff changeset
  2601
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  2602
            public List<Type> visitClassType(ClassType t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2603
                if (t.interfaces_field == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2604
                    List<Type> interfaces = ((ClassSymbol)t.tsym).getInterfaces();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2605
                    if (t.interfaces_field == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2606
                        // If t.interfaces_field is null, then t must
06bc494ca11e Initial load
duke
parents:
diff changeset
  2607
                        // be a parameterized type (not to be confused
06bc494ca11e Initial load
duke
parents:
diff changeset
  2608
                        // with a generic type declaration).
06bc494ca11e Initial load
duke
parents:
diff changeset
  2609
                        // Terminology:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2610
                        //    Parameterized type: List<String>
06bc494ca11e Initial load
duke
parents:
diff changeset
  2611
                        //    Generic type declaration: class List<E> { ... }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2612
                        // So t corresponds to List<String> and
06bc494ca11e Initial load
duke
parents:
diff changeset
  2613
                        // t.tsym.type corresponds to List<E>.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2614
                        // The reason t must be parameterized type is
06bc494ca11e Initial load
duke
parents:
diff changeset
  2615
                        // that completion will happen as a side
06bc494ca11e Initial load
duke
parents:
diff changeset
  2616
                        // effect of calling
06bc494ca11e Initial load
duke
parents:
diff changeset
  2617
                        // ClassSymbol.getInterfaces.  Since
06bc494ca11e Initial load
duke
parents:
diff changeset
  2618
                        // t.interfaces_field is null after
06bc494ca11e Initial load
duke
parents:
diff changeset
  2619
                        // completion, we can assume that t is not the
06bc494ca11e Initial load
duke
parents:
diff changeset
  2620
                        // type of a class/interface declaration.
8032
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents: 7681
diff changeset
  2621
                        Assert.check(t != t.tsym.type, t);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2622
                        List<Type> actuals = t.allparams();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2623
                        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
  2624
                        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
  2625
                            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
  2626
                        } else if (formals.nonEmpty()) {
24612
75dc732b45af 8042338: Refactor Types.upperBound to treat wildcards and variables separately
dlsmith
parents: 24604
diff changeset
  2627
                            t.interfaces_field = subst(interfaces, formals, actuals);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2628
                        }
514
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  2629
                        else {
3942d9cdc81c 6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents: 326
diff changeset
  2630
                            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
  2631
                        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2632
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2633
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2634
                return t.interfaces_field;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2635
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2636
06bc494ca11e Initial load
duke
parents:
diff changeset
  2637
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  2638
            public List<Type> visitTypeVar(TypeVar t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2639
                if (t.bound.isCompound())
06bc494ca11e Initial load
duke
parents:
diff changeset
  2640
                    return interfaces(t.bound);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2641
06bc494ca11e Initial load
duke
parents:
diff changeset
  2642
                if (t.bound.isInterface())
06bc494ca11e Initial load
duke
parents:
diff changeset
  2643
                    return List.of(t.bound);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2644
06bc494ca11e Initial load
duke
parents:
diff changeset
  2645
                return List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2646
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2647
        };
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2648
20252
fae5c47ba7a4 6499673: Assertion check for TypeVariable.getUpperBound() fails.
emc
parents: 20249
diff changeset
  2649
    public List<Type> directSupertypes(Type t) {
fae5c47ba7a4 6499673: Assertion check for TypeVariable.getUpperBound() fails.
emc
parents: 20249
diff changeset
  2650
        return directSupertypes.visit(t);
fae5c47ba7a4 6499673: Assertion check for TypeVariable.getUpperBound() fails.
emc
parents: 20249
diff changeset
  2651
    }
fae5c47ba7a4 6499673: Assertion check for TypeVariable.getUpperBound() fails.
emc
parents: 20249
diff changeset
  2652
    // where
fae5c47ba7a4 6499673: Assertion check for TypeVariable.getUpperBound() fails.
emc
parents: 20249
diff changeset
  2653
        private final UnaryVisitor<List<Type>> directSupertypes = new UnaryVisitor<List<Type>>() {
fae5c47ba7a4 6499673: Assertion check for TypeVariable.getUpperBound() fails.
emc
parents: 20249
diff changeset
  2654
fae5c47ba7a4 6499673: Assertion check for TypeVariable.getUpperBound() fails.
emc
parents: 20249
diff changeset
  2655
            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
  2656
                if (!type.isIntersection()) {
20252
fae5c47ba7a4 6499673: Assertion check for TypeVariable.getUpperBound() fails.
emc
parents: 20249
diff changeset
  2657
                    final Type sup = supertype(type);
fae5c47ba7a4 6499673: Assertion check for TypeVariable.getUpperBound() fails.
emc
parents: 20249
diff changeset
  2658
                    return (sup == Type.noType || sup == type || sup == null)
fae5c47ba7a4 6499673: Assertion check for TypeVariable.getUpperBound() fails.
emc
parents: 20249
diff changeset
  2659
                        ? interfaces(type)
fae5c47ba7a4 6499673: Assertion check for TypeVariable.getUpperBound() fails.
emc
parents: 20249
diff changeset
  2660
                        : interfaces(type).prepend(sup);
fae5c47ba7a4 6499673: Assertion check for TypeVariable.getUpperBound() fails.
emc
parents: 20249
diff changeset
  2661
                } else {
33556
a14a556cf2c9 8141343: Subtle semantics changes for union types in cast conversion
mcimadamore
parents: 32915
diff changeset
  2662
                    return ((IntersectionClassType)type).getExplicitComponents();
20252
fae5c47ba7a4 6499673: Assertion check for TypeVariable.getUpperBound() fails.
emc
parents: 20249
diff changeset
  2663
                }
fae5c47ba7a4 6499673: Assertion check for TypeVariable.getUpperBound() fails.
emc
parents: 20249
diff changeset
  2664
            }
fae5c47ba7a4 6499673: Assertion check for TypeVariable.getUpperBound() fails.
emc
parents: 20249
diff changeset
  2665
        };
fae5c47ba7a4 6499673: Assertion check for TypeVariable.getUpperBound() fails.
emc
parents: 20249
diff changeset
  2666
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14443
diff changeset
  2667
    public boolean isDirectSuperInterface(TypeSymbol isym, TypeSymbol origin) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14443
diff changeset
  2668
        for (Type i2 : interfaces(origin.type)) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14443
diff changeset
  2669
            if (isym == i2.tsym) return true;
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2670
        }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2671
        return false;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2672
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2673
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  2674
06bc494ca11e Initial load
duke
parents:
diff changeset
  2675
    // <editor-fold defaultstate="collapsed" desc="isDerivedRaw">
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21712
diff changeset
  2676
    Map<Type,Boolean> isDerivedRawCache = new HashMap<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2677
06bc494ca11e Initial load
duke
parents:
diff changeset
  2678
    public boolean isDerivedRaw(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2679
        Boolean result = isDerivedRawCache.get(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2680
        if (result == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2681
            result = isDerivedRawInternal(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2682
            isDerivedRawCache.put(t, result);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2683
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2684
        return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2685
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2686
06bc494ca11e Initial load
duke
parents:
diff changeset
  2687
    public boolean isDerivedRawInternal(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2688
        if (t.isErroneous())
06bc494ca11e Initial load
duke
parents:
diff changeset
  2689
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2690
        return
06bc494ca11e Initial load
duke
parents:
diff changeset
  2691
            t.isRaw() ||
25276
59c17f73fee3 8034147: javac crashes with a NullPointerException during bounds checking
dlsmith
parents: 25273
diff changeset
  2692
            supertype(t) != Type.noType && isDerivedRaw(supertype(t)) ||
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2693
            isDerivedRaw(interfaces(t));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2694
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2695
06bc494ca11e Initial load
duke
parents:
diff changeset
  2696
    public boolean isDerivedRaw(List<Type> ts) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2697
        List<Type> l = ts;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2698
        while (l.nonEmpty() && !isDerivedRaw(l.head)) l = l.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2699
        return l.nonEmpty();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2700
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2701
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  2702
06bc494ca11e Initial load
duke
parents:
diff changeset
  2703
    // <editor-fold defaultstate="collapsed" desc="setBounds">
06bc494ca11e Initial load
duke
parents:
diff changeset
  2704
    /**
29147
4cba0458106b 8071291: Compiler crashes trying to cast UnionType to IntersectionClassType
mcimadamore
parents: 29146
diff changeset
  2705
     * 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
  2706
     * 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
  2707
     * 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
  2708
     * 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
  2709
     * 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
  2710
     *
4cba0458106b 8071291: Compiler crashes trying to cast UnionType to IntersectionClassType
mcimadamore
parents: 29146
diff changeset
  2711
     * @param t         a type variable
4cba0458106b 8071291: Compiler crashes trying to cast UnionType to IntersectionClassType
mcimadamore
parents: 29146
diff changeset
  2712
     * @param bounds    the bounds, must be nonempty
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2713
     */
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
  2714
    public void setBounds(TypeVar t, List<Type> bounds) {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
  2715
        setBounds(t, bounds, bounds.head.tsym.isInterface());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2716
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2717
06bc494ca11e Initial load
duke
parents:
diff changeset
  2718
    /**
29147
4cba0458106b 8071291: Compiler crashes trying to cast UnionType to IntersectionClassType
mcimadamore
parents: 29146
diff changeset
  2719
     * 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
  2720
     * 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
  2721
     * 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
  2722
     *
4cba0458106b 8071291: Compiler crashes trying to cast UnionType to IntersectionClassType
mcimadamore
parents: 29146
diff changeset
  2723
     * @param t             a type variable
4cba0458106b 8071291: Compiler crashes trying to cast UnionType to IntersectionClassType
mcimadamore
parents: 29146
diff changeset
  2724
     * @param bounds        the bounds, must be nonempty
4cba0458106b 8071291: Compiler crashes trying to cast UnionType to IntersectionClassType
mcimadamore
parents: 29146
diff changeset
  2725
     * @param allInterfaces are all bounds interface types?
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2726
     */
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
  2727
    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
  2728
        t.bound = bounds.tail.isEmpty() ?
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
  2729
                bounds.head :
29147
4cba0458106b 8071291: Compiler crashes trying to cast UnionType to IntersectionClassType
mcimadamore
parents: 29146
diff changeset
  2730
                makeIntersectionType(bounds, allInterfaces);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2731
        t.rank_field = -1;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2732
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2733
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  2734
06bc494ca11e Initial load
duke
parents:
diff changeset
  2735
    // <editor-fold defaultstate="collapsed" desc="getBounds">
06bc494ca11e Initial load
duke
parents:
diff changeset
  2736
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  2737
     * Return list of bounds of the given type variable.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2738
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2739
    public List<Type> getBounds(TypeVar t) {
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14723
diff changeset
  2740
        if (t.bound.hasTag(NONE))
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14443
diff changeset
  2741
            return List.nil();
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14443
diff changeset
  2742
        else if (t.bound.isErroneous() || !t.bound.isCompound())
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2743
            return List.of(t.bound);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2744
        else if ((erasure(t).tsym.flags() & INTERFACE) == 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2745
            return interfaces(t).prepend(supertype(t));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2746
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
  2747
            // No superclass was given in bounds.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2748
            // In this case, supertype is Object, erasure is first interface.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2749
            return interfaces(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2750
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2751
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  2752
06bc494ca11e Initial load
duke
parents:
diff changeset
  2753
    // <editor-fold defaultstate="collapsed" desc="classBound">
06bc494ca11e Initial load
duke
parents:
diff changeset
  2754
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  2755
     * If the given type is a (possibly selected) type variable,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2756
     * return the bounding class of this type, otherwise return the
06bc494ca11e Initial load
duke
parents:
diff changeset
  2757
     * type itself.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2758
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2759
    public Type classBound(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2760
        return classBound.visit(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2761
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2762
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
  2763
        private UnaryVisitor<Type> classBound = new UnaryVisitor<Type>() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2764
06bc494ca11e Initial load
duke
parents:
diff changeset
  2765
            public Type visitType(Type t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2766
                return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2767
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2768
06bc494ca11e Initial load
duke
parents:
diff changeset
  2769
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  2770
            public Type visitClassType(ClassType t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2771
                Type outer1 = classBound(t.getEnclosingType());
06bc494ca11e Initial load
duke
parents:
diff changeset
  2772
                if (outer1 != t.getEnclosingType())
24396
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 24293
diff changeset
  2773
                    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
  2774
                                         t.getMetadata());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2775
                else
06bc494ca11e Initial load
duke
parents:
diff changeset
  2776
                    return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2777
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2778
06bc494ca11e Initial load
duke
parents:
diff changeset
  2779
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  2780
            public Type visitTypeVar(TypeVar t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2781
                return classBound(supertype(t));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2782
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2783
06bc494ca11e Initial load
duke
parents:
diff changeset
  2784
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  2785
            public Type visitErrorType(ErrorType t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2786
                return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2787
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2788
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
  2789
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  2790
06bc494ca11e Initial load
duke
parents:
diff changeset
  2791
    // <editor-fold defaultstate="collapsed" desc="sub signature / override equivalence">
06bc494ca11e Initial load
duke
parents:
diff changeset
  2792
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  2793
     * Returns true iff the first signature is a <em>sub
06bc494ca11e Initial load
duke
parents:
diff changeset
  2794
     * signature</em> of the other.  This is <b>not</b> an equivalence
06bc494ca11e Initial load
duke
parents:
diff changeset
  2795
     * relation.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2796
     *
9303
eae35c201e19 7032975: API files in javax.annotation.processing need to be updated for references to JLS
jjh
parents: 9301
diff changeset
  2797
     * @jls section 8.4.2.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2798
     * @see #overrideEquivalent(Type t, Type s)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2799
     * @param t first signature (possibly raw).
06bc494ca11e Initial load
duke
parents:
diff changeset
  2800
     * @param s second signature (could be subjected to erasure).
06bc494ca11e Initial load
duke
parents:
diff changeset
  2801
     * @return true if t is a sub signature of s.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2802
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2803
    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
  2804
        return isSubSignature(t, s, true);
9e31f1c01bd4 7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents: 8625
diff changeset
  2805
    }
9e31f1c01bd4 7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents: 8625
diff changeset
  2806
9e31f1c01bd4 7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents: 8625
diff changeset
  2807
    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
  2808
        return hasSameArgs(t, s, strict) || hasSameArgs(t, erasure(s), strict);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2809
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2810
06bc494ca11e Initial load
duke
parents:
diff changeset
  2811
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  2812
     * Returns true iff these signatures are related by <em>override
06bc494ca11e Initial load
duke
parents:
diff changeset
  2813
     * equivalence</em>.  This is the natural extension of
06bc494ca11e Initial load
duke
parents:
diff changeset
  2814
     * isSubSignature to an equivalence relation.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2815
     *
9303
eae35c201e19 7032975: API files in javax.annotation.processing need to be updated for references to JLS
jjh
parents: 9301
diff changeset
  2816
     * @jls section 8.4.2.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2817
     * @see #isSubSignature(Type t, Type s)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2818
     * @param t a signature (possible raw, could be subjected to
06bc494ca11e Initial load
duke
parents:
diff changeset
  2819
     * erasure).
06bc494ca11e Initial load
duke
parents:
diff changeset
  2820
     * @param s a signature (possible raw, could be subjected to
06bc494ca11e Initial load
duke
parents:
diff changeset
  2821
     * erasure).
06bc494ca11e Initial load
duke
parents:
diff changeset
  2822
     * @return true if either argument is a sub signature of the other.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2823
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2824
    public boolean overrideEquivalent(Type t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2825
        return hasSameArgs(t, s) ||
06bc494ca11e Initial load
duke
parents:
diff changeset
  2826
            hasSameArgs(t, erasure(s)) || hasSameArgs(erasure(t), s);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2827
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2828
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2829
    public boolean overridesObjectMethod(TypeSymbol origin, Symbol msym) {
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25441
diff changeset
  2830
        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
  2831
            if (msym.overrides(sym, origin, Types.this, true)) {
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2832
                return true;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2833
            }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2834
        }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2835
        return false;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2836
    }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2837
41526
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2838
    /**
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2839
     * This enum defines the strategy for implementing most specific return type check
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2840
     * during the most specific and functional interface checks.
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2841
     */
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2842
    public enum MostSpecificReturnCheck {
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2843
        /**
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2844
         * Return r1 is more specific than r2 if {@code r1 <: r2}. Extra care required for (i) handling
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2845
         * method type variables (if either method is generic) and (ii) subtyping should be replaced
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2846
         * by type-equivalence for primitives. This is essentially an inlined version of
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2847
         * {@link Types#resultSubtype(Type, Type, Warner)}, where the assignability check has been
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2848
         * replaced with a strict subtyping check.
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2849
         */
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2850
        BASIC() {
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2851
            @Override
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2852
            public boolean test(Type mt1, Type mt2, Types types) {
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2853
                List<Type> tvars = mt1.getTypeArguments();
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2854
                List<Type> svars = mt2.getTypeArguments();
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2855
                Type t = mt1.getReturnType();
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2856
                Type s = types.subst(mt2.getReturnType(), svars, tvars);
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2857
                return types.isSameType(t, s) ||
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2858
                    !t.isPrimitive() &&
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2859
                    !s.isPrimitive() &&
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2860
                    types.isSubtype(t, s);
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2861
            }
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2862
        },
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2863
        /**
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2864
         * Return r1 is more specific than r2 if r1 is return-type-substitutable for r2.
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2865
         */
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2866
        RTS() {
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2867
            @Override
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2868
            public boolean test(Type mt1, Type mt2, Types types) {
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2869
                return types.returnTypeSubstitutable(mt1, mt2);
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2870
            }
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2871
        };
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2872
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2873
        public abstract boolean test(Type mt1, Type mt2, Types types);
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2874
    }
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2875
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2876
    /**
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2877
     * Merge multiple abstract methods. The preferred method is a method that is a subsignature
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2878
     * of all the other signatures and whose return type is more specific {@see MostSpecificReturnCheck}.
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2879
     * The resulting preferred method has a thrown clause that is the intersection of the merged
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2880
     * methods' clauses.
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2881
     */
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2882
    public Optional<Symbol> mergeAbstracts(List<Symbol> ambiguousInOrder, Type site, boolean sigCheck) {
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2883
        //first check for preconditions
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2884
        boolean shouldErase = false;
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2885
        List<Type> erasedParams = ambiguousInOrder.head.erasure(this).getParameterTypes();
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2886
        for (Symbol s : ambiguousInOrder) {
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2887
            if ((s.flags() & ABSTRACT) == 0 ||
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2888
                    (sigCheck && !isSameTypes(erasedParams, s.erasure(this).getParameterTypes()))) {
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2889
                return Optional.empty();
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2890
            } else if (s.type.hasTag(FORALL)) {
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2891
                shouldErase = true;
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2892
            }
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2893
        }
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2894
        //then merge abstracts
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2895
        for (MostSpecificReturnCheck mostSpecificReturnCheck : MostSpecificReturnCheck.values()) {
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2896
            outer: for (Symbol s : ambiguousInOrder) {
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2897
                Type mt = memberType(site, s);
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2898
                List<Type> allThrown = mt.getThrownTypes();
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2899
                for (Symbol s2 : ambiguousInOrder) {
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2900
                    if (s != s2) {
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2901
                        Type mt2 = memberType(site, s2);
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2902
                        if (!isSubSignature(mt, mt2) ||
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2903
                                !mostSpecificReturnCheck.test(mt, mt2, this)) {
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2904
                            //ambiguity cannot be resolved
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2905
                            continue outer;
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2906
                        } else {
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2907
                            List<Type> thrownTypes2 = mt2.getThrownTypes();
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2908
                            if (!mt.hasTag(FORALL) && shouldErase) {
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2909
                                thrownTypes2 = erasure(thrownTypes2);
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2910
                            } else if (mt.hasTag(FORALL)) {
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2911
                                //subsignature implies that if most specific is generic, then all other
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2912
                                //methods are too
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2913
                                Assert.check(mt2.hasTag(FORALL));
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2914
                                // if both are generic methods, adjust thrown types ahead of intersection computation
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2915
                                thrownTypes2 = subst(thrownTypes2, mt2.getTypeArguments(), mt.getTypeArguments());
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2916
                            }
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2917
                            allThrown = chk.intersect(allThrown, thrownTypes2);
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2918
                        }
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2919
                    }
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2920
                }
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2921
                return (allThrown == mt.getThrownTypes()) ?
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2922
                        Optional.of(s) :
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2923
                        Optional.of(new MethodSymbol(
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2924
                                s.flags(),
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2925
                                s.name,
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2926
                                createMethodTypeWithThrown(s.type, allThrown),
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2927
                                s.owner) {
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2928
                            @Override
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2929
                            public Symbol baseSymbol() {
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2930
                                return s;
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2931
                            }
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2932
                        });
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2933
            }
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2934
        }
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2935
        return Optional.empty();
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2936
    }
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 40591
diff changeset
  2937
6591
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2938
    // <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
  2939
    class ImplementationCache {
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2940
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21712
diff changeset
  2941
        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
  2942
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2943
        class Entry {
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2944
            final MethodSymbol cachedImpl;
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2945
            final Filter<Symbol> implFilter;
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2946
            final boolean checkResult;
8427
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents: 8242
diff changeset
  2947
            final int prevMark;
6591
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2948
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2949
            public Entry(MethodSymbol cachedImpl,
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2950
                    Filter<Symbol> scopeFilter,
8427
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents: 8242
diff changeset
  2951
                    boolean checkResult,
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents: 8242
diff changeset
  2952
                    int prevMark) {
6591
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2953
                this.cachedImpl = cachedImpl;
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2954
                this.implFilter = scopeFilter;
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2955
                this.checkResult = checkResult;
8427
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents: 8242
diff changeset
  2956
                this.prevMark = prevMark;
6591
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2957
            }
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2958
8427
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents: 8242
diff changeset
  2959
            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
  2960
                return this.implFilter == scopeFilter &&
8427
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents: 8242
diff changeset
  2961
                        this.checkResult == checkResult &&
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents: 8242
diff changeset
  2962
                        this.prevMark == mark;
6591
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2963
            }
3539
3c265cb6f4e0 6827648: Extremely slow compilation time for visitor pattern code + generics
mcimadamore
parents: 3140
diff changeset
  2964
        }
6591
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2965
8242
3873b4aaf4a8 7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents: 8230
diff changeset
  2966
        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
  2967
            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
  2968
            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
  2969
            if (cache == null) {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21712
diff changeset
  2970
                cache = new HashMap<>();
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21712
diff changeset
  2971
                _map.put(ms, new SoftReference<>(cache));
6591
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2972
            }
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2973
            Entry e = cache.get(origin);
9812
f716e42cb230 7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents: 9721
diff changeset
  2974
            CompoundScope members = membersClosure(origin.type, true);
6591
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2975
            if (e == null ||
8427
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents: 8242
diff changeset
  2976
                    !e.matches(implFilter, checkResult, members.getMark())) {
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents: 8242
diff changeset
  2977
                MethodSymbol impl = implementationInternal(ms, origin, checkResult, implFilter);
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents: 8242
diff changeset
  2978
                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
  2979
                return impl;
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2980
            }
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2981
            else {
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2982
                return e.cachedImpl;
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2983
            }
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2984
        }
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  2985
8427
703181b01773 7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents: 8242
diff changeset
  2986
        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
  2987
            for (Type t = origin.type; t.hasTag(CLASS) || t.hasTag(TYPEVAR); t = supertype(t)) {
29292
c10d63c667cd 8073842: Invalid method reference when referencing a method on a wildcard type
mcimadamore
parents: 29147
diff changeset
  2988
                t = skipTypeVars(t, false);
3539
3c265cb6f4e0 6827648: Extremely slow compilation time for visitor pattern code + generics
mcimadamore
parents: 3140
diff changeset
  2989
                TypeSymbol c = t.tsym;
27390
02221d4ed524 6987475: Order of declarations affects whether abstract method considered overridden
mcimadamore
parents: 27228
diff changeset
  2990
                Symbol bestSoFar = null;
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25441
diff changeset
  2991
                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
  2992
                    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
  2993
                        bestSoFar = sym;
02221d4ed524 6987475: Order of declarations affects whether abstract method considered overridden
mcimadamore
parents: 27228
diff changeset
  2994
                        if ((sym.flags() & ABSTRACT) == 0) {
02221d4ed524 6987475: Order of declarations affects whether abstract method considered overridden
mcimadamore
parents: 27228
diff changeset
  2995
                            //if concrete impl is found, exit immediately
02221d4ed524 6987475: Order of declarations affects whether abstract method considered overridden
mcimadamore
parents: 27228
diff changeset
  2996
                            break;
02221d4ed524 6987475: Order of declarations affects whether abstract method considered overridden
mcimadamore
parents: 27228
diff changeset
  2997
                        }
02221d4ed524 6987475: Order of declarations affects whether abstract method considered overridden
mcimadamore
parents: 27228
diff changeset
  2998
                    }
02221d4ed524 6987475: Order of declarations affects whether abstract method considered overridden
mcimadamore
parents: 27228
diff changeset
  2999
                }
02221d4ed524 6987475: Order of declarations affects whether abstract method considered overridden
mcimadamore
parents: 27228
diff changeset
  3000
                if (bestSoFar != null) {
02221d4ed524 6987475: Order of declarations affects whether abstract method considered overridden
mcimadamore
parents: 27228
diff changeset
  3001
                    //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
  3002
                    return (MethodSymbol)bestSoFar;
3539
3c265cb6f4e0 6827648: Extremely slow compilation time for visitor pattern code + generics
mcimadamore
parents: 3140
diff changeset
  3003
                }
3c265cb6f4e0 6827648: Extremely slow compilation time for visitor pattern code + generics
mcimadamore
parents: 3140
diff changeset
  3004
            }
6591
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  3005
            return null;
3539
3c265cb6f4e0 6827648: Extremely slow compilation time for visitor pattern code + generics
mcimadamore
parents: 3140
diff changeset
  3006
        }
3c265cb6f4e0 6827648: Extremely slow compilation time for visitor pattern code + generics
mcimadamore
parents: 3140
diff changeset
  3007
    }
3c265cb6f4e0 6827648: Extremely slow compilation time for visitor pattern code + generics
mcimadamore
parents: 3140
diff changeset
  3008
6591
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  3009
    private ImplementationCache implCache = new ImplementationCache();
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  3010
8242
3873b4aaf4a8 7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents: 8230
diff changeset
  3011
    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
  3012
        return implCache.get(ms, origin, checkResult, implFilter);
3873b4aaf4a8 7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents: 8230
diff changeset
  3013
    }
3873b4aaf4a8 7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents: 8230
diff changeset
  3014
    // </editor-fold>
3873b4aaf4a8 7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents: 8230
diff changeset
  3015
3873b4aaf4a8 7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents: 8230
diff changeset
  3016
    // <editor-fold defaultstate="collapsed" desc="compute transitive closure of all members in given site">
31005
673532e90337 8039262: Java compiler performance degradation jdk1.7 vs. jdk1.6 should be amended
jlahoda
parents: 30845
diff changeset
  3017
    class MembersClosureCache extends SimpleVisitor<Scope.CompoundScope, Void> {
673532e90337 8039262: Java compiler performance degradation jdk1.7 vs. jdk1.6 should be amended
jlahoda
parents: 30845
diff changeset
  3018
673532e90337 8039262: Java compiler performance degradation jdk1.7 vs. jdk1.6 should be amended
jlahoda
parents: 30845
diff changeset
  3019
        private Map<TypeSymbol, CompoundScope> _map = new HashMap<>();
673532e90337 8039262: Java compiler performance degradation jdk1.7 vs. jdk1.6 should be amended
jlahoda
parents: 30845
diff changeset
  3020
673532e90337 8039262: Java compiler performance degradation jdk1.7 vs. jdk1.6 should be amended
jlahoda
parents: 30845
diff changeset
  3021
        Set<TypeSymbol> seenTypes = new HashSet<>();
673532e90337 8039262: Java compiler performance degradation jdk1.7 vs. jdk1.6 should be amended
jlahoda
parents: 30845
diff changeset
  3022
673532e90337 8039262: Java compiler performance degradation jdk1.7 vs. jdk1.6 should be amended
jlahoda
parents: 30845
diff changeset
  3023
        class MembersScope extends CompoundScope {
673532e90337 8039262: Java compiler performance degradation jdk1.7 vs. jdk1.6 should be amended
jlahoda
parents: 30845
diff changeset
  3024
673532e90337 8039262: Java compiler performance degradation jdk1.7 vs. jdk1.6 should be amended
jlahoda
parents: 30845
diff changeset
  3025
            CompoundScope scope;
673532e90337 8039262: Java compiler performance degradation jdk1.7 vs. jdk1.6 should be amended
jlahoda
parents: 30845
diff changeset
  3026
673532e90337 8039262: Java compiler performance degradation jdk1.7 vs. jdk1.6 should be amended
jlahoda
parents: 30845
diff changeset
  3027
            public MembersScope(CompoundScope scope) {
673532e90337 8039262: Java compiler performance degradation jdk1.7 vs. jdk1.6 should be amended
jlahoda
parents: 30845
diff changeset
  3028
                super(scope.owner);
673532e90337 8039262: Java compiler performance degradation jdk1.7 vs. jdk1.6 should be amended
jlahoda
parents: 30845
diff changeset
  3029
                this.scope = scope;
9812
f716e42cb230 7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents: 9721
diff changeset
  3030
            }
f716e42cb230 7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents: 9721
diff changeset
  3031
31005
673532e90337 8039262: Java compiler performance degradation jdk1.7 vs. jdk1.6 should be amended
jlahoda
parents: 30845
diff changeset
  3032
            Filter<Symbol> combine(Filter<Symbol> sf) {
673532e90337 8039262: Java compiler performance degradation jdk1.7 vs. jdk1.6 should be amended
jlahoda
parents: 30845
diff changeset
  3033
                return s -> !s.owner.isInterface() && (sf == null || sf.accepts(s));
673532e90337 8039262: Java compiler performance degradation jdk1.7 vs. jdk1.6 should be amended
jlahoda
parents: 30845
diff changeset
  3034
            }
673532e90337 8039262: Java compiler performance degradation jdk1.7 vs. jdk1.6 should be amended
jlahoda
parents: 30845
diff changeset
  3035
673532e90337 8039262: Java compiler performance degradation jdk1.7 vs. jdk1.6 should be amended
jlahoda
parents: 30845
diff changeset
  3036
            @Override
673532e90337 8039262: Java compiler performance degradation jdk1.7 vs. jdk1.6 should be amended
jlahoda
parents: 30845
diff changeset
  3037
            public Iterable<Symbol> getSymbols(Filter<Symbol> sf, LookupKind lookupKind) {
673532e90337 8039262: Java compiler performance degradation jdk1.7 vs. jdk1.6 should be amended
jlahoda
parents: 30845
diff changeset
  3038
                return scope.getSymbols(combine(sf), lookupKind);
673532e90337 8039262: Java compiler performance degradation jdk1.7 vs. jdk1.6 should be amended
jlahoda
parents: 30845
diff changeset
  3039
            }
673532e90337 8039262: Java compiler performance degradation jdk1.7 vs. jdk1.6 should be amended
jlahoda
parents: 30845
diff changeset
  3040
673532e90337 8039262: Java compiler performance degradation jdk1.7 vs. jdk1.6 should be amended
jlahoda
parents: 30845
diff changeset
  3041
            @Override
673532e90337 8039262: Java compiler performance degradation jdk1.7 vs. jdk1.6 should be amended
jlahoda
parents: 30845
diff changeset
  3042
            public Iterable<Symbol> getSymbolsByName(Name name, Filter<Symbol> sf, LookupKind lookupKind) {
673532e90337 8039262: Java compiler performance degradation jdk1.7 vs. jdk1.6 should be amended
jlahoda
parents: 30845
diff changeset
  3043
                return scope.getSymbolsByName(name, combine(sf), lookupKind);
673532e90337 8039262: Java compiler performance degradation jdk1.7 vs. jdk1.6 should be amended
jlahoda
parents: 30845
diff changeset
  3044
            }
673532e90337 8039262: Java compiler performance degradation jdk1.7 vs. jdk1.6 should be amended
jlahoda
parents: 30845
diff changeset
  3045
673532e90337 8039262: Java compiler performance degradation jdk1.7 vs. jdk1.6 should be amended
jlahoda
parents: 30845
diff changeset
  3046
            @Override
673532e90337 8039262: Java compiler performance degradation jdk1.7 vs. jdk1.6 should be amended
jlahoda
parents: 30845
diff changeset
  3047
            public int getMark() {
673532e90337 8039262: Java compiler performance degradation jdk1.7 vs. jdk1.6 should be amended
jlahoda
parents: 30845
diff changeset
  3048
                return scope.getMark();
9812
f716e42cb230 7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents: 9721
diff changeset
  3049
            }
f716e42cb230 7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents: 9721
diff changeset
  3050
        }
f716e42cb230 7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents: 9721
diff changeset
  3051
31005
673532e90337 8039262: Java compiler performance degradation jdk1.7 vs. jdk1.6 should be amended
jlahoda
parents: 30845
diff changeset
  3052
        CompoundScope nilScope;
10453
41fe3e16698a 7079713: javac hangs when compiling a class that references a cyclically inherited class
mcimadamore
parents: 10452
diff changeset
  3053
9812
f716e42cb230 7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents: 9721
diff changeset
  3054
        /** members closure visitor methods **/
f716e42cb230 7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents: 9721
diff changeset
  3055
31005
673532e90337 8039262: Java compiler performance degradation jdk1.7 vs. jdk1.6 should be amended
jlahoda
parents: 30845
diff changeset
  3056
        public CompoundScope visitType(Type t, Void _unused) {
673532e90337 8039262: Java compiler performance degradation jdk1.7 vs. jdk1.6 should be amended
jlahoda
parents: 30845
diff changeset
  3057
            if (nilScope == null) {
673532e90337 8039262: Java compiler performance degradation jdk1.7 vs. jdk1.6 should be amended
jlahoda
parents: 30845
diff changeset
  3058
                nilScope = new CompoundScope(syms.noSymbol);
673532e90337 8039262: Java compiler performance degradation jdk1.7 vs. jdk1.6 should be amended
jlahoda
parents: 30845
diff changeset
  3059
            }
673532e90337 8039262: Java compiler performance degradation jdk1.7 vs. jdk1.6 should be amended
jlahoda
parents: 30845
diff changeset
  3060
            return nilScope;
8242
3873b4aaf4a8 7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents: 8230
diff changeset
  3061
        }
3873b4aaf4a8 7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents: 8230
diff changeset
  3062
3873b4aaf4a8 7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents: 8230
diff changeset
  3063
        @Override
31005
673532e90337 8039262: Java compiler performance degradation jdk1.7 vs. jdk1.6 should be amended
jlahoda
parents: 30845
diff changeset
  3064
        public CompoundScope visitClassType(ClassType t, Void _unused) {
673532e90337 8039262: Java compiler performance degradation jdk1.7 vs. jdk1.6 should be amended
jlahoda
parents: 30845
diff changeset
  3065
            if (!seenTypes.add(t.tsym)) {
10453
41fe3e16698a 7079713: javac hangs when compiling a class that references a cyclically inherited class
mcimadamore
parents: 10452
diff changeset
  3066
                //this is possible when an interface is implemented in multiple
31005
673532e90337 8039262: Java compiler performance degradation jdk1.7 vs. jdk1.6 should be amended
jlahoda
parents: 30845
diff changeset
  3067
                //superclasses, or when a class hierarchy is circular - in such
10453
41fe3e16698a 7079713: javac hangs when compiling a class that references a cyclically inherited class
mcimadamore
parents: 10452
diff changeset
  3068
                //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
  3069
                return new CompoundScope(t.tsym);
41fe3e16698a 7079713: javac hangs when compiling a class that references a cyclically inherited class
mcimadamore
parents: 10452
diff changeset
  3070
            }
41fe3e16698a 7079713: javac hangs when compiling a class that references a cyclically inherited class
mcimadamore
parents: 10452
diff changeset
  3071
            try {
31005
673532e90337 8039262: Java compiler performance degradation jdk1.7 vs. jdk1.6 should be amended
jlahoda
parents: 30845
diff changeset
  3072
                seenTypes.add(t.tsym);
10453
41fe3e16698a 7079713: javac hangs when compiling a class that references a cyclically inherited class
mcimadamore
parents: 10452
diff changeset
  3073
                ClassSymbol csym = (ClassSymbol)t.tsym;
31005
673532e90337 8039262: Java compiler performance degradation jdk1.7 vs. jdk1.6 should be amended
jlahoda
parents: 30845
diff changeset
  3074
                CompoundScope membersClosure = _map.get(csym);
673532e90337 8039262: Java compiler performance degradation jdk1.7 vs. jdk1.6 should be amended
jlahoda
parents: 30845
diff changeset
  3075
                if (membersClosure == null) {
673532e90337 8039262: Java compiler performance degradation jdk1.7 vs. jdk1.6 should be amended
jlahoda
parents: 30845
diff changeset
  3076
                    membersClosure = new CompoundScope(csym);
673532e90337 8039262: Java compiler performance degradation jdk1.7 vs. jdk1.6 should be amended
jlahoda
parents: 30845
diff changeset
  3077
                    for (Type i : interfaces(t)) {
673532e90337 8039262: Java compiler performance degradation jdk1.7 vs. jdk1.6 should be amended
jlahoda
parents: 30845
diff changeset
  3078
                        membersClosure.prependSubScope(visit(i, null));
9812
f716e42cb230 7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents: 9721
diff changeset
  3079
                    }
31005
673532e90337 8039262: Java compiler performance degradation jdk1.7 vs. jdk1.6 should be amended
jlahoda
parents: 30845
diff changeset
  3080
                    membersClosure.prependSubScope(visit(supertype(t), null));
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25441
diff changeset
  3081
                    membersClosure.prependSubScope(csym.members());
31005
673532e90337 8039262: Java compiler performance degradation jdk1.7 vs. jdk1.6 should be amended
jlahoda
parents: 30845
diff changeset
  3082
                    _map.put(csym, membersClosure);
8242
3873b4aaf4a8 7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents: 8230
diff changeset
  3083
                }
31005
673532e90337 8039262: Java compiler performance degradation jdk1.7 vs. jdk1.6 should be amended
jlahoda
parents: 30845
diff changeset
  3084
                return membersClosure;
8242
3873b4aaf4a8 7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents: 8230
diff changeset
  3085
            }
10453
41fe3e16698a 7079713: javac hangs when compiling a class that references a cyclically inherited class
mcimadamore
parents: 10452
diff changeset
  3086
            finally {
31005
673532e90337 8039262: Java compiler performance degradation jdk1.7 vs. jdk1.6 should be amended
jlahoda
parents: 30845
diff changeset
  3087
                seenTypes.remove(t.tsym);
10453
41fe3e16698a 7079713: javac hangs when compiling a class that references a cyclically inherited class
mcimadamore
parents: 10452
diff changeset
  3088
            }
8242
3873b4aaf4a8 7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents: 8230
diff changeset
  3089
        }
3873b4aaf4a8 7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents: 8230
diff changeset
  3090
3873b4aaf4a8 7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents: 8230
diff changeset
  3091
        @Override
31005
673532e90337 8039262: Java compiler performance degradation jdk1.7 vs. jdk1.6 should be amended
jlahoda
parents: 30845
diff changeset
  3092
        public CompoundScope visitTypeVar(TypeVar t, Void _unused) {
673532e90337 8039262: Java compiler performance degradation jdk1.7 vs. jdk1.6 should be amended
jlahoda
parents: 30845
diff changeset
  3093
            return visit(t.getUpperBound(), null);
8242
3873b4aaf4a8 7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents: 8230
diff changeset
  3094
        }
9812
f716e42cb230 7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents: 9721
diff changeset
  3095
    }
f716e42cb230 7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents: 9721
diff changeset
  3096
f716e42cb230 7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents: 9721
diff changeset
  3097
    private MembersClosureCache membersCache = new MembersClosureCache();
f716e42cb230 7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents: 9721
diff changeset
  3098
f716e42cb230 7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents: 9721
diff changeset
  3099
    public CompoundScope membersClosure(Type site, boolean skipInterface) {
31005
673532e90337 8039262: Java compiler performance degradation jdk1.7 vs. jdk1.6 should be amended
jlahoda
parents: 30845
diff changeset
  3100
        CompoundScope cs = membersCache.visit(site, null);
673532e90337 8039262: Java compiler performance degradation jdk1.7 vs. jdk1.6 should be amended
jlahoda
parents: 30845
diff changeset
  3101
        Assert.checkNonNull(cs, () -> "type " + site);
673532e90337 8039262: Java compiler performance degradation jdk1.7 vs. jdk1.6 should be amended
jlahoda
parents: 30845
diff changeset
  3102
        return skipInterface ? membersCache.new MembersScope(cs) : cs;
9812
f716e42cb230 7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents: 9721
diff changeset
  3103
    }
6591
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  3104
    // </editor-fold>
a953c8c6b85e 6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents: 6583
diff changeset
  3105
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3106
28459
48a68a485760 8068517: Compiler may generate wrong InnerClasses attribute for static enum reference
jlahoda
parents: 28451
diff changeset
  3107
    /** Return first abstract member of class `sym'.
48a68a485760 8068517: Compiler may generate wrong InnerClasses attribute for static enum reference
jlahoda
parents: 28451
diff changeset
  3108
     */
48a68a485760 8068517: Compiler may generate wrong InnerClasses attribute for static enum reference
jlahoda
parents: 28451
diff changeset
  3109
    public MethodSymbol firstUnimplementedAbstract(ClassSymbol sym) {
48a68a485760 8068517: Compiler may generate wrong InnerClasses attribute for static enum reference
jlahoda
parents: 28451
diff changeset
  3110
        try {
48a68a485760 8068517: Compiler may generate wrong InnerClasses attribute for static enum reference
jlahoda
parents: 28451
diff changeset
  3111
            return firstUnimplementedAbstractImpl(sym, sym);
48a68a485760 8068517: Compiler may generate wrong InnerClasses attribute for static enum reference
jlahoda
parents: 28451
diff changeset
  3112
        } catch (CompletionFailure ex) {
48a68a485760 8068517: Compiler may generate wrong InnerClasses attribute for static enum reference
jlahoda
parents: 28451
diff changeset
  3113
            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
  3114
            return null;
48a68a485760 8068517: Compiler may generate wrong InnerClasses attribute for static enum reference
jlahoda
parents: 28451
diff changeset
  3115
        }
48a68a485760 8068517: Compiler may generate wrong InnerClasses attribute for static enum reference
jlahoda
parents: 28451
diff changeset
  3116
    }
48a68a485760 8068517: Compiler may generate wrong InnerClasses attribute for static enum reference
jlahoda
parents: 28451
diff changeset
  3117
        //where:
48a68a485760 8068517: Compiler may generate wrong InnerClasses attribute for static enum reference
jlahoda
parents: 28451
diff changeset
  3118
        private MethodSymbol firstUnimplementedAbstractImpl(ClassSymbol impl, ClassSymbol c) {
48a68a485760 8068517: Compiler may generate wrong InnerClasses attribute for static enum reference
jlahoda
parents: 28451
diff changeset
  3119
            MethodSymbol undef = null;
48a68a485760 8068517: Compiler may generate wrong InnerClasses attribute for static enum reference
jlahoda
parents: 28451
diff changeset
  3120
            // 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
  3121
            // since they cannot have abstract members.
48a68a485760 8068517: Compiler may generate wrong InnerClasses attribute for static enum reference
jlahoda
parents: 28451
diff changeset
  3122
            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
  3123
                Scope s = c.members();
48a68a485760 8068517: Compiler may generate wrong InnerClasses attribute for static enum reference
jlahoda
parents: 28451
diff changeset
  3124
                for (Symbol sym : s.getSymbols(NON_RECURSIVE)) {
48a68a485760 8068517: Compiler may generate wrong InnerClasses attribute for static enum reference
jlahoda
parents: 28451
diff changeset
  3125
                    if (sym.kind == MTH &&
33709
4929e3da9137 8142467: Remove all references Flags.IPROXY
sadayapalam
parents: 33556
diff changeset
  3126
                        (sym.flags() & (ABSTRACT|DEFAULT|PRIVATE)) == ABSTRACT) {
28459
48a68a485760 8068517: Compiler may generate wrong InnerClasses attribute for static enum reference
jlahoda
parents: 28451
diff changeset
  3127
                        MethodSymbol absmeth = (MethodSymbol)sym;
48a68a485760 8068517: Compiler may generate wrong InnerClasses attribute for static enum reference
jlahoda
parents: 28451
diff changeset
  3128
                        MethodSymbol implmeth = absmeth.implementation(impl, this, true);
48a68a485760 8068517: Compiler may generate wrong InnerClasses attribute for static enum reference
jlahoda
parents: 28451
diff changeset
  3129
                        if (implmeth == null || implmeth == absmeth) {
48a68a485760 8068517: Compiler may generate wrong InnerClasses attribute for static enum reference
jlahoda
parents: 28451
diff changeset
  3130
                            //look for default implementations
48a68a485760 8068517: Compiler may generate wrong InnerClasses attribute for static enum reference
jlahoda
parents: 28451
diff changeset
  3131
                            if (allowDefaultMethods) {
48a68a485760 8068517: Compiler may generate wrong InnerClasses attribute for static enum reference
jlahoda
parents: 28451
diff changeset
  3132
                                MethodSymbol prov = interfaceCandidates(impl.type, absmeth).head;
48a68a485760 8068517: Compiler may generate wrong InnerClasses attribute for static enum reference
jlahoda
parents: 28451
diff changeset
  3133
                                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
  3134
                                    implmeth = prov;
48a68a485760 8068517: Compiler may generate wrong InnerClasses attribute for static enum reference
jlahoda
parents: 28451
diff changeset
  3135
                                }
48a68a485760 8068517: Compiler may generate wrong InnerClasses attribute for static enum reference
jlahoda
parents: 28451
diff changeset
  3136
                            }
48a68a485760 8068517: Compiler may generate wrong InnerClasses attribute for static enum reference
jlahoda
parents: 28451
diff changeset
  3137
                        }
48a68a485760 8068517: Compiler may generate wrong InnerClasses attribute for static enum reference
jlahoda
parents: 28451
diff changeset
  3138
                        if (implmeth == null || implmeth == absmeth) {
48a68a485760 8068517: Compiler may generate wrong InnerClasses attribute for static enum reference
jlahoda
parents: 28451
diff changeset
  3139
                            undef = absmeth;
48a68a485760 8068517: Compiler may generate wrong InnerClasses attribute for static enum reference
jlahoda
parents: 28451
diff changeset
  3140
                            break;
48a68a485760 8068517: Compiler may generate wrong InnerClasses attribute for static enum reference
jlahoda
parents: 28451
diff changeset
  3141
                        }
48a68a485760 8068517: Compiler may generate wrong InnerClasses attribute for static enum reference
jlahoda
parents: 28451
diff changeset
  3142
                    }
48a68a485760 8068517: Compiler may generate wrong InnerClasses attribute for static enum reference
jlahoda
parents: 28451
diff changeset
  3143
                }
48a68a485760 8068517: Compiler may generate wrong InnerClasses attribute for static enum reference
jlahoda
parents: 28451
diff changeset
  3144
                if (undef == null) {
48a68a485760 8068517: Compiler may generate wrong InnerClasses attribute for static enum reference
jlahoda
parents: 28451
diff changeset
  3145
                    Type st = supertype(c.type);
48a68a485760 8068517: Compiler may generate wrong InnerClasses attribute for static enum reference
jlahoda
parents: 28451
diff changeset
  3146
                    if (st.hasTag(CLASS))
48a68a485760 8068517: Compiler may generate wrong InnerClasses attribute for static enum reference
jlahoda
parents: 28451
diff changeset
  3147
                        undef = firstUnimplementedAbstractImpl(impl, (ClassSymbol)st.tsym);
48a68a485760 8068517: Compiler may generate wrong InnerClasses attribute for static enum reference
jlahoda
parents: 28451
diff changeset
  3148
                }
48a68a485760 8068517: Compiler may generate wrong InnerClasses attribute for static enum reference
jlahoda
parents: 28451
diff changeset
  3149
                for (List<Type> l = interfaces(c.type);
48a68a485760 8068517: Compiler may generate wrong InnerClasses attribute for static enum reference
jlahoda
parents: 28451
diff changeset
  3150
                     undef == null && l.nonEmpty();
48a68a485760 8068517: Compiler may generate wrong InnerClasses attribute for static enum reference
jlahoda
parents: 28451
diff changeset
  3151
                     l = l.tail) {
48a68a485760 8068517: Compiler may generate wrong InnerClasses attribute for static enum reference
jlahoda
parents: 28451
diff changeset
  3152
                    undef = firstUnimplementedAbstractImpl(impl, (ClassSymbol)l.head.tsym);
48a68a485760 8068517: Compiler may generate wrong InnerClasses attribute for static enum reference
jlahoda
parents: 28451
diff changeset
  3153
                }
48a68a485760 8068517: Compiler may generate wrong InnerClasses attribute for static enum reference
jlahoda
parents: 28451
diff changeset
  3154
            }
48a68a485760 8068517: Compiler may generate wrong InnerClasses attribute for static enum reference
jlahoda
parents: 28451
diff changeset
  3155
            return undef;
48a68a485760 8068517: Compiler may generate wrong InnerClasses attribute for static enum reference
jlahoda
parents: 28451
diff changeset
  3156
        }
48a68a485760 8068517: Compiler may generate wrong InnerClasses attribute for static enum reference
jlahoda
parents: 28451
diff changeset
  3157
41862
471a0cb1b986 8167431: javac takes too long time to resolve interface dependency
vromero
parents: 41856
diff changeset
  3158
    public class CandidatesCache {
471a0cb1b986 8167431: javac takes too long time to resolve interface dependency
vromero
parents: 41856
diff changeset
  3159
        public Map<Entry, List<MethodSymbol>> cache = new WeakHashMap<>();
471a0cb1b986 8167431: javac takes too long time to resolve interface dependency
vromero
parents: 41856
diff changeset
  3160
471a0cb1b986 8167431: javac takes too long time to resolve interface dependency
vromero
parents: 41856
diff changeset
  3161
        class Entry {
471a0cb1b986 8167431: javac takes too long time to resolve interface dependency
vromero
parents: 41856
diff changeset
  3162
            Type site;
471a0cb1b986 8167431: javac takes too long time to resolve interface dependency
vromero
parents: 41856
diff changeset
  3163
            MethodSymbol msym;
471a0cb1b986 8167431: javac takes too long time to resolve interface dependency
vromero
parents: 41856
diff changeset
  3164
471a0cb1b986 8167431: javac takes too long time to resolve interface dependency
vromero
parents: 41856
diff changeset
  3165
            Entry(Type site, MethodSymbol msym) {
471a0cb1b986 8167431: javac takes too long time to resolve interface dependency
vromero
parents: 41856
diff changeset
  3166
                this.site = site;
471a0cb1b986 8167431: javac takes too long time to resolve interface dependency
vromero
parents: 41856
diff changeset
  3167
                this.msym = msym;
471a0cb1b986 8167431: javac takes too long time to resolve interface dependency
vromero
parents: 41856
diff changeset
  3168
            }
471a0cb1b986 8167431: javac takes too long time to resolve interface dependency
vromero
parents: 41856
diff changeset
  3169
471a0cb1b986 8167431: javac takes too long time to resolve interface dependency
vromero
parents: 41856
diff changeset
  3170
            @Override
471a0cb1b986 8167431: javac takes too long time to resolve interface dependency
vromero
parents: 41856
diff changeset
  3171
            public boolean equals(Object obj) {
471a0cb1b986 8167431: javac takes too long time to resolve interface dependency
vromero
parents: 41856
diff changeset
  3172
                if (obj instanceof Entry) {
471a0cb1b986 8167431: javac takes too long time to resolve interface dependency
vromero
parents: 41856
diff changeset
  3173
                    Entry e = (Entry)obj;
471a0cb1b986 8167431: javac takes too long time to resolve interface dependency
vromero
parents: 41856
diff changeset
  3174
                    return e.msym == msym && isSameType(site, e.site);
471a0cb1b986 8167431: javac takes too long time to resolve interface dependency
vromero
parents: 41856
diff changeset
  3175
                } else {
471a0cb1b986 8167431: javac takes too long time to resolve interface dependency
vromero
parents: 41856
diff changeset
  3176
                    return false;
471a0cb1b986 8167431: javac takes too long time to resolve interface dependency
vromero
parents: 41856
diff changeset
  3177
                }
471a0cb1b986 8167431: javac takes too long time to resolve interface dependency
vromero
parents: 41856
diff changeset
  3178
            }
471a0cb1b986 8167431: javac takes too long time to resolve interface dependency
vromero
parents: 41856
diff changeset
  3179
471a0cb1b986 8167431: javac takes too long time to resolve interface dependency
vromero
parents: 41856
diff changeset
  3180
            @Override
471a0cb1b986 8167431: javac takes too long time to resolve interface dependency
vromero
parents: 41856
diff changeset
  3181
            public int hashCode() {
471a0cb1b986 8167431: javac takes too long time to resolve interface dependency
vromero
parents: 41856
diff changeset
  3182
                return Types.this.hashCode(site) & ~msym.hashCode();
471a0cb1b986 8167431: javac takes too long time to resolve interface dependency
vromero
parents: 41856
diff changeset
  3183
            }
471a0cb1b986 8167431: javac takes too long time to resolve interface dependency
vromero
parents: 41856
diff changeset
  3184
        }
471a0cb1b986 8167431: javac takes too long time to resolve interface dependency
vromero
parents: 41856
diff changeset
  3185
471a0cb1b986 8167431: javac takes too long time to resolve interface dependency
vromero
parents: 41856
diff changeset
  3186
        public List<MethodSymbol> get(Entry e) {
471a0cb1b986 8167431: javac takes too long time to resolve interface dependency
vromero
parents: 41856
diff changeset
  3187
            return cache.get(e);
471a0cb1b986 8167431: javac takes too long time to resolve interface dependency
vromero
parents: 41856
diff changeset
  3188
        }
471a0cb1b986 8167431: javac takes too long time to resolve interface dependency
vromero
parents: 41856
diff changeset
  3189
471a0cb1b986 8167431: javac takes too long time to resolve interface dependency
vromero
parents: 41856
diff changeset
  3190
        public void put(Entry e, List<MethodSymbol> msymbols) {
471a0cb1b986 8167431: javac takes too long time to resolve interface dependency
vromero
parents: 41856
diff changeset
  3191
            cache.put(e, msymbols);
471a0cb1b986 8167431: javac takes too long time to resolve interface dependency
vromero
parents: 41856
diff changeset
  3192
        }
471a0cb1b986 8167431: javac takes too long time to resolve interface dependency
vromero
parents: 41856
diff changeset
  3193
    }
471a0cb1b986 8167431: javac takes too long time to resolve interface dependency
vromero
parents: 41856
diff changeset
  3194
471a0cb1b986 8167431: javac takes too long time to resolve interface dependency
vromero
parents: 41856
diff changeset
  3195
    public CandidatesCache candidatesCache = new CandidatesCache();
28459
48a68a485760 8068517: Compiler may generate wrong InnerClasses attribute for static enum reference
jlahoda
parents: 28451
diff changeset
  3196
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3197
    //where
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3198
    public List<MethodSymbol> interfaceCandidates(Type site, MethodSymbol ms) {
41862
471a0cb1b986 8167431: javac takes too long time to resolve interface dependency
vromero
parents: 41856
diff changeset
  3199
        CandidatesCache.Entry e = candidatesCache.new Entry(site, ms);
471a0cb1b986 8167431: javac takes too long time to resolve interface dependency
vromero
parents: 41856
diff changeset
  3200
        List<MethodSymbol> candidates = candidatesCache.get(e);
471a0cb1b986 8167431: javac takes too long time to resolve interface dependency
vromero
parents: 41856
diff changeset
  3201
        if (candidates == null) {
471a0cb1b986 8167431: javac takes too long time to resolve interface dependency
vromero
parents: 41856
diff changeset
  3202
            Filter<Symbol> filter = new MethodFilter(ms, site);
471a0cb1b986 8167431: javac takes too long time to resolve interface dependency
vromero
parents: 41856
diff changeset
  3203
            List<MethodSymbol> candidates2 = List.nil();
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25441
diff changeset
  3204
            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
  3205
                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
  3206
                    return List.of((MethodSymbol)s);
41862
471a0cb1b986 8167431: javac takes too long time to resolve interface dependency
vromero
parents: 41856
diff changeset
  3207
                } else if (!candidates2.contains(s)) {
471a0cb1b986 8167431: javac takes too long time to resolve interface dependency
vromero
parents: 41856
diff changeset
  3208
                    candidates2 = candidates2.prepend((MethodSymbol)s);
16809
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16567
diff changeset
  3209
                }
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3210
            }
41862
471a0cb1b986 8167431: javac takes too long time to resolve interface dependency
vromero
parents: 41856
diff changeset
  3211
            candidates = prune(candidates2);
471a0cb1b986 8167431: javac takes too long time to resolve interface dependency
vromero
parents: 41856
diff changeset
  3212
            candidatesCache.put(e, candidates);
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3213
        }
41862
471a0cb1b986 8167431: javac takes too long time to resolve interface dependency
vromero
parents: 41856
diff changeset
  3214
        return candidates;
471a0cb1b986 8167431: javac takes too long time to resolve interface dependency
vromero
parents: 41856
diff changeset
  3215
    }
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3216
16295
8b76e466f3cb 8007535: Compiler crashes on @FunctionalInterface used on interface with two inherited methods with same signatures
mcimadamore
parents: 16292
diff changeset
  3217
    public List<MethodSymbol> prune(List<MethodSymbol> methods) {
20249
93f8eae31092 6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents: 19914
diff changeset
  3218
        ListBuffer<MethodSymbol> methodsMin = new ListBuffer<>();
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3219
        for (MethodSymbol m1 : methods) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3220
            boolean isMin_m1 = true;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3221
            for (MethodSymbol m2 : methods) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3222
                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
  3223
                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
  3224
                        asSuper(m2.owner.type, m1.owner) != null) {
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3225
                    isMin_m1 = false;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3226
                    break;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3227
                }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3228
            }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3229
            if (isMin_m1)
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3230
                methodsMin.append(m1);
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3231
        }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3232
        return methodsMin.toList();
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3233
    }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3234
    // where
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3235
            private class MethodFilter implements Filter<Symbol> {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3236
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3237
                Symbol msym;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3238
                Type site;
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14443
diff changeset
  3239
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14443
diff changeset
  3240
                MethodFilter(Symbol msym, Type site) {
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3241
                    this.msym = msym;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3242
                    this.site = site;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3243
                }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3244
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3245
                public boolean accepts(Symbol s) {
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 27125
diff changeset
  3246
                    return s.kind == MTH &&
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3247
                            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
  3248
                            (s.flags() & SYNTHETIC) == 0 &&
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3249
                            s.isInheritedIn(site.tsym, Types.this) &&
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3250
                            overrideEquivalent(memberType(site, s), memberType(site, msym));
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3251
                }
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21712
diff changeset
  3252
            }
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3253
    // </editor-fold>
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3254
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3255
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  3256
     * Does t have the same arguments as s?  It is assumed that both
06bc494ca11e Initial load
duke
parents:
diff changeset
  3257
     * types are (possibly polymorphic) method types.  Monomorphic
06bc494ca11e Initial load
duke
parents:
diff changeset
  3258
     * method types "have the same arguments", if their argument lists
06bc494ca11e Initial load
duke
parents:
diff changeset
  3259
     * are equal.  Polymorphic method types "have the same arguments",
06bc494ca11e Initial load
duke
parents:
diff changeset
  3260
     * if they have the same arguments after renaming all type
06bc494ca11e Initial load
duke
parents:
diff changeset
  3261
     * variables of one to corresponding type variables in the other,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3262
     * where correspondence is by position in the type parameter list.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3263
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3264
    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
  3265
        return hasSameArgs(t, s, true);
9e31f1c01bd4 7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents: 8625
diff changeset
  3266
    }
9e31f1c01bd4 7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents: 8625
diff changeset
  3267
9e31f1c01bd4 7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents: 8625
diff changeset
  3268
    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
  3269
        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
  3270
    }
9e31f1c01bd4 7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents: 8625
diff changeset
  3271
9e31f1c01bd4 7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents: 8625
diff changeset
  3272
    private boolean hasSameArgs(Type t, Type s, TypeRelation hasSameArgs) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3273
        return hasSameArgs.visit(t, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3274
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3275
    // where
8628
9e31f1c01bd4 7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents: 8625
diff changeset
  3276
        private class HasSameArgs extends TypeRelation {
9e31f1c01bd4 7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents: 8625
diff changeset
  3277
9e31f1c01bd4 7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents: 8625
diff changeset
  3278
            boolean strict;
9e31f1c01bd4 7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents: 8625
diff changeset
  3279
9e31f1c01bd4 7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents: 8625
diff changeset
  3280
            public HasSameArgs(boolean strict) {
9e31f1c01bd4 7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents: 8625
diff changeset
  3281
                this.strict = strict;
9e31f1c01bd4 7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents: 8625
diff changeset
  3282
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3283
06bc494ca11e Initial load
duke
parents:
diff changeset
  3284
            public Boolean visitType(Type t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3285
                throw new AssertionError();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3286
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3287
06bc494ca11e Initial load
duke
parents:
diff changeset
  3288
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  3289
            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
  3290
                return s.hasTag(METHOD)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3291
                    && containsTypeEquivalent(t.argtypes, s.getParameterTypes());
06bc494ca11e Initial load
duke
parents:
diff changeset
  3292
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3293
06bc494ca11e Initial load
duke
parents:
diff changeset
  3294
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  3295
            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
  3296
                if (!s.hasTag(FORALL))
8628
9e31f1c01bd4 7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents: 8625
diff changeset
  3297
                    return strict ? false : visitMethodType(t.asMethodType(), s);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3298
06bc494ca11e Initial load
duke
parents:
diff changeset
  3299
                ForAll forAll = (ForAll)s;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3300
                return hasSameBounds(t, forAll)
06bc494ca11e Initial load
duke
parents:
diff changeset
  3301
                    && visit(t.qtype, subst(forAll.qtype, forAll.tvars, t.tvars));
06bc494ca11e Initial load
duke
parents:
diff changeset
  3302
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3303
06bc494ca11e Initial load
duke
parents:
diff changeset
  3304
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  3305
            public Boolean visitErrorType(ErrorType t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3306
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3307
            }
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21712
diff changeset
  3308
        }
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21712
diff changeset
  3309
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21712
diff changeset
  3310
    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
  3311
        TypeRelation hasSameArgs_nonstrict = new HasSameArgs(false);
9e31f1c01bd4 7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents: 8625
diff changeset
  3312
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3313
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  3314
06bc494ca11e Initial load
duke
parents:
diff changeset
  3315
    // <editor-fold defaultstate="collapsed" desc="subst">
06bc494ca11e Initial load
duke
parents:
diff changeset
  3316
    public List<Type> subst(List<Type> ts,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3317
                            List<Type> from,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3318
                            List<Type> to) {
29842
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29555
diff changeset
  3319
        return ts.map(new Subst(from, to));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3320
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3321
06bc494ca11e Initial load
duke
parents:
diff changeset
  3322
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  3323
     * Substitute all occurrences of a type in `from' with the
06bc494ca11e Initial load
duke
parents:
diff changeset
  3324
     * corresponding type in `to' in 't'. Match lists `from' and `to'
06bc494ca11e Initial load
duke
parents:
diff changeset
  3325
     * from the right: If lists have different length, discard leading
06bc494ca11e Initial load
duke
parents:
diff changeset
  3326
     * elements of the longer list.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3327
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3328
    public Type subst(Type t, List<Type> from, List<Type> to) {
29842
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29555
diff changeset
  3329
        return t.map(new Subst(from, to));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3330
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3331
44186
fe848a208b20 8176265: Method overload resolution on a covariant base type doesn't work in 9
mcimadamore
parents: 43025
diff changeset
  3332
    private class Subst extends StructuralTypeMapping<Void> {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3333
        List<Type> from;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3334
        List<Type> to;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3335
06bc494ca11e Initial load
duke
parents:
diff changeset
  3336
        public Subst(List<Type> from, List<Type> to) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3337
            int fromLength = from.length();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3338
            int toLength = to.length();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3339
            while (fromLength > toLength) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3340
                fromLength--;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3341
                from = from.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3342
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3343
            while (fromLength < toLength) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3344
                toLength--;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3345
                to = to.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3346
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3347
            this.from = from;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3348
            this.to = to;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3349
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3350
06bc494ca11e Initial load
duke
parents:
diff changeset
  3351
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  3352
        public Type visitTypeVar(TypeVar t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3353
            for (List<Type> from = this.from, to = this.to;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3354
                 from.nonEmpty();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3355
                 from = from.tail, to = to.tail) {
29842
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29555
diff changeset
  3356
                if (t.equalsIgnoreMetadata(from.head)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3357
                    return to.head.withTypeVar(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3358
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3359
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3360
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3361
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3362
06bc494ca11e Initial load
duke
parents:
diff changeset
  3363
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  3364
        public Type visitClassType(ClassType t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3365
            if (!t.isCompound()) {
29842
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29555
diff changeset
  3366
                return super.visitClassType(t, ignored);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3367
            } else {
29842
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29555
diff changeset
  3368
                Type st = visit(supertype(t));
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29555
diff changeset
  3369
                List<Type> is = visit(interfaces(t), ignored);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3370
                if (st == supertype(t) && is == interfaces(t))
06bc494ca11e Initial load
duke
parents:
diff changeset
  3371
                    return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3372
                else
29147
4cba0458106b 8071291: Compiler crashes trying to cast UnionType to IntersectionClassType
mcimadamore
parents: 29146
diff changeset
  3373
                    return makeIntersectionType(is.prepend(st));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3374
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3375
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3376
06bc494ca11e Initial load
duke
parents:
diff changeset
  3377
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  3378
        public Type visitWildcardType(WildcardType t, Void ignored) {
29842
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29555
diff changeset
  3379
            WildcardType t2 = (WildcardType)super.visitWildcardType(t, ignored);
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29555
diff changeset
  3380
            if (t2 != t && t.isExtendsBound() && t2.type.isExtendsBound()) {
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29555
diff changeset
  3381
                t2.type = wildUpperBound(t2.type);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3382
            }
29842
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29555
diff changeset
  3383
            return t2;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3384
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3385
06bc494ca11e Initial load
duke
parents:
diff changeset
  3386
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  3387
        public Type visitForAll(ForAll t, Void ignored) {
8230
b761a23e78d8 6838943: inference: javac is not handling type-variable substitution properly
mcimadamore
parents: 8032
diff changeset
  3388
            if (Type.containsAny(to, t.tvars)) {
b761a23e78d8 6838943: inference: javac is not handling type-variable substitution properly
mcimadamore
parents: 8032
diff changeset
  3389
                //perform alpha-renaming of free-variables in 't'
b761a23e78d8 6838943: inference: javac is not handling type-variable substitution properly
mcimadamore
parents: 8032
diff changeset
  3390
                //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
  3391
                List<Type> freevars = newInstances(t.tvars);
b761a23e78d8 6838943: inference: javac is not handling type-variable substitution properly
mcimadamore
parents: 8032
diff changeset
  3392
                t = new ForAll(freevars,
24396
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 24293
diff changeset
  3393
                               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
  3394
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3395
            List<Type> tvars1 = substBounds(t.tvars, from, to);
29842
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29555
diff changeset
  3396
            Type qtype1 = visit(t.qtype);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3397
            if (tvars1 == t.tvars && qtype1 == t.qtype) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3398
                return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3399
            } else if (tvars1 == t.tvars) {
29842
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29555
diff changeset
  3400
                return new ForAll(tvars1, qtype1) {
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29555
diff changeset
  3401
                    @Override
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29555
diff changeset
  3402
                    public boolean needsStripping() {
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29555
diff changeset
  3403
                        return true;
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29555
diff changeset
  3404
                    }
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29555
diff changeset
  3405
                };
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3406
            } else {
29842
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29555
diff changeset
  3407
                return new ForAll(tvars1, Types.this.subst(qtype1, t.tvars, tvars1)) {
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29555
diff changeset
  3408
                    @Override
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29555
diff changeset
  3409
                    public boolean needsStripping() {
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29555
diff changeset
  3410
                        return true;
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29555
diff changeset
  3411
                    }
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29555
diff changeset
  3412
                };
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3413
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3414
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3415
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3416
06bc494ca11e Initial load
duke
parents:
diff changeset
  3417
    public List<Type> substBounds(List<Type> tvars,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3418
                                  List<Type> from,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3419
                                  List<Type> to) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3420
        if (tvars.isEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
  3421
            return tvars;
20249
93f8eae31092 6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents: 19914
diff changeset
  3422
        ListBuffer<Type> newBoundsBuf = new ListBuffer<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3423
        boolean changed = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3424
        // calculate new bounds
06bc494ca11e Initial load
duke
parents:
diff changeset
  3425
        for (Type t : tvars) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3426
            TypeVar tv = (TypeVar) t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3427
            Type bound = subst(tv.bound, from, to);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3428
            if (bound != tv.bound)
06bc494ca11e Initial load
duke
parents:
diff changeset
  3429
                changed = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3430
            newBoundsBuf.append(bound);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3431
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3432
        if (!changed)
06bc494ca11e Initial load
duke
parents:
diff changeset
  3433
            return tvars;
20249
93f8eae31092 6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents: 19914
diff changeset
  3434
        ListBuffer<Type> newTvars = new ListBuffer<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3435
        // create new type variables without bounds
06bc494ca11e Initial load
duke
parents:
diff changeset
  3436
        for (Type t : tvars) {
24396
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 24293
diff changeset
  3437
            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
  3438
                                        t.getMetadata()));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3439
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3440
        // the new bounds should use the new type variables in place
06bc494ca11e Initial load
duke
parents:
diff changeset
  3441
        // of the old
06bc494ca11e Initial load
duke
parents:
diff changeset
  3442
        List<Type> newBounds = newBoundsBuf.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3443
        from = tvars;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3444
        to = newTvars.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3445
        for (; !newBounds.isEmpty(); newBounds = newBounds.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3446
            newBounds.head = subst(newBounds.head, from, to);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3447
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3448
        newBounds = newBoundsBuf.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3449
        // set the bounds of new type variables to the new bounds
06bc494ca11e Initial load
duke
parents:
diff changeset
  3450
        for (Type t : newTvars.toList()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3451
            TypeVar tv = (TypeVar) t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3452
            tv.bound = newBounds.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3453
            newBounds = newBounds.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3454
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3455
        return newTvars.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3456
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3457
06bc494ca11e Initial load
duke
parents:
diff changeset
  3458
    public TypeVar substBound(TypeVar t, List<Type> from, List<Type> to) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3459
        Type bound1 = subst(t.bound, from, to);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3460
        if (bound1 == t.bound)
06bc494ca11e Initial load
duke
parents:
diff changeset
  3461
            return t;
1993
9b37ef07ba64 6729401: Compiler error when using F-bounded generics with free type variables
mcimadamore
parents: 1992
diff changeset
  3462
        else {
9b37ef07ba64 6729401: Compiler error when using F-bounded generics with free type variables
mcimadamore
parents: 1992
diff changeset
  3463
            // create new type variable without bounds
24396
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 24293
diff changeset
  3464
            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
  3465
                                     t.getMetadata());
1993
9b37ef07ba64 6729401: Compiler error when using F-bounded generics with free type variables
mcimadamore
parents: 1992
diff changeset
  3466
            // 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
  3467
            // of the old
42828
cce89649f958 8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents: 42819
diff changeset
  3468
            tv.bound = subst(bound1, List.of(t), List.of(tv));
1993
9b37ef07ba64 6729401: Compiler error when using F-bounded generics with free type variables
mcimadamore
parents: 1992
diff changeset
  3469
            return tv;
9b37ef07ba64 6729401: Compiler error when using F-bounded generics with free type variables
mcimadamore
parents: 1992
diff changeset
  3470
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3471
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3472
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  3473
06bc494ca11e Initial load
duke
parents:
diff changeset
  3474
    // <editor-fold defaultstate="collapsed" desc="hasSameBounds">
06bc494ca11e Initial load
duke
parents:
diff changeset
  3475
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  3476
     * Does t have the same bounds for quantified variables as s?
06bc494ca11e Initial load
duke
parents:
diff changeset
  3477
     */
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19655
diff changeset
  3478
    public boolean hasSameBounds(ForAll t, ForAll s) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3479
        List<Type> l1 = t.tvars;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3480
        List<Type> l2 = s.tvars;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3481
        while (l1.nonEmpty() && l2.nonEmpty() &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  3482
               isSameType(l1.head.getUpperBound(),
06bc494ca11e Initial load
duke
parents:
diff changeset
  3483
                          subst(l2.head.getUpperBound(),
06bc494ca11e Initial load
duke
parents:
diff changeset
  3484
                                s.tvars,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3485
                                t.tvars))) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3486
            l1 = l1.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3487
            l2 = l2.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3488
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3489
        return l1.isEmpty() && l2.isEmpty();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3490
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3491
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  3492
06bc494ca11e Initial load
duke
parents:
diff changeset
  3493
    // <editor-fold defaultstate="collapsed" desc="newInstances">
06bc494ca11e Initial load
duke
parents:
diff changeset
  3494
    /** Create new vector of type variables from list of variables
06bc494ca11e Initial load
duke
parents:
diff changeset
  3495
     *  changing all recursive bounds from old to new list.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3496
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3497
    public List<Type> newInstances(List<Type> tvars) {
29554
6d7957bd6866 8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents: 29292
diff changeset
  3498
        List<Type> tvars1 = tvars.map(newInstanceFun);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3499
        for (List<Type> l = tvars1; l.nonEmpty(); l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3500
            TypeVar tv = (TypeVar) l.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3501
            tv.bound = subst(tv.bound, tvars, tvars1);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3502
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3503
        return tvars1;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3504
    }
29554
6d7957bd6866 8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents: 29292
diff changeset
  3505
        private static final TypeMapping<Void> newInstanceFun = new TypeMapping<Void>() {
6d7957bd6866 8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents: 29292
diff changeset
  3506
            @Override
6d7957bd6866 8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents: 29292
diff changeset
  3507
            public TypeVar visitTypeVar(TypeVar t, Void _unused) {
6d7957bd6866 8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents: 29292
diff changeset
  3508
                return new TypeVar(t.tsym, t.getUpperBound(), t.getLowerBound(), t.getMetadata());
6d7957bd6866 8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents: 29292
diff changeset
  3509
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3510
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
  3511
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  3512
8430
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  3513
    public Type createMethodTypeWithParameters(Type original, List<Type> newParams) {
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  3514
        return original.accept(methodWithParameters, newParams);
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  3515
    }
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  3516
    // where
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  3517
        private final MapVisitor<List<Type>> methodWithParameters = new MapVisitor<List<Type>>() {
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  3518
            public Type visitType(Type t, List<Type> newParams) {
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  3519
                throw new IllegalArgumentException("Not a method type: " + t);
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  3520
            }
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  3521
            public Type visitMethodType(MethodType t, List<Type> newParams) {
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  3522
                return new MethodType(newParams, t.restype, t.thrown, t.tsym);
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  3523
            }
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  3524
            public Type visitForAll(ForAll t, List<Type> newParams) {
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  3525
                return new ForAll(t.tvars, t.qtype.accept(this, newParams));
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  3526
            }
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  3527
        };
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  3528
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  3529
    public Type createMethodTypeWithThrown(Type original, List<Type> newThrown) {
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  3530
        return original.accept(methodWithThrown, newThrown);
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  3531
    }
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  3532
    // where
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  3533
        private final MapVisitor<List<Type>> methodWithThrown = new MapVisitor<List<Type>>() {
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  3534
            public Type visitType(Type t, List<Type> newThrown) {
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  3535
                throw new IllegalArgumentException("Not a method type: " + t);
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  3536
            }
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  3537
            public Type visitMethodType(MethodType t, List<Type> newThrown) {
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  3538
                return new MethodType(t.argtypes, t.restype, newThrown, t.tsym);
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  3539
            }
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  3540
            public Type visitForAll(ForAll t, List<Type> newThrown) {
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  3541
                return new ForAll(t.tvars, t.qtype.accept(this, newThrown));
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  3542
            }
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  3543
        };
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8427
diff changeset
  3544
9075
cba34854a40e 7030150: Type inference for generic instance creation failed for formal type parameter
mcimadamore
parents: 8628
diff changeset
  3545
    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
  3546
        return original.accept(methodWithReturn, newReturn);
cba34854a40e 7030150: Type inference for generic instance creation failed for formal type parameter
mcimadamore
parents: 8628
diff changeset
  3547
    }
cba34854a40e 7030150: Type inference for generic instance creation failed for formal type parameter
mcimadamore
parents: 8628
diff changeset
  3548
    // where
cba34854a40e 7030150: Type inference for generic instance creation failed for formal type parameter
mcimadamore
parents: 8628
diff changeset
  3549
        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
  3550
            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
  3551
                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
  3552
            }
cba34854a40e 7030150: Type inference for generic instance creation failed for formal type parameter
mcimadamore
parents: 8628
diff changeset
  3553
            public Type visitMethodType(MethodType t, Type newReturn) {
32709
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31005
diff changeset
  3554
                return new MethodType(t.argtypes, newReturn, t.thrown, t.tsym) {
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31005
diff changeset
  3555
                    @Override
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31005
diff changeset
  3556
                    public Type baseType() {
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31005
diff changeset
  3557
                        return t;
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31005
diff changeset
  3558
                    }
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31005
diff changeset
  3559
                };
9075
cba34854a40e 7030150: Type inference for generic instance creation failed for formal type parameter
mcimadamore
parents: 8628
diff changeset
  3560
            }
cba34854a40e 7030150: Type inference for generic instance creation failed for formal type parameter
mcimadamore
parents: 8628
diff changeset
  3561
            public Type visitForAll(ForAll t, Type newReturn) {
32709
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31005
diff changeset
  3562
                return new ForAll(t.tvars, t.qtype.accept(this, newReturn)) {
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31005
diff changeset
  3563
                    @Override
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31005
diff changeset
  3564
                    public Type baseType() {
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31005
diff changeset
  3565
                        return t;
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31005
diff changeset
  3566
                    }
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31005
diff changeset
  3567
                };
9075
cba34854a40e 7030150: Type inference for generic instance creation failed for formal type parameter
mcimadamore
parents: 8628
diff changeset
  3568
            }
cba34854a40e 7030150: Type inference for generic instance creation failed for formal type parameter
mcimadamore
parents: 8628
diff changeset
  3569
        };
cba34854a40e 7030150: Type inference for generic instance creation failed for formal type parameter
mcimadamore
parents: 8628
diff changeset
  3570
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
  3571
    // <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
  3572
    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
  3573
        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
  3574
    }
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 1206
diff changeset
  3575
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 1206
diff changeset
  3576
    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
  3577
        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
  3578
    }
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 1206
diff changeset
  3579
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 1206
diff changeset
  3580
    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
  3581
        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
  3582
    }
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 1206
diff changeset
  3583
    // </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
  3584
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3585
    // <editor-fold defaultstate="collapsed" desc="rank">
06bc494ca11e Initial load
duke
parents:
diff changeset
  3586
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  3587
     * The rank of a class is the length of the longest path between
06bc494ca11e Initial load
duke
parents:
diff changeset
  3588
     * the class and java.lang.Object in the class inheritance
06bc494ca11e Initial load
duke
parents:
diff changeset
  3589
     * graph. Undefined for all but reference types.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3590
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3591
    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
  3592
        switch(t.getTag()) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3593
        case CLASS: {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3594
            ClassType cls = (ClassType)t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3595
            if (cls.rank_field < 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3596
                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
  3597
                if (fullname == names.java_lang_Object)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3598
                    cls.rank_field = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3599
                else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3600
                    int r = rank(supertype(cls));
06bc494ca11e Initial load
duke
parents:
diff changeset
  3601
                    for (List<Type> l = interfaces(cls);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3602
                         l.nonEmpty();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3603
                         l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3604
                        if (rank(l.head) > r)
06bc494ca11e Initial load
duke
parents:
diff changeset
  3605
                            r = rank(l.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3606
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3607
                    cls.rank_field = r + 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3608
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3609
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3610
            return cls.rank_field;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3611
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3612
        case TYPEVAR: {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3613
            TypeVar tvar = (TypeVar)t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3614
            if (tvar.rank_field < 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3615
                int r = rank(supertype(tvar));
06bc494ca11e Initial load
duke
parents:
diff changeset
  3616
                for (List<Type> l = interfaces(tvar);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3617
                     l.nonEmpty();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3618
                     l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3619
                    if (rank(l.head) > r) r = rank(l.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3620
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3621
                tvar.rank_field = r + 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3622
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3623
            return tvar.rank_field;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3624
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3625
        case ERROR:
23399
a7f649f8e860 8036007: javac crashes when encountering an unresolvable interface
vromero
parents: 22442
diff changeset
  3626
        case NONE:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3627
            return 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3628
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  3629
            throw new AssertionError();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3630
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3631
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3632
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  3633
1357
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  3634
    /**
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents: 2218
diff changeset
  3635
     * 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
  3636
     * accordingly to a given locale
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  3637
     */
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  3638
    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
  3639
        return Printer.createStandardPrinter(messages).visit(t, locale);
1357
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  3640
    }
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  3641
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  3642
    /**
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents: 2218
diff changeset
  3643
     * 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
  3644
     * accordingly to a given locale
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  3645
     */
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  3646
    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
  3647
        return Printer.createStandardPrinter(messages).visit(t, locale);
1357
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  3648
    }
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  3649
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3650
    // <editor-fold defaultstate="collapsed" desc="toString">
06bc494ca11e Initial load
duke
parents:
diff changeset
  3651
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  3652
     * 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
  3653
     *
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  3654
     * @deprecated Types.toString(Type t, Locale l) provides better support
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  3655
     * for localization
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3656
     */
1357
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  3657
    @Deprecated
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3658
    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
  3659
        if (t.hasTag(FORALL)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3660
            ForAll forAll = (ForAll)t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3661
            return typaramsString(forAll.tvars) + forAll.qtype;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3662
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3663
        return "" + t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3664
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3665
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
  3666
        private String typaramsString(List<Type> tvars) {
8625
6b51ef804d49 6639645: Modeling type implementing missing interfaces
jjg
parents: 8617
diff changeset
  3667
            StringBuilder s = new StringBuilder();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3668
            s.append('<');
06bc494ca11e Initial load
duke
parents:
diff changeset
  3669
            boolean first = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3670
            for (Type t : tvars) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3671
                if (!first) s.append(", ");
06bc494ca11e Initial load
duke
parents:
diff changeset
  3672
                first = false;
24396
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 24293
diff changeset
  3673
                appendTyparamString(((TypeVar)t), s);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3674
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3675
            s.append('>');
06bc494ca11e Initial load
duke
parents:
diff changeset
  3676
            return s.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3677
        }
8625
6b51ef804d49 6639645: Modeling type implementing missing interfaces
jjg
parents: 8617
diff changeset
  3678
        private void appendTyparamString(TypeVar t, StringBuilder buf) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3679
            buf.append(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3680
            if (t.bound == null ||
06bc494ca11e Initial load
duke
parents:
diff changeset
  3681
                t.bound.tsym.getQualifiedName() == names.java_lang_Object)
06bc494ca11e Initial load
duke
parents:
diff changeset
  3682
                return;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3683
            buf.append(" extends "); // Java syntax; no need for i18n
06bc494ca11e Initial load
duke
parents:
diff changeset
  3684
            Type bound = t.bound;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3685
            if (!bound.isCompound()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3686
                buf.append(bound);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3687
            } else if ((erasure(t).tsym.flags() & INTERFACE) == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3688
                buf.append(supertype(t));
06bc494ca11e Initial load
duke
parents:
diff changeset
  3689
                for (Type intf : interfaces(t)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3690
                    buf.append('&');
06bc494ca11e Initial load
duke
parents:
diff changeset
  3691
                    buf.append(intf);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3692
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3693
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3694
                // No superclass was given in bounds.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3695
                // In this case, supertype is Object, erasure is first interface.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3696
                boolean first = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3697
                for (Type intf : interfaces(t)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3698
                    if (!first) buf.append('&');
06bc494ca11e Initial load
duke
parents:
diff changeset
  3699
                    first = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3700
                    buf.append(intf);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3701
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3702
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3703
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3704
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  3705
06bc494ca11e Initial load
duke
parents:
diff changeset
  3706
    // <editor-fold defaultstate="collapsed" desc="Determining least upper bounds of types">
06bc494ca11e Initial load
duke
parents:
diff changeset
  3707
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  3708
     * A cache for closures.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3709
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  3710
     * <p>A closure is a list of all the supertypes and interfaces of
06bc494ca11e Initial load
duke
parents:
diff changeset
  3711
     * a class or interface type, ordered by ClassSymbol.precedes
06bc494ca11e Initial load
duke
parents:
diff changeset
  3712
     * (that is, subclasses come first, arbitrary but fixed
06bc494ca11e Initial load
duke
parents:
diff changeset
  3713
     * otherwise).
06bc494ca11e Initial load
duke
parents:
diff changeset
  3714
     */
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21712
diff changeset
  3715
    private Map<Type,List<Type>> closureCache = new HashMap<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3716
06bc494ca11e Initial load
duke
parents:
diff changeset
  3717
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  3718
     * Returns the closure of a class or interface type.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3719
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3720
    public List<Type> closure(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3721
        List<Type> cl = closureCache.get(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3722
        if (cl == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3723
            Type st = supertype(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3724
            if (!t.isCompound()) {
18646
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18412
diff changeset
  3725
                if (st.hasTag(CLASS)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3726
                    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
  3727
                } else if (st.hasTag(TYPEVAR)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3728
                    cl = closure(st).prepend(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3729
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3730
                    cl = List.of(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3731
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3732
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3733
                cl = closure(supertype(t));
06bc494ca11e Initial load
duke
parents:
diff changeset
  3734
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3735
            for (List<Type> l = interfaces(t); l.nonEmpty(); l = l.tail)
06bc494ca11e Initial load
duke
parents:
diff changeset
  3736
                cl = union(cl, closure(l.head));
06bc494ca11e Initial load
duke
parents:
diff changeset
  3737
            closureCache.put(t, cl);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3738
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3739
        return cl;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3740
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3741
06bc494ca11e Initial load
duke
parents:
diff changeset
  3742
    /**
29555
71f15ff4b409 8048838: type inference performance regression
mcimadamore
parents: 29554
diff changeset
  3743
     * Collect types into a new closure (using a @code{ClosureHolder})
71f15ff4b409 8048838: type inference performance regression
mcimadamore
parents: 29554
diff changeset
  3744
     */
71f15ff4b409 8048838: type inference performance regression
mcimadamore
parents: 29554
diff changeset
  3745
    public Collector<Type, ClosureHolder, List<Type>> closureCollector(boolean minClosure, BiPredicate<Type, Type> shouldSkip) {
71f15ff4b409 8048838: type inference performance regression
mcimadamore
parents: 29554
diff changeset
  3746
        return Collector.of(() -> new ClosureHolder(minClosure, shouldSkip),
71f15ff4b409 8048838: type inference performance regression
mcimadamore
parents: 29554
diff changeset
  3747
                ClosureHolder::add,
71f15ff4b409 8048838: type inference performance regression
mcimadamore
parents: 29554
diff changeset
  3748
                ClosureHolder::merge,
71f15ff4b409 8048838: type inference performance regression
mcimadamore
parents: 29554
diff changeset
  3749
                ClosureHolder::closure);
71f15ff4b409 8048838: type inference performance regression
mcimadamore
parents: 29554
diff changeset
  3750
    }
71f15ff4b409 8048838: type inference performance regression
mcimadamore
parents: 29554
diff changeset
  3751
    //where
71f15ff4b409 8048838: type inference performance regression
mcimadamore
parents: 29554
diff changeset
  3752
        class ClosureHolder {
71f15ff4b409 8048838: type inference performance regression
mcimadamore
parents: 29554
diff changeset
  3753
            List<Type> closure;
71f15ff4b409 8048838: type inference performance regression
mcimadamore
parents: 29554
diff changeset
  3754
            final boolean minClosure;
71f15ff4b409 8048838: type inference performance regression
mcimadamore
parents: 29554
diff changeset
  3755
            final BiPredicate<Type, Type> shouldSkip;
71f15ff4b409 8048838: type inference performance regression
mcimadamore
parents: 29554
diff changeset
  3756
71f15ff4b409 8048838: type inference performance regression
mcimadamore
parents: 29554
diff changeset
  3757
            ClosureHolder(boolean minClosure, BiPredicate<Type, Type> shouldSkip) {
71f15ff4b409 8048838: type inference performance regression
mcimadamore
parents: 29554
diff changeset
  3758
                this.closure = List.nil();
71f15ff4b409 8048838: type inference performance regression
mcimadamore
parents: 29554
diff changeset
  3759
                this.minClosure = minClosure;
71f15ff4b409 8048838: type inference performance regression
mcimadamore
parents: 29554
diff changeset
  3760
                this.shouldSkip = shouldSkip;
71f15ff4b409 8048838: type inference performance regression
mcimadamore
parents: 29554
diff changeset
  3761
            }
71f15ff4b409 8048838: type inference performance regression
mcimadamore
parents: 29554
diff changeset
  3762
71f15ff4b409 8048838: type inference performance regression
mcimadamore
parents: 29554
diff changeset
  3763
            void add(Type type) {
71f15ff4b409 8048838: type inference performance regression
mcimadamore
parents: 29554
diff changeset
  3764
                closure = insert(closure, type, shouldSkip);
71f15ff4b409 8048838: type inference performance regression
mcimadamore
parents: 29554
diff changeset
  3765
            }
71f15ff4b409 8048838: type inference performance regression
mcimadamore
parents: 29554
diff changeset
  3766
71f15ff4b409 8048838: type inference performance regression
mcimadamore
parents: 29554
diff changeset
  3767
            ClosureHolder merge(ClosureHolder other) {
71f15ff4b409 8048838: type inference performance regression
mcimadamore
parents: 29554
diff changeset
  3768
                closure = union(closure, other.closure, shouldSkip);
71f15ff4b409 8048838: type inference performance regression
mcimadamore
parents: 29554
diff changeset
  3769
                return this;
71f15ff4b409 8048838: type inference performance regression
mcimadamore
parents: 29554
diff changeset
  3770
            }
71f15ff4b409 8048838: type inference performance regression
mcimadamore
parents: 29554
diff changeset
  3771
71f15ff4b409 8048838: type inference performance regression
mcimadamore
parents: 29554
diff changeset
  3772
            List<Type> closure() {
71f15ff4b409 8048838: type inference performance regression
mcimadamore
parents: 29554
diff changeset
  3773
                return minClosure ? closureMin(closure) : closure;
71f15ff4b409 8048838: type inference performance regression
mcimadamore
parents: 29554
diff changeset
  3774
            }
71f15ff4b409 8048838: type inference performance regression
mcimadamore
parents: 29554
diff changeset
  3775
        }
71f15ff4b409 8048838: type inference performance regression
mcimadamore
parents: 29554
diff changeset
  3776
71f15ff4b409 8048838: type inference performance regression
mcimadamore
parents: 29554
diff changeset
  3777
    BiPredicate<Type, Type> basicClosureSkip = (t1, t2) -> t1.tsym == t2.tsym;
71f15ff4b409 8048838: type inference performance regression
mcimadamore
parents: 29554
diff changeset
  3778
71f15ff4b409 8048838: type inference performance regression
mcimadamore
parents: 29554
diff changeset
  3779
    /**
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3780
     * Insert a type in a closure
06bc494ca11e Initial load
duke
parents:
diff changeset
  3781
     */
29555
71f15ff4b409 8048838: type inference performance regression
mcimadamore
parents: 29554
diff changeset
  3782
    public List<Type> insert(List<Type> cl, Type t, BiPredicate<Type, Type> shouldSkip) {
24293
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
  3783
        if (cl.isEmpty()) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3784
            return cl.prepend(t);
29555
71f15ff4b409 8048838: type inference performance regression
mcimadamore
parents: 29554
diff changeset
  3785
        } else if (shouldSkip.test(t, cl.head)) {
24293
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
  3786
            return cl;
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
  3787
        } else if (t.tsym.precedes(cl.head.tsym, this)) {
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
  3788
            return cl.prepend(t);
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
            // t comes after head, or the two are unrelated
29555
71f15ff4b409 8048838: type inference performance regression
mcimadamore
parents: 29554
diff changeset
  3791
            return insert(cl.tail, t, shouldSkip).prepend(cl.head);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3792
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3793
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3794
29555
71f15ff4b409 8048838: type inference performance regression
mcimadamore
parents: 29554
diff changeset
  3795
    public List<Type> insert(List<Type> cl, Type t) {
71f15ff4b409 8048838: type inference performance regression
mcimadamore
parents: 29554
diff changeset
  3796
        return insert(cl, t, basicClosureSkip);
71f15ff4b409 8048838: type inference performance regression
mcimadamore
parents: 29554
diff changeset
  3797
    }
71f15ff4b409 8048838: type inference performance regression
mcimadamore
parents: 29554
diff changeset
  3798
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3799
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  3800
     * Form the union of two closures
06bc494ca11e Initial load
duke
parents:
diff changeset
  3801
     */
29555
71f15ff4b409 8048838: type inference performance regression
mcimadamore
parents: 29554
diff changeset
  3802
    public List<Type> union(List<Type> cl1, List<Type> cl2, BiPredicate<Type, Type> shouldSkip) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3803
        if (cl1.isEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3804
            return cl2;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3805
        } else if (cl2.isEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3806
            return cl1;
29555
71f15ff4b409 8048838: type inference performance regression
mcimadamore
parents: 29554
diff changeset
  3807
        } else if (shouldSkip.test(cl1.head, cl2.head)) {
71f15ff4b409 8048838: type inference performance regression
mcimadamore
parents: 29554
diff changeset
  3808
            return union(cl1.tail, cl2.tail, shouldSkip).prepend(cl1.head);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3809
        } else if (cl1.head.tsym.precedes(cl2.head.tsym, this)) {
29555
71f15ff4b409 8048838: type inference performance regression
mcimadamore
parents: 29554
diff changeset
  3810
            return union(cl1.tail, cl2, shouldSkip).prepend(cl1.head);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3811
        } else if (cl2.head.tsym.precedes(cl1.head.tsym, this)) {
29555
71f15ff4b409 8048838: type inference performance regression
mcimadamore
parents: 29554
diff changeset
  3812
            return union(cl1, cl2.tail, shouldSkip).prepend(cl2.head);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3813
        } else {
24293
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
  3814
            // unrelated types
29555
71f15ff4b409 8048838: type inference performance regression
mcimadamore
parents: 29554
diff changeset
  3815
            return union(cl1.tail, cl2, shouldSkip).prepend(cl1.head);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3816
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3817
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3818
29555
71f15ff4b409 8048838: type inference performance regression
mcimadamore
parents: 29554
diff changeset
  3819
    public List<Type> union(List<Type> cl1, List<Type> cl2) {
71f15ff4b409 8048838: type inference performance regression
mcimadamore
parents: 29554
diff changeset
  3820
        return union(cl1, cl2, basicClosureSkip);
71f15ff4b409 8048838: type inference performance regression
mcimadamore
parents: 29554
diff changeset
  3821
    }
71f15ff4b409 8048838: type inference performance regression
mcimadamore
parents: 29554
diff changeset
  3822
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3823
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  3824
     * Intersect two closures
06bc494ca11e Initial load
duke
parents:
diff changeset
  3825
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3826
    public List<Type> intersect(List<Type> cl1, List<Type> cl2) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3827
        if (cl1 == cl2)
06bc494ca11e Initial load
duke
parents:
diff changeset
  3828
            return cl1;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3829
        if (cl1.isEmpty() || cl2.isEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
  3830
            return List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3831
        if (cl1.head.tsym.precedes(cl2.head.tsym, this))
06bc494ca11e Initial load
duke
parents:
diff changeset
  3832
            return intersect(cl1.tail, cl2);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3833
        if (cl2.head.tsym.precedes(cl1.head.tsym, this))
06bc494ca11e Initial load
duke
parents:
diff changeset
  3834
            return intersect(cl1, cl2.tail);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3835
        if (isSameType(cl1.head, cl2.head))
06bc494ca11e Initial load
duke
parents:
diff changeset
  3836
            return intersect(cl1.tail, cl2.tail).prepend(cl1.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3837
        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
  3838
            cl1.head.hasTag(CLASS) && cl2.head.hasTag(CLASS)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3839
            if (cl1.head.isParameterized() && cl2.head.isParameterized()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3840
                Type merge = merge(cl1.head,cl2.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3841
                return intersect(cl1.tail, cl2.tail).prepend(merge);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3842
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3843
            if (cl1.head.isRaw() || cl2.head.isRaw())
06bc494ca11e Initial load
duke
parents:
diff changeset
  3844
                return intersect(cl1.tail, cl2.tail).prepend(erasure(cl1.head));
06bc494ca11e Initial load
duke
parents:
diff changeset
  3845
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3846
        return intersect(cl1.tail, cl2.tail);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3847
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3848
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
  3849
        class TypePair {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3850
            final Type t1;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3851
            final Type t2;
24899
ead62a035849 8043725: javac fails with StackOverflowException
mcimadamore
parents: 24647
diff changeset
  3852
            boolean strict;
ead62a035849 8043725: javac fails with StackOverflowException
mcimadamore
parents: 24647
diff changeset
  3853
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3854
            TypePair(Type t1, Type t2) {
24899
ead62a035849 8043725: javac fails with StackOverflowException
mcimadamore
parents: 24647
diff changeset
  3855
                this(t1, t2, false);
ead62a035849 8043725: javac fails with StackOverflowException
mcimadamore
parents: 24647
diff changeset
  3856
            }
ead62a035849 8043725: javac fails with StackOverflowException
mcimadamore
parents: 24647
diff changeset
  3857
ead62a035849 8043725: javac fails with StackOverflowException
mcimadamore
parents: 24647
diff changeset
  3858
            TypePair(Type t1, Type t2, boolean strict) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3859
                this.t1 = t1;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3860
                this.t2 = t2;
24899
ead62a035849 8043725: javac fails with StackOverflowException
mcimadamore
parents: 24647
diff changeset
  3861
                this.strict = strict;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3862
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3863
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  3864
            public int hashCode() {
14949
45f43822bbde 8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents: 14801
diff changeset
  3865
                return 127 * Types.this.hashCode(t1) + Types.this.hashCode(t2);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3866
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3867
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  3868
            public boolean equals(Object obj) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3869
                if (!(obj instanceof TypePair))
06bc494ca11e Initial load
duke
parents:
diff changeset
  3870
                    return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3871
                TypePair typePair = (TypePair)obj;
24899
ead62a035849 8043725: javac fails with StackOverflowException
mcimadamore
parents: 24647
diff changeset
  3872
                return isSameType(t1, typePair.t1, strict)
ead62a035849 8043725: javac fails with StackOverflowException
mcimadamore
parents: 24647
diff changeset
  3873
                    && isSameType(t2, typePair.t2, strict);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3874
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3875
        }
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21712
diff changeset
  3876
        Set<TypePair> mergeCache = new HashSet<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3877
        private Type merge(Type c1, Type c2) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3878
            ClassType class1 = (ClassType) c1;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3879
            List<Type> act1 = class1.getTypeArguments();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3880
            ClassType class2 = (ClassType) c2;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3881
            List<Type> act2 = class2.getTypeArguments();
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21712
diff changeset
  3882
            ListBuffer<Type> merged = new ListBuffer<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3883
            List<Type> typarams = class1.tsym.type.getTypeArguments();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3884
06bc494ca11e Initial load
duke
parents:
diff changeset
  3885
            while (act1.nonEmpty() && act2.nonEmpty() && typarams.nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3886
                if (containsType(act1.head, act2.head)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3887
                    merged.append(act1.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3888
                } else if (containsType(act2.head, act1.head)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3889
                    merged.append(act2.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3890
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3891
                    TypePair pair = new TypePair(c1, c2);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3892
                    Type m;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3893
                    if (mergeCache.add(pair)) {
24612
75dc732b45af 8042338: Refactor Types.upperBound to treat wildcards and variables separately
dlsmith
parents: 24604
diff changeset
  3894
                        m = new WildcardType(lub(wildUpperBound(act1.head),
75dc732b45af 8042338: Refactor Types.upperBound to treat wildcards and variables separately
dlsmith
parents: 24604
diff changeset
  3895
                                                 wildUpperBound(act2.head)),
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3896
                                             BoundKind.EXTENDS,
26781
a786b07c7b91 8048614: Add TypeMetadata to contain type annotations and other type information
emc
parents: 26107
diff changeset
  3897
                                             syms.boundClass);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3898
                        mergeCache.remove(pair);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3899
                    } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3900
                        m = new WildcardType(syms.objectType,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3901
                                             BoundKind.UNBOUND,
26781
a786b07c7b91 8048614: Add TypeMetadata to contain type annotations and other type information
emc
parents: 26107
diff changeset
  3902
                                             syms.boundClass);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3903
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3904
                    merged.append(m.withTypeVar(typarams.head));
06bc494ca11e Initial load
duke
parents:
diff changeset
  3905
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3906
                act1 = act1.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3907
                act2 = act2.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3908
                typarams = typarams.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3909
            }
8032
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents: 7681
diff changeset
  3910
            Assert.check(act1.isEmpty() && act2.isEmpty() && typarams.isEmpty());
24396
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 24293
diff changeset
  3911
            // There is no spec detailing how type annotations are to
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 24293
diff changeset
  3912
            // be inherited.  So set it to noAnnotations for now
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 24293
diff changeset
  3913
            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
  3914
                                 class1.tsym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3915
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3916
06bc494ca11e Initial load
duke
parents:
diff changeset
  3917
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  3918
     * Return the minimum type of a closure, a compound type if no
06bc494ca11e Initial load
duke
parents:
diff changeset
  3919
     * unique minimum exists.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3920
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3921
    private Type compoundMin(List<Type> cl) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3922
        if (cl.isEmpty()) return syms.objectType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3923
        List<Type> compound = closureMin(cl);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3924
        if (compound.isEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
  3925
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3926
        else if (compound.tail.isEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
  3927
            return compound.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3928
        else
29147
4cba0458106b 8071291: Compiler crashes trying to cast UnionType to IntersectionClassType
mcimadamore
parents: 29146
diff changeset
  3929
            return makeIntersectionType(compound);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3930
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3931
06bc494ca11e Initial load
duke
parents:
diff changeset
  3932
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  3933
     * Return the minimum types of a closure, suitable for computing
06bc494ca11e Initial load
duke
parents:
diff changeset
  3934
     * compoundMin or glb.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3935
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3936
    private List<Type> closureMin(List<Type> cl) {
20249
93f8eae31092 6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents: 19914
diff changeset
  3937
        ListBuffer<Type> classes = new ListBuffer<>();
93f8eae31092 6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents: 19914
diff changeset
  3938
        ListBuffer<Type> interfaces = new ListBuffer<>();
24293
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
  3939
        Set<Type> toSkip = new HashSet<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3940
        while (!cl.isEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3941
            Type current = cl.head;
24293
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
  3942
            boolean keep = !toSkip.contains(current);
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
  3943
            if (keep && current.hasTag(TYPEVAR)) {
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
  3944
                // skip lower-bounded variables with a subtype in cl.tail
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
  3945
                for (Type t : cl.tail) {
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
  3946
                    if (isSubtypeNoCapture(t, current)) {
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
  3947
                        keep = false;
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
  3948
                        break;
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
  3949
                    }
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
  3950
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3951
            }
24293
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
  3952
            if (keep) {
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
  3953
                if (current.isInterface())
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
  3954
                    interfaces.append(current);
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
  3955
                else
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
  3956
                    classes.append(current);
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
  3957
                for (Type t : cl.tail) {
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
  3958
                    // skip supertypes of 'current' in cl.tail
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
  3959
                    if (isSubtypeNoCapture(current, t))
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
  3960
                        toSkip.add(t);
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
  3961
                }
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
  3962
            }
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
  3963
            cl = cl.tail;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3964
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3965
        return classes.appendList(interfaces).toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3966
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3967
06bc494ca11e Initial load
duke
parents:
diff changeset
  3968
    /**
27125
6c718c5086b3 8058511: StackOverflowError at com.sun.tools.javac.code.Types.lub
mcimadamore
parents: 26781
diff changeset
  3969
     * Return the least upper bound of list of types.  if the lub does
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3970
     * not exist return null.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3971
     */
27125
6c718c5086b3 8058511: StackOverflowError at com.sun.tools.javac.code.Types.lub
mcimadamore
parents: 26781
diff changeset
  3972
    public Type lub(List<Type> ts) {
6c718c5086b3 8058511: StackOverflowError at com.sun.tools.javac.code.Types.lub
mcimadamore
parents: 26781
diff changeset
  3973
        return lub(ts.toArray(new Type[ts.length()]));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3974
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3975
06bc494ca11e Initial load
duke
parents:
diff changeset
  3976
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  3977
     * Return the least upper bound (lub) of set of types.  If the lub
06bc494ca11e Initial load
duke
parents:
diff changeset
  3978
     * does not exist return the type of null (bottom).
06bc494ca11e Initial load
duke
parents:
diff changeset
  3979
     */
27125
6c718c5086b3 8058511: StackOverflowError at com.sun.tools.javac.code.Types.lub
mcimadamore
parents: 26781
diff changeset
  3980
    public Type lub(Type... ts) {
6c718c5086b3 8058511: StackOverflowError at com.sun.tools.javac.code.Types.lub
mcimadamore
parents: 26781
diff changeset
  3981
        final int UNKNOWN_BOUND = 0;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3982
        final int ARRAY_BOUND = 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3983
        final int CLASS_BOUND = 2;
27125
6c718c5086b3 8058511: StackOverflowError at com.sun.tools.javac.code.Types.lub
mcimadamore
parents: 26781
diff changeset
  3984
6c718c5086b3 8058511: StackOverflowError at com.sun.tools.javac.code.Types.lub
mcimadamore
parents: 26781
diff changeset
  3985
        int[] kinds = new int[ts.length];
6c718c5086b3 8058511: StackOverflowError at com.sun.tools.javac.code.Types.lub
mcimadamore
parents: 26781
diff changeset
  3986
6c718c5086b3 8058511: StackOverflowError at com.sun.tools.javac.code.Types.lub
mcimadamore
parents: 26781
diff changeset
  3987
        int boundkind = UNKNOWN_BOUND;
6c718c5086b3 8058511: StackOverflowError at com.sun.tools.javac.code.Types.lub
mcimadamore
parents: 26781
diff changeset
  3988
        for (int i = 0 ; i < ts.length ; i++) {
6c718c5086b3 8058511: StackOverflowError at com.sun.tools.javac.code.Types.lub
mcimadamore
parents: 26781
diff changeset
  3989
            Type t = ts[i];
18646
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18412
diff changeset
  3990
            switch (t.getTag()) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3991
            case CLASS:
27125
6c718c5086b3 8058511: StackOverflowError at com.sun.tools.javac.code.Types.lub
mcimadamore
parents: 26781
diff changeset
  3992
                boundkind |= kinds[i] = CLASS_BOUND;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3993
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3994
            case ARRAY:
27125
6c718c5086b3 8058511: StackOverflowError at com.sun.tools.javac.code.Types.lub
mcimadamore
parents: 26781
diff changeset
  3995
                boundkind |= kinds[i] = ARRAY_BOUND;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3996
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3997
            case  TYPEVAR:
06bc494ca11e Initial load
duke
parents:
diff changeset
  3998
                do {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3999
                    t = t.getUpperBound();
18646
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18412
diff changeset
  4000
                } while (t.hasTag(TYPEVAR));
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18412
diff changeset
  4001
                if (t.hasTag(ARRAY)) {
27125
6c718c5086b3 8058511: StackOverflowError at com.sun.tools.javac.code.Types.lub
mcimadamore
parents: 26781
diff changeset
  4002
                    boundkind |= kinds[i] = ARRAY_BOUND;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4003
                } else {
27125
6c718c5086b3 8058511: StackOverflowError at com.sun.tools.javac.code.Types.lub
mcimadamore
parents: 26781
diff changeset
  4004
                    boundkind |= kinds[i] = CLASS_BOUND;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4005
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4006
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4007
            default:
27125
6c718c5086b3 8058511: StackOverflowError at com.sun.tools.javac.code.Types.lub
mcimadamore
parents: 26781
diff changeset
  4008
                kinds[i] = UNKNOWN_BOUND;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4009
                if (t.isPrimitive())
162
6620f2a8e265 6611449: Internal Error thrown during generic method/constructor invocation
mcimadamore
parents: 10
diff changeset
  4010
                    return syms.errType;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4011
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4012
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4013
        switch (boundkind) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4014
        case 0:
06bc494ca11e Initial load
duke
parents:
diff changeset
  4015
            return syms.botType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4016
06bc494ca11e Initial load
duke
parents:
diff changeset
  4017
        case ARRAY_BOUND:
06bc494ca11e Initial load
duke
parents:
diff changeset
  4018
            // calculate lub(A[], B[])
27125
6c718c5086b3 8058511: StackOverflowError at com.sun.tools.javac.code.Types.lub
mcimadamore
parents: 26781
diff changeset
  4019
            Type[] elements = new Type[ts.length];
6c718c5086b3 8058511: StackOverflowError at com.sun.tools.javac.code.Types.lub
mcimadamore
parents: 26781
diff changeset
  4020
            for (int i = 0 ; i < ts.length ; i++) {
6c718c5086b3 8058511: StackOverflowError at com.sun.tools.javac.code.Types.lub
mcimadamore
parents: 26781
diff changeset
  4021
                Type elem = elements[i] = elemTypeFun.apply(ts[i]);
6c718c5086b3 8058511: StackOverflowError at com.sun.tools.javac.code.Types.lub
mcimadamore
parents: 26781
diff changeset
  4022
                if (elem.isPrimitive()) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4023
                    // if a primitive type is found, then return
06bc494ca11e Initial load
duke
parents:
diff changeset
  4024
                    // arraySuperType unless all the types are the
06bc494ca11e Initial load
duke
parents:
diff changeset
  4025
                    // same
27125
6c718c5086b3 8058511: StackOverflowError at com.sun.tools.javac.code.Types.lub
mcimadamore
parents: 26781
diff changeset
  4026
                    Type first = ts[0];
6c718c5086b3 8058511: StackOverflowError at com.sun.tools.javac.code.Types.lub
mcimadamore
parents: 26781
diff changeset
  4027
                    for (int j = 1 ; j < ts.length ; j++) {
6c718c5086b3 8058511: StackOverflowError at com.sun.tools.javac.code.Types.lub
mcimadamore
parents: 26781
diff changeset
  4028
                        if (!isSameType(first, ts[j])) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4029
                             // lub(int[], B[]) is Cloneable & Serializable
06bc494ca11e Initial load
duke
parents:
diff changeset
  4030
                            return arraySuperType();
06bc494ca11e Initial load
duke
parents:
diff changeset
  4031
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4032
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4033
                    // all the array types are the same, return one
06bc494ca11e Initial load
duke
parents:
diff changeset
  4034
                    // lub(int[], int[]) is int[]
06bc494ca11e Initial load
duke
parents:
diff changeset
  4035
                    return first;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4036
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4037
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4038
            // 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
  4039
            return new ArrayType(lub(elements), syms.arrayClass);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4040
06bc494ca11e Initial load
duke
parents:
diff changeset
  4041
        case CLASS_BOUND:
06bc494ca11e Initial load
duke
parents:
diff changeset
  4042
            // calculate lub(A, B)
27125
6c718c5086b3 8058511: StackOverflowError at com.sun.tools.javac.code.Types.lub
mcimadamore
parents: 26781
diff changeset
  4043
            int startIdx = 0;
6c718c5086b3 8058511: StackOverflowError at com.sun.tools.javac.code.Types.lub
mcimadamore
parents: 26781
diff changeset
  4044
            for (int i = 0; i < ts.length ; i++) {
6c718c5086b3 8058511: StackOverflowError at com.sun.tools.javac.code.Types.lub
mcimadamore
parents: 26781
diff changeset
  4045
                Type t = ts[i];
6c718c5086b3 8058511: StackOverflowError at com.sun.tools.javac.code.Types.lub
mcimadamore
parents: 26781
diff changeset
  4046
                if (t.hasTag(CLASS) || t.hasTag(TYPEVAR)) {
6c718c5086b3 8058511: StackOverflowError at com.sun.tools.javac.code.Types.lub
mcimadamore
parents: 26781
diff changeset
  4047
                    break;
6c718c5086b3 8058511: StackOverflowError at com.sun.tools.javac.code.Types.lub
mcimadamore
parents: 26781
diff changeset
  4048
                } else {
6c718c5086b3 8058511: StackOverflowError at com.sun.tools.javac.code.Types.lub
mcimadamore
parents: 26781
diff changeset
  4049
                    startIdx++;
6c718c5086b3 8058511: StackOverflowError at com.sun.tools.javac.code.Types.lub
mcimadamore
parents: 26781
diff changeset
  4050
                }
18646
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18412
diff changeset
  4051
            }
27125
6c718c5086b3 8058511: StackOverflowError at com.sun.tools.javac.code.Types.lub
mcimadamore
parents: 26781
diff changeset
  4052
            Assert.check(startIdx < ts.length);
8617
6a05d1669247 7015715: lub gets stuck on type with complex supertype
mcimadamore
parents: 8430
diff changeset
  4053
            //step 1 - compute erased candidate set (EC)
27125
6c718c5086b3 8058511: StackOverflowError at com.sun.tools.javac.code.Types.lub
mcimadamore
parents: 26781
diff changeset
  4054
            List<Type> cl = erasedSupertypes(ts[startIdx]);
6c718c5086b3 8058511: StackOverflowError at com.sun.tools.javac.code.Types.lub
mcimadamore
parents: 26781
diff changeset
  4055
            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
  4056
                Type t = ts[i];
18646
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18412
diff changeset
  4057
                if (t.hasTag(CLASS) || t.hasTag(TYPEVAR))
8617
6a05d1669247 7015715: lub gets stuck on type with complex supertype
mcimadamore
parents: 8430
diff changeset
  4058
                    cl = intersect(cl, erasedSupertypes(t));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4059
            }
8617
6a05d1669247 7015715: lub gets stuck on type with complex supertype
mcimadamore
parents: 8430
diff changeset
  4060
            //step 2 - compute minimal erased candidate set (MEC)
6a05d1669247 7015715: lub gets stuck on type with complex supertype
mcimadamore
parents: 8430
diff changeset
  4061
            List<Type> mec = closureMin(cl);
6a05d1669247 7015715: lub gets stuck on type with complex supertype
mcimadamore
parents: 8430
diff changeset
  4062
            //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
  4063
            List<Type> candidates = List.nil();
6a05d1669247 7015715: lub gets stuck on type with complex supertype
mcimadamore
parents: 8430
diff changeset
  4064
            for (Type erasedSupertype : mec) {
27125
6c718c5086b3 8058511: StackOverflowError at com.sun.tools.javac.code.Types.lub
mcimadamore
parents: 26781
diff changeset
  4065
                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
  4066
                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
  4067
                    Type superType = asSuper(ts[i], erasedSupertype.tsym);
42828
cce89649f958 8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents: 42819
diff changeset
  4068
                    lci = intersect(lci, superType != null ? List.of(superType) : List.nil());
8617
6a05d1669247 7015715: lub gets stuck on type with complex supertype
mcimadamore
parents: 8430
diff changeset
  4069
                }
6a05d1669247 7015715: lub gets stuck on type with complex supertype
mcimadamore
parents: 8430
diff changeset
  4070
                candidates = candidates.appendList(lci);
6a05d1669247 7015715: lub gets stuck on type with complex supertype
mcimadamore
parents: 8430
diff changeset
  4071
            }
6a05d1669247 7015715: lub gets stuck on type with complex supertype
mcimadamore
parents: 8430
diff changeset
  4072
            //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
  4073
            //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
  4074
            return compoundMin(candidates);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4075
06bc494ca11e Initial load
duke
parents:
diff changeset
  4076
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  4077
            // calculate lub(A, B[])
06bc494ca11e Initial load
duke
parents:
diff changeset
  4078
            List<Type> classes = List.of(arraySuperType());
27125
6c718c5086b3 8058511: StackOverflowError at com.sun.tools.javac.code.Types.lub
mcimadamore
parents: 26781
diff changeset
  4079
            for (int i = 0 ; i < ts.length ; i++) {
6c718c5086b3 8058511: StackOverflowError at com.sun.tools.javac.code.Types.lub
mcimadamore
parents: 26781
diff changeset
  4080
                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
  4081
                    classes = classes.prepend(ts[i]);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4082
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4083
            // lub(A, B[]) is lub(A, arraySuperType)
06bc494ca11e Initial load
duke
parents:
diff changeset
  4084
            return lub(classes);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4085
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4086
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4087
    // where
8617
6a05d1669247 7015715: lub gets stuck on type with complex supertype
mcimadamore
parents: 8430
diff changeset
  4088
        List<Type> erasedSupertypes(Type t) {
20249
93f8eae31092 6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents: 19914
diff changeset
  4089
            ListBuffer<Type> buf = new ListBuffer<>();
8617
6a05d1669247 7015715: lub gets stuck on type with complex supertype
mcimadamore
parents: 8430
diff changeset
  4090
            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
  4091
                if (sup.hasTag(TYPEVAR)) {
8617
6a05d1669247 7015715: lub gets stuck on type with complex supertype
mcimadamore
parents: 8430
diff changeset
  4092
                    buf.append(sup);
6a05d1669247 7015715: lub gets stuck on type with complex supertype
mcimadamore
parents: 8430
diff changeset
  4093
                } else {
6a05d1669247 7015715: lub gets stuck on type with complex supertype
mcimadamore
parents: 8430
diff changeset
  4094
                    buf.append(erasure(sup));
6a05d1669247 7015715: lub gets stuck on type with complex supertype
mcimadamore
parents: 8430
diff changeset
  4095
                }
6a05d1669247 7015715: lub gets stuck on type with complex supertype
mcimadamore
parents: 8430
diff changeset
  4096
            }
6a05d1669247 7015715: lub gets stuck on type with complex supertype
mcimadamore
parents: 8430
diff changeset
  4097
            return buf.toList();
6a05d1669247 7015715: lub gets stuck on type with complex supertype
mcimadamore
parents: 8430
diff changeset
  4098
        }
6a05d1669247 7015715: lub gets stuck on type with complex supertype
mcimadamore
parents: 8430
diff changeset
  4099
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4100
        private Type arraySuperType = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4101
        private Type arraySuperType() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4102
            // initialized lazily to avoid problems during compiler startup
06bc494ca11e Initial load
duke
parents:
diff changeset
  4103
            if (arraySuperType == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4104
                synchronized (this) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4105
                    if (arraySuperType == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4106
                        // 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
  4107
                        arraySuperType = makeIntersectionType(List.of(syms.serializableType,
4cba0458106b 8071291: Compiler crashes trying to cast UnionType to IntersectionClassType
mcimadamore
parents: 29146
diff changeset
  4108
                                syms.cloneableType), true);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4109
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4110
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4111
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4112
            return arraySuperType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4113
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4114
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  4115
06bc494ca11e Initial load
duke
parents:
diff changeset
  4116
    // <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
  4117
    public Type glb(List<Type> ts) {
aafb4bf914ee 6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents: 1874
diff changeset
  4118
        Type t1 = ts.head;
aafb4bf914ee 6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents: 1874
diff changeset
  4119
        for (Type t2 : ts.tail) {
aafb4bf914ee 6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents: 1874
diff changeset
  4120
            if (t1.isErroneous())
aafb4bf914ee 6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents: 1874
diff changeset
  4121
                return t1;
aafb4bf914ee 6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents: 1874
diff changeset
  4122
            t1 = glb(t1, t2);
aafb4bf914ee 6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents: 1874
diff changeset
  4123
        }
aafb4bf914ee 6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents: 1874
diff changeset
  4124
        return t1;
aafb4bf914ee 6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents: 1874
diff changeset
  4125
    }
aafb4bf914ee 6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents: 1874
diff changeset
  4126
    //where
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4127
    public Type glb(Type t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4128
        if (s == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  4129
            return t;
7331
02ffc087c654 6995200: JDK 7 compiler crashes when type-variable is inferred from expected primitive type
mcimadamore
parents: 7204
diff changeset
  4130
        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
  4131
            return syms.errType;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4132
        else if (isSubtypeNoCapture(t, s))
06bc494ca11e Initial load
duke
parents:
diff changeset
  4133
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4134
        else if (isSubtypeNoCapture(s, t))
06bc494ca11e Initial load
duke
parents:
diff changeset
  4135
            return s;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4136
06bc494ca11e Initial load
duke
parents:
diff changeset
  4137
        List<Type> closure = union(closure(t), closure(s));
24293
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
  4138
        return glbFlattened(closure, t);
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
  4139
    }
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
  4140
    //where
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
  4141
    /**
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
  4142
     * 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
  4143
     * redundant elements are removed.  Bounds should be ordered according to
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
  4144
     * {@link Symbol#precedes(TypeSymbol,Types)}.
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
  4145
     *
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
  4146
     * @param flatBounds List of type to glb
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
  4147
     * @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
  4148
     */
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
  4149
    private Type glbFlattened(List<Type> flatBounds, Type errT) {
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
  4150
        List<Type> bounds = closureMin(flatBounds);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4151
06bc494ca11e Initial load
duke
parents:
diff changeset
  4152
        if (bounds.isEmpty()) {             // length == 0
06bc494ca11e Initial load
duke
parents:
diff changeset
  4153
            return syms.objectType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4154
        } else if (bounds.tail.isEmpty()) { // length == 1
06bc494ca11e Initial load
duke
parents:
diff changeset
  4155
            return bounds.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4156
        } else {                            // length > 1
06bc494ca11e Initial load
duke
parents:
diff changeset
  4157
            int classCount = 0;
43025
b6e87a8600a4 8144066: StackOverflowException when computing glb
dlsmith
parents: 42973
diff changeset
  4158
            List<Type> cvars = List.nil();
24293
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
  4159
            List<Type> lowers = List.nil();
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
  4160
            for (Type bound : bounds) {
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
  4161
                if (!bound.isInterface()) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4162
                    classCount++;
24293
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
  4163
                    Type lower = cvarLowerBound(bound);
43025
b6e87a8600a4 8144066: StackOverflowException when computing glb
dlsmith
parents: 42973
diff changeset
  4164
                    if (bound != lower && !lower.hasTag(BOT)) {
b6e87a8600a4 8144066: StackOverflowException when computing glb
dlsmith
parents: 42973
diff changeset
  4165
                        cvars = cvars.append(bound);
b6e87a8600a4 8144066: StackOverflowException when computing glb
dlsmith
parents: 42973
diff changeset
  4166
                        lowers = lowers.append(lower);
b6e87a8600a4 8144066: StackOverflowException when computing glb
dlsmith
parents: 42973
diff changeset
  4167
                    }
24293
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
  4168
                }
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
  4169
            }
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
  4170
            if (classCount > 1) {
43025
b6e87a8600a4 8144066: StackOverflowException when computing glb
dlsmith
parents: 42973
diff changeset
  4171
                if (lowers.isEmpty()) {
24293
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
  4172
                    return createErrorType(errT);
43025
b6e87a8600a4 8144066: StackOverflowException when computing glb
dlsmith
parents: 42973
diff changeset
  4173
                } else {
b6e87a8600a4 8144066: StackOverflowException when computing glb
dlsmith
parents: 42973
diff changeset
  4174
                    // try again with lower bounds included instead of capture variables
b6e87a8600a4 8144066: StackOverflowException when computing glb
dlsmith
parents: 42973
diff changeset
  4175
                    List<Type> newBounds = bounds.diff(cvars).appendList(lowers);
b6e87a8600a4 8144066: StackOverflowException when computing glb
dlsmith
parents: 42973
diff changeset
  4176
                    return glb(newBounds);
b6e87a8600a4 8144066: StackOverflowException when computing glb
dlsmith
parents: 42973
diff changeset
  4177
                }
24293
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
  4178
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4179
        }
29147
4cba0458106b 8071291: Compiler crashes trying to cast UnionType to IntersectionClassType
mcimadamore
parents: 29146
diff changeset
  4180
        return makeIntersectionType(bounds);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4181
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4182
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  4183
06bc494ca11e Initial load
duke
parents:
diff changeset
  4184
    // <editor-fold defaultstate="collapsed" desc="hashCode">
06bc494ca11e Initial load
duke
parents:
diff changeset
  4185
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  4186
     * Compute a hash code on a type.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4187
     */
14949
45f43822bbde 8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents: 14801
diff changeset
  4188
    public int hashCode(Type t) {
32915
eb290482d42f 8138914: javac, method visitTypeVar() at visitor Types.hashCode generates the same hash code for different type variables
vromero
parents: 32709
diff changeset
  4189
        return hashCode(t, false);
eb290482d42f 8138914: javac, method visitTypeVar() at visitor Types.hashCode generates the same hash code for different type variables
vromero
parents: 32709
diff changeset
  4190
    }
eb290482d42f 8138914: javac, method visitTypeVar() at visitor Types.hashCode generates the same hash code for different type variables
vromero
parents: 32709
diff changeset
  4191
eb290482d42f 8138914: javac, method visitTypeVar() at visitor Types.hashCode generates the same hash code for different type variables
vromero
parents: 32709
diff changeset
  4192
    public int hashCode(Type t, boolean strict) {
eb290482d42f 8138914: javac, method visitTypeVar() at visitor Types.hashCode generates the same hash code for different type variables
vromero
parents: 32709
diff changeset
  4193
        return strict ?
eb290482d42f 8138914: javac, method visitTypeVar() at visitor Types.hashCode generates the same hash code for different type variables
vromero
parents: 32709
diff changeset
  4194
                hashCodeStrictVisitor.visit(t) :
eb290482d42f 8138914: javac, method visitTypeVar() at visitor Types.hashCode generates the same hash code for different type variables
vromero
parents: 32709
diff changeset
  4195
                hashCodeVisitor.visit(t);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4196
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4197
    // where
32915
eb290482d42f 8138914: javac, method visitTypeVar() at visitor Types.hashCode generates the same hash code for different type variables
vromero
parents: 32709
diff changeset
  4198
        private static final HashCodeVisitor hashCodeVisitor = new HashCodeVisitor();
eb290482d42f 8138914: javac, method visitTypeVar() at visitor Types.hashCode generates the same hash code for different type variables
vromero
parents: 32709
diff changeset
  4199
        private static final HashCodeVisitor hashCodeStrictVisitor = new HashCodeVisitor() {
eb290482d42f 8138914: javac, method visitTypeVar() at visitor Types.hashCode generates the same hash code for different type variables
vromero
parents: 32709
diff changeset
  4200
            @Override
eb290482d42f 8138914: javac, method visitTypeVar() at visitor Types.hashCode generates the same hash code for different type variables
vromero
parents: 32709
diff changeset
  4201
            public Integer visitTypeVar(TypeVar t, Void ignored) {
eb290482d42f 8138914: javac, method visitTypeVar() at visitor Types.hashCode generates the same hash code for different type variables
vromero
parents: 32709
diff changeset
  4202
                return System.identityHashCode(t);
eb290482d42f 8138914: javac, method visitTypeVar() at visitor Types.hashCode generates the same hash code for different type variables
vromero
parents: 32709
diff changeset
  4203
            }
eb290482d42f 8138914: javac, method visitTypeVar() at visitor Types.hashCode generates the same hash code for different type variables
vromero
parents: 32709
diff changeset
  4204
        };
eb290482d42f 8138914: javac, method visitTypeVar() at visitor Types.hashCode generates the same hash code for different type variables
vromero
parents: 32709
diff changeset
  4205
eb290482d42f 8138914: javac, method visitTypeVar() at visitor Types.hashCode generates the same hash code for different type variables
vromero
parents: 32709
diff changeset
  4206
        private static class HashCodeVisitor extends UnaryVisitor<Integer> {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4207
            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
  4208
                return t.getTag().ordinal();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4209
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4210
06bc494ca11e Initial load
duke
parents:
diff changeset
  4211
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  4212
            public Integer visitClassType(ClassType t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4213
                int result = visit(t.getEnclosingType());
06bc494ca11e Initial load
duke
parents:
diff changeset
  4214
                result *= 127;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4215
                result += t.tsym.flatName().hashCode();
06bc494ca11e Initial load
duke
parents:
diff changeset
  4216
                for (Type s : t.getTypeArguments()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4217
                    result *= 127;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4218
                    result += visit(s);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4219
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4220
                return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4221
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4222
06bc494ca11e Initial load
duke
parents:
diff changeset
  4223
            @Override
14949
45f43822bbde 8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents: 14801
diff changeset
  4224
            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
  4225
                int h = METHOD.ordinal();
45f43822bbde 8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents: 14801
diff changeset
  4226
                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
  4227
                     thisargs.tail != null;
45f43822bbde 8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents: 14801
diff changeset
  4228
                     thisargs = thisargs.tail)
45f43822bbde 8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents: 14801
diff changeset
  4229
                    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
  4230
                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
  4231
            }
45f43822bbde 8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents: 14801
diff changeset
  4232
45f43822bbde 8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents: 14801
diff changeset
  4233
            @Override
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4234
            public Integer visitWildcardType(WildcardType t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4235
                int result = t.kind.hashCode();
06bc494ca11e Initial load
duke
parents:
diff changeset
  4236
                if (t.type != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4237
                    result *= 127;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4238
                    result += visit(t.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4239
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4240
                return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4241
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4242
06bc494ca11e Initial load
duke
parents:
diff changeset
  4243
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  4244
            public Integer visitArrayType(ArrayType t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4245
                return visit(t.elemtype) + 12;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4246
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4247
06bc494ca11e Initial load
duke
parents:
diff changeset
  4248
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  4249
            public Integer visitTypeVar(TypeVar t, Void ignored) {
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33915
diff changeset
  4250
                return System.identityHashCode(t);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4251
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4252
06bc494ca11e Initial load
duke
parents:
diff changeset
  4253
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  4254
            public Integer visitUndetVar(UndetVar t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4255
                return System.identityHashCode(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4256
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4257
06bc494ca11e Initial load
duke
parents:
diff changeset
  4258
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  4259
            public Integer visitErrorType(ErrorType t, Void ignored) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4260
                return 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4261
            }
32915
eb290482d42f 8138914: javac, method visitTypeVar() at visitor Types.hashCode generates the same hash code for different type variables
vromero
parents: 32709
diff changeset
  4262
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4263
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  4264
06bc494ca11e Initial load
duke
parents:
diff changeset
  4265
    // <editor-fold defaultstate="collapsed" desc="Return-Type-Substitutable">
06bc494ca11e Initial load
duke
parents:
diff changeset
  4266
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  4267
     * Does t have a result that is a subtype of the result type of s,
06bc494ca11e Initial load
duke
parents:
diff changeset
  4268
     * suitable for covariant returns?  It is assumed that both types
06bc494ca11e Initial load
duke
parents:
diff changeset
  4269
     * are (possibly polymorphic) method types.  Monomorphic method
06bc494ca11e Initial load
duke
parents:
diff changeset
  4270
     * types are handled in the obvious way.  Polymorphic method types
06bc494ca11e Initial load
duke
parents:
diff changeset
  4271
     * require renaming all type variables of one to corresponding
06bc494ca11e Initial load
duke
parents:
diff changeset
  4272
     * type variables in the other, where correspondence is by
06bc494ca11e Initial load
duke
parents:
diff changeset
  4273
     * position in the type parameter list. */
06bc494ca11e Initial load
duke
parents:
diff changeset
  4274
    public boolean resultSubtype(Type t, Type s, Warner warner) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4275
        List<Type> tvars = t.getTypeArguments();
06bc494ca11e Initial load
duke
parents:
diff changeset
  4276
        List<Type> svars = s.getTypeArguments();
06bc494ca11e Initial load
duke
parents:
diff changeset
  4277
        Type tres = t.getReturnType();
06bc494ca11e Initial load
duke
parents:
diff changeset
  4278
        Type sres = subst(s.getReturnType(), svars, tvars);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4279
        return covariantReturnType(tres, sres, warner);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4280
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4281
06bc494ca11e Initial load
duke
parents:
diff changeset
  4282
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  4283
     * 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
  4284
     * @jls section 8.4.5
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4285
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  4286
    public boolean returnTypeSubstitutable(Type r1, Type r2) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4287
        if (hasSameArgs(r1, r2))
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14443
diff changeset
  4288
            return resultSubtype(r1, r2, noWarnings);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4289
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
  4290
            return covariantReturnType(r1.getReturnType(),
1874
891c2d6823bb 6797463: 6557199 breaks the jax-ws workspace
tbell
parents: 1868
diff changeset
  4291
                                       erasure(r2.getReturnType()),
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14443
diff changeset
  4292
                                       noWarnings);
1874
891c2d6823bb 6797463: 6557199 breaks the jax-ws workspace
tbell
parents: 1868
diff changeset
  4293
    }
891c2d6823bb 6797463: 6557199 breaks the jax-ws workspace
tbell
parents: 1868
diff changeset
  4294
891c2d6823bb 6797463: 6557199 breaks the jax-ws workspace
tbell
parents: 1868
diff changeset
  4295
    public boolean returnTypeSubstitutable(Type r1,
891c2d6823bb 6797463: 6557199 breaks the jax-ws workspace
tbell
parents: 1868
diff changeset
  4296
                                           Type r2, Type r2res,
891c2d6823bb 6797463: 6557199 breaks the jax-ws workspace
tbell
parents: 1868
diff changeset
  4297
                                           Warner warner) {
891c2d6823bb 6797463: 6557199 breaks the jax-ws workspace
tbell
parents: 1868
diff changeset
  4298
        if (isSameType(r1.getReturnType(), r2res))
891c2d6823bb 6797463: 6557199 breaks the jax-ws workspace
tbell
parents: 1868
diff changeset
  4299
            return true;
891c2d6823bb 6797463: 6557199 breaks the jax-ws workspace
tbell
parents: 1868
diff changeset
  4300
        if (r1.getReturnType().isPrimitive() || r2res.isPrimitive())
891c2d6823bb 6797463: 6557199 breaks the jax-ws workspace
tbell
parents: 1868
diff changeset
  4301
            return false;
891c2d6823bb 6797463: 6557199 breaks the jax-ws workspace
tbell
parents: 1868
diff changeset
  4302
891c2d6823bb 6797463: 6557199 breaks the jax-ws workspace
tbell
parents: 1868
diff changeset
  4303
        if (hasSameArgs(r1, r2))
891c2d6823bb 6797463: 6557199 breaks the jax-ws workspace
tbell
parents: 1868
diff changeset
  4304
            return covariantReturnType(r1.getReturnType(), r2res, warner);
891c2d6823bb 6797463: 6557199 breaks the jax-ws workspace
tbell
parents: 1868
diff changeset
  4305
        if (isSubtypeUnchecked(r1.getReturnType(), r2res, warner))
891c2d6823bb 6797463: 6557199 breaks the jax-ws workspace
tbell
parents: 1868
diff changeset
  4306
            return true;
891c2d6823bb 6797463: 6557199 breaks the jax-ws workspace
tbell
parents: 1868
diff changeset
  4307
        if (!isSubtype(r1.getReturnType(), erasure(r2res)))
891c2d6823bb 6797463: 6557199 breaks the jax-ws workspace
tbell
parents: 1868
diff changeset
  4308
            return false;
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 7637
diff changeset
  4309
        warner.warn(LintCategory.UNCHECKED);
1874
891c2d6823bb 6797463: 6557199 breaks the jax-ws workspace
tbell
parents: 1868
diff changeset
  4310
        return true;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4311
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4312
06bc494ca11e Initial load
duke
parents:
diff changeset
  4313
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  4314
     * Is t an appropriate return type in an overrider for a
06bc494ca11e Initial load
duke
parents:
diff changeset
  4315
     * method that returns s?
06bc494ca11e Initial load
duke
parents:
diff changeset
  4316
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  4317
    public boolean covariantReturnType(Type t, Type s, Warner warner) {
1874
891c2d6823bb 6797463: 6557199 breaks the jax-ws workspace
tbell
parents: 1868
diff changeset
  4318
        return
891c2d6823bb 6797463: 6557199 breaks the jax-ws workspace
tbell
parents: 1868
diff changeset
  4319
            isSameType(t, s) ||
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4320
            !t.isPrimitive() &&
1874
891c2d6823bb 6797463: 6557199 breaks the jax-ws workspace
tbell
parents: 1868
diff changeset
  4321
            !s.isPrimitive() &&
891c2d6823bb 6797463: 6557199 breaks the jax-ws workspace
tbell
parents: 1868
diff changeset
  4322
            isAssignable(t, s, warner);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4323
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4324
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  4325
06bc494ca11e Initial load
duke
parents:
diff changeset
  4326
    // <editor-fold defaultstate="collapsed" desc="Box/unbox support">
06bc494ca11e Initial load
duke
parents:
diff changeset
  4327
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  4328
     * Return the class that boxes the given primitive.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4329
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  4330
    public ClassSymbol boxedClass(Type t) {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34995
diff changeset
  4331
        return syms.enterClass(syms.java_base, syms.boxedName[t.getTag().ordinal()]);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4332
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4333
06bc494ca11e Initial load
duke
parents:
diff changeset
  4334
    /**
7331
02ffc087c654 6995200: JDK 7 compiler crashes when type-variable is inferred from expected primitive type
mcimadamore
parents: 7204
diff changeset
  4335
     * 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
  4336
     */
02ffc087c654 6995200: JDK 7 compiler crashes when type-variable is inferred from expected primitive type
mcimadamore
parents: 7204
diff changeset
  4337
    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
  4338
        return t.isPrimitive() ?
02ffc087c654 6995200: JDK 7 compiler crashes when type-variable is inferred from expected primitive type
mcimadamore
parents: 7204
diff changeset
  4339
            boxedClass(t).type :
02ffc087c654 6995200: JDK 7 compiler crashes when type-variable is inferred from expected primitive type
mcimadamore
parents: 7204
diff changeset
  4340
            t;
02ffc087c654 6995200: JDK 7 compiler crashes when type-variable is inferred from expected primitive type
mcimadamore
parents: 7204
diff changeset
  4341
    }
02ffc087c654 6995200: JDK 7 compiler crashes when type-variable is inferred from expected primitive type
mcimadamore
parents: 7204
diff changeset
  4342
02ffc087c654 6995200: JDK 7 compiler crashes when type-variable is inferred from expected primitive type
mcimadamore
parents: 7204
diff changeset
  4343
    /**
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4344
     * Return the primitive type corresponding to a boxed type.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4345
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  4346
    public Type unboxedType(Type t) {
25445
603f0c93d5c9 8011044: Remove support for 1.5 and earlier source and target options
ntoda
parents: 25443
diff changeset
  4347
        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
  4348
            Name box = syms.boxedName[i];
603f0c93d5c9 8011044: Remove support for 1.5 and earlier source and target options
ntoda
parents: 25443
diff changeset
  4349
            if (box != null &&
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34995
diff changeset
  4350
                asSuper(t, syms.enterClass(syms.java_base, box)) != null)
25445
603f0c93d5c9 8011044: Remove support for 1.5 and earlier source and target options
ntoda
parents: 25443
diff changeset
  4351
                return syms.typeOfTag[i];
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4352
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4353
        return Type.noType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4354
    }
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
  4355
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
  4356
    /**
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
  4357
     * 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
  4358
     */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
  4359
    public Type unboxedTypeOrType(Type t) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
  4360
        Type unboxedType = unboxedType(t);
18646
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18412
diff changeset
  4361
        return unboxedType.hasTag(NONE) ? t : unboxedType;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
  4362
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4363
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  4364
06bc494ca11e Initial load
duke
parents:
diff changeset
  4365
    // <editor-fold defaultstate="collapsed" desc="Capture conversion">
06bc494ca11e Initial load
duke
parents:
diff changeset
  4366
    /*
9303
eae35c201e19 7032975: API files in javax.annotation.processing need to be updated for references to JLS
jjh
parents: 9301
diff changeset
  4367
     * JLS 5.1.10 Capture Conversion:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4368
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  4369
     * Let G name a generic type declaration with n formal type
06bc494ca11e Initial load
duke
parents:
diff changeset
  4370
     * parameters A1 ... An with corresponding bounds U1 ... Un. There
06bc494ca11e Initial load
duke
parents:
diff changeset
  4371
     * exists a capture conversion from G<T1 ... Tn> to G<S1 ... Sn>,
06bc494ca11e Initial load
duke
parents:
diff changeset
  4372
     * where, for 1 <= i <= n:
06bc494ca11e Initial load
duke
parents:
diff changeset
  4373
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  4374
     * + If Ti is a wildcard type argument (4.5.1) of the form ? then
06bc494ca11e Initial load
duke
parents:
diff changeset
  4375
     *   Si is a fresh type variable whose upper bound is
06bc494ca11e Initial load
duke
parents:
diff changeset
  4376
     *   Ui[A1 := S1, ..., An := Sn] and whose lower bound is the null
06bc494ca11e Initial load
duke
parents:
diff changeset
  4377
     *   type.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4378
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  4379
     * + If Ti is a wildcard type argument of the form ? extends Bi,
06bc494ca11e Initial load
duke
parents:
diff changeset
  4380
     *   then Si is a fresh type variable whose upper bound is
06bc494ca11e Initial load
duke
parents:
diff changeset
  4381
     *   glb(Bi, Ui[A1 := S1, ..., An := Sn]) and whose lower bound is
06bc494ca11e Initial load
duke
parents:
diff changeset
  4382
     *   the null type, where glb(V1,... ,Vm) is V1 & ... & Vm. It is
06bc494ca11e Initial load
duke
parents:
diff changeset
  4383
     *   a compile-time error if for any two classes (not interfaces)
06bc494ca11e Initial load
duke
parents:
diff changeset
  4384
     *   Vi and Vj,Vi is not a subclass of Vj or vice versa.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4385
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  4386
     * + If Ti is a wildcard type argument of the form ? super Bi,
06bc494ca11e Initial load
duke
parents:
diff changeset
  4387
     *   then Si is a fresh type variable whose upper bound is
06bc494ca11e Initial load
duke
parents:
diff changeset
  4388
     *   Ui[A1 := S1, ..., An := Sn] and whose lower bound is Bi.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4389
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  4390
     * + Otherwise, Si = Ti.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4391
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  4392
     * Capture conversion on any type other than a parameterized type
06bc494ca11e Initial load
duke
parents:
diff changeset
  4393
     * (4.5) acts as an identity conversion (5.1.1). Capture
06bc494ca11e Initial load
duke
parents:
diff changeset
  4394
     * conversions never require a special action at run time and
06bc494ca11e Initial load
duke
parents:
diff changeset
  4395
     * therefore never throw an exception at run time.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4396
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  4397
     * Capture conversion is not applied recursively.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4398
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  4399
    /**
9303
eae35c201e19 7032975: API files in javax.annotation.processing need to be updated for references to JLS
jjh
parents: 9301
diff changeset
  4400
     * Capture conversion as specified by the JLS.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4401
     */
3140
15a274b13051 6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents: 2223
diff changeset
  4402
15a274b13051 6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents: 2223
diff changeset
  4403
    public List<Type> capture(List<Type> ts) {
15a274b13051 6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents: 2223
diff changeset
  4404
        List<Type> buf = List.nil();
15a274b13051 6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents: 2223
diff changeset
  4405
        for (Type t : ts) {
15a274b13051 6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents: 2223
diff changeset
  4406
            buf = buf.prepend(capture(t));
15a274b13051 6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents: 2223
diff changeset
  4407
        }
15a274b13051 6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents: 2223
diff changeset
  4408
        return buf.reverse();
15a274b13051 6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents: 2223
diff changeset
  4409
    }
24226
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23810
diff changeset
  4410
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4411
    public Type capture(Type t) {
24226
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23810
diff changeset
  4412
        if (!t.hasTag(CLASS)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4413
            return t;
24226
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23810
diff changeset
  4414
        }
6350
7070b0722ebb 6886247: regression: javac crashes with an assertion error in Attr.java
mcimadamore
parents: 5847
diff changeset
  4415
        if (t.getEnclosingType() != Type.noType) {
7070b0722ebb 6886247: regression: javac crashes with an assertion error in Attr.java
mcimadamore
parents: 5847
diff changeset
  4416
            Type capturedEncl = capture(t.getEnclosingType());
7070b0722ebb 6886247: regression: javac crashes with an assertion error in Attr.java
mcimadamore
parents: 5847
diff changeset
  4417
            if (capturedEncl != t.getEnclosingType()) {
7070b0722ebb 6886247: regression: javac crashes with an assertion error in Attr.java
mcimadamore
parents: 5847
diff changeset
  4418
                Type type1 = memberType(capturedEncl, t.tsym);
7070b0722ebb 6886247: regression: javac crashes with an assertion error in Attr.java
mcimadamore
parents: 5847
diff changeset
  4419
                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
  4420
            }
7070b0722ebb 6886247: regression: javac crashes with an assertion error in Attr.java
mcimadamore
parents: 5847
diff changeset
  4421
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4422
        ClassType cls = (ClassType)t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4423
        if (cls.isRaw() || !cls.isParameterized())
06bc494ca11e Initial load
duke
parents:
diff changeset
  4424
            return cls;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4425
06bc494ca11e Initial load
duke
parents:
diff changeset
  4426
        ClassType G = (ClassType)cls.asElement().asType();
06bc494ca11e Initial load
duke
parents:
diff changeset
  4427
        List<Type> A = G.getTypeArguments();
06bc494ca11e Initial load
duke
parents:
diff changeset
  4428
        List<Type> T = cls.getTypeArguments();
06bc494ca11e Initial load
duke
parents:
diff changeset
  4429
        List<Type> S = freshTypeVariables(T);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4430
06bc494ca11e Initial load
duke
parents:
diff changeset
  4431
        List<Type> currentA = A;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4432
        List<Type> currentT = T;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4433
        List<Type> currentS = S;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4434
        boolean captured = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4435
        while (!currentA.isEmpty() &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  4436
               !currentT.isEmpty() &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  4437
               !currentS.isEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4438
            if (currentS.head != currentT.head) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4439
                captured = true;
24396
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 24293
diff changeset
  4440
                WildcardType Ti = (WildcardType)currentT.head;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4441
                Type Ui = currentA.head.getUpperBound();
24396
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 24293
diff changeset
  4442
                CapturedType Si = (CapturedType)currentS.head;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4443
                if (Ui == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  4444
                    Ui = syms.objectType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4445
                switch (Ti.kind) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4446
                case UNBOUND:
06bc494ca11e Initial load
duke
parents:
diff changeset
  4447
                    Si.bound = subst(Ui, A, S);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4448
                    Si.lower = syms.botType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4449
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4450
                case EXTENDS:
06bc494ca11e Initial load
duke
parents:
diff changeset
  4451
                    Si.bound = glb(Ti.getExtendsBound(), subst(Ui, A, S));
06bc494ca11e Initial load
duke
parents:
diff changeset
  4452
                    Si.lower = syms.botType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4453
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4454
                case SUPER:
06bc494ca11e Initial load
duke
parents:
diff changeset
  4455
                    Si.bound = subst(Ui, A, S);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4456
                    Si.lower = Ti.getSuperBound();
06bc494ca11e Initial load
duke
parents:
diff changeset
  4457
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4458
                }
24291
440746208be7 8033437: javac, inconsistent generic types behaviour when compiling together vs. separate
pgovereau
parents: 24226
diff changeset
  4459
                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
  4460
                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
  4461
                if (!Si.bound.hasTag(ERROR) &&
440746208be7 8033437: javac, inconsistent generic types behaviour when compiling together vs. separate
pgovereau
parents: 24226
diff changeset
  4462
                    !Si.lower.hasTag(ERROR) &&
440746208be7 8033437: javac, inconsistent generic types behaviour when compiling together vs. separate
pgovereau
parents: 24226
diff changeset
  4463
                    isSameType(tmpBound, tmpLower, false)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4464
                    currentS.head = Si.bound;
24291
440746208be7 8033437: javac, inconsistent generic types behaviour when compiling together vs. separate
pgovereau
parents: 24226
diff changeset
  4465
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4466
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4467
            currentA = currentA.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4468
            currentT = currentT.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4469
            currentS = currentS.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4470
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4471
        if (!currentA.isEmpty() || !currentT.isEmpty() || !currentS.isEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
  4472
            return erasure(t); // some "rare" type involved
06bc494ca11e Initial load
duke
parents:
diff changeset
  4473
06bc494ca11e Initial load
duke
parents:
diff changeset
  4474
        if (captured)
24396
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 24293
diff changeset
  4475
            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
  4476
                                 cls.getMetadata());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4477
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
  4478
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4479
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4480
    // where
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents: 2218
diff changeset
  4481
        public List<Type> freshTypeVariables(List<Type> types) {
20249
93f8eae31092 6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents: 19914
diff changeset
  4482
            ListBuffer<Type> result = new ListBuffer<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4483
            for (Type t : types) {
18646
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18412
diff changeset
  4484
                if (t.hasTag(WILDCARD)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4485
                    Type bound = ((WildcardType)t).getExtendsBound();
06bc494ca11e Initial load
duke
parents:
diff changeset
  4486
                    if (bound == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  4487
                        bound = syms.objectType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4488
                    result.append(new CapturedType(capturedName,
06bc494ca11e Initial load
duke
parents:
diff changeset
  4489
                                                   syms.noSymbol,
06bc494ca11e Initial load
duke
parents:
diff changeset
  4490
                                                   bound,
06bc494ca11e Initial load
duke
parents:
diff changeset
  4491
                                                   syms.botType,
26781
a786b07c7b91 8048614: Add TypeMetadata to contain type annotations and other type information
emc
parents: 26107
diff changeset
  4492
                                                   (WildcardType)t));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4493
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4494
                    result.append(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4495
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4496
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4497
            return result.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  4498
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4499
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  4500
06bc494ca11e Initial load
duke
parents:
diff changeset
  4501
    // <editor-fold defaultstate="collapsed" desc="Internal utility methods">
06bc494ca11e Initial load
duke
parents:
diff changeset
  4502
    private boolean sideCast(Type from, Type to, Warner warn) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4503
        // We are casting from type $from$ to type $to$, which are
06bc494ca11e Initial load
duke
parents:
diff changeset
  4504
        // non-final unrelated types.  This method
06bc494ca11e Initial load
duke
parents:
diff changeset
  4505
        // tries to reject a cast by transferring type parameters
06bc494ca11e Initial load
duke
parents:
diff changeset
  4506
        // from $to$ to $from$ by common superinterfaces.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4507
        boolean reverse = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4508
        Type target = to;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4509
        if ((to.tsym.flags() & INTERFACE) == 0) {
8032
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents: 7681
diff changeset
  4510
            Assert.check((from.tsym.flags() & INTERFACE) != 0);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4511
            reverse = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4512
            to = from;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4513
            from = target;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4514
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4515
        List<Type> commonSupers = superClosure(to, erasure(from));
06bc494ca11e Initial load
duke
parents:
diff changeset
  4516
        boolean giveWarning = commonSupers.isEmpty();
06bc494ca11e Initial load
duke
parents:
diff changeset
  4517
        // The arguments to the supers could be unified here to
06bc494ca11e Initial load
duke
parents:
diff changeset
  4518
        // get a more accurate analysis
06bc494ca11e Initial load
duke
parents:
diff changeset
  4519
        while (commonSupers.nonEmpty()) {
21712
d363f367d4c0 8027439: Compile-time error in the case of ((Integer[] & Serializable)new Integer[1]).getClass()
emc
parents: 21041
diff changeset
  4520
            Type t1 = asSuper(from, commonSupers.head.tsym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4521
            Type t2 = commonSupers.head; // same as asSuper(to, commonSupers.head.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4522
            if (disjointTypes(t1.getTypeArguments(), t2.getTypeArguments()))
06bc494ca11e Initial load
duke
parents:
diff changeset
  4523
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4524
            giveWarning = giveWarning || (reverse ? giveWarning(t2, t1) : giveWarning(t1, t2));
06bc494ca11e Initial load
duke
parents:
diff changeset
  4525
            commonSupers = commonSupers.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4526
        }
1792
985e9406d42a 6558559: Extra "unchecked" diagnostic
mcimadamore
parents: 1790
diff changeset
  4527
        if (giveWarning && !isReifiable(reverse ? from : to))
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 7637
diff changeset
  4528
            warn.warn(LintCategory.UNCHECKED);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4529
        return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4530
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4531
06bc494ca11e Initial load
duke
parents:
diff changeset
  4532
    private boolean sideCastFinal(Type from, Type to, Warner warn) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4533
        // We are casting from type $from$ to type $to$, which are
06bc494ca11e Initial load
duke
parents:
diff changeset
  4534
        // unrelated types one of which is final and the other of
06bc494ca11e Initial load
duke
parents:
diff changeset
  4535
        // which is an interface.  This method
06bc494ca11e Initial load
duke
parents:
diff changeset
  4536
        // tries to reject a cast by transferring type parameters
06bc494ca11e Initial load
duke
parents:
diff changeset
  4537
        // from the final class to the interface.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4538
        boolean reverse = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4539
        Type target = to;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4540
        if ((to.tsym.flags() & INTERFACE) == 0) {
8032
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents: 7681
diff changeset
  4541
            Assert.check((from.tsym.flags() & INTERFACE) != 0);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4542
            reverse = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4543
            to = from;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4544
            from = target;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4545
        }
8032
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents: 7681
diff changeset
  4546
        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
  4547
        Type t1 = asSuper(from, to.tsym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4548
        if (t1 == null) return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4549
        Type t2 = to;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4550
        if (disjointTypes(t1.getTypeArguments(), t2.getTypeArguments()))
06bc494ca11e Initial load
duke
parents:
diff changeset
  4551
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4552
        if (!isReifiable(target) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  4553
            (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
  4554
            warn.warn(LintCategory.UNCHECKED);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4555
        return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4556
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4557
06bc494ca11e Initial load
duke
parents:
diff changeset
  4558
    private boolean giveWarning(Type from, Type to) {
16565
aee0d0aaec44 8010101: Intersection type cast issues redundant unchecked warning
mcimadamore
parents: 16556
diff changeset
  4559
        List<Type> bounds = to.isCompound() ?
33556
a14a556cf2c9 8141343: Subtle semantics changes for union types in cast conversion
mcimadamore
parents: 32915
diff changeset
  4560
                directSupertypes(to) : List.of(to);
16565
aee0d0aaec44 8010101: Intersection type cast issues redundant unchecked warning
mcimadamore
parents: 16556
diff changeset
  4561
        for (Type b : bounds) {
aee0d0aaec44 8010101: Intersection type cast issues redundant unchecked warning
mcimadamore
parents: 16556
diff changeset
  4562
            Type subFrom = asSub(from, b.tsym);
aee0d0aaec44 8010101: Intersection type cast issues redundant unchecked warning
mcimadamore
parents: 16556
diff changeset
  4563
            if (b.isParameterized() &&
aee0d0aaec44 8010101: Intersection type cast issues redundant unchecked warning
mcimadamore
parents: 16556
diff changeset
  4564
                    (!(isUnbounded(b) ||
aee0d0aaec44 8010101: Intersection type cast issues redundant unchecked warning
mcimadamore
parents: 16556
diff changeset
  4565
                    isSubtype(from, b) ||
aee0d0aaec44 8010101: Intersection type cast issues redundant unchecked warning
mcimadamore
parents: 16556
diff changeset
  4566
                    ((subFrom != null) && containsType(b.allparams(), subFrom.allparams()))))) {
aee0d0aaec44 8010101: Intersection type cast issues redundant unchecked warning
mcimadamore
parents: 16556
diff changeset
  4567
                return true;
aee0d0aaec44 8010101: Intersection type cast issues redundant unchecked warning
mcimadamore
parents: 16556
diff changeset
  4568
            }
aee0d0aaec44 8010101: Intersection type cast issues redundant unchecked warning
mcimadamore
parents: 16556
diff changeset
  4569
        }
aee0d0aaec44 8010101: Intersection type cast issues redundant unchecked warning
mcimadamore
parents: 16556
diff changeset
  4570
        return false;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4571
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4572
06bc494ca11e Initial load
duke
parents:
diff changeset
  4573
    private List<Type> superClosure(Type t, Type s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4574
        List<Type> cl = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
  4575
        for (List<Type> l = interfaces(t); l.nonEmpty(); l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4576
            if (isSubtype(s, erasure(l.head))) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4577
                cl = insert(cl, l.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4578
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4579
                cl = union(cl, superClosure(l.head, s));
06bc494ca11e Initial load
duke
parents:
diff changeset
  4580
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4581
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4582
        return cl;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4583
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4584
06bc494ca11e Initial load
duke
parents:
diff changeset
  4585
    private boolean containsTypeEquivalent(Type t, Type s) {
29842
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29555
diff changeset
  4586
        return isSameType(t, s) || // shortcut
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4587
            containsType(t, s) && containsType(s, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4588
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4589
1473
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4590
    // <editor-fold defaultstate="collapsed" desc="adapt">
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4591
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  4592
     * Adapt a type by computing a substitution which maps a source
06bc494ca11e Initial load
duke
parents:
diff changeset
  4593
     * type to a target type.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4594
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  4595
     * @param source    the source type
06bc494ca11e Initial load
duke
parents:
diff changeset
  4596
     * @param target    the target type
06bc494ca11e Initial load
duke
parents:
diff changeset
  4597
     * @param from      the type variables of the computed substitution
06bc494ca11e Initial load
duke
parents:
diff changeset
  4598
     * @param to        the types of the computed substitution.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4599
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  4600
    public void adapt(Type source,
06bc494ca11e Initial load
duke
parents:
diff changeset
  4601
                       Type target,
06bc494ca11e Initial load
duke
parents:
diff changeset
  4602
                       ListBuffer<Type> from,
06bc494ca11e Initial load
duke
parents:
diff changeset
  4603
                       ListBuffer<Type> to) throws AdaptFailure {
1473
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4604
        new Adapter(from, to).adapt(source, target);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4605
    }
1473
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4606
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4607
    class Adapter extends SimpleVisitor<Void, Type> {
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4608
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4609
        ListBuffer<Type> from;
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4610
        ListBuffer<Type> to;
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4611
        Map<Symbol,Type> mapping;
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4612
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4613
        Adapter(ListBuffer<Type> from, ListBuffer<Type> to) {
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4614
            this.from = from;
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4615
            this.to = to;
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21712
diff changeset
  4616
            mapping = new HashMap<>();
1473
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4617
        }
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4618
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4619
        public void adapt(Type source, Type target) throws AdaptFailure {
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4620
            visit(source, target);
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4621
            List<Type> fromList = from.toList();
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4622
            List<Type> toList = to.toList();
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4623
            while (!fromList.isEmpty()) {
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4624
                Type val = mapping.get(fromList.head.tsym);
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4625
                if (toList.head != val)
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4626
                    toList.head = val;
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4627
                fromList = fromList.tail;
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4628
                toList = toList.tail;
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4629
            }
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4630
        }
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4631
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4632
        @Override
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4633
        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
  4634
            if (target.hasTag(CLASS))
1473
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4635
                adaptRecursive(source.allparams(), target.allparams());
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4636
            return null;
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4637
        }
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4638
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4639
        @Override
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4640
        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
  4641
            if (target.hasTag(ARRAY))
1473
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4642
                adaptRecursive(elemtype(source), elemtype(target));
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4643
            return null;
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4644
        }
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4645
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4646
        @Override
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4647
        public Void visitWildcardType(WildcardType source, Type target) throws AdaptFailure {
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4648
            if (source.isExtendsBound())
24612
75dc732b45af 8042338: Refactor Types.upperBound to treat wildcards and variables separately
dlsmith
parents: 24604
diff changeset
  4649
                adaptRecursive(wildUpperBound(source), wildUpperBound(target));
1473
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4650
            else if (source.isSuperBound())
24293
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
  4651
                adaptRecursive(wildLowerBound(source), wildLowerBound(target));
1473
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4652
            return null;
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4653
        }
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4654
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4655
        @Override
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4656
        public Void visitTypeVar(TypeVar source, Type target) throws AdaptFailure {
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4657
            // Check to see if there is
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4658
            // already a mapping for $source$, in which case
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4659
            // the old mapping will be merged with the new
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4660
            Type val = mapping.get(source.tsym);
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4661
            if (val != null) {
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4662
                if (val.isSuperBound() && target.isSuperBound()) {
24293
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24291
diff changeset
  4663
                    val = isSubtype(wildLowerBound(val), wildLowerBound(target))
1473
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4664
                        ? target : val;
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4665
                } else if (val.isExtendsBound() && target.isExtendsBound()) {
24612
75dc732b45af 8042338: Refactor Types.upperBound to treat wildcards and variables separately
dlsmith
parents: 24604
diff changeset
  4666
                    val = isSubtype(wildUpperBound(val), wildUpperBound(target))
1473
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4667
                        ? val : target;
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4668
                } else if (!isSameType(val, target)) {
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4669
                    throw new AdaptFailure();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4670
                }
1473
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4671
            } else {
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4672
                val = target;
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4673
                from.append(source);
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4674
                to.append(target);
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4675
            }
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4676
            mapping.put(source.tsym, val);
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4677
            return null;
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4678
        }
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4679
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4680
        @Override
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4681
        public Void visitType(Type source, Type target) {
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4682
            return null;
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4683
        }
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4684
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21712
diff changeset
  4685
        private Set<TypePair> cache = new HashSet<>();
1473
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4686
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4687
        private void adaptRecursive(Type source, Type target) {
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4688
            TypePair pair = new TypePair(source, target);
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4689
            if (cache.add(pair)) {
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4690
                try {
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4691
                    visit(source, target);
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4692
                } finally {
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4693
                    cache.remove(pair);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4694
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4695
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4696
        }
1473
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4697
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4698
        private void adaptRecursive(List<Type> source, List<Type> target) {
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4699
            if (source.length() == target.length()) {
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4700
                while (source.nonEmpty()) {
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4701
                    adaptRecursive(source.head, target.head);
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4702
                    source = source.tail;
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4703
                    target = target.tail;
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4704
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4705
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4706
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4707
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4708
1473
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4709
    public static class AdaptFailure extends RuntimeException {
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4710
        static final long serialVersionUID = -7490231548272701566L;
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4711
    }
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4712
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4713
    private void adaptSelf(Type t,
06bc494ca11e Initial load
duke
parents:
diff changeset
  4714
                           ListBuffer<Type> from,
06bc494ca11e Initial load
duke
parents:
diff changeset
  4715
                           ListBuffer<Type> to) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4716
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4717
            //if (t.tsym.type != t)
06bc494ca11e Initial load
duke
parents:
diff changeset
  4718
                adapt(t.tsym.type, t, from, to);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4719
        } catch (AdaptFailure ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4720
            // Adapt should never fail calculating a mapping from
06bc494ca11e Initial load
duke
parents:
diff changeset
  4721
            // t.tsym.type to t as there can be no merge problem.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4722
            throw new AssertionError(ex);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4723
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4724
    }
1473
16ba23bb25e6 6586091: javac crashes with StackOverflowError
mcimadamore
parents: 1471
diff changeset
  4725
    // </editor-fold>
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4726
06bc494ca11e Initial load
duke
parents:
diff changeset
  4727
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  4728
     * Rewrite all type variables (universal quantifiers) in the given
06bc494ca11e Initial load
duke
parents:
diff changeset
  4729
     * type to wildcards (existential quantifiers).  This is used to
06bc494ca11e Initial load
duke
parents:
diff changeset
  4730
     * determine if a cast is allowed.  For example, if high is true
06bc494ca11e Initial load
duke
parents:
diff changeset
  4731
     * and {@code T <: Number}, then {@code List<T>} is rewritten to
06bc494ca11e Initial load
duke
parents:
diff changeset
  4732
     * {@code List<?  extends Number>}.  Since {@code List<Integer> <:
06bc494ca11e Initial load
duke
parents:
diff changeset
  4733
     * List<? extends Number>} a {@code List<T>} can be cast to {@code
06bc494ca11e Initial load
duke
parents:
diff changeset
  4734
     * List<Integer>} with a warning.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4735
     * @param t a type
06bc494ca11e Initial load
duke
parents:
diff changeset
  4736
     * @param high if true return an upper bound; otherwise a lower
06bc494ca11e Initial load
duke
parents:
diff changeset
  4737
     * bound
06bc494ca11e Initial load
duke
parents:
diff changeset
  4738
     * @param rewriteTypeVars only rewrite captured wildcards if false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4739
     * otherwise rewrite all type variables
06bc494ca11e Initial load
duke
parents:
diff changeset
  4740
     * @return the type rewritten with wildcards (existential
06bc494ca11e Initial load
duke
parents:
diff changeset
  4741
     * quantifiers) only
06bc494ca11e Initial load
duke
parents:
diff changeset
  4742
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  4743
    private Type rewriteQuantifiers(Type t, boolean high, boolean rewriteTypeVars) {
6353
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  4744
        return new Rewriter(high, rewriteTypeVars).visit(t);
1530
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4745
    }
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4746
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4747
    class Rewriter extends UnaryVisitor<Type> {
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4748
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4749
        boolean high;
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4750
        boolean rewriteTypeVars;
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4751
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4752
        Rewriter(boolean high, boolean rewriteTypeVars) {
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4753
            this.high = high;
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4754
            this.rewriteTypeVars = rewriteTypeVars;
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4755
        }
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4756
6353
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  4757
        @Override
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  4758
        public Type visitClassType(ClassType t, Void s) {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21712
diff changeset
  4759
            ListBuffer<Type> rewritten = new ListBuffer<>();
1530
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4760
            boolean changed = false;
6353
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  4761
            for (Type arg : t.allparams()) {
1530
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4762
                Type bound = visit(arg);
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4763
                if (arg != bound) {
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4764
                    changed = true;
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4765
                }
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4766
                rewritten.append(bound);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4767
            }
1530
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4768
            if (changed)
6353
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  4769
                return subst(t.tsym.type,
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  4770
                        t.tsym.type.allparams(),
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  4771
                        rewritten.toList());
1530
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4772
            else
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4773
                return t;
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4774
        }
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4775
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4776
        public Type visitType(Type t, Void s) {
24612
75dc732b45af 8042338: Refactor Types.upperBound to treat wildcards and variables separately
dlsmith
parents: 24604
diff changeset
  4777
            return t;
1530
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4778
        }
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4779
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4780
        @Override
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4781
        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
  4782
            Type w_bound = t.wildcard.type;
cc901a28adc3 7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents: 10810
diff changeset
  4783
            Type bound = w_bound.contains(t) ?
cc901a28adc3 7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents: 10810
diff changeset
  4784
                        erasure(w_bound) :
cc901a28adc3 7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents: 10810
diff changeset
  4785
                        visit(w_bound);
cc901a28adc3 7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents: 10810
diff changeset
  4786
            return rewriteAsWildcardType(visit(bound), t.wildcard.bound, t.wildcard.kind);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4787
        }
1530
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4788
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4789
        @Override
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4790
        public Type visitTypeVar(TypeVar t, Void s) {
6353
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  4791
            if (rewriteTypeVars) {
11549
cc901a28adc3 7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents: 10810
diff changeset
  4792
                Type bound = t.bound.contains(t) ?
7627
de2a86da165e 7005095: Cast: compile reject sensible cast from final class to interface
mcimadamore
parents: 7331
diff changeset
  4793
                        erasure(t.bound) :
11549
cc901a28adc3 7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents: 10810
diff changeset
  4794
                        visit(t.bound);
cc901a28adc3 7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents: 10810
diff changeset
  4795
                return rewriteAsWildcardType(bound, t, EXTENDS);
cc901a28adc3 7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents: 10810
diff changeset
  4796
            } else {
cc901a28adc3 7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents: 10810
diff changeset
  4797
                return t;
6353
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  4798
            }
1530
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4799
        }
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4800
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4801
        @Override
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4802
        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
  4803
            Type bound2 = visit(t.type);
cc901a28adc3 7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents: 10810
diff changeset
  4804
            return t.type == bound2 ? t : rewriteAsWildcardType(bound2, t.bound, t.kind);
6353
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  4805
        }
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  4806
11549
cc901a28adc3 7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents: 10810
diff changeset
  4807
        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
  4808
            switch (bk) {
cc901a28adc3 7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents: 10810
diff changeset
  4809
               case EXTENDS: return high ?
cc901a28adc3 7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents: 10810
diff changeset
  4810
                       makeExtendsWildcard(B(bound), formal) :
cc901a28adc3 7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents: 10810
diff changeset
  4811
                       makeExtendsWildcard(syms.objectType, formal);
cc901a28adc3 7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents: 10810
diff changeset
  4812
               case SUPER: return high ?
cc901a28adc3 7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents: 10810
diff changeset
  4813
                       makeSuperWildcard(syms.botType, formal) :
cc901a28adc3 7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents: 10810
diff changeset
  4814
                       makeSuperWildcard(B(bound), formal);
cc901a28adc3 7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents: 10810
diff changeset
  4815
               case UNBOUND: return makeExtendsWildcard(syms.objectType, formal);
cc901a28adc3 7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents: 10810
diff changeset
  4816
               default:
cc901a28adc3 7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents: 10810
diff changeset
  4817
                   Assert.error("Invalid bound kind " + bk);
cc901a28adc3 7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents: 10810
diff changeset
  4818
                   return null;
cc901a28adc3 7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents: 10810
diff changeset
  4819
            }
6353
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  4820
        }
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  4821
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  4822
        Type B(Type t) {
18646
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18412
diff changeset
  4823
            while (t.hasTag(WILDCARD)) {
24396
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 24293
diff changeset
  4824
                WildcardType w = (WildcardType)t;
6353
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  4825
                t = high ?
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  4826
                    w.getExtendsBound() :
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  4827
                    w.getSuperBound();
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  4828
                if (t == null) {
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  4829
                    t = high ? syms.objectType : syms.botType;
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  4830
                }
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  4831
            }
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  4832
            return t;
1530
0cb573a51f92 6548436: Incorrect inconvertible types error
mcimadamore
parents: 1473
diff changeset
  4833
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4834
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4835
6353
bb5c39054bf3 6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents: 6350
diff changeset
  4836
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4837
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  4838
     * Create a wildcard with the given upper (extends) bound; create
06bc494ca11e Initial load
duke
parents:
diff changeset
  4839
     * an unbounded wildcard if bound is Object.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4840
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  4841
     * @param bound the upper bound
06bc494ca11e Initial load
duke
parents:
diff changeset
  4842
     * @param formal the formal type parameter that will be
06bc494ca11e Initial load
duke
parents:
diff changeset
  4843
     * substituted by the wildcard
06bc494ca11e Initial load
duke
parents:
diff changeset
  4844
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  4845
    private WildcardType makeExtendsWildcard(Type bound, TypeVar formal) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4846
        if (bound == syms.objectType) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4847
            return new WildcardType(syms.objectType,
06bc494ca11e Initial load
duke
parents:
diff changeset
  4848
                                    BoundKind.UNBOUND,
06bc494ca11e Initial load
duke
parents:
diff changeset
  4849
                                    syms.boundClass,
26781
a786b07c7b91 8048614: Add TypeMetadata to contain type annotations and other type information
emc
parents: 26107
diff changeset
  4850
                                    formal);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4851
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4852
            return new WildcardType(bound,
06bc494ca11e Initial load
duke
parents:
diff changeset
  4853
                                    BoundKind.EXTENDS,
06bc494ca11e Initial load
duke
parents:
diff changeset
  4854
                                    syms.boundClass,
26781
a786b07c7b91 8048614: Add TypeMetadata to contain type annotations and other type information
emc
parents: 26107
diff changeset
  4855
                                    formal);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4856
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4857
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4858
06bc494ca11e Initial load
duke
parents:
diff changeset
  4859
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  4860
     * Create a wildcard with the given lower (super) bound; create an
06bc494ca11e Initial load
duke
parents:
diff changeset
  4861
     * unbounded wildcard if bound is bottom (type of {@code null}).
06bc494ca11e Initial load
duke
parents:
diff changeset
  4862
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  4863
     * @param bound the lower bound
06bc494ca11e Initial load
duke
parents:
diff changeset
  4864
     * @param formal the formal type parameter that will be
06bc494ca11e Initial load
duke
parents:
diff changeset
  4865
     * substituted by the wildcard
06bc494ca11e Initial load
duke
parents:
diff changeset
  4866
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  4867
    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
  4868
        if (bound.hasTag(BOT)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4869
            return new WildcardType(syms.objectType,
06bc494ca11e Initial load
duke
parents:
diff changeset
  4870
                                    BoundKind.UNBOUND,
06bc494ca11e Initial load
duke
parents:
diff changeset
  4871
                                    syms.boundClass,
26781
a786b07c7b91 8048614: Add TypeMetadata to contain type annotations and other type information
emc
parents: 26107
diff changeset
  4872
                                    formal);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4873
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4874
            return new WildcardType(bound,
06bc494ca11e Initial load
duke
parents:
diff changeset
  4875
                                    BoundKind.SUPER,
06bc494ca11e Initial load
duke
parents:
diff changeset
  4876
                                    syms.boundClass,
26781
a786b07c7b91 8048614: Add TypeMetadata to contain type annotations and other type information
emc
parents: 26107
diff changeset
  4877
                                    formal);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4878
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4879
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4880
06bc494ca11e Initial load
duke
parents:
diff changeset
  4881
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  4882
     * A wrapper for a type that allows use in sets.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4883
     */
14949
45f43822bbde 8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents: 14801
diff changeset
  4884
    public static class UniqueType {
45f43822bbde 8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents: 14801
diff changeset
  4885
        public final Type type;
45f43822bbde 8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents: 14801
diff changeset
  4886
        final Types types;
45f43822bbde 8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents: 14801
diff changeset
  4887
45f43822bbde 8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents: 14801
diff changeset
  4888
        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
  4889
            this.type = type;
45f43822bbde 8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents: 14801
diff changeset
  4890
            this.types = types;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4891
        }
14949
45f43822bbde 8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents: 14801
diff changeset
  4892
45f43822bbde 8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents: 14801
diff changeset
  4893
        public int hashCode() {
45f43822bbde 8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents: 14801
diff changeset
  4894
            return types.hashCode(type);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4895
        }
14949
45f43822bbde 8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents: 14801
diff changeset
  4896
45f43822bbde 8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents: 14801
diff changeset
  4897
        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
  4898
            return (obj instanceof UniqueType) &&
24396
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 24293
diff changeset
  4899
                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
  4900
        }
45f43822bbde 8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents: 14801
diff changeset
  4901
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4902
        public String toString() {
14949
45f43822bbde 8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents: 14801
diff changeset
  4903
            return type.toString();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4904
        }
14949
45f43822bbde 8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents: 14801
diff changeset
  4905
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4906
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4907
    // </editor-fold>
06bc494ca11e Initial load
duke
parents:
diff changeset
  4908
06bc494ca11e Initial load
duke
parents:
diff changeset
  4909
    // <editor-fold defaultstate="collapsed" desc="Visitors">
06bc494ca11e Initial load
duke
parents:
diff changeset
  4910
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  4911
     * A default visitor for types.  All visitor methods except
06bc494ca11e Initial load
duke
parents:
diff changeset
  4912
     * visitType are implemented by delegating to visitType.  Concrete
06bc494ca11e Initial load
duke
parents:
diff changeset
  4913
     * subclasses must provide an implementation of visitType and can
06bc494ca11e Initial load
duke
parents:
diff changeset
  4914
     * override other methods as needed.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4915
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  4916
     * @param <R> the return type of the operation implemented by this
06bc494ca11e Initial load
duke
parents:
diff changeset
  4917
     * visitor; use Void if no return type is needed.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4918
     * @param <S> the type of the second argument (the first being the
06bc494ca11e Initial load
duke
parents:
diff changeset
  4919
     * type itself) of the operation implemented by this visitor; use
06bc494ca11e Initial load
duke
parents:
diff changeset
  4920
     * Void if a second argument is not needed.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4921
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  4922
    public static abstract class DefaultTypeVisitor<R,S> implements Type.Visitor<R,S> {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4923
        final public R visit(Type t, S s)               { return t.accept(this, s); }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4924
        public R visitClassType(ClassType t, S s)       { return visitType(t, s); }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4925
        public R visitWildcardType(WildcardType t, S s) { return visitType(t, s); }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4926
        public R visitArrayType(ArrayType t, S s)       { return visitType(t, s); }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4927
        public R visitMethodType(MethodType t, S s)     { return visitType(t, s); }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4928
        public R visitPackageType(PackageType t, S s)   { return visitType(t, s); }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34995
diff changeset
  4929
        public R visitModuleType(ModuleType t, S s)     { return visitType(t, s); }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4930
        public R visitTypeVar(TypeVar t, S s)           { return visitType(t, s); }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4931
        public R visitCapturedType(CapturedType t, S s) { return visitType(t, s); }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4932
        public R visitForAll(ForAll t, S s)             { return visitType(t, s); }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4933
        public R visitUndetVar(UndetVar t, S s)         { return visitType(t, s); }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4934
        public R visitErrorType(ErrorType t, S s)       { return visitType(t, s); }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4935
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4936
06bc494ca11e Initial load
duke
parents:
diff changeset
  4937
    /**
1357
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  4938
     * A default visitor for symbols.  All visitor methods except
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  4939
     * visitSymbol are implemented by delegating to visitSymbol.  Concrete
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  4940
     * subclasses must provide an implementation of visitSymbol and can
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  4941
     * override other methods as needed.
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  4942
     *
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  4943
     * @param <R> the return type of the operation implemented by this
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  4944
     * visitor; use Void if no return type is needed.
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  4945
     * @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
  4946
     * symbol itself) of the operation implemented by this visitor; use
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  4947
     * Void if a second argument is not needed.
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  4948
     */
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  4949
    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
  4950
        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
  4951
        public R visitClassSymbol(ClassSymbol s, S arg)         { return visitSymbol(s, arg); }
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  4952
        public R visitMethodSymbol(MethodSymbol s, S arg)       { return visitSymbol(s, arg); }
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  4953
        public R visitOperatorSymbol(OperatorSymbol s, S arg)   { return visitSymbol(s, arg); }
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  4954
        public R visitPackageSymbol(PackageSymbol s, S arg)     { return visitSymbol(s, arg); }
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  4955
        public R visitTypeSymbol(TypeSymbol s, S arg)           { return visitSymbol(s, arg); }
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  4956
        public R visitVarSymbol(VarSymbol s, S arg)             { return visitSymbol(s, arg); }
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  4957
    }
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  4958
e2d4f3e1f805 6735840: Need a type/symbol visitor printer
mcimadamore
parents: 1260
diff changeset
  4959
    /**
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4960
     * A <em>simple</em> visitor for types.  This visitor is simple as
06bc494ca11e Initial load
duke
parents:
diff changeset
  4961
     * captured wildcards, for-all types (generic methods), and
06bc494ca11e Initial load
duke
parents:
diff changeset
  4962
     * undetermined type variables (part of inference) are hidden.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4963
     * Captured wildcards are hidden by treating them as type
06bc494ca11e Initial load
duke
parents:
diff changeset
  4964
     * variables and the rest are hidden by visiting their qtypes.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4965
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  4966
     * @param <R> the return type of the operation implemented by this
06bc494ca11e Initial load
duke
parents:
diff changeset
  4967
     * visitor; use Void if no return type is needed.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4968
     * @param <S> the type of the second argument (the first being the
06bc494ca11e Initial load
duke
parents:
diff changeset
  4969
     * type itself) of the operation implemented by this visitor; use
06bc494ca11e Initial load
duke
parents:
diff changeset
  4970
     * Void if a second argument is not needed.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4971
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  4972
    public static abstract class SimpleVisitor<R,S> extends DefaultTypeVisitor<R,S> {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4973
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  4974
        public R visitCapturedType(CapturedType t, S s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4975
            return visitTypeVar(t, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4976
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4977
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  4978
        public R visitForAll(ForAll t, S s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4979
            return visit(t.qtype, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4980
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4981
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  4982
        public R visitUndetVar(UndetVar t, S s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4983
            return visit(t.qtype, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4984
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4985
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4986
06bc494ca11e Initial load
duke
parents:
diff changeset
  4987
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  4988
     * A plain relation on types.  That is a 2-ary function on the
06bc494ca11e Initial load
duke
parents:
diff changeset
  4989
     * form Type&nbsp;&times;&nbsp;Type&nbsp;&rarr;&nbsp;Boolean.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4990
     * <!-- In plain text: Type x Type -> Boolean -->
06bc494ca11e Initial load
duke
parents:
diff changeset
  4991
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  4992
    public static abstract class TypeRelation extends SimpleVisitor<Boolean,Type> {}
06bc494ca11e Initial load
duke
parents:
diff changeset
  4993
06bc494ca11e Initial load
duke
parents:
diff changeset
  4994
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  4995
     * A convenience visitor for implementing operations that only
06bc494ca11e Initial load
duke
parents:
diff changeset
  4996
     * require one argument (the type itself), that is, unary
06bc494ca11e Initial load
duke
parents:
diff changeset
  4997
     * operations.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4998
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  4999
     * @param <R> the return type of the operation implemented by this
06bc494ca11e Initial load
duke
parents:
diff changeset
  5000
     * visitor; use Void if no return type is needed.
06bc494ca11e Initial load
duke
parents:
diff changeset
  5001
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  5002
    public static abstract class UnaryVisitor<R> extends SimpleVisitor<R,Void> {
06bc494ca11e Initial load
duke
parents:
diff changeset
  5003
        final public R visit(Type t) { return t.accept(this, null); }
06bc494ca11e Initial load
duke
parents:
diff changeset
  5004
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  5005
06bc494ca11e Initial load
duke
parents:
diff changeset
  5006
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  5007
     * A visitor for implementing a mapping from types to types.  The
06bc494ca11e Initial load
duke
parents:
diff changeset
  5008
     * default behavior of this class is to implement the identity
06bc494ca11e Initial load
duke
parents:
diff changeset
  5009
     * mapping (mapping a type to itself).  This can be overridden in
06bc494ca11e Initial load
duke
parents:
diff changeset
  5010
     * subclasses.
06bc494ca11e Initial load
duke
parents:
diff changeset
  5011
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  5012
     * @param <S> the type of the second argument (the first being the
06bc494ca11e Initial load
duke
parents:
diff changeset
  5013
     * type itself) of this mapping; use Void if a second argument is
06bc494ca11e Initial load
duke
parents:
diff changeset
  5014
     * not needed.
06bc494ca11e Initial load
duke
parents:
diff changeset
  5015
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  5016
    public static class MapVisitor<S> extends DefaultTypeVisitor<Type,S> {
06bc494ca11e Initial load
duke
parents:
diff changeset
  5017
        final public Type visit(Type t) { return t.accept(this, null); }
06bc494ca11e Initial load
duke
parents:
diff changeset
  5018
        public Type visitType(Type t, S s) { return t; }
06bc494ca11e Initial load
duke
parents:
diff changeset
  5019
    }
44186
fe848a208b20 8176265: Method overload resolution on a covariant base type doesn't work in 9
mcimadamore
parents: 43025
diff changeset
  5020
fe848a208b20 8176265: Method overload resolution on a covariant base type doesn't work in 9
mcimadamore
parents: 43025
diff changeset
  5021
    /**
fe848a208b20 8176265: Method overload resolution on a covariant base type doesn't work in 9
mcimadamore
parents: 43025
diff changeset
  5022
     * An abstract class for mappings from types to types (see {@link Type#map(TypeMapping)}.
fe848a208b20 8176265: Method overload resolution on a covariant base type doesn't work in 9
mcimadamore
parents: 43025
diff changeset
  5023
     * This class implements the functional interface {@code Function}, that allows it to be used
fe848a208b20 8176265: Method overload resolution on a covariant base type doesn't work in 9
mcimadamore
parents: 43025
diff changeset
  5024
     * fluently in stream-like processing.
fe848a208b20 8176265: Method overload resolution on a covariant base type doesn't work in 9
mcimadamore
parents: 43025
diff changeset
  5025
     */
fe848a208b20 8176265: Method overload resolution on a covariant base type doesn't work in 9
mcimadamore
parents: 43025
diff changeset
  5026
    public static class TypeMapping<S> extends MapVisitor<S> implements Function<Type, Type> {
fe848a208b20 8176265: Method overload resolution on a covariant base type doesn't work in 9
mcimadamore
parents: 43025
diff changeset
  5027
        @Override
fe848a208b20 8176265: Method overload resolution on a covariant base type doesn't work in 9
mcimadamore
parents: 43025
diff changeset
  5028
        public Type apply(Type type) { return visit(type); }
fe848a208b20 8176265: Method overload resolution on a covariant base type doesn't work in 9
mcimadamore
parents: 43025
diff changeset
  5029
fe848a208b20 8176265: Method overload resolution on a covariant base type doesn't work in 9
mcimadamore
parents: 43025
diff changeset
  5030
        List<Type> visit(List<Type> ts, S s) {
fe848a208b20 8176265: Method overload resolution on a covariant base type doesn't work in 9
mcimadamore
parents: 43025
diff changeset
  5031
            return ts.map(t -> visit(t, s));
fe848a208b20 8176265: Method overload resolution on a covariant base type doesn't work in 9
mcimadamore
parents: 43025
diff changeset
  5032
        }
fe848a208b20 8176265: Method overload resolution on a covariant base type doesn't work in 9
mcimadamore
parents: 43025
diff changeset
  5033
fe848a208b20 8176265: Method overload resolution on a covariant base type doesn't work in 9
mcimadamore
parents: 43025
diff changeset
  5034
        @Override
fe848a208b20 8176265: Method overload resolution on a covariant base type doesn't work in 9
mcimadamore
parents: 43025
diff changeset
  5035
        public Type visitCapturedType(CapturedType t, S s) {
fe848a208b20 8176265: Method overload resolution on a covariant base type doesn't work in 9
mcimadamore
parents: 43025
diff changeset
  5036
            return visitTypeVar(t, s);
fe848a208b20 8176265: Method overload resolution on a covariant base type doesn't work in 9
mcimadamore
parents: 43025
diff changeset
  5037
        }
fe848a208b20 8176265: Method overload resolution on a covariant base type doesn't work in 9
mcimadamore
parents: 43025
diff changeset
  5038
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  5039
    // </editor-fold>
6575
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents: 6353
diff changeset
  5040
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents: 6353
diff changeset
  5041
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents: 6353
diff changeset
  5042
    // <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
  5043
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents: 6353
diff changeset
  5044
    public RetentionPolicy getRetention(Attribute.Compound a) {
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 13633
diff changeset
  5045
        return getRetention(a.type.tsym);
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 13633
diff changeset
  5046
    }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 13633
diff changeset
  5047
29842
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29555
diff changeset
  5048
    public RetentionPolicy getRetention(TypeSymbol sym) {
6575
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents: 6353
diff changeset
  5049
        RetentionPolicy vis = RetentionPolicy.CLASS; // the default
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 13633
diff changeset
  5050
        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
  5051
        if (c != null) {
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents: 6353
diff changeset
  5052
            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
  5053
            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
  5054
                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
  5055
                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
  5056
                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
  5057
                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
  5058
                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
  5059
            }
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents: 6353
diff changeset
  5060
        }
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents: 6353
diff changeset
  5061
        return vis;
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents: 6353
diff changeset
  5062
    }
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents: 6353
diff changeset
  5063
    // </editor-fold>
16300
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5064
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5065
    // <editor-fold defaultstate="collapsed" desc="Signature Generation">
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5066
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5067
    public static abstract class SignatureGenerator {
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5068
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5069
        private final Types types;
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5070
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5071
        protected abstract void append(char ch);
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5072
        protected abstract void append(byte[] ba);
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5073
        protected abstract void append(Name name);
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5074
        protected void classReference(ClassSymbol c) { /* by default: no-op */ }
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5075
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5076
        protected SignatureGenerator(Types types) {
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5077
            this.types = types;
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5078
        }
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5079
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5080
        /**
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5081
         * Assemble signature of given type in string buffer.
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5082
         */
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5083
        public void assembleSig(Type type) {
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5084
            switch (type.getTag()) {
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5085
                case BYTE:
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5086
                    append('B');
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5087
                    break;
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5088
                case SHORT:
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5089
                    append('S');
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5090
                    break;
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5091
                case CHAR:
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5092
                    append('C');
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5093
                    break;
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5094
                case INT:
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5095
                    append('I');
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5096
                    break;
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5097
                case LONG:
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5098
                    append('J');
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5099
                    break;
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5100
                case FLOAT:
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5101
                    append('F');
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5102
                    break;
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5103
                case DOUBLE:
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5104
                    append('D');
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5105
                    break;
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5106
                case BOOLEAN:
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5107
                    append('Z');
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5108
                    break;
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5109
                case VOID:
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5110
                    append('V');
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5111
                    break;
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5112
                case CLASS:
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5113
                    append('L');
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5114
                    assembleClassSig(type);
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5115
                    append(';');
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5116
                    break;
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5117
                case ARRAY:
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5118
                    ArrayType at = (ArrayType) type;
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5119
                    append('[');
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5120
                    assembleSig(at.elemtype);
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5121
                    break;
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5122
                case METHOD:
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5123
                    MethodType mt = (MethodType) type;
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5124
                    append('(');
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5125
                    assembleSig(mt.argtypes);
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5126
                    append(')');
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5127
                    assembleSig(mt.restype);
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5128
                    if (hasTypeVar(mt.thrown)) {
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5129
                        for (List<Type> l = mt.thrown; l.nonEmpty(); l = l.tail) {
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5130
                            append('^');
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5131
                            assembleSig(l.head);
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5132
                        }
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5133
                    }
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5134
                    break;
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5135
                case WILDCARD: {
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5136
                    Type.WildcardType ta = (Type.WildcardType) type;
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5137
                    switch (ta.kind) {
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5138
                        case SUPER:
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5139
                            append('-');
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5140
                            assembleSig(ta.type);
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5141
                            break;
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5142
                        case EXTENDS:
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5143
                            append('+');
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5144
                            assembleSig(ta.type);
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5145
                            break;
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5146
                        case UNBOUND:
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5147
                            append('*');
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5148
                            break;
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5149
                        default:
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5150
                            throw new AssertionError(ta.kind);
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5151
                    }
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5152
                    break;
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5153
                }
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5154
                case TYPEVAR:
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5155
                    append('T');
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5156
                    append(type.tsym.name);
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5157
                    append(';');
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5158
                    break;
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5159
                case FORALL:
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5160
                    Type.ForAll ft = (Type.ForAll) type;
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5161
                    assembleParamsSig(ft.tvars);
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5162
                    assembleSig(ft.qtype);
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5163
                    break;
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5164
                default:
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5165
                    throw new AssertionError("typeSig " + type.getTag());
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5166
            }
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5167
        }
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5168
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5169
        public boolean hasTypeVar(List<Type> l) {
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5170
            while (l.nonEmpty()) {
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5171
                if (l.head.hasTag(TypeTag.TYPEVAR)) {
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5172
                    return true;
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5173
                }
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5174
                l = l.tail;
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5175
            }
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5176
            return false;
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5177
        }
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5178
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5179
        public void assembleClassSig(Type type) {
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5180
            ClassType ct = (ClassType) type;
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5181
            ClassSymbol c = (ClassSymbol) ct.tsym;
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5182
            classReference(c);
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5183
            Type outer = ct.getEnclosingType();
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5184
            if (outer.allparams().nonEmpty()) {
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5185
                boolean rawOuter =
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 27125
diff changeset
  5186
                        c.owner.kind == MTH || // either a local class
16300
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5187
                        c.name == types.names.empty; // or anonymous
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5188
                assembleClassSig(rawOuter
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5189
                        ? types.erasure(outer)
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5190
                        : outer);
24647
4435712075de 8037934: Javac generates invalid signatures for local types
pgovereau
parents: 24612
diff changeset
  5191
                append(rawOuter ? '$' : '.');
16300
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5192
                Assert.check(c.flatname.startsWith(c.owner.enclClass().flatname));
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5193
                append(rawOuter
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5194
                        ? c.flatname.subName(c.owner.enclClass().flatname.getByteLength() + 1, c.flatname.getByteLength())
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5195
                        : c.name);
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5196
            } else {
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5197
                append(externalize(c.flatname));
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5198
            }
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5199
            if (ct.getTypeArguments().nonEmpty()) {
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5200
                append('<');
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5201
                assembleSig(ct.getTypeArguments());
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5202
                append('>');
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5203
            }
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5204
        }
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5205
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5206
        public void assembleParamsSig(List<Type> typarams) {
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5207
            append('<');
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5208
            for (List<Type> ts = typarams; ts.nonEmpty(); ts = ts.tail) {
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5209
                Type.TypeVar tvar = (Type.TypeVar) ts.head;
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5210
                append(tvar.tsym.name);
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5211
                List<Type> bounds = types.getBounds(tvar);
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5212
                if ((bounds.head.tsym.flags() & INTERFACE) != 0) {
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5213
                    append(':');
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5214
                }
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5215
                for (List<Type> l = bounds; l.nonEmpty(); l = l.tail) {
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5216
                    append(':');
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5217
                    assembleSig(l.head);
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5218
                }
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5219
            }
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5220
            append('>');
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5221
        }
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5222
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5223
        private void assembleSig(List<Type> types) {
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5224
            for (List<Type> ts = types; ts.nonEmpty(); ts = ts.tail) {
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5225
                assembleSig(ts.head);
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5226
            }
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5227
        }
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5228
    }
7cf27559c8df 8004969: Generate $deserializeLambda$ method
rfield
parents: 16295
diff changeset
  5229
    // </editor-fold>
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23399
diff changeset
  5230
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23399
diff changeset
  5231
    public void newRound() {
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23399
diff changeset
  5232
        descCache._map.clear();
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23399
diff changeset
  5233
        isDerivedRawCache.clear();
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23399
diff changeset
  5234
        implCache._map.clear();
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23399
diff changeset
  5235
        membersCache._map.clear();
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23399
diff changeset
  5236
        closureCache.clear();
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23399
diff changeset
  5237
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  5238
}