langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java
author mcimadamore
Tue, 06 Jan 2015 15:46:41 +0000
changeset 28331 43ed6b2e0e3b
parent 27849 df6cd9799fcd
child 28336 9a6ae3c3efb5
permissions -rw-r--r--
8067883: Javac misses some opportunities for diagnostic simplification Summary: Javac always report full inference diagnostic, even when message does not mention inference variables. Reviewed-by: jlahoda
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
22440
d40c30326317 8028389: NullPointerException compiling annotation values that have bodies
alundblad
parents: 22167
diff changeset
     2
 * Copyright (c) 1999, 2014, 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: 5489
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: 5489
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: 5489
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5489
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5489
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.comp;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
21718
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
    28
import com.sun.source.tree.MemberReferenceTree.ReferenceMode;
10816
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
    29
import com.sun.tools.javac.api.Formattable.LocalizedString;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import com.sun.tools.javac.code.*;
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
    31
import com.sun.tools.javac.code.Scope.WriteableScope;
14052
8b839ae9074b 8000233: Fix issues in recent push
mcimadamore
parents: 14051
diff changeset
    32
import com.sun.tools.javac.code.Symbol.*;
10816
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
    33
import com.sun.tools.javac.code.Type.*;
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
    34
import com.sun.tools.javac.comp.Attr.ResultInfo;
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
    35
import com.sun.tools.javac.comp.Check.CheckContext;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
    36
import com.sun.tools.javac.comp.DeferredAttr.AttrMode;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
    37
import com.sun.tools.javac.comp.DeferredAttr.DeferredAttrContext;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
    38
import com.sun.tools.javac.comp.DeferredAttr.DeferredType;
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 14045
diff changeset
    39
import com.sun.tools.javac.comp.Infer.InferenceContext;
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
    40
import com.sun.tools.javac.comp.Infer.FreeTypeListener;
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
    41
import com.sun.tools.javac.comp.Resolve.MethodResolutionContext.Candidate;
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
    42
import com.sun.tools.javac.comp.Resolve.MethodResolutionDiagHelper.Template;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
import com.sun.tools.javac.jvm.*;
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
    44
import com.sun.tools.javac.main.Option;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
import com.sun.tools.javac.tree.*;
10816
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
    46
import com.sun.tools.javac.tree.JCTree.*;
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
    47
import com.sun.tools.javac.tree.JCTree.JCMemberReference.ReferenceKind;
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
    48
import com.sun.tools.javac.tree.JCTree.JCPolyExpression.*;
10816
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
    49
import com.sun.tools.javac.util.*;
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
    50
import com.sun.tools.javac.util.DefinedBy.Api;
10816
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
    51
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
    52
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
    53
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticType;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
10816
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
    55
import java.util.Arrays;
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
    56
import java.util.Collection;
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
    57
import java.util.EnumSet;
14052
8b839ae9074b 8000233: Fix issues in recent push
mcimadamore
parents: 14051
diff changeset
    58
import java.util.Iterator;
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
    59
import java.util.LinkedHashMap;
10816
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
    60
import java.util.Map;
28331
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
    61
import java.util.function.BiPredicate;
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
    62
import java.util.stream.Stream;
10816
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
    63
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
    64
import javax.lang.model.element.ElementVisitor;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
import static com.sun.tools.javac.code.Flags.*;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10816
diff changeset
    67
import static com.sun.tools.javac.code.Flags.BLOCK;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
import static com.sun.tools.javac.code.Kinds.*;
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
    69
import static com.sun.tools.javac.code.Kinds.Kind.*;
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14062
diff changeset
    70
import static com.sun.tools.javac.code.TypeTag.*;
10816
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
    71
import static com.sun.tools.javac.comp.Resolve.MethodResolutionPhase.*;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10816
diff changeset
    72
import static com.sun.tools.javac.tree.JCTree.Tag.*;
1533
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
    73
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
/** Helper class for name resolution, used mostly by the attribution phase.
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5846
diff changeset
    76
 *  <p><b>This is NOT part of any supported API.
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5846
diff changeset
    77
 *  If you write code that depends on this, you do so at your own risk.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
 *  This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
 *  deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
public class Resolve {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22161
diff changeset
    82
    protected static final Context.Key<Resolve> resolveKey = new Context.Key<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 1257
diff changeset
    84
    Names names;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
    Log log;
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
    Symtab syms;
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
    87
    Attr attr;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
    88
    DeferredAttr deferredAttr;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
    Check chk;
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
    Infer infer;
24604
7f68545b5128 8041422: Split javac ClassReader into ClassReader+ClassFinder
jjg
parents: 24404
diff changeset
    91
    ClassFinder finder;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
    TreeInfo treeinfo;
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
    Types types;
1040
c0f5acfd9d15 6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents: 939
diff changeset
    94
    JCDiagnostic.Factory diags;
6592
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6356
diff changeset
    95
    public final boolean allowMethodHandles;
24404
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
    96
    public final boolean allowFunctionalInterfaceMostSpecific;
25435
928866794984 8049075: javac, wildcards and generic vararg method invocation not accepted
vromero
parents: 25279
diff changeset
    97
    public final boolean checkVarargsAccessAfterResolution;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
    private final boolean debugResolve;
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
    99
    private final boolean compactMethodDiags;
10816
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   100
    final EnumSet<VerboseResolutionMode> verboseResolutionMode;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
   102
    WriteableScope polymorphicSignatureScope;
6592
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6356
diff changeset
   103
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   104
    protected Resolve(Context context) {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   105
        context.put(resolveKey, this);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   106
        syms = Symtab.instance(context);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   107
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   108
        varNotFound = new
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   109
            SymbolNotFoundError(ABSENT_VAR);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   110
        methodNotFound = new
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   111
            SymbolNotFoundError(ABSENT_MTH);
21718
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
   112
        methodWithCorrectStaticnessNotFound = new
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
   113
            SymbolNotFoundError(WRONG_STATICNESS,
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
   114
                "method found has incorrect staticness");
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   115
        typeNotFound = new
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   116
            SymbolNotFoundError(ABSENT_TYP);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   117
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   118
        names = Names.instance(context);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   119
        log = Log.instance(context);
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   120
        attr = Attr.instance(context);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
   121
        deferredAttr = DeferredAttr.instance(context);
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   122
        chk = Check.instance(context);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   123
        infer = Infer.instance(context);
24604
7f68545b5128 8041422: Split javac ClassReader into ClassReader+ClassFinder
jjg
parents: 24404
diff changeset
   124
        finder = ClassFinder.instance(context);
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   125
        treeinfo = TreeInfo.instance(context);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   126
        types = Types.instance(context);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   127
        diags = JCDiagnostic.Factory.instance(context);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   128
        Source source = Source.instance(context);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   129
        Options options = Options.instance(context);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   130
        debugResolve = options.isSet("debugresolve");
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
   131
        compactMethodDiags = options.isSet(Option.XDIAGS, "compact") ||
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
   132
                options.isUnset(Option.XDIAGS) && options.isUnset("rawDiagnostics");
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   133
        verboseResolutionMode = VerboseResolutionMode.getVerboseResolutionMode(options);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   134
        Target target = Target.instance(context);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   135
        allowMethodHandles = target.hasMethodHandles();
24404
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
   136
        allowFunctionalInterfaceMostSpecific = source.allowFunctionalInterfaceMostSpecific();
25435
928866794984 8049075: javac, wildcards and generic vararg method invocation not accepted
vromero
parents: 25279
diff changeset
   137
        checkVarargsAccessAfterResolution =
25008
969bfaae5bce 8036953: Fix timing of varargs access check, per JDK-8016205
vromero
parents: 24612
diff changeset
   138
                source.allowPostApplicabilityVarargsAccessCheck();
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
   139
        polymorphicSignatureScope = WriteableScope.create(syms.noSymbol);
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   140
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   141
        inapplicableMethodException = new InapplicableMethodException(diags);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   142
    }
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   143
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   144
    /** error symbols, which are returned when resolution fails
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   145
     */
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   146
    private final SymbolNotFoundError varNotFound;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   147
    private final SymbolNotFoundError methodNotFound;
21718
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
   148
    private final SymbolNotFoundError methodWithCorrectStaticnessNotFound;
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   149
    private final SymbolNotFoundError typeNotFound;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   150
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   151
    public static Resolve instance(Context context) {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   152
        Resolve instance = context.get(resolveKey);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   153
        if (instance == null)
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   154
            instance = new Resolve(context);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   155
        return instance;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   156
    }
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   157
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
   158
    private static Symbol bestOf(Symbol s1,
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
   159
                                 Symbol s2) {
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
   160
        return s1.kind.betterThan(s2.kind) ? s1 : s2;
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
   161
    }
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
   162
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   163
    // <editor-fold defaultstate="collapsed" desc="Verbose resolution diagnostics support">
10816
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   164
    enum VerboseResolutionMode {
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   165
        SUCCESS("success"),
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   166
        FAILURE("failure"),
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   167
        APPLICABLE("applicable"),
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   168
        INAPPLICABLE("inapplicable"),
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   169
        DEFERRED_INST("deferred-inference"),
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   170
        PREDEF("predef"),
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   171
        OBJECT_INIT("object-init"),
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   172
        INTERNAL("internal");
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   173
14801
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents: 14724
diff changeset
   174
        final String opt;
10816
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   175
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   176
        private VerboseResolutionMode(String opt) {
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   177
            this.opt = opt;
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   178
        }
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   179
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   180
        static EnumSet<VerboseResolutionMode> getVerboseResolutionMode(Options opts) {
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   181
            String s = opts.get("verboseResolution");
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   182
            EnumSet<VerboseResolutionMode> res = EnumSet.noneOf(VerboseResolutionMode.class);
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   183
            if (s == null) return res;
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   184
            if (s.contains("all")) {
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   185
                res = EnumSet.allOf(VerboseResolutionMode.class);
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   186
            }
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   187
            Collection<String> args = Arrays.asList(s.split(","));
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   188
            for (VerboseResolutionMode mode : values()) {
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   189
                if (args.contains(mode.opt)) {
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   190
                    res.add(mode);
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   191
                } else if (args.contains("-" + mode.opt)) {
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   192
                    res.remove(mode);
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   193
                }
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   194
            }
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   195
            return res;
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   196
        }
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   197
    }
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   198
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   199
    void reportVerboseResolutionDiagnostic(DiagnosticPosition dpos, Name name, Type site,
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   200
            List<Type> argtypes, List<Type> typeargtypes, Symbol bestSoFar) {
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
   201
        boolean success = !bestSoFar.kind.isOverloadError();
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   202
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   203
        if (success && !verboseResolutionMode.contains(VerboseResolutionMode.SUCCESS)) {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   204
            return;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   205
        } else if (!success && !verboseResolutionMode.contains(VerboseResolutionMode.FAILURE)) {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   206
            return;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   207
        }
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   208
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   209
        if (bestSoFar.name == names.init &&
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   210
                bestSoFar.owner == syms.objectType.tsym &&
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   211
                !verboseResolutionMode.contains(VerboseResolutionMode.OBJECT_INIT)) {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   212
            return; //skip diags for Object constructor resolution
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   213
        } else if (site == syms.predefClass.type &&
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   214
                !verboseResolutionMode.contains(VerboseResolutionMode.PREDEF)) {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   215
            return; //skip spurious diags for predef symbols (i.e. operators)
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   216
        } else if (currentResolutionContext.internalResolution &&
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   217
                !verboseResolutionMode.contains(VerboseResolutionMode.INTERNAL)) {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   218
            return;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   219
        }
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   220
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   221
        int pos = 0;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   222
        int mostSpecificPos = -1;
20249
93f8eae31092 6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents: 19940
diff changeset
   223
        ListBuffer<JCDiagnostic> subDiags = new ListBuffer<>();
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   224
        for (Candidate c : currentResolutionContext.candidates) {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   225
            if (currentResolutionContext.step != c.step ||
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   226
                    (c.isApplicable() && !verboseResolutionMode.contains(VerboseResolutionMode.APPLICABLE)) ||
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   227
                    (!c.isApplicable() && !verboseResolutionMode.contains(VerboseResolutionMode.INAPPLICABLE))) {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   228
                continue;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   229
            } else {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   230
                subDiags.append(c.isApplicable() ?
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   231
                        getVerboseApplicableCandidateDiag(pos, c.sym, c.mtype) :
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   232
                        getVerboseInapplicableCandidateDiag(pos, c.sym, c.details));
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   233
                if (c.sym == bestSoFar)
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   234
                    mostSpecificPos = pos;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   235
                pos++;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   236
            }
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   237
        }
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   238
        String key = success ? "verbose.resolve.multi" : "verbose.resolve.multi.1";
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
   239
        List<Type> argtypes2 = Type.map(argtypes,
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
   240
                    deferredAttr.new RecoveryDeferredTypeMap(AttrMode.SPECULATIVE, bestSoFar, currentResolutionContext.step));
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   241
        JCDiagnostic main = diags.note(log.currentSource(), dpos, key, name,
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   242
                site.tsym, mostSpecificPos, currentResolutionContext.step,
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
   243
                methodArguments(argtypes2),
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
   244
                methodArguments(typeargtypes));
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   245
        JCDiagnostic d = new JCDiagnostic.MultilineDiagnostic(main, subDiags.toList());
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   246
        log.report(d);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   249
    JCDiagnostic getVerboseApplicableCandidateDiag(int pos, Symbol sym, Type inst) {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   250
        JCDiagnostic subDiag = null;
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14062
diff changeset
   251
        if (sym.type.hasTag(FORALL)) {
12915
28cf1e0dafdc 7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents: 12335
diff changeset
   252
            subDiag = diags.fragment("partial.inst.sig", inst);
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   253
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   255
        String key = subDiag == null ?
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   256
                "applicable.method.found" :
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   257
                "applicable.method.found.1";
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
   258
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   259
        return diags.fragment(key, pos, sym, subDiag);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   262
    JCDiagnostic getVerboseInapplicableCandidateDiag(int pos, Symbol sym, JCDiagnostic subDiag) {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   263
        return diags.fragment("not.applicable.method.found", pos, sym, subDiag);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   264
    }
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   265
    // </editor-fold>
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
/* ************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
 * Identifier resolution
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
 *************************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
    /** An environment is "static" if its static level is greater than
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
     *  the one of its outer environment
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
     */
14056
0ea78d6e0b7b 7198582: (java) Minor refactor of JavacParser
ksrini
parents: 14052
diff changeset
   274
    protected static boolean isStatic(Env<AttrContext> env) {
22440
d40c30326317 8028389: NullPointerException compiling annotation values that have bodies
alundblad
parents: 22167
diff changeset
   275
        return env.outer != null && env.info.staticLevel > env.outer.info.staticLevel;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
    /** An environment is an "initializer" if it is a constructor or
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
     *  an instance initializer.
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
    static boolean isInitializer(Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
        Symbol owner = env.info.scope.owner;
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
        return owner.isConstructor() ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
            owner.owner.kind == TYP &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
            (owner.kind == VAR ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
             owner.kind == MTH && (owner.flags() & BLOCK) != 0) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
            (owner.flags() & STATIC) == 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
    /** Is class accessible in given evironment?
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
     *  @param env    The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
     *  @param c      The class whose accessibility is checked.
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
    public boolean isAccessible(Env<AttrContext> env, TypeSymbol c) {
7209
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   295
        return isAccessible(env, c, false);
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   296
    }
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   297
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   298
    public boolean isAccessible(Env<AttrContext> env, TypeSymbol c, boolean checkInner) {
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   299
        boolean isAccessible = false;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
        switch ((short)(c.flags() & AccessFlags)) {
7209
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   301
            case PRIVATE:
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   302
                isAccessible =
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   303
                    env.enclClass.sym.outermostClass() ==
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   304
                    c.owner.outermostClass();
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   305
                break;
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   306
            case 0:
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   307
                isAccessible =
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   308
                    env.toplevel.packge == c.owner // fast special case
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   309
                    ||
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   310
                    env.toplevel.packge == c.packge()
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   311
                    ||
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   312
                    // Hack: this case is added since synthesized default constructors
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   313
                    // of anonymous classes should be allowed to access
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   314
                    // classes which would be inaccessible otherwise.
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   315
                    env.enclMethod != null &&
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   316
                    (env.enclMethod.mods.flags & ANONCONSTR) != 0;
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   317
                break;
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   318
            default: // error recovery
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   319
            case PUBLIC:
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   320
                isAccessible = true;
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   321
                break;
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   322
            case PROTECTED:
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   323
                isAccessible =
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   324
                    env.toplevel.packge == c.owner // fast special case
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   325
                    ||
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   326
                    env.toplevel.packge == c.packge()
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   327
                    ||
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   328
                    isInnerSubClass(env.enclClass.sym, c.owner);
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   329
                break;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
        }
7209
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   331
        return (checkInner == false || c.type.getEnclosingType() == Type.noType) ?
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   332
            isAccessible :
7637
467bc8fc514e 6990134: minor (but red) findbugs warnings
jjg
parents: 7635
diff changeset
   333
            isAccessible && isAccessible(env, c.type.getEnclosingType(), checkInner);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
    //where
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
        /** Is given class a subclass of given base class, or an inner class
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
         *  of a subclass?
06bc494ca11e Initial load
duke
parents:
diff changeset
   338
         *  Return null if no such class exists.
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
         *  @param c     The class which is the subclass or is contained in it.
06bc494ca11e Initial load
duke
parents:
diff changeset
   340
         *  @param base  The base class
06bc494ca11e Initial load
duke
parents:
diff changeset
   341
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   342
        private boolean isInnerSubClass(ClassSymbol c, Symbol base) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
            while (c != null && !c.isSubClass(base, types)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
                c = c.owner.enclClass();
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
            return c != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   347
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   348
06bc494ca11e Initial load
duke
parents:
diff changeset
   349
    boolean isAccessible(Env<AttrContext> env, Type t) {
7209
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   350
        return isAccessible(env, t, false);
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   351
    }
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   352
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   353
    boolean isAccessible(Env<AttrContext> env, Type t, boolean checkInner) {
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14062
diff changeset
   354
        return (t.hasTag(ARRAY))
24612
75dc732b45af 8042338: Refactor Types.upperBound to treat wildcards and variables separately
dlsmith
parents: 24604
diff changeset
   355
            ? isAccessible(env, types.cvarUpperBound(types.elemtype(t)))
7209
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   356
            : isAccessible(env, t.tsym, checkInner);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   357
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   358
17998
02a5abb2ae2f 7179353: try-with-resources fails to compile with generic exception parameters
vromero
parents: 17804
diff changeset
   359
    /** Is symbol accessible as a member of given type in given environment?
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   360
     *  @param env    The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
   361
     *  @param site   The type of which the tested symbol is regarded
06bc494ca11e Initial load
duke
parents:
diff changeset
   362
     *                as a member.
06bc494ca11e Initial load
duke
parents:
diff changeset
   363
     *  @param sym    The symbol.
06bc494ca11e Initial load
duke
parents:
diff changeset
   364
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   365
    public boolean isAccessible(Env<AttrContext> env, Type site, Symbol sym) {
7209
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   366
        return isAccessible(env, site, sym, false);
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   367
    }
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   368
    public boolean isAccessible(Env<AttrContext> env, Type site, Symbol sym, boolean checkInner) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   369
        if (sym.name == names.init && sym.owner != site.tsym) return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   370
        switch ((short)(sym.flags() & AccessFlags)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   371
        case PRIVATE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   372
            return
06bc494ca11e Initial load
duke
parents:
diff changeset
   373
                (env.enclClass.sym == sym.owner // fast special case
06bc494ca11e Initial load
duke
parents:
diff changeset
   374
                 ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   375
                 env.enclClass.sym.outermostClass() ==
06bc494ca11e Initial load
duke
parents:
diff changeset
   376
                 sym.owner.outermostClass())
06bc494ca11e Initial load
duke
parents:
diff changeset
   377
                &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   378
                sym.isInheritedIn(site.tsym, types);
06bc494ca11e Initial load
duke
parents:
diff changeset
   379
        case 0:
06bc494ca11e Initial load
duke
parents:
diff changeset
   380
            return
06bc494ca11e Initial load
duke
parents:
diff changeset
   381
                (env.toplevel.packge == sym.owner.owner // fast special case
06bc494ca11e Initial load
duke
parents:
diff changeset
   382
                 ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   383
                 env.toplevel.packge == sym.packge())
06bc494ca11e Initial load
duke
parents:
diff changeset
   384
                &&
7209
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   385
                isAccessible(env, site, checkInner)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   386
                &&
2511
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   387
                sym.isInheritedIn(site.tsym, types)
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   388
                &&
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   389
                notOverriddenIn(site, sym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   390
        case PROTECTED:
06bc494ca11e Initial load
duke
parents:
diff changeset
   391
            return
06bc494ca11e Initial load
duke
parents:
diff changeset
   392
                (env.toplevel.packge == sym.owner.owner // fast special case
06bc494ca11e Initial load
duke
parents:
diff changeset
   393
                 ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   394
                 env.toplevel.packge == sym.packge()
06bc494ca11e Initial load
duke
parents:
diff changeset
   395
                 ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   396
                 isProtectedAccessible(sym, env.enclClass.sym, site)
06bc494ca11e Initial load
duke
parents:
diff changeset
   397
                 ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   398
                 // OK to select instance method or field from 'super' or type name
06bc494ca11e Initial load
duke
parents:
diff changeset
   399
                 // (but type names should be disallowed elsewhere!)
06bc494ca11e Initial load
duke
parents:
diff changeset
   400
                 env.info.selectSuper && (sym.flags() & STATIC) == 0 && sym.kind != TYP)
06bc494ca11e Initial load
duke
parents:
diff changeset
   401
                &&
7209
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   402
                isAccessible(env, site, checkInner)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   403
                &&
2511
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   404
                notOverriddenIn(site, sym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   405
        default: // this case includes erroneous combinations as well
7209
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   406
            return isAccessible(env, site, checkInner) && notOverriddenIn(site, sym);
2511
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   407
        }
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   408
    }
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   409
    //where
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   410
    /* `sym' is accessible only if not overridden by
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   411
     * another symbol which is a member of `site'
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   412
     * (because, if it is overridden, `sym' is not strictly
6592
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6356
diff changeset
   413
     * speaking a member of `site'). A polymorphic signature method
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6356
diff changeset
   414
     * cannot be overridden (e.g. MH.invokeExact(Object[])).
2511
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   415
     */
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   416
    private boolean notOverriddenIn(Type site, Symbol sym) {
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   417
        if (sym.kind != MTH || sym.isConstructor() || sym.isStatic())
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   418
            return true;
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   419
        else {
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   420
            Symbol s2 = ((MethodSymbol)sym).implementation(site.tsym, types, true);
8228
4e6ee38974b2 6910550: javac 1.5.0_17 fails with incorrect error message
mcimadamore
parents: 8045
diff changeset
   421
            return (s2 == null || s2 == sym || sym.owner == s2.owner ||
3372
544ec1ab333c 6846972: cannot access member of raw type when erasure change overriding into overloading
mcimadamore
parents: 3143
diff changeset
   422
                    !types.isSubSignature(types.memberType(site, s2), types.memberType(site, sym)));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   423
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   424
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   425
    //where
06bc494ca11e Initial load
duke
parents:
diff changeset
   426
        /** Is given protected symbol accessible if it is selected from given site
06bc494ca11e Initial load
duke
parents:
diff changeset
   427
         *  and the selection takes place in given class?
06bc494ca11e Initial load
duke
parents:
diff changeset
   428
         *  @param sym     The symbol with protected access
06bc494ca11e Initial load
duke
parents:
diff changeset
   429
         *  @param c       The class where the access takes place
06bc494ca11e Initial load
duke
parents:
diff changeset
   430
         *  @site          The type of the qualifier
06bc494ca11e Initial load
duke
parents:
diff changeset
   431
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   432
        private
06bc494ca11e Initial load
duke
parents:
diff changeset
   433
        boolean isProtectedAccessible(Symbol sym, ClassSymbol c, Type site) {
21024
e1efbecefefa 8025816: javac crash with method reference with a type variable as the site
vromero
parents: 20249
diff changeset
   434
            Type newSite = site.hasTag(TYPEVAR) ? site.getUpperBound() : site;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   435
            while (c != null &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   436
                   !(c.isSubClass(sym.owner, types) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   437
                     (c.flags() & INTERFACE) == 0 &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   438
                     // In JLS 2e 6.6.2.1, the subclass restriction applies
06bc494ca11e Initial load
duke
parents:
diff changeset
   439
                     // only to instance fields and methods -- types are excluded
06bc494ca11e Initial load
duke
parents:
diff changeset
   440
                     // regardless of whether they are declared 'static' or not.
21024
e1efbecefefa 8025816: javac crash with method reference with a type variable as the site
vromero
parents: 20249
diff changeset
   441
                     ((sym.flags() & STATIC) != 0 || sym.kind == TYP || newSite.tsym.isSubClass(c, types))))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   442
                c = c.owner.enclClass();
06bc494ca11e Initial load
duke
parents:
diff changeset
   443
            return c != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   444
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   445
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   446
    /**
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   447
     * Performs a recursive scan of a type looking for accessibility problems
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   448
     * from current attribution environment
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   449
     */
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   450
    void checkAccessibleType(Env<AttrContext> env, Type t) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   451
        accessibilityChecker.visit(t, env);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   452
    }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   453
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   454
    /**
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   455
     * Accessibility type-visitor
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   456
     */
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   457
    Types.SimpleVisitor<Void, Env<AttrContext>> accessibilityChecker =
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   458
            new Types.SimpleVisitor<Void, Env<AttrContext>>() {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   459
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   460
        void visit(List<Type> ts, Env<AttrContext> env) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   461
            for (Type t : ts) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   462
                visit(t, env);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   463
            }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   464
        }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   465
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   466
        public Void visitType(Type t, Env<AttrContext> env) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   467
            return null;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   468
        }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   469
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   470
        @Override
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   471
        public Void visitArrayType(ArrayType t, Env<AttrContext> env) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   472
            visit(t.elemtype, env);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   473
            return null;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   474
        }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   475
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   476
        @Override
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   477
        public Void visitClassType(ClassType t, Env<AttrContext> env) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   478
            visit(t.getTypeArguments(), env);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   479
            if (!isAccessible(env, t, true)) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   480
                accessBase(new AccessError(t.tsym), env.tree.pos(), env.enclClass.sym, t, t.tsym.name, true);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   481
            }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   482
            return null;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   483
        }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   484
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   485
        @Override
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   486
        public Void visitWildcardType(WildcardType t, Env<AttrContext> env) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   487
            visit(t.type, env);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   488
            return null;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   489
        }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   490
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   491
        @Override
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   492
        public Void visitMethodType(MethodType t, Env<AttrContext> env) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   493
            visit(t.getParameterTypes(), env);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   494
            visit(t.getReturnType(), env);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   495
            visit(t.getThrownTypes(), env);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   496
            return null;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   497
        }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   498
    };
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   499
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   500
    /** Try to instantiate the type of a method so that it fits
17998
02a5abb2ae2f 7179353: try-with-resources fails to compile with generic exception parameters
vromero
parents: 17804
diff changeset
   501
     *  given type arguments and argument types. If successful, return
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   502
     *  the method's instantiated type, else return null.
06bc494ca11e Initial load
duke
parents:
diff changeset
   503
     *  The instantiation will take into account an additional leading
06bc494ca11e Initial load
duke
parents:
diff changeset
   504
     *  formal parameter if the method is an instance method seen as a member
17998
02a5abb2ae2f 7179353: try-with-resources fails to compile with generic exception parameters
vromero
parents: 17804
diff changeset
   505
     *  of an under determined site. In this case, we treat site as an additional
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   506
     *  parameter and the parameters of the class containing the method as
06bc494ca11e Initial load
duke
parents:
diff changeset
   507
     *  additional type variables that get instantiated.
06bc494ca11e Initial load
duke
parents:
diff changeset
   508
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   509
     *  @param env         The current environment
06bc494ca11e Initial load
duke
parents:
diff changeset
   510
     *  @param site        The type of which the method is a member.
06bc494ca11e Initial load
duke
parents:
diff changeset
   511
     *  @param m           The method symbol.
06bc494ca11e Initial load
duke
parents:
diff changeset
   512
     *  @param argtypes    The invocation's given value arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
   513
     *  @param typeargtypes    The invocation's given type arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
   514
     *  @param allowBoxing Allow boxing conversions of arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
   515
     *  @param useVarargs Box trailing arguments into an array for varargs.
06bc494ca11e Initial load
duke
parents:
diff changeset
   516
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   517
    Type rawInstantiate(Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
   518
                        Type site,
06bc494ca11e Initial load
duke
parents:
diff changeset
   519
                        Symbol m,
12915
28cf1e0dafdc 7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents: 12335
diff changeset
   520
                        ResultInfo resultInfo,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   521
                        List<Type> argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
   522
                        List<Type> typeargtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
   523
                        boolean allowBoxing,
06bc494ca11e Initial load
duke
parents:
diff changeset
   524
                        boolean useVarargs,
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
   525
                        Warner warn) throws Infer.InferenceException {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   526
        Type mt = types.memberType(site, m);
06bc494ca11e Initial load
duke
parents:
diff changeset
   527
        // tvars is the list of formal type variables for which type arguments
06bc494ca11e Initial load
duke
parents:
diff changeset
   528
        // need to inferred.
12915
28cf1e0dafdc 7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents: 12335
diff changeset
   529
        List<Type> tvars = List.nil();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   530
        if (typeargtypes == null) typeargtypes = List.nil();
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14062
diff changeset
   531
        if (!mt.hasTag(FORALL) && typeargtypes.nonEmpty()) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   532
            // This is not a polymorphic method, but typeargs are supplied
9303
eae35c201e19 7032975: API files in javax.annotation.processing need to be updated for references to JLS
jjh
parents: 9302
diff changeset
   533
            // which is fine, see JLS 15.12.2.1
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14062
diff changeset
   534
        } else if (mt.hasTag(FORALL) && typeargtypes.nonEmpty()) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   535
            ForAll pmt = (ForAll) mt;
06bc494ca11e Initial load
duke
parents:
diff changeset
   536
            if (typeargtypes.length() != pmt.tvars.length())
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
   537
                throw inapplicableMethodException.setMessage("arg.length.mismatch"); // not enough args
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   538
            // Check type arguments are within bounds
06bc494ca11e Initial load
duke
parents:
diff changeset
   539
            List<Type> formals = pmt.tvars;
06bc494ca11e Initial load
duke
parents:
diff changeset
   540
            List<Type> actuals = typeargtypes;
06bc494ca11e Initial load
duke
parents:
diff changeset
   541
            while (formals.nonEmpty() && actuals.nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   542
                List<Type> bounds = types.subst(types.getBounds((TypeVar)formals.head),
06bc494ca11e Initial load
duke
parents:
diff changeset
   543
                                                pmt.tvars, typeargtypes);
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
   544
                for (; bounds.nonEmpty(); bounds = bounds.tail) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   545
                    if (!types.isSubtypeUnchecked(actuals.head, bounds.head, warn))
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
   546
                        throw inapplicableMethodException.setMessage("explicit.param.do.not.conform.to.bounds",actuals.head, bounds);
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
   547
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   548
                formals = formals.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   549
                actuals = actuals.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   550
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   551
            mt = types.subst(pmt.qtype, pmt.tvars, typeargtypes);
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14062
diff changeset
   552
        } else if (mt.hasTag(FORALL)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   553
            ForAll pmt = (ForAll) mt;
06bc494ca11e Initial load
duke
parents:
diff changeset
   554
            List<Type> tvars1 = types.newInstances(pmt.tvars);
06bc494ca11e Initial load
duke
parents:
diff changeset
   555
            tvars = tvars.appendList(tvars1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   556
            mt = types.subst(pmt.qtype, pmt.tvars, tvars1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   557
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   558
06bc494ca11e Initial load
duke
parents:
diff changeset
   559
        // find out whether we need to go the slow route via infer
12335
4725d88691dd 7133185: Update 292 overload resolution logic to match JLS
mcimadamore
parents: 12334
diff changeset
   560
        boolean instNeeded = tvars.tail != null; /*inlined: tvars.nonEmpty()*/
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   561
        for (List<Type> l = argtypes;
06bc494ca11e Initial load
duke
parents:
diff changeset
   562
             l.tail != null/*inlined: l.nonEmpty()*/ && !instNeeded;
06bc494ca11e Initial load
duke
parents:
diff changeset
   563
             l = l.tail) {
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14062
diff changeset
   564
            if (l.head.hasTag(FORALL)) instNeeded = true;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   565
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   566
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
   567
        if (instNeeded) {
12335
4725d88691dd 7133185: Update 292 overload resolution logic to match JLS
mcimadamore
parents: 12334
diff changeset
   568
            return infer.instantiateMethod(env,
5489
e7af65bf7577 6730476: invalid "unchecked generic array" warning
mcimadamore
parents: 5321
diff changeset
   569
                                    tvars,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   570
                                    (MethodType)mt,
12915
28cf1e0dafdc 7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents: 12335
diff changeset
   571
                                    resultInfo,
24226
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23132
diff changeset
   572
                                    (MethodSymbol)m,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   573
                                    argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
   574
                                    allowBoxing,
06bc494ca11e Initial load
duke
parents:
diff changeset
   575
                                    useVarargs,
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
   576
                                    currentResolutionContext,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   577
                                    warn);
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
   578
        }
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
   579
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19652
diff changeset
   580
        DeferredAttr.DeferredAttrContext dc = currentResolutionContext.deferredAttrContext(m, infer.emptyContext, resultInfo, warn);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19652
diff changeset
   581
        currentResolutionContext.methodCheck.argumentsAcceptable(env, dc,
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   582
                                argtypes, mt.getParameterTypes(), warn);
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19652
diff changeset
   583
        dc.complete();
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
   584
        return mt;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   585
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   586
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
   587
    Type checkMethod(Env<AttrContext> env,
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
   588
                     Type site,
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
   589
                     Symbol m,
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
   590
                     ResultInfo resultInfo,
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
   591
                     List<Type> argtypes,
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
   592
                     List<Type> typeargtypes,
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
   593
                     Warner warn) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
   594
        MethodResolutionContext prevContext = currentResolutionContext;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
   595
        try {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
   596
            currentResolutionContext = new MethodResolutionContext();
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
   597
            currentResolutionContext.attrMode = DeferredAttr.AttrMode.CHECK;
18910
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   598
            if (env.tree.hasTag(JCTree.Tag.REFERENCE)) {
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   599
                //method/constructor references need special check class
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   600
                //to handle inference variables in 'argtypes' (might happen
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   601
                //during an unsticking round)
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   602
                currentResolutionContext.methodCheck =
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   603
                        new MethodReferenceCheck(resultInfo.checkContext.inferenceContext());
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   604
            }
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
   605
            MethodResolutionPhase step = currentResolutionContext.step = env.info.pendingResolutionPhase;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
   606
            return rawInstantiate(env, site, m, resultInfo, argtypes, typeargtypes,
16805
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   607
                    step.isBoxingRequired(), step.isVarargsRequired(), warn);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
   608
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
   609
        finally {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
   610
            currentResolutionContext = prevContext;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
   611
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
   612
    }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
   613
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   614
    /** Same but returns null instead throwing a NoInstanceException
06bc494ca11e Initial load
duke
parents:
diff changeset
   615
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   616
    Type instantiate(Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
   617
                     Type site,
06bc494ca11e Initial load
duke
parents:
diff changeset
   618
                     Symbol m,
12915
28cf1e0dafdc 7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents: 12335
diff changeset
   619
                     ResultInfo resultInfo,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   620
                     List<Type> argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
   621
                     List<Type> typeargtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
   622
                     boolean allowBoxing,
06bc494ca11e Initial load
duke
parents:
diff changeset
   623
                     boolean useVarargs,
06bc494ca11e Initial load
duke
parents:
diff changeset
   624
                     Warner warn) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   625
        try {
12915
28cf1e0dafdc 7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents: 12335
diff changeset
   626
            return rawInstantiate(env, site, m, resultInfo, argtypes, typeargtypes,
16805
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   627
                                  allowBoxing, useVarargs, warn);
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
   628
        } catch (InapplicableMethodException ex) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   629
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   630
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   631
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   632
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   633
    /**
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   634
     * This interface defines an entry point that should be used to perform a
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   635
     * method check. A method check usually consist in determining as to whether
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   636
     * a set of types (actuals) is compatible with another set of types (formals).
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   637
     * Since the notion of compatibility can vary depending on the circumstances,
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   638
     * this interfaces allows to easily add new pluggable method check routines.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   639
     */
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   640
    interface MethodCheck {
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   641
        /**
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   642
         * Main method check routine. A method check usually consist in determining
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   643
         * as to whether a set of types (actuals) is compatible with another set of
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   644
         * types (formals). If an incompatibility is found, an unchecked exception
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   645
         * is assumed to be thrown.
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   646
         */
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   647
        void argumentsAcceptable(Env<AttrContext> env,
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   648
                                DeferredAttrContext deferredAttrContext,
8229
39266c1b1b0e 6313164: javac generates code that fails byte code verification for the varargs feature
mcimadamore
parents: 8228
diff changeset
   649
                                List<Type> argtypes,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   650
                                List<Type> formals,
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   651
                                Warner warn);
16805
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   652
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   653
        /**
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   654
         * Retrieve the method check object that will be used during a
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   655
         * most specific check.
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   656
         */
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   657
        MethodCheck mostSpecificCheck(List<Type> actuals, boolean strict);
11707
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   658
    }
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   659
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   660
    /**
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   661
     * Helper enum defining all method check diagnostics (used by resolveMethodCheck).
11707
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   662
     */
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   663
    enum MethodCheckDiag {
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   664
        /**
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   665
         * Actuals and formals differs in length.
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   666
         */
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   667
        ARITY_MISMATCH("arg.length.mismatch", "infer.arg.length.mismatch"),
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   668
        /**
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   669
         * An actual is incompatible with a formal.
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   670
         */
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   671
        ARG_MISMATCH("no.conforming.assignment.exists", "infer.no.conforming.assignment.exists"),
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   672
        /**
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   673
         * An actual is incompatible with the varargs element type.
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   674
         */
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   675
        VARARG_MISMATCH("varargs.argument.mismatch", "infer.varargs.argument.mismatch"),
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   676
        /**
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   677
         * The varargs element type is inaccessible.
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   678
         */
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   679
        INACCESSIBLE_VARARGS("inaccessible.varargs.type", "inaccessible.varargs.type");
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   680
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   681
        final String basicKey;
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   682
        final String inferKey;
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   683
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   684
        MethodCheckDiag(String basicKey, String inferKey) {
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   685
            this.basicKey = basicKey;
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   686
            this.inferKey = inferKey;
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   687
        }
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
   688
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
   689
        String regex() {
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
   690
            return String.format("([a-z]*\\.)*(%s|%s)", basicKey, inferKey);
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
   691
        }
11707
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   692
    }
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   693
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   694
    /**
16805
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   695
     * Dummy method check object. All methods are deemed applicable, regardless
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   696
     * of their formal parameter types.
11707
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   697
     */
16805
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   698
    MethodCheck nilMethodCheck = new MethodCheck() {
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   699
        public void argumentsAcceptable(Env<AttrContext> env, DeferredAttrContext deferredAttrContext, List<Type> argtypes, List<Type> formals, Warner warn) {
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   700
            //do nothing - method always applicable regardless of actuals
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   701
        }
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   702
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   703
        public MethodCheck mostSpecificCheck(List<Type> actuals, boolean strict) {
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   704
            return this;
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   705
        }
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   706
    };
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   707
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   708
    /**
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   709
     * Base class for 'real' method checks. The class defines the logic for
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   710
     * iterating through formals and actuals and provides and entry point
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   711
     * that can be used by subclasses in order to define the actual check logic.
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   712
     */
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   713
    abstract class AbstractMethodCheck implements MethodCheck {
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   714
        @Override
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   715
        public void argumentsAcceptable(final Env<AttrContext> env,
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   716
                                    DeferredAttrContext deferredAttrContext,
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   717
                                    List<Type> argtypes,
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   718
                                    List<Type> formals,
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   719
                                    Warner warn) {
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   720
            //should we expand formals?
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   721
            boolean useVarargs = deferredAttrContext.phase.isVarargsRequired();
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
   722
            List<JCExpression> trees = TreeInfo.args(env.tree);
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   723
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   724
            //inference context used during this method check
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   725
            InferenceContext inferenceContext = deferredAttrContext.inferenceContext;
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   726
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   727
            Type varargsFormal = useVarargs ? formals.last() : null;
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   728
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   729
            if (varargsFormal == null &&
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   730
                    argtypes.size() != formals.size()) {
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
   731
                reportMC(env.tree, MethodCheckDiag.ARITY_MISMATCH, inferenceContext); // not enough args
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   732
            }
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   733
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   734
            while (argtypes.nonEmpty() && formals.head != varargsFormal) {
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
   735
                DiagnosticPosition pos = trees != null ? trees.head : null;
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
   736
                checkArg(pos, false, argtypes.head, formals.head, deferredAttrContext, warn);
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
   737
                argtypes = argtypes.tail;
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   738
                formals = formals.tail;
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
   739
                trees = trees != null ? trees.tail : trees;
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
   740
            }
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   741
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   742
            if (formals.head != varargsFormal) {
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
   743
                reportMC(env.tree, MethodCheckDiag.ARITY_MISMATCH, inferenceContext); // not enough args
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   744
            }
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   745
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   746
            if (useVarargs) {
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   747
                //note: if applicability check is triggered by most specific test,
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   748
                //the last argument of a varargs is _not_ an array type (see JLS 15.12.2.5)
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   749
                final Type elt = types.elemtype(varargsFormal);
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   750
                while (argtypes.nonEmpty()) {
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
   751
                    DiagnosticPosition pos = trees != null ? trees.head : null;
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
   752
                    checkArg(pos, true, argtypes.head, elt, deferredAttrContext, warn);
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   753
                    argtypes = argtypes.tail;
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
   754
                    trees = trees != null ? trees.tail : trees;
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 14045
diff changeset
   755
                }
8229
39266c1b1b0e 6313164: javac generates code that fails byte code verification for the varargs feature
mcimadamore
parents: 8228
diff changeset
   756
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   757
        }
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   758
16805
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   759
        /**
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   760
         * Does the actual argument conforms to the corresponding formal?
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   761
         */
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
   762
        abstract void checkArg(DiagnosticPosition pos, boolean varargs, Type actual, Type formal, DeferredAttrContext deferredAttrContext, Warner warn);
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
   763
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
   764
        protected void reportMC(DiagnosticPosition pos, MethodCheckDiag diag, InferenceContext inferenceContext, Object... args) {
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   765
            boolean inferDiag = inferenceContext != infer.emptyContext;
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   766
            InapplicableMethodException ex = inferDiag ?
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   767
                    infer.inferenceException : inapplicableMethodException;
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   768
            if (inferDiag && (!diag.inferKey.equals(diag.basicKey))) {
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   769
                Object[] args2 = new Object[args.length + 1];
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   770
                System.arraycopy(args, 0, args2, 1, args.length);
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   771
                args2[0] = inferenceContext.inferenceVars();
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   772
                args = args2;
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   773
            }
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
   774
            String key = inferDiag ? diag.inferKey : diag.basicKey;
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
   775
            throw ex.setMessage(diags.create(DiagnosticType.FRAGMENT, log.currentSource(), pos, key, args));
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   776
        }
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   777
16805
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   778
        public MethodCheck mostSpecificCheck(List<Type> actuals, boolean strict) {
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   779
            return nilMethodCheck;
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   780
        }
24226
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23132
diff changeset
   781
16805
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   782
    }
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   783
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   784
    /**
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   785
     * Arity-based method check. A method is applicable if the number of actuals
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   786
     * supplied conforms to the method signature.
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   787
     */
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   788
    MethodCheck arityMethodCheck = new AbstractMethodCheck() {
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   789
        @Override
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
   790
        void checkArg(DiagnosticPosition pos, boolean varargs, Type actual, Type formal, DeferredAttrContext deferredAttrContext, Warner warn) {
16805
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   791
            //do nothing - actual always compatible to formals
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   792
        }
24226
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23132
diff changeset
   793
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23132
diff changeset
   794
        @Override
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23132
diff changeset
   795
        public String toString() {
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23132
diff changeset
   796
            return "arityMethodCheck";
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23132
diff changeset
   797
        }
16805
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   798
    };
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   799
18910
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   800
    List<Type> dummyArgs(int length) {
20249
93f8eae31092 6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents: 19940
diff changeset
   801
        ListBuffer<Type> buf = new ListBuffer<>();
18910
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   802
        for (int i = 0 ; i < length ; i++) {
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   803
            buf.append(Type.noType);
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   804
        }
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   805
        return buf.toList();
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   806
    }
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   807
16805
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   808
    /**
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   809
     * Main method applicability routine. Given a list of actual types A,
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   810
     * a list of formal types F, determines whether the types in A are
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   811
     * compatible (by method invocation conversion) with the types in F.
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   812
     *
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   813
     * Since this routine is shared between overload resolution and method
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   814
     * type-inference, a (possibly empty) inference context is used to convert
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   815
     * formal types to the corresponding 'undet' form ahead of a compatibility
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   816
     * check so that constraints can be propagated and collected.
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   817
     *
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   818
     * Moreover, if one or more types in A is a deferred type, this routine uses
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   819
     * DeferredAttr in order to perform deferred attribution. If one or more actual
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   820
     * deferred types are stuck, they are placed in a queue and revisited later
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   821
     * after the remainder of the arguments have been seen. If this is not sufficient
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   822
     * to 'unstuck' the argument, a cyclic inference error is called out.
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   823
     *
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   824
     * A method check handler (see above) is used in order to report errors.
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   825
     */
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   826
    MethodCheck resolveMethodCheck = new AbstractMethodCheck() {
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   827
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   828
        @Override
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
   829
        void checkArg(DiagnosticPosition pos, boolean varargs, Type actual, Type formal, DeferredAttrContext deferredAttrContext, Warner warn) {
16805
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   830
            ResultInfo mresult = methodCheckResult(varargs, formal, deferredAttrContext, warn);
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
   831
            mresult.check(pos, actual);
16805
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   832
        }
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   833
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   834
        @Override
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   835
        public void argumentsAcceptable(final Env<AttrContext> env,
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   836
                                    DeferredAttrContext deferredAttrContext,
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   837
                                    List<Type> argtypes,
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   838
                                    List<Type> formals,
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   839
                                    Warner warn) {
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   840
            super.argumentsAcceptable(env, deferredAttrContext, argtypes, formals, warn);
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   841
            //should we expand formals?
25435
928866794984 8049075: javac, wildcards and generic vararg method invocation not accepted
vromero
parents: 25279
diff changeset
   842
            if (deferredAttrContext.phase.isVarargsRequired()) {
928866794984 8049075: javac, wildcards and generic vararg method invocation not accepted
vromero
parents: 25279
diff changeset
   843
                Type typeToCheck = null;
928866794984 8049075: javac, wildcards and generic vararg method invocation not accepted
vromero
parents: 25279
diff changeset
   844
                if (!checkVarargsAccessAfterResolution) {
928866794984 8049075: javac, wildcards and generic vararg method invocation not accepted
vromero
parents: 25279
diff changeset
   845
                    typeToCheck = types.elemtype(formals.last());
928866794984 8049075: javac, wildcards and generic vararg method invocation not accepted
vromero
parents: 25279
diff changeset
   846
                } else if (deferredAttrContext.mode == AttrMode.CHECK) {
928866794984 8049075: javac, wildcards and generic vararg method invocation not accepted
vromero
parents: 25279
diff changeset
   847
                    typeToCheck = types.erasure(types.elemtype(formals.last()));
928866794984 8049075: javac, wildcards and generic vararg method invocation not accepted
vromero
parents: 25279
diff changeset
   848
                }
928866794984 8049075: javac, wildcards and generic vararg method invocation not accepted
vromero
parents: 25279
diff changeset
   849
                if (typeToCheck != null) {
928866794984 8049075: javac, wildcards and generic vararg method invocation not accepted
vromero
parents: 25279
diff changeset
   850
                    varargsAccessible(env, typeToCheck, deferredAttrContext.inferenceContext);
928866794984 8049075: javac, wildcards and generic vararg method invocation not accepted
vromero
parents: 25279
diff changeset
   851
                }
16805
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   852
            }
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   853
        }
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   854
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   855
        private void varargsAccessible(final Env<AttrContext> env, final Type t, final InferenceContext inferenceContext) {
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   856
            if (inferenceContext.free(t)) {
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   857
                inferenceContext.addFreeTypeListener(List.of(t), new FreeTypeListener() {
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   858
                    @Override
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   859
                    public void typesInferred(InferenceContext inferenceContext) {
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
   860
                        varargsAccessible(env, inferenceContext.asInstType(t), inferenceContext);
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   861
                    }
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   862
                });
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   863
            } else {
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   864
                if (!isAccessible(env, t)) {
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   865
                    Symbol location = env.enclClass.sym;
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
   866
                    reportMC(env.tree, MethodCheckDiag.INACCESSIBLE_VARARGS, inferenceContext, t, Kinds.kindName(location), location);
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   867
                }
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   868
            }
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   869
        }
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   870
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   871
        private ResultInfo methodCheckResult(final boolean varargsCheck, Type to,
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   872
                final DeferredAttr.DeferredAttrContext deferredAttrContext, Warner rsWarner) {
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   873
            CheckContext checkContext = new MethodCheckContext(!deferredAttrContext.phase.isBoxingRequired(), deferredAttrContext, rsWarner) {
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   874
                MethodCheckDiag methodDiag = varargsCheck ?
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   875
                                 MethodCheckDiag.VARARG_MISMATCH : MethodCheckDiag.ARG_MISMATCH;
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   876
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   877
                @Override
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   878
                public void report(DiagnosticPosition pos, JCDiagnostic details) {
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
   879
                    reportMC(pos, methodDiag, deferredAttrContext.inferenceContext, details);
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   880
                }
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   881
            };
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   882
            return new MethodResultInfo(to, checkContext);
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   883
        }
16805
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   884
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   885
        @Override
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   886
        public MethodCheck mostSpecificCheck(List<Type> actuals, boolean strict) {
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   887
            return new MostSpecificCheck(strict, actuals);
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   888
        }
24226
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23132
diff changeset
   889
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23132
diff changeset
   890
        @Override
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23132
diff changeset
   891
        public String toString() {
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23132
diff changeset
   892
            return "resolveMethodCheck";
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23132
diff changeset
   893
        }
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   894
    };
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   895
21718
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
   896
    /**
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
   897
     * This class handles method reference applicability checks; since during
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
   898
     * these checks it's sometime possible to have inference variables on
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
   899
     * the actual argument types list, the method applicability check must be
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
   900
     * extended so that inference variables are 'opened' as needed.
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
   901
     */
18910
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   902
    class MethodReferenceCheck extends AbstractMethodCheck {
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   903
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   904
        InferenceContext pendingInferenceContext;
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   905
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   906
        MethodReferenceCheck(InferenceContext pendingInferenceContext) {
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   907
            this.pendingInferenceContext = pendingInferenceContext;
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   908
        }
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   909
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   910
        @Override
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   911
        void checkArg(DiagnosticPosition pos, boolean varargs, Type actual, Type formal, DeferredAttrContext deferredAttrContext, Warner warn) {
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   912
            ResultInfo mresult = methodCheckResult(varargs, formal, deferredAttrContext, warn);
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   913
            mresult.check(pos, actual);
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   914
        }
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   915
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   916
        private ResultInfo methodCheckResult(final boolean varargsCheck, Type to,
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   917
                final DeferredAttr.DeferredAttrContext deferredAttrContext, Warner rsWarner) {
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   918
            CheckContext checkContext = new MethodCheckContext(!deferredAttrContext.phase.isBoxingRequired(), deferredAttrContext, rsWarner) {
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   919
                MethodCheckDiag methodDiag = varargsCheck ?
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   920
                                 MethodCheckDiag.VARARG_MISMATCH : MethodCheckDiag.ARG_MISMATCH;
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   921
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   922
                @Override
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   923
                public boolean compatible(Type found, Type req, Warner warn) {
23132
00a059740d87 8035891: javac, rename method asFree() in InferenceContext to asUndetVar() which reflects better it's purpose
vromero
parents: 23105
diff changeset
   924
                    found = pendingInferenceContext.asUndetVar(found);
24226
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23132
diff changeset
   925
                    if (found.hasTag(UNDETVAR) && req.isPrimitive()) {
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23132
diff changeset
   926
                        req = types.boxedClass(req).type;
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23132
diff changeset
   927
                    }
18910
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   928
                    return super.compatible(found, req, warn);
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   929
                }
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   930
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   931
                @Override
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   932
                public void report(DiagnosticPosition pos, JCDiagnostic details) {
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   933
                    reportMC(pos, methodDiag, deferredAttrContext.inferenceContext, details);
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   934
                }
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   935
            };
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   936
            return new MethodResultInfo(to, checkContext);
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   937
        }
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   938
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   939
        @Override
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   940
        public MethodCheck mostSpecificCheck(List<Type> actuals, boolean strict) {
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   941
            return new MostSpecificCheck(strict, actuals);
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   942
        }
24226
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23132
diff changeset
   943
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23132
diff changeset
   944
        @Override
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23132
diff changeset
   945
        public String toString() {
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23132
diff changeset
   946
            return "MethodReferenceCheck";
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23132
diff changeset
   947
        }
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22161
diff changeset
   948
    }
18910
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   949
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   950
    /**
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   951
     * Check context to be used during method applicability checks. A method check
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   952
     * context might contain inference variables.
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   953
     */
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   954
    abstract class MethodCheckContext implements CheckContext {
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   955
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   956
        boolean strict;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
   957
        DeferredAttrContext deferredAttrContext;
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   958
        Warner rsWarner;
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
   959
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   960
        public MethodCheckContext(boolean strict, DeferredAttrContext deferredAttrContext, Warner rsWarner) {
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   961
           this.strict = strict;
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   962
           this.deferredAttrContext = deferredAttrContext;
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   963
           this.rsWarner = rsWarner;
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   964
        }
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   965
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   966
        public boolean compatible(Type found, Type req, Warner warn) {
25279
a665d5cbb8c3 8044546: Crash on faulty reduce/lambda
vromero
parents: 25008
diff changeset
   967
            InferenceContext inferenceContext = deferredAttrContext.inferenceContext;
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   968
            return strict ?
25279
a665d5cbb8c3 8044546: Crash on faulty reduce/lambda
vromero
parents: 25008
diff changeset
   969
                    types.isSubtypeUnchecked(inferenceContext.asUndetVar(found), inferenceContext.asUndetVar(req), warn) :
a665d5cbb8c3 8044546: Crash on faulty reduce/lambda
vromero
parents: 25008
diff changeset
   970
                    types.isConvertible(inferenceContext.asUndetVar(found), inferenceContext.asUndetVar(req), warn);
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   971
        }
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   972
13438
83729994273a 7175911: Simplify error reporting API in Check.CheckContext interface
mcimadamore
parents: 12915
diff changeset
   973
        public void report(DiagnosticPosition pos, JCDiagnostic details) {
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   974
            throw inapplicableMethodException.setMessage(details);
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   975
        }
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   976
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   977
        public Warner checkWarner(DiagnosticPosition pos, Type found, Type req) {
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   978
            return rsWarner;
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   979
        }
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 14045
diff changeset
   980
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 14045
diff changeset
   981
        public InferenceContext inferenceContext() {
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   982
            return deferredAttrContext.inferenceContext;
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 14045
diff changeset
   983
        }
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
   984
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
   985
        public DeferredAttrContext deferredAttrContext() {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
   986
            return deferredAttrContext;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
   987
        }
24226
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23132
diff changeset
   988
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23132
diff changeset
   989
        @Override
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23132
diff changeset
   990
        public String toString() {
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23132
diff changeset
   991
            return "MethodCheckContext";
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23132
diff changeset
   992
        }
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   993
    }
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
   994
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   995
    /**
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   996
     * ResultInfo class to be used during method applicability checks. Check
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   997
     * for deferred types goes through special path.
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   998
     */
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
   999
    class MethodResultInfo extends ResultInfo {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  1000
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
  1001
        public MethodResultInfo(Type pt, CheckContext checkContext) {
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  1002
            attr.super(KindSelector.VAL, pt, checkContext);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  1003
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  1004
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  1005
        @Override
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  1006
        protected Type check(DiagnosticPosition pos, Type found) {
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14062
diff changeset
  1007
            if (found.hasTag(DEFERRED)) {
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  1008
                DeferredType dt = (DeferredType)found;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  1009
                return dt.check(this);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  1010
            } else {
24226
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23132
diff changeset
  1011
                Type uResult = U(found.baseType());
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23132
diff changeset
  1012
                Type capturedType = pos == null || pos.getTree() == null ?
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23132
diff changeset
  1013
                        types.capture(uResult) :
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23132
diff changeset
  1014
                        checkContext.inferenceContext()
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23132
diff changeset
  1015
                            .cachedCapture(pos.getTree(), uResult, true);
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23132
diff changeset
  1016
                return super.check(pos, chk.checkNonVoid(pos, capturedType));
8229
39266c1b1b0e 6313164: javac generates code that fails byte code verification for the varargs feature
mcimadamore
parents: 8228
diff changeset
  1017
            }
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  1018
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  1019
18911
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1020
        /**
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1021
         * javac has a long-standing 'simplification' (see 6391995):
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1022
         * given an actual argument type, the method check is performed
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1023
         * on its upper bound. This leads to inconsistencies when an
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1024
         * argument type is checked against itself. For example, given
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1025
         * a type-variable T, it is not true that {@code U(T) <: T},
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1026
         * so we need to guard against that.
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1027
         */
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1028
        private Type U(Type found) {
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1029
            return found == pt ?
24612
75dc732b45af 8042338: Refactor Types.upperBound to treat wildcards and variables separately
dlsmith
parents: 24604
diff changeset
  1030
                    found : types.cvarUpperBound(found);
18911
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1031
        }
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1032
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  1033
        @Override
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  1034
        protected MethodResultInfo dup(Type newPt) {
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
  1035
            return new MethodResultInfo(newPt, checkContext);
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  1036
        }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  1037
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  1038
        @Override
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  1039
        protected ResultInfo dup(CheckContext newContext) {
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
  1040
            return new MethodResultInfo(pt, newContext);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  1041
        }
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
  1042
    }
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
  1043
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1044
    /**
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1045
     * Most specific method applicability routine. Given a list of actual types A,
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1046
     * a list of formal types F1, and a list of formal types F2, the routine determines
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1047
     * as to whether the types in F1 can be considered more specific than those in F2 w.r.t.
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1048
     * argument types A.
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1049
     */
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1050
    class MostSpecificCheck implements MethodCheck {
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1051
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1052
        boolean strict;
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1053
        List<Type> actuals;
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1054
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1055
        MostSpecificCheck(boolean strict, List<Type> actuals) {
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1056
            this.strict = strict;
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1057
            this.actuals = actuals;
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1058
        }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1059
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1060
        @Override
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1061
        public void argumentsAcceptable(final Env<AttrContext> env,
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1062
                                    DeferredAttrContext deferredAttrContext,
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1063
                                    List<Type> formals1,
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1064
                                    List<Type> formals2,
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1065
                                    Warner warn) {
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1066
            formals2 = adjustArgs(formals2, deferredAttrContext.msym, formals1.length(), deferredAttrContext.phase.isVarargsRequired());
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1067
            while (formals2.nonEmpty()) {
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1068
                ResultInfo mresult = methodCheckResult(formals2.head, deferredAttrContext, warn, actuals.head);
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1069
                mresult.check(null, formals1.head);
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1070
                formals1 = formals1.tail;
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1071
                formals2 = formals2.tail;
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1072
                actuals = actuals.isEmpty() ? actuals : actuals.tail;
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1073
            }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1074
        }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1075
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1076
       /**
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1077
        * Create a method check context to be used during the most specific applicability check
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1078
        */
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1079
        ResultInfo methodCheckResult(Type to, DeferredAttr.DeferredAttrContext deferredAttrContext,
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1080
               Warner rsWarner, Type actual) {
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  1081
            return attr.new ResultInfo(KindSelector.VAL, to,
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1082
                   new MostSpecificCheckContext(strict, deferredAttrContext, rsWarner, actual));
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1083
        }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1084
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1085
        /**
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1086
         * Subclass of method check context class that implements most specific
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1087
         * method conversion. If the actual type under analysis is a deferred type
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1088
         * a full blown structural analysis is carried out.
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1089
         */
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1090
        class MostSpecificCheckContext extends MethodCheckContext {
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1091
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1092
            Type actual;
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1093
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1094
            public MostSpecificCheckContext(boolean strict, DeferredAttrContext deferredAttrContext, Warner rsWarner, Type actual) {
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1095
                super(strict, deferredAttrContext, rsWarner);
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1096
                this.actual = actual;
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1097
            }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1098
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1099
            public boolean compatible(Type found, Type req, Warner warn) {
24404
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1100
                if (allowFunctionalInterfaceMostSpecific &&
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1101
                        unrelatedFunctionalInterfaces(found, req) &&
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1102
                        (actual != null && actual.getTag() == DEFERRED)) {
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1103
                    DeferredType dt = (DeferredType) actual;
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1104
                    DeferredType.SpeculativeCache.Entry e =
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1105
                            dt.speculativeCache.get(deferredAttrContext.msym, deferredAttrContext.phase);
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1106
                    if (e != null && e.speculativeTree != deferredAttr.stuckTree) {
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1107
                        return functionalInterfaceMostSpecific(found, req, e.speculativeTree, warn);
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1108
                    }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1109
                }
24404
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1110
                return super.compatible(found, req, warn);
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1111
            }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1112
24404
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1113
            /** Whether {@code t} and {@code s} are unrelated functional interface types. */
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1114
            private boolean unrelatedFunctionalInterfaces(Type t, Type s) {
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1115
                return types.isFunctionalInterface(t.tsym) &&
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1116
                       types.isFunctionalInterface(s.tsym) &&
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1117
                       types.asSuper(t, s.tsym) == null &&
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1118
                       types.asSuper(s, t.tsym) == null;
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1119
            }
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1120
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1121
            /** Parameters {@code t} and {@code s} are unrelated functional interface types. */
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1122
            private boolean functionalInterfaceMostSpecific(Type t, Type s, JCTree tree, Warner warn) {
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1123
                FunctionalInterfaceMostSpecificChecker msc = new FunctionalInterfaceMostSpecificChecker(t, s, warn);
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1124
                msc.scan(tree);
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1125
                return msc.result;
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1126
            }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1127
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1128
            /**
24404
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1129
             * Tests whether one functional interface type can be considered more specific
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1130
             * than another unrelated functional interface type for the scanned expression.
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1131
             */
24404
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1132
            class FunctionalInterfaceMostSpecificChecker extends DeferredAttr.PolyScanner {
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1133
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1134
                final Type t;
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1135
                final Type s;
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1136
                final Warner warn;
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1137
                boolean result;
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1138
24404
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1139
                /** Parameters {@code t} and {@code s} are unrelated functional interface types. */
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1140
                FunctionalInterfaceMostSpecificChecker(Type t, Type s, Warner warn) {
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1141
                    this.t = t;
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1142
                    this.s = s;
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1143
                    this.warn = warn;
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1144
                    result = true;
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1145
                }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1146
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1147
                @Override
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1148
                void skip(JCTree tree) {
24404
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1149
                    result &= false;
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1150
                }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1151
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1152
                @Override
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1153
                public void visitConditional(JCConditional tree) {
24404
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1154
                    scan(tree.truepart);
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1155
                    scan(tree.falsepart);
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1156
                }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1157
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1158
                @Override
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1159
                public void visitReference(JCMemberReference tree) {
24404
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1160
                    Type desc_t = types.findDescriptorType(t);
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1161
                    Type desc_s = types.findDescriptorType(s);
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1162
                    // use inference variables here for more-specific inference (18.5.4)
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1163
                    if (!types.isSameTypes(desc_t.getParameterTypes(),
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1164
                            inferenceContext().asUndetVars(desc_s.getParameterTypes()))) {
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1165
                        result &= false;
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1166
                    } else {
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1167
                        // compare return types
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1168
                        Type ret_t = desc_t.getReturnType();
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1169
                        Type ret_s = desc_s.getReturnType();
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1170
                        if (ret_s.hasTag(VOID)) {
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1171
                            result &= true;
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1172
                        } else if (ret_t.hasTag(VOID)) {
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1173
                            result &= false;
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1174
                        } else if (ret_t.isPrimitive() != ret_s.isPrimitive()) {
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1175
                            boolean retValIsPrimitive =
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1176
                                    tree.refPolyKind == PolyKind.STANDALONE &&
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1177
                                    tree.sym.type.getReturnType().isPrimitive();
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1178
                            result &= (retValIsPrimitive == ret_t.isPrimitive()) &&
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1179
                                      (retValIsPrimitive != ret_s.isPrimitive());
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1180
                        } else {
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1181
                            result &= MostSpecificCheckContext.super.compatible(ret_t, ret_s, warn);
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1182
                        }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1183
                    }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1184
                }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1185
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1186
                @Override
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1187
                public void visitLambda(JCLambda tree) {
24404
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1188
                    Type desc_t = types.findDescriptorType(t);
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1189
                    Type desc_s = types.findDescriptorType(s);
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1190
                    // use inference variables here for more-specific inference (18.5.4)
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1191
                    if (!types.isSameTypes(desc_t.getParameterTypes(),
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1192
                            inferenceContext().asUndetVars(desc_s.getParameterTypes()))) {
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1193
                        result &= false;
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1194
                    } else {
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1195
                        // compare return types
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1196
                        Type ret_t = desc_t.getReturnType();
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1197
                        Type ret_s = desc_s.getReturnType();
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1198
                        if (ret_s.hasTag(VOID)) {
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1199
                            result &= true;
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1200
                        } else if (ret_t.hasTag(VOID)) {
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1201
                            result &= false;
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1202
                        } else if (unrelatedFunctionalInterfaces(ret_t, ret_s)) {
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1203
                            for (JCExpression expr : lambdaResults(tree)) {
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1204
                                result &= functionalInterfaceMostSpecific(ret_t, ret_s, expr, warn);
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1205
                            }
24404
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1206
                        } else if (ret_t.isPrimitive() != ret_s.isPrimitive()) {
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1207
                            for (JCExpression expr : lambdaResults(tree)) {
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1208
                                boolean retValIsPrimitive = expr.isStandalone() && expr.type.isPrimitive();
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1209
                                result &= (retValIsPrimitive == ret_t.isPrimitive()) &&
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1210
                                          (retValIsPrimitive != ret_s.isPrimitive());
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1211
                            }
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1212
                        } else {
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1213
                            result &= MostSpecificCheckContext.super.compatible(ret_t, ret_s, warn);
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1214
                        }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1215
                    }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1216
                }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1217
                //where
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1218
24404
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1219
                private List<JCExpression> lambdaResults(JCLambda lambda) {
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1220
                    if (lambda.getBodyKind() == JCTree.JCLambda.BodyKind.EXPRESSION) {
24404
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1221
                        return List.of((JCExpression) lambda.body);
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1222
                    } else {
24404
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1223
                        final ListBuffer<JCExpression> buffer = new ListBuffer<>();
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1224
                        DeferredAttr.LambdaReturnScanner lambdaScanner =
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1225
                                new DeferredAttr.LambdaReturnScanner() {
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1226
                                    @Override
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1227
                                    public void visitReturn(JCReturn tree) {
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1228
                                        if (tree.expr != null) {
24404
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1229
                                            buffer.append(tree.expr);
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1230
                                        }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1231
                                    }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1232
                                };
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1233
                        lambdaScanner.scan(lambda.body);
24404
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1234
                        return buffer.toList();
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1235
                    }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1236
                }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1237
            }
24404
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1238
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1239
        }
16805
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  1240
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  1241
        public MethodCheck mostSpecificCheck(List<Type> actuals, boolean strict) {
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  1242
            Assert.error("Cannot get here!");
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  1243
            return null;
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  1244
        }
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1245
    }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1246
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
  1247
    public static class InapplicableMethodException extends RuntimeException {
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
  1248
        private static final long serialVersionUID = 0;
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
  1249
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
  1250
        JCDiagnostic diagnostic;
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
  1251
        JCDiagnostic.Factory diags;
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
  1252
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
  1253
        InapplicableMethodException(JCDiagnostic.Factory diags) {
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
  1254
            this.diagnostic = null;
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
  1255
            this.diags = diags;
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
  1256
        }
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
  1257
        InapplicableMethodException setMessage() {
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 14045
diff changeset
  1258
            return setMessage((JCDiagnostic)null);
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
  1259
        }
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
  1260
        InapplicableMethodException setMessage(String key) {
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 14045
diff changeset
  1261
            return setMessage(key != null ? diags.fragment(key) : null);
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
  1262
        }
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
  1263
        InapplicableMethodException setMessage(String key, Object... args) {
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 14045
diff changeset
  1264
            return setMessage(key != null ? diags.fragment(key, args) : null);
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
  1265
        }
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
  1266
        InapplicableMethodException setMessage(JCDiagnostic diag) {
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
  1267
            this.diagnostic = diag;
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
  1268
            return this;
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
  1269
        }
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
  1270
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
  1271
        public JCDiagnostic getDiagnostic() {
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
  1272
            return diagnostic;
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
  1273
        }
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
  1274
    }
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
  1275
    private final InapplicableMethodException inapplicableMethodException;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1276
06bc494ca11e Initial load
duke
parents:
diff changeset
  1277
/* ***************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
  1278
 *  Symbol lookup
06bc494ca11e Initial load
duke
parents:
diff changeset
  1279
 *  the following naming conventions for arguments are used
06bc494ca11e Initial load
duke
parents:
diff changeset
  1280
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
  1281
 *       env      is the environment where the symbol was mentioned
06bc494ca11e Initial load
duke
parents:
diff changeset
  1282
 *       site     is the type of which the symbol is a member
06bc494ca11e Initial load
duke
parents:
diff changeset
  1283
 *       name     is the symbol's name
06bc494ca11e Initial load
duke
parents:
diff changeset
  1284
 *                if no arguments are given
06bc494ca11e Initial load
duke
parents:
diff changeset
  1285
 *       argtypes are the value arguments, if we search for a method
06bc494ca11e Initial load
duke
parents:
diff changeset
  1286
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
  1287
 *  If no symbol was found, a ResolveError detailing the problem is returned.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1288
 ****************************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
  1289
06bc494ca11e Initial load
duke
parents:
diff changeset
  1290
    /** Find field. Synthetic fields are always skipped.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1291
     *  @param env     The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1292
     *  @param site    The original type from where the selection takes place.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1293
     *  @param name    The name of the field.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1294
     *  @param c       The class to search for the field. This is always
06bc494ca11e Initial load
duke
parents:
diff changeset
  1295
     *                 a superclass or implemented interface of site's class.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1296
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1297
    Symbol findField(Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1298
                     Type site,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1299
                     Name name,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1300
                     TypeSymbol c) {
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14062
diff changeset
  1301
        while (c.type.hasTag(TYPEVAR))
326
d51f30ce6796 6531090: Cannot access methods/fields of a captured type belonging to an intersection type
mcimadamore
parents: 10
diff changeset
  1302
            c = c.type.getUpperBound().tsym;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1303
        Symbol bestSoFar = varNotFound;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1304
        Symbol sym;
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1305
        for (Symbol s : c.members().getSymbolsByName(name)) {
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1306
            if (s.kind == VAR && (s.flags_field & SYNTHETIC) == 0) {
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1307
                return isAccessible(env, site, s)
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1308
                    ? s : new AccessError(env, site, s);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1309
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1310
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1311
        Type st = types.supertype(c.type);
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14062
diff changeset
  1312
        if (st != null && (st.hasTag(CLASS) || st.hasTag(TYPEVAR))) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1313
            sym = findField(env, site, name, st.tsym);
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  1314
            bestSoFar = bestOf(bestSoFar, sym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1315
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1316
        for (List<Type> l = types.interfaces(c.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1317
             bestSoFar.kind != AMBIGUOUS && l.nonEmpty();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1318
             l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1319
            sym = findField(env, site, name, l.head.tsym);
18378
00267721fe3a 6360970: javac erroneously accept ambiguous field reference
mcimadamore
parents: 17998
diff changeset
  1320
            if (bestSoFar.exists() && sym.exists() &&
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1321
                sym.owner != bestSoFar.owner)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1322
                bestSoFar = new AmbiguityError(bestSoFar, sym);
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  1323
            else
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  1324
                bestSoFar = bestOf(bestSoFar, sym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1325
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1326
        return bestSoFar;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1327
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1328
06bc494ca11e Initial load
duke
parents:
diff changeset
  1329
    /** Resolve a field identifier, throw a fatal error if not found.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1330
     *  @param pos       The position to use for error reporting.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1331
     *  @param env       The environment current at the method invocation.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1332
     *  @param site      The type of the qualifying expression, in which
06bc494ca11e Initial load
duke
parents:
diff changeset
  1333
     *                   identifier is searched.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1334
     *  @param name      The identifier's name.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1335
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1336
    public VarSymbol resolveInternalField(DiagnosticPosition pos, Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1337
                                          Type site, Name name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1338
        Symbol sym = findField(env, site, name, site.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1339
        if (sym.kind == VAR) return (VarSymbol)sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1340
        else throw new FatalError(
1040
c0f5acfd9d15 6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents: 939
diff changeset
  1341
                 diags.fragment("fatal.err.cant.locate.field",
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1342
                                name));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1343
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1344
06bc494ca11e Initial load
duke
parents:
diff changeset
  1345
    /** Find unqualified variable or field with given name.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1346
     *  Synthetic fields always skipped.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1347
     *  @param env     The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1348
     *  @param name    The name of the variable or field.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1349
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1350
    Symbol findVar(Env<AttrContext> env, Name name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1351
        Symbol bestSoFar = varNotFound;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1352
        Env<AttrContext> env1 = env;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1353
        boolean staticOnly = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1354
        while (env1.outer != null) {
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1355
            Symbol sym = null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1356
            if (isStatic(env1)) staticOnly = true;
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1357
            for (Symbol s : env1.info.scope.getSymbolsByName(name)) {
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1358
                if (s.kind == VAR && (s.flags_field & SYNTHETIC) == 0) {
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1359
                    sym = s;
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1360
                    break;
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1361
                }
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1362
            }
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1363
            if (sym == null) {
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1364
                sym = findField(env1, env1.enclClass.sym.type, name, env1.enclClass.sym);
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1365
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1366
            if (sym.exists()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1367
                if (staticOnly &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1368
                    sym.kind == VAR &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1369
                    sym.owner.kind == TYP &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1370
                    (sym.flags() & STATIC) == 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1371
                    return new StaticError(sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1372
                else
06bc494ca11e Initial load
duke
parents:
diff changeset
  1373
                    return sym;
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  1374
            } else {
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  1375
                bestSoFar = bestOf(bestSoFar, sym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1376
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1377
06bc494ca11e Initial load
duke
parents:
diff changeset
  1378
            if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1379
            env1 = env1.outer;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1380
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1381
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1382
        Symbol sym = findField(env, syms.predefClass.type, name, syms.predefClass);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1383
        if (sym.exists())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1384
            return sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1385
        if (bestSoFar.exists())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1386
            return bestSoFar;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1387
19492
767d7330011f 6537020: JCK tests: a compile-time error should be given in case of ambiguously imported fields (types, methods)
mcimadamore
parents: 19129
diff changeset
  1388
        Symbol origin = null;
767d7330011f 6537020: JCK tests: a compile-time error should be given in case of ambiguously imported fields (types, methods)
mcimadamore
parents: 19129
diff changeset
  1389
        for (Scope sc : new Scope[] { env.toplevel.namedImportScope, env.toplevel.starImportScope }) {
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1390
            for (Symbol currentSymbol : sc.getSymbolsByName(name)) {
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1391
                if (currentSymbol.kind != VAR)
19492
767d7330011f 6537020: JCK tests: a compile-time error should be given in case of ambiguously imported fields (types, methods)
mcimadamore
parents: 19129
diff changeset
  1392
                    continue;
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  1393
                // invariant: sym.kind == Symbol.Kind.VAR
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  1394
                if (!bestSoFar.kind.isOverloadError() &&
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  1395
                    currentSymbol.owner != bestSoFar.owner)
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1396
                    return new AmbiguityError(bestSoFar, currentSymbol);
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  1397
                else if (!bestSoFar.kind.betterThan(VAR)) {
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1398
                    origin = sc.getOrigin(currentSymbol).owner;
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1399
                    bestSoFar = isAccessible(env, origin.type, currentSymbol)
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1400
                        ? currentSymbol : new AccessError(env, origin.type, currentSymbol);
19492
767d7330011f 6537020: JCK tests: a compile-time error should be given in case of ambiguously imported fields (types, methods)
mcimadamore
parents: 19129
diff changeset
  1401
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1402
            }
19492
767d7330011f 6537020: JCK tests: a compile-time error should be given in case of ambiguously imported fields (types, methods)
mcimadamore
parents: 19129
diff changeset
  1403
            if (bestSoFar.exists()) break;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1404
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1405
        if (bestSoFar.kind == VAR && bestSoFar.owner.type != origin.type)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1406
            return bestSoFar.clone(origin);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1407
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
  1408
            return bestSoFar;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1409
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1410
06bc494ca11e Initial load
duke
parents:
diff changeset
  1411
    Warner noteWarner = new Warner();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1412
06bc494ca11e Initial load
duke
parents:
diff changeset
  1413
    /** Select the best method for a call site among two choices.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1414
     *  @param env              The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1415
     *  @param site             The original type from where the
06bc494ca11e Initial load
duke
parents:
diff changeset
  1416
     *                          selection takes place.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1417
     *  @param argtypes         The invocation's value arguments,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1418
     *  @param typeargtypes     The invocation's type arguments,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1419
     *  @param sym              Proposed new best match.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1420
     *  @param bestSoFar        Previously found best match.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1421
     *  @param allowBoxing Allow boxing conversions of arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1422
     *  @param useVarargs Box trailing arguments into an array for varargs.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1423
     */
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
  1424
    @SuppressWarnings("fallthrough")
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1425
    Symbol selectBest(Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1426
                      Type site,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1427
                      List<Type> argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1428
                      List<Type> typeargtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1429
                      Symbol sym,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1430
                      Symbol bestSoFar,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1431
                      boolean allowBoxing,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1432
                      boolean useVarargs,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1433
                      boolean operator) {
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  1434
        if (sym.kind == ERR ||
16312
7fb570abcb54 8007461: Regression: bad overload resolution when inner class and outer class have method with same name
mcimadamore
parents: 16301
diff changeset
  1435
                !sym.isInheritedIn(site.tsym, types)) {
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  1436
            return bestSoFar;
16312
7fb570abcb54 8007461: Regression: bad overload resolution when inner class and outer class have method with same name
mcimadamore
parents: 16301
diff changeset
  1437
        } else if (useVarargs && (sym.flags() & VARARGS) == 0) {
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  1438
            return bestSoFar.kind.isOverloadError() ?
24400
f05df7f58567 8037379: fix for JDK-8029569 doesn't cover all possible cases
vromero
parents: 24396
diff changeset
  1439
                    new BadVarargsMethod((ResolveError)bestSoFar.baseSymbol()) :
16312
7fb570abcb54 8007461: Regression: bad overload resolution when inner class and outer class have method with same name
mcimadamore
parents: 16301
diff changeset
  1440
                    bestSoFar;
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  1441
        }
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  1442
        Assert.check(!sym.kind.isOverloadError());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1443
        try {
12915
28cf1e0dafdc 7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents: 12335
diff changeset
  1444
            Type mt = rawInstantiate(env, site, sym, null, argtypes, typeargtypes,
16805
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  1445
                               allowBoxing, useVarargs, types.noWarnings);
17804
6bed8a263318 8014649: Regression: bug in Resolve.resolveOperator
mcimadamore
parents: 17582
diff changeset
  1446
            if (!operator || verboseResolutionMode.contains(VerboseResolutionMode.PREDEF))
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1447
                currentResolutionContext.addApplicableCandidate(sym, mt);
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
  1448
        } catch (InapplicableMethodException ex) {
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1449
            if (!operator)
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1450
                currentResolutionContext.addInapplicableCandidate(sym, ex.getDiagnostic());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1451
            switch (bestSoFar.kind) {
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  1452
                case ABSENT_MTH:
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  1453
                    return new InapplicableSymbolError(currentResolutionContext);
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  1454
                case WRONG_MTH:
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  1455
                    if (operator) return bestSoFar;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  1456
                    bestSoFar = new InapplicableSymbolsError(currentResolutionContext);
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  1457
                default:
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  1458
                    return bestSoFar;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1459
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1460
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1461
        if (!isAccessible(env, site, sym)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1462
            return (bestSoFar.kind == ABSENT_MTH)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1463
                ? new AccessError(env, site, sym)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1464
                : bestSoFar;
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1465
        }
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  1466
        return (bestSoFar.kind.isOverloadError() && bestSoFar.kind != AMBIGUOUS)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1467
            ? sym
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1468
            : mostSpecific(argtypes, sym, bestSoFar, env, site,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1469
                           allowBoxing && operator, useVarargs);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1470
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1471
06bc494ca11e Initial load
duke
parents:
diff changeset
  1472
    /* Return the most specific of the two methods for a call,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1473
     *  given that both are accessible and applicable.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1474
     *  @param m1               A new candidate for most specific.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1475
     *  @param m2               The previous most specific candidate.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1476
     *  @param env              The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1477
     *  @param site             The original type from where the selection
06bc494ca11e Initial load
duke
parents:
diff changeset
  1478
     *                          takes place.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1479
     *  @param allowBoxing Allow boxing conversions of arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1480
     *  @param useVarargs Box trailing arguments into an array for varargs.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1481
     */
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1482
    Symbol mostSpecific(List<Type> argtypes, Symbol m1,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1483
                        Symbol m2,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1484
                        Env<AttrContext> env,
2511
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
  1485
                        final Type site,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1486
                        boolean allowBoxing,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1487
                        boolean useVarargs) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1488
        switch (m2.kind) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1489
        case MTH:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1490
            if (m1 == m2) return m1;
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1491
            boolean m1SignatureMoreSpecific =
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1492
                    signatureMoreSpecific(argtypes, env, site, m1, m2, allowBoxing, useVarargs);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1493
            boolean m2SignatureMoreSpecific =
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1494
                    signatureMoreSpecific(argtypes, env, site, m2, m1, allowBoxing, useVarargs);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1495
            if (m1SignatureMoreSpecific && m2SignatureMoreSpecific) {
7623
66d98f7ba8c7 5088429: varargs overloading problem
mcimadamore
parents: 7209
diff changeset
  1496
                Type mt1 = types.memberType(site, m1);
66d98f7ba8c7 5088429: varargs overloading problem
mcimadamore
parents: 7209
diff changeset
  1497
                Type mt2 = types.memberType(site, m2);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1498
                if (!types.overrideEquivalent(mt1, mt2))
8228
4e6ee38974b2 6910550: javac 1.5.0_17 fails with incorrect error message
mcimadamore
parents: 8045
diff changeset
  1499
                    return ambiguityError(m1, m2);
4e6ee38974b2 6910550: javac 1.5.0_17 fails with incorrect error message
mcimadamore
parents: 8045
diff changeset
  1500
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1501
                // same signature; select (a) the non-bridge method, or
06bc494ca11e Initial load
duke
parents:
diff changeset
  1502
                // (b) the one that overrides the other, or (c) the concrete
06bc494ca11e Initial load
duke
parents:
diff changeset
  1503
                // one, or (d) merge both abstract signatures
8228
4e6ee38974b2 6910550: javac 1.5.0_17 fails with incorrect error message
mcimadamore
parents: 8045
diff changeset
  1504
                if ((m1.flags() & BRIDGE) != (m2.flags() & BRIDGE))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1505
                    return ((m1.flags() & BRIDGE) != 0) ? m2 : m1;
8228
4e6ee38974b2 6910550: javac 1.5.0_17 fails with incorrect error message
mcimadamore
parents: 8045
diff changeset
  1506
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1507
                // if one overrides or hides the other, use it
06bc494ca11e Initial load
duke
parents:
diff changeset
  1508
                TypeSymbol m1Owner = (TypeSymbol)m1.owner;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1509
                TypeSymbol m2Owner = (TypeSymbol)m2.owner;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1510
                if (types.asSuper(m1Owner.type, m2Owner) != null &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1511
                    ((m1.owner.flags_field & INTERFACE) == 0 ||
06bc494ca11e Initial load
duke
parents:
diff changeset
  1512
                     (m2.owner.flags_field & INTERFACE) != 0) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1513
                    m1.overrides(m2, m1Owner, types, false))
06bc494ca11e Initial load
duke
parents:
diff changeset
  1514
                    return m1;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1515
                if (types.asSuper(m2Owner.type, m1Owner) != null &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1516
                    ((m2.owner.flags_field & INTERFACE) == 0 ||
06bc494ca11e Initial load
duke
parents:
diff changeset
  1517
                     (m1.owner.flags_field & INTERFACE) != 0) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1518
                    m2.overrides(m1, m2Owner, types, false))
06bc494ca11e Initial load
duke
parents:
diff changeset
  1519
                    return m2;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1520
                boolean m1Abstract = (m1.flags() & ABSTRACT) != 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1521
                boolean m2Abstract = (m2.flags() & ABSTRACT) != 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1522
                if (m1Abstract && !m2Abstract) return m2;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1523
                if (m2Abstract && !m1Abstract) return m1;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1524
                // both abstract or both concrete
15038
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  1525
                return ambiguityError(m1, m2);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1526
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1527
            if (m1SignatureMoreSpecific) return m1;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1528
            if (m2SignatureMoreSpecific) return m2;
8228
4e6ee38974b2 6910550: javac 1.5.0_17 fails with incorrect error message
mcimadamore
parents: 8045
diff changeset
  1529
            return ambiguityError(m1, m2);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1530
        case AMBIGUOUS:
24393
381ade32a034 8041663: Sensitive dependence on location of nested interface
jlahoda
parents: 24293
diff changeset
  1531
            //compare m1 to ambiguous methods in m2
22161
d79fb23b5dca 8029569: internal javac cast exception when resolving varargs ambiguity
vromero
parents: 21718
diff changeset
  1532
            AmbiguityError e = (AmbiguityError)m2.baseSymbol();
24393
381ade32a034 8041663: Sensitive dependence on location of nested interface
jlahoda
parents: 24293
diff changeset
  1533
            boolean m1MoreSpecificThanAnyAmbiguous = true;
381ade32a034 8041663: Sensitive dependence on location of nested interface
jlahoda
parents: 24293
diff changeset
  1534
            boolean allAmbiguousMoreSpecificThanM1 = true;
15038
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  1535
            for (Symbol s : e.ambiguousSyms) {
24393
381ade32a034 8041663: Sensitive dependence on location of nested interface
jlahoda
parents: 24293
diff changeset
  1536
                Symbol moreSpecific = mostSpecific(argtypes, m1, s, env, site, allowBoxing, useVarargs);
381ade32a034 8041663: Sensitive dependence on location of nested interface
jlahoda
parents: 24293
diff changeset
  1537
                m1MoreSpecificThanAnyAmbiguous &= moreSpecific == m1;
381ade32a034 8041663: Sensitive dependence on location of nested interface
jlahoda
parents: 24293
diff changeset
  1538
                allAmbiguousMoreSpecificThanM1 &= moreSpecific == s;
15038
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  1539
            }
24393
381ade32a034 8041663: Sensitive dependence on location of nested interface
jlahoda
parents: 24293
diff changeset
  1540
            if (m1MoreSpecificThanAnyAmbiguous)
381ade32a034 8041663: Sensitive dependence on location of nested interface
jlahoda
parents: 24293
diff changeset
  1541
                return m1;
381ade32a034 8041663: Sensitive dependence on location of nested interface
jlahoda
parents: 24293
diff changeset
  1542
            //if m1 is more specific than some ambiguous methods, but other ambiguous methods are
381ade32a034 8041663: Sensitive dependence on location of nested interface
jlahoda
parents: 24293
diff changeset
  1543
            //more specific than m1, add it as a new ambiguous method:
381ade32a034 8041663: Sensitive dependence on location of nested interface
jlahoda
parents: 24293
diff changeset
  1544
            if (!allAmbiguousMoreSpecificThanM1)
381ade32a034 8041663: Sensitive dependence on location of nested interface
jlahoda
parents: 24293
diff changeset
  1545
                e.addAmbiguousSymbol(m1);
381ade32a034 8041663: Sensitive dependence on location of nested interface
jlahoda
parents: 24293
diff changeset
  1546
            return e;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1547
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1548
            throw new AssertionError();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1549
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1550
    }
7623
66d98f7ba8c7 5088429: varargs overloading problem
mcimadamore
parents: 7209
diff changeset
  1551
    //where
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1552
    private boolean signatureMoreSpecific(List<Type> actuals, Env<AttrContext> env, Type site, Symbol m1, Symbol m2, boolean allowBoxing, boolean useVarargs) {
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1553
        noteWarner.clear();
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1554
        int maxLength = Math.max(
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1555
                            Math.max(m1.type.getParameterTypes().length(), actuals.length()),
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1556
                            m2.type.getParameterTypes().length());
16805
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  1557
        MethodResolutionContext prevResolutionContext = currentResolutionContext;
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  1558
        try {
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  1559
            currentResolutionContext = new MethodResolutionContext();
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  1560
            currentResolutionContext.step = prevResolutionContext.step;
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  1561
            currentResolutionContext.methodCheck =
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  1562
                    prevResolutionContext.methodCheck.mostSpecificCheck(actuals, !allowBoxing);
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  1563
            Type mst = instantiate(env, site, m2, null,
24293
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24226
diff changeset
  1564
                    adjustArgs(types.cvarLowerBounds(types.memberType(site, m1).getParameterTypes()), m1, maxLength, useVarargs), null,
16805
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  1565
                    allowBoxing, useVarargs, noteWarner);
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  1566
            return mst != null &&
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  1567
                    !noteWarner.hasLint(Lint.LintCategory.UNCHECKED);
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  1568
        } finally {
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  1569
            currentResolutionContext = prevResolutionContext;
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  1570
        }
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1571
    }
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19652
diff changeset
  1572
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19652
diff changeset
  1573
    List<Type> adjustArgs(List<Type> args, Symbol msym, int length, boolean allowVarargs) {
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1574
        if ((msym.flags() & VARARGS) != 0 && allowVarargs) {
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1575
            Type varargsElem = types.elemtype(args.last());
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1576
            if (varargsElem == null) {
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1577
                Assert.error("Bad varargs = " + args.last() + " " + msym);
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1578
            }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1579
            List<Type> newArgs = args.reverse().tail.prepend(varargsElem).reverse();
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1580
            while (newArgs.length() < length) {
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1581
                newArgs = newArgs.append(newArgs.last());
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1582
            }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1583
            return newArgs;
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1584
        } else {
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1585
            return args;
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
  1586
        }
7623
66d98f7ba8c7 5088429: varargs overloading problem
mcimadamore
parents: 7209
diff changeset
  1587
    }
66d98f7ba8c7 5088429: varargs overloading problem
mcimadamore
parents: 7209
diff changeset
  1588
    //where
10197
28afe0fb34c8 7062745: Regression: difference in overload resolution when two methods are maximally specific
mcimadamore
parents: 9720
diff changeset
  1589
    Type mostSpecificReturnType(Type mt1, Type mt2) {
28afe0fb34c8 7062745: Regression: difference in overload resolution when two methods are maximally specific
mcimadamore
parents: 9720
diff changeset
  1590
        Type rt1 = mt1.getReturnType();
28afe0fb34c8 7062745: Regression: difference in overload resolution when two methods are maximally specific
mcimadamore
parents: 9720
diff changeset
  1591
        Type rt2 = mt2.getReturnType();
28afe0fb34c8 7062745: Regression: difference in overload resolution when two methods are maximally specific
mcimadamore
parents: 9720
diff changeset
  1592
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14062
diff changeset
  1593
        if (mt1.hasTag(FORALL) && mt2.hasTag(FORALL)) {
10197
28afe0fb34c8 7062745: Regression: difference in overload resolution when two methods are maximally specific
mcimadamore
parents: 9720
diff changeset
  1594
            //if both are generic methods, adjust return type ahead of subtyping check
28afe0fb34c8 7062745: Regression: difference in overload resolution when two methods are maximally specific
mcimadamore
parents: 9720
diff changeset
  1595
            rt1 = types.subst(rt1, mt1.getTypeArguments(), mt2.getTypeArguments());
28afe0fb34c8 7062745: Regression: difference in overload resolution when two methods are maximally specific
mcimadamore
parents: 9720
diff changeset
  1596
        }
28afe0fb34c8 7062745: Regression: difference in overload resolution when two methods are maximally specific
mcimadamore
parents: 9720
diff changeset
  1597
        //first use subtyping, then return type substitutability
28afe0fb34c8 7062745: Regression: difference in overload resolution when two methods are maximally specific
mcimadamore
parents: 9720
diff changeset
  1598
        if (types.isSubtype(rt1, rt2)) {
28afe0fb34c8 7062745: Regression: difference in overload resolution when two methods are maximally specific
mcimadamore
parents: 9720
diff changeset
  1599
            return mt1;
28afe0fb34c8 7062745: Regression: difference in overload resolution when two methods are maximally specific
mcimadamore
parents: 9720
diff changeset
  1600
        } else if (types.isSubtype(rt2, rt1)) {
28afe0fb34c8 7062745: Regression: difference in overload resolution when two methods are maximally specific
mcimadamore
parents: 9720
diff changeset
  1601
            return mt2;
28afe0fb34c8 7062745: Regression: difference in overload resolution when two methods are maximally specific
mcimadamore
parents: 9720
diff changeset
  1602
        } else if (types.returnTypeSubstitutable(mt1, mt2)) {
28afe0fb34c8 7062745: Regression: difference in overload resolution when two methods are maximally specific
mcimadamore
parents: 9720
diff changeset
  1603
            return mt1;
28afe0fb34c8 7062745: Regression: difference in overload resolution when two methods are maximally specific
mcimadamore
parents: 9720
diff changeset
  1604
        } else if (types.returnTypeSubstitutable(mt2, mt1)) {
28afe0fb34c8 7062745: Regression: difference in overload resolution when two methods are maximally specific
mcimadamore
parents: 9720
diff changeset
  1605
            return mt2;
28afe0fb34c8 7062745: Regression: difference in overload resolution when two methods are maximally specific
mcimadamore
parents: 9720
diff changeset
  1606
        } else {
28afe0fb34c8 7062745: Regression: difference in overload resolution when two methods are maximally specific
mcimadamore
parents: 9720
diff changeset
  1607
            return null;
28afe0fb34c8 7062745: Regression: difference in overload resolution when two methods are maximally specific
mcimadamore
parents: 9720
diff changeset
  1608
        }
28afe0fb34c8 7062745: Regression: difference in overload resolution when two methods are maximally specific
mcimadamore
parents: 9720
diff changeset
  1609
    }
28afe0fb34c8 7062745: Regression: difference in overload resolution when two methods are maximally specific
mcimadamore
parents: 9720
diff changeset
  1610
    //where
8228
4e6ee38974b2 6910550: javac 1.5.0_17 fails with incorrect error message
mcimadamore
parents: 8045
diff changeset
  1611
    Symbol ambiguityError(Symbol m1, Symbol m2) {
4e6ee38974b2 6910550: javac 1.5.0_17 fails with incorrect error message
mcimadamore
parents: 8045
diff changeset
  1612
        if (((m1.flags() | m2.flags()) & CLASH) != 0) {
4e6ee38974b2 6910550: javac 1.5.0_17 fails with incorrect error message
mcimadamore
parents: 8045
diff changeset
  1613
            return (m1.flags() & CLASH) == 0 ? m1 : m2;
4e6ee38974b2 6910550: javac 1.5.0_17 fails with incorrect error message
mcimadamore
parents: 8045
diff changeset
  1614
        } else {
4e6ee38974b2 6910550: javac 1.5.0_17 fails with incorrect error message
mcimadamore
parents: 8045
diff changeset
  1615
            return new AmbiguityError(m1, m2);
4e6ee38974b2 6910550: javac 1.5.0_17 fails with incorrect error message
mcimadamore
parents: 8045
diff changeset
  1616
        }
4e6ee38974b2 6910550: javac 1.5.0_17 fails with incorrect error message
mcimadamore
parents: 8045
diff changeset
  1617
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1618
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  1619
    Symbol findMethodInScope(Env<AttrContext> env,
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1620
            Type site,
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1621
            Name name,
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1622
            List<Type> argtypes,
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1623
            List<Type> typeargtypes,
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1624
            Scope sc,
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1625
            Symbol bestSoFar,
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1626
            boolean allowBoxing,
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1627
            boolean useVarargs,
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1628
            boolean operator,
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1629
            boolean abstractok) {
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1630
        for (Symbol s : sc.getSymbolsByName(name, new LookupFilter(abstractok))) {
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1631
            bestSoFar = selectBest(env, site, argtypes, typeargtypes, s,
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1632
                    bestSoFar, allowBoxing, useVarargs, operator);
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1633
        }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1634
        return bestSoFar;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1635
    }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1636
    //where
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1637
        class LookupFilter implements Filter<Symbol> {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1638
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1639
            boolean abstractOk;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1640
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1641
            LookupFilter(boolean abstractOk) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1642
                this.abstractOk = abstractOk;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1643
            }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1644
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1645
            public boolean accepts(Symbol s) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1646
                long flags = s.flags();
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1647
                return s.kind == MTH &&
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1648
                        (flags & SYNTHETIC) == 0 &&
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1649
                        (abstractOk ||
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1650
                        (flags & DEFAULT) != 0 ||
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1651
                        (flags & ABSTRACT) == 0);
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1652
            }
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22161
diff changeset
  1653
        }
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1654
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1655
    /** Find best qualified method matching given name, type and value
06bc494ca11e Initial load
duke
parents:
diff changeset
  1656
     *  arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1657
     *  @param env       The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1658
     *  @param site      The original type from where the selection
06bc494ca11e Initial load
duke
parents:
diff changeset
  1659
     *                   takes place.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1660
     *  @param name      The method's name.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1661
     *  @param argtypes  The method's value arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1662
     *  @param typeargtypes The method's type arguments
06bc494ca11e Initial load
duke
parents:
diff changeset
  1663
     *  @param allowBoxing Allow boxing conversions of arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1664
     *  @param useVarargs Box trailing arguments into an array for varargs.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1665
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1666
    Symbol findMethod(Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1667
                      Type site,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1668
                      Name name,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1669
                      List<Type> argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1670
                      List<Type> typeargtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1671
                      boolean allowBoxing,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1672
                      boolean useVarargs,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1673
                      boolean operator) {
5736
ee0850472ca1 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 5321
diff changeset
  1674
        Symbol bestSoFar = methodNotFound;
10816
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
  1675
        bestSoFar = findMethod(env,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1676
                          site,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1677
                          name,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1678
                          argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1679
                          typeargtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1680
                          site.tsym.type,
5736
ee0850472ca1 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 5321
diff changeset
  1681
                          bestSoFar,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1682
                          allowBoxing,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1683
                          useVarargs,
14045
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1684
                          operator);
10816
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
  1685
        return bestSoFar;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1686
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1687
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
  1688
    private Symbol findMethod(Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1689
                              Type site,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1690
                              Name name,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1691
                              List<Type> argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1692
                              List<Type> typeargtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1693
                              Type intype,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1694
                              Symbol bestSoFar,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1695
                              boolean allowBoxing,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1696
                              boolean useVarargs,
14045
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1697
                              boolean operator) {
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1698
        @SuppressWarnings({"unchecked","rawtypes"})
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1699
        List<Type>[] itypes = (List<Type>[])new List[] { List.<Type>nil(), List.<Type>nil() };
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  1700
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1701
        InterfaceLookupPhase iphase = InterfaceLookupPhase.ABSTRACT_OK;
14045
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1702
        for (TypeSymbol s : superclasses(intype)) {
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  1703
            bestSoFar = findMethodInScope(env, site, name, argtypes, typeargtypes,
14045
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1704
                    s.members(), bestSoFar, allowBoxing, useVarargs, operator, true);
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1705
            if (name == names.init) return bestSoFar;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1706
            iphase = (iphase == null) ? null : iphase.update(s, this);
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1707
            if (iphase != null) {
14045
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1708
                for (Type itype : types.interfaces(s.type)) {
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1709
                    itypes[iphase.ordinal()] = types.union(types.closure(itype), itypes[iphase.ordinal()]);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1710
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1711
            }
14045
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1712
        }
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1713
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  1714
        Symbol concrete = bestSoFar.kind.isValid() &&
14045
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1715
                (bestSoFar.flags() & ABSTRACT) == 0 ?
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1716
                bestSoFar : methodNotFound;
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1717
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1718
        for (InterfaceLookupPhase iphase2 : InterfaceLookupPhase.values()) {
14045
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1719
            //keep searching for abstract methods
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1720
            for (Type itype : itypes[iphase2.ordinal()]) {
14045
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1721
                if (!itype.isInterface()) continue; //skip j.l.Object (included by Types.closure())
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1722
                if (iphase2 == InterfaceLookupPhase.DEFAULT_OK &&
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1723
                        (itype.tsym.flags() & DEFAULT) == 0) continue;
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  1724
                bestSoFar = findMethodInScope(env, site, name, argtypes, typeargtypes,
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1725
                        itype.tsym.members(), bestSoFar, allowBoxing, useVarargs, operator, true);
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1726
                if (concrete != bestSoFar &&
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  1727
                    concrete.kind.isValid() &&
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  1728
                    bestSoFar.kind.isValid() &&
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1729
                        types.isSubSignature(concrete.type, bestSoFar.type)) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1730
                    //this is an hack - as javac does not do full membership checks
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1731
                    //most specific ends up comparing abstract methods that might have
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1732
                    //been implemented by some concrete method in a subclass and,
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1733
                    //because of raw override, it is possible for an abstract method
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1734
                    //to be more specific than the concrete method - so we need
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1735
                    //to explicitly call that out (see CR 6178365)
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1736
                    bestSoFar = concrete;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1737
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1738
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1739
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1740
        return bestSoFar;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1741
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1742
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1743
    enum InterfaceLookupPhase {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1744
        ABSTRACT_OK() {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1745
            @Override
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1746
            InterfaceLookupPhase update(Symbol s, Resolve rs) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1747
                //We should not look for abstract methods if receiver is a concrete class
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1748
                //(as concrete classes are expected to implement all abstracts coming
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1749
                //from superinterfaces)
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1750
                if ((s.flags() & (ABSTRACT | INTERFACE | ENUM)) != 0) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1751
                    return this;
23105
1d1ea26a10ae 8030855: Default methods should be visible under source previous to 8
vromero
parents: 22442
diff changeset
  1752
                } else {
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1753
                    return DEFAULT_OK;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1754
                }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1755
            }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1756
        },
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1757
        DEFAULT_OK() {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1758
            @Override
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1759
            InterfaceLookupPhase update(Symbol s, Resolve rs) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1760
                return this;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1761
            }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1762
        };
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1763
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1764
        abstract InterfaceLookupPhase update(Symbol s, Resolve rs);
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1765
    }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1766
14045
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1767
    /**
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1768
     * Return an Iterable object to scan the superclasses of a given type.
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1769
     * It's crucial that the scan is done lazily, as we don't want to accidentally
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1770
     * access more supertypes than strictly needed (as this could trigger completion
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1771
     * errors if some of the not-needed supertypes are missing/ill-formed).
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1772
     */
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1773
    Iterable<TypeSymbol> superclasses(final Type intype) {
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1774
        return new Iterable<TypeSymbol>() {
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1775
            public Iterator<TypeSymbol> iterator() {
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1776
                return new Iterator<TypeSymbol>() {
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1777
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1778
                    List<TypeSymbol> seen = List.nil();
14052
8b839ae9074b 8000233: Fix issues in recent push
mcimadamore
parents: 14051
diff changeset
  1779
                    TypeSymbol currentSym = symbolFor(intype);
8b839ae9074b 8000233: Fix issues in recent push
mcimadamore
parents: 14051
diff changeset
  1780
                    TypeSymbol prevSym = null;
14045
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1781
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1782
                    public boolean hasNext() {
14052
8b839ae9074b 8000233: Fix issues in recent push
mcimadamore
parents: 14051
diff changeset
  1783
                        if (currentSym == syms.noSymbol) {
8b839ae9074b 8000233: Fix issues in recent push
mcimadamore
parents: 14051
diff changeset
  1784
                            currentSym = symbolFor(types.supertype(prevSym.type));
8b839ae9074b 8000233: Fix issues in recent push
mcimadamore
parents: 14051
diff changeset
  1785
                        }
14045
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1786
                        return currentSym != null;
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1787
                    }
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1788
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1789
                    public TypeSymbol next() {
14052
8b839ae9074b 8000233: Fix issues in recent push
mcimadamore
parents: 14051
diff changeset
  1790
                        prevSym = currentSym;
8b839ae9074b 8000233: Fix issues in recent push
mcimadamore
parents: 14051
diff changeset
  1791
                        currentSym = syms.noSymbol;
8b839ae9074b 8000233: Fix issues in recent push
mcimadamore
parents: 14051
diff changeset
  1792
                        Assert.check(prevSym != null || prevSym != syms.noSymbol);
14045
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1793
                        return prevSym;
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1794
                    }
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1795
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1796
                    public void remove() {
14052
8b839ae9074b 8000233: Fix issues in recent push
mcimadamore
parents: 14051
diff changeset
  1797
                        throw new UnsupportedOperationException();
14045
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1798
                    }
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1799
14052
8b839ae9074b 8000233: Fix issues in recent push
mcimadamore
parents: 14051
diff changeset
  1800
                    TypeSymbol symbolFor(Type t) {
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14062
diff changeset
  1801
                        if (!t.hasTag(CLASS) &&
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14062
diff changeset
  1802
                                !t.hasTag(TYPEVAR)) {
14045
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1803
                            return null;
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1804
                        }
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14062
diff changeset
  1805
                        while (t.hasTag(TYPEVAR))
14052
8b839ae9074b 8000233: Fix issues in recent push
mcimadamore
parents: 14051
diff changeset
  1806
                            t = t.getUpperBound();
8b839ae9074b 8000233: Fix issues in recent push
mcimadamore
parents: 14051
diff changeset
  1807
                        if (seen.contains(t.tsym)) {
14045
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1808
                            //degenerate case in which we have a circular
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1809
                            //class hierarchy - because of ill-formed classfiles
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1810
                            return null;
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1811
                        }
14052
8b839ae9074b 8000233: Fix issues in recent push
mcimadamore
parents: 14051
diff changeset
  1812
                        seen = seen.prepend(t.tsym);
8b839ae9074b 8000233: Fix issues in recent push
mcimadamore
parents: 14051
diff changeset
  1813
                        return t.tsym;
14045
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1814
                    }
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1815
                };
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1816
            }
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1817
        };
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1818
    }
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1819
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1820
    /** Find unqualified method matching given name, type and value arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1821
     *  @param env       The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1822
     *  @param name      The method's name.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1823
     *  @param argtypes  The method's value arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1824
     *  @param typeargtypes  The method's type arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1825
     *  @param allowBoxing Allow boxing conversions of arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1826
     *  @param useVarargs Box trailing arguments into an array for varargs.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1827
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1828
    Symbol findFun(Env<AttrContext> env, Name name,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1829
                   List<Type> argtypes, List<Type> typeargtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1830
                   boolean allowBoxing, boolean useVarargs) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1831
        Symbol bestSoFar = methodNotFound;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1832
        Env<AttrContext> env1 = env;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1833
        boolean staticOnly = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1834
        while (env1.outer != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1835
            if (isStatic(env1)) staticOnly = true;
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1836
            Symbol sym = findMethod(
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1837
                env1, env1.enclClass.sym.type, name, argtypes, typeargtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1838
                allowBoxing, useVarargs, false);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1839
            if (sym.exists()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1840
                if (staticOnly &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1841
                    sym.kind == MTH &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1842
                    sym.owner.kind == TYP &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1843
                    (sym.flags() & STATIC) == 0) return new StaticError(sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1844
                else return sym;
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  1845
            } else {
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  1846
                bestSoFar = bestOf(bestSoFar, sym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1847
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1848
            if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1849
            env1 = env1.outer;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1850
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1851
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1852
        Symbol sym = findMethod(env, syms.predefClass.type, name, argtypes,
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1853
                                typeargtypes, allowBoxing, useVarargs, false);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1854
        if (sym.exists())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1855
            return sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1856
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1857
        for (Symbol currentSym : env.toplevel.namedImportScope.getSymbolsByName(name)) {
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1858
            Symbol origin = env.toplevel.namedImportScope.getOrigin(currentSym).owner;
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1859
            if (currentSym.kind == MTH) {
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1860
                if (currentSym.owner.type != origin.type)
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1861
                    currentSym = currentSym.clone(origin);
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1862
                if (!isAccessible(env, origin.type, currentSym))
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1863
                    currentSym = new AccessError(env, origin.type, currentSym);
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1864
                bestSoFar = selectBest(env, origin.type,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1865
                                       argtypes, typeargtypes,
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1866
                                       currentSym, bestSoFar,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1867
                                       allowBoxing, useVarargs, false);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1868
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1869
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1870
        if (bestSoFar.exists())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1871
            return bestSoFar;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1872
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1873
        for (Symbol currentSym : env.toplevel.starImportScope.getSymbolsByName(name)) {
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1874
            Symbol origin = env.toplevel.starImportScope.getOrigin(currentSym).owner;
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1875
            if (currentSym.kind == MTH) {
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1876
                if (currentSym.owner.type != origin.type)
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1877
                    currentSym = currentSym.clone(origin);
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1878
                if (!isAccessible(env, origin.type, currentSym))
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1879
                    currentSym = new AccessError(env, origin.type, currentSym);
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1880
                bestSoFar = selectBest(env, origin.type,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1881
                                       argtypes, typeargtypes,
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1882
                                       currentSym, bestSoFar,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1883
                                       allowBoxing, useVarargs, false);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1884
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1885
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1886
        return bestSoFar;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1887
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1888
06bc494ca11e Initial load
duke
parents:
diff changeset
  1889
    /** Load toplevel or member class with given fully qualified name and
06bc494ca11e Initial load
duke
parents:
diff changeset
  1890
     *  verify that it is accessible.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1891
     *  @param env       The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1892
     *  @param name      The fully qualified name of the class to be loaded.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1893
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1894
    Symbol loadClass(Env<AttrContext> env, Name name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1895
        try {
24604
7f68545b5128 8041422: Split javac ClassReader into ClassReader+ClassFinder
jjg
parents: 24404
diff changeset
  1896
            ClassSymbol c = finder.loadClass(name);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1897
            return isAccessible(env, c) ? c : new AccessError(c);
24604
7f68545b5128 8041422: Split javac ClassReader into ClassReader+ClassFinder
jjg
parents: 24404
diff changeset
  1898
        } catch (ClassFinder.BadClassFile err) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1899
            throw err;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1900
        } catch (CompletionFailure ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1901
            return typeNotFound;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1902
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1903
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1904
19652
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  1905
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  1906
    /**
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  1907
     * Find a type declared in a scope (not inherited).  Return null
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  1908
     * if none is found.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1909
     *  @param env       The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1910
     *  @param site      The original type from where the selection takes
06bc494ca11e Initial load
duke
parents:
diff changeset
  1911
     *                   place.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1912
     *  @param name      The type's name.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1913
     *  @param c         The class to search for the member type. This is
06bc494ca11e Initial load
duke
parents:
diff changeset
  1914
     *                   always a superclass or implemented interface of
06bc494ca11e Initial load
duke
parents:
diff changeset
  1915
     *                   site's class.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1916
     */
19652
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  1917
    Symbol findImmediateMemberType(Env<AttrContext> env,
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  1918
                                   Type site,
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  1919
                                   Name name,
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  1920
                                   TypeSymbol c) {
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1921
        for (Symbol sym : c.members().getSymbolsByName(name)) {
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1922
            if (sym.kind == TYP) {
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1923
                return isAccessible(env, site, sym)
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1924
                    ? sym
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1925
                    : new AccessError(env, site, sym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1926
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1927
        }
19652
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  1928
        return typeNotFound;
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  1929
    }
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  1930
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  1931
    /** Find a member type inherited from a superclass or interface.
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  1932
     *  @param env       The current environment.
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  1933
     *  @param site      The original type from where the selection takes
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  1934
     *                   place.
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  1935
     *  @param name      The type's name.
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  1936
     *  @param c         The class to search for the member type. This is
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  1937
     *                   always a superclass or implemented interface of
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  1938
     *                   site's class.
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  1939
     */
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  1940
    Symbol findInheritedMemberType(Env<AttrContext> env,
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  1941
                                   Type site,
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  1942
                                   Name name,
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  1943
                                   TypeSymbol c) {
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  1944
        Symbol bestSoFar = typeNotFound;
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  1945
        Symbol sym;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1946
        Type st = types.supertype(c.type);
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14062
diff changeset
  1947
        if (st != null && st.hasTag(CLASS)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1948
            sym = findMemberType(env, site, name, st.tsym);
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  1949
            bestSoFar = bestOf(bestSoFar, sym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1950
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1951
        for (List<Type> l = types.interfaces(c.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1952
             bestSoFar.kind != AMBIGUOUS && l.nonEmpty();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1953
             l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1954
            sym = findMemberType(env, site, name, l.head.tsym);
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  1955
            if (!bestSoFar.kind.isOverloadError() &&
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  1956
                !sym.kind.isOverloadError() &&
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1957
                sym.owner != bestSoFar.owner)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1958
                bestSoFar = new AmbiguityError(bestSoFar, sym);
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  1959
            else
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  1960
                bestSoFar = bestOf(bestSoFar, sym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1961
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1962
        return bestSoFar;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1963
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1964
19652
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  1965
    /** Find qualified member type.
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  1966
     *  @param env       The current environment.
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  1967
     *  @param site      The original type from where the selection takes
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  1968
     *                   place.
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  1969
     *  @param name      The type's name.
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  1970
     *  @param c         The class to search for the member type. This is
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  1971
     *                   always a superclass or implemented interface of
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  1972
     *                   site's class.
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  1973
     */
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  1974
    Symbol findMemberType(Env<AttrContext> env,
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  1975
                          Type site,
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  1976
                          Name name,
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  1977
                          TypeSymbol c) {
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  1978
        Symbol sym = findImmediateMemberType(env, site, name, c);
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  1979
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  1980
        if (sym != typeNotFound)
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  1981
            return sym;
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  1982
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  1983
        return findInheritedMemberType(env, site, name, c);
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  1984
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  1985
    }
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  1986
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1987
    /** Find a global type in given scope and load corresponding class.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1988
     *  @param env       The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1989
     *  @param scope     The scope in which to look for the type.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1990
     *  @param name      The type's name.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1991
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1992
    Symbol findGlobalType(Env<AttrContext> env, Scope scope, Name name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1993
        Symbol bestSoFar = typeNotFound;
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1994
        for (Symbol s : scope.getSymbolsByName(name)) {
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1995
            Symbol sym = loadClass(env, s.flatName());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1996
            if (bestSoFar.kind == TYP && sym.kind == TYP &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1997
                bestSoFar != sym)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1998
                return new AmbiguityError(bestSoFar, sym);
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  1999
            else
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2000
                bestSoFar = bestOf(bestSoFar, sym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2001
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2002
        return bestSoFar;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2003
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2004
19652
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2005
    Symbol findTypeVar(Env<AttrContext> env, Name name, boolean staticOnly) {
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  2006
        for (Symbol sym : env.info.scope.getSymbolsByName(name)) {
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  2007
            if (sym.kind == TYP) {
19652
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2008
                if (staticOnly &&
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  2009
                    sym.type.hasTag(TYPEVAR) &&
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  2010
                    sym.owner.kind == TYP)
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  2011
                    return new StaticError(sym);
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  2012
                return sym;
19652
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2013
            }
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2014
        }
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2015
        return typeNotFound;
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2016
    }
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2017
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2018
    /** Find an unqualified type symbol.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2019
     *  @param env       The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2020
     *  @param name      The type's name.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2021
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2022
    Symbol findType(Env<AttrContext> env, Name name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2023
        Symbol bestSoFar = typeNotFound;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2024
        Symbol sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2025
        boolean staticOnly = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2026
        for (Env<AttrContext> env1 = env; env1.outer != null; env1 = env1.outer) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2027
            if (isStatic(env1)) staticOnly = true;
19652
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2028
            // First, look for a type variable and the first member type
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2029
            final Symbol tyvar = findTypeVar(env1, name, staticOnly);
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2030
            sym = findImmediateMemberType(env1, env1.enclClass.sym.type,
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2031
                                          name, env1.enclClass.sym);
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2032
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2033
            // Return the type variable if we have it, and have no
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2034
            // immediate member, OR the type variable is for a method.
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2035
            if (tyvar != typeNotFound) {
27386
784414cffd9a 8035259: javac, incorrect shadowing of classes vs type parameters
emc
parents: 27224
diff changeset
  2036
                if (env.baseClause || sym == typeNotFound ||
19652
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2037
                    (tyvar.kind == TYP && tyvar.exists() &&
27386
784414cffd9a 8035259: javac, incorrect shadowing of classes vs type parameters
emc
parents: 27224
diff changeset
  2038
                     tyvar.owner.kind == MTH)) {
19652
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2039
                    return tyvar;
27386
784414cffd9a 8035259: javac, incorrect shadowing of classes vs type parameters
emc
parents: 27224
diff changeset
  2040
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2041
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2042
19652
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2043
            // If the environment is a class def, finish up,
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2044
            // otherwise, do the entire findMemberType
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2045
            if (sym == typeNotFound)
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2046
                sym = findInheritedMemberType(env1, env1.enclClass.sym.type,
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2047
                                              name, env1.enclClass.sym);
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2048
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2049
            if (staticOnly && sym.kind == TYP &&
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14062
diff changeset
  2050
                sym.type.hasTag(CLASS) &&
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14062
diff changeset
  2051
                sym.type.getEnclosingType().hasTag(CLASS) &&
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2052
                env1.enclClass.sym.type.isParameterized() &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  2053
                sym.type.getEnclosingType().isParameterized())
06bc494ca11e Initial load
duke
parents:
diff changeset
  2054
                return new StaticError(sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2055
            else if (sym.exists()) return sym;
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2056
            else bestSoFar = bestOf(bestSoFar, sym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2057
06bc494ca11e Initial load
duke
parents:
diff changeset
  2058
            JCClassDecl encl = env1.baseClause ? (JCClassDecl)env1.tree : env1.enclClass;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2059
            if ((encl.sym.flags() & STATIC) != 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2060
                staticOnly = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2061
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2062
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10816
diff changeset
  2063
        if (!env.tree.hasTag(IMPORT)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2064
            sym = findGlobalType(env, env.toplevel.namedImportScope, name);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2065
            if (sym.exists()) return sym;
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2066
            else bestSoFar = bestOf(bestSoFar, sym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2067
06bc494ca11e Initial load
duke
parents:
diff changeset
  2068
            sym = findGlobalType(env, env.toplevel.packge.members(), name);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2069
            if (sym.exists()) return sym;
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2070
            else bestSoFar = bestOf(bestSoFar, sym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2071
06bc494ca11e Initial load
duke
parents:
diff changeset
  2072
            sym = findGlobalType(env, env.toplevel.starImportScope, name);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2073
            if (sym.exists()) return sym;
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2074
            else bestSoFar = bestOf(bestSoFar, sym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2075
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2076
06bc494ca11e Initial load
duke
parents:
diff changeset
  2077
        return bestSoFar;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2078
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2079
06bc494ca11e Initial load
duke
parents:
diff changeset
  2080
    /** Find an unqualified identifier which matches a specified kind set.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2081
     *  @param env       The current environment.
14541
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14446
diff changeset
  2082
     *  @param name      The identifier's name.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2083
     *  @param kind      Indicates the possible symbol kinds
06bc494ca11e Initial load
duke
parents:
diff changeset
  2084
     *                   (a subset of VAL, TYP, PCK).
06bc494ca11e Initial load
duke
parents:
diff changeset
  2085
     */
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2086
    Symbol findIdent(Env<AttrContext> env, Name name, KindSelector kind) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2087
        Symbol bestSoFar = typeNotFound;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2088
        Symbol sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2089
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2090
        if (kind.contains(KindSelector.VAL)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2091
            sym = findVar(env, name);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2092
            if (sym.exists()) return sym;
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2093
            else bestSoFar = bestOf(bestSoFar, sym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2094
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2095
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2096
        if (kind.contains(KindSelector.TYP)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2097
            sym = findType(env, name);
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2098
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2099
            if (sym.exists()) return sym;
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2100
            else bestSoFar = bestOf(bestSoFar, sym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2101
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2102
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2103
        if (kind.contains(KindSelector.PCK))
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2104
            return syms.enterPackage(name);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2105
        else return bestSoFar;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2106
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2107
06bc494ca11e Initial load
duke
parents:
diff changeset
  2108
    /** Find an identifier in a package which matches a specified kind set.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2109
     *  @param env       The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2110
     *  @param name      The identifier's name.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2111
     *  @param kind      Indicates the possible symbol kinds
06bc494ca11e Initial load
duke
parents:
diff changeset
  2112
     *                   (a nonempty subset of TYP, PCK).
06bc494ca11e Initial load
duke
parents:
diff changeset
  2113
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2114
    Symbol findIdentInPackage(Env<AttrContext> env, TypeSymbol pck,
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2115
                              Name name, KindSelector kind) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2116
        Name fullname = TypeSymbol.formFullName(name, pck);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2117
        Symbol bestSoFar = typeNotFound;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2118
        PackageSymbol pack = null;
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2119
        if (kind.contains(KindSelector.PCK)) {
22442
8fd30fc4e3a3 8029690: Move symbol creation from ClassReader to Symtab
jfranck
parents: 22440
diff changeset
  2120
            pack = syms.enterPackage(fullname);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2121
            if (pack.exists()) return pack;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2122
        }
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2123
        if (kind.contains(KindSelector.TYP)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2124
            Symbol sym = loadClass(env, fullname);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2125
            if (sym.exists()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2126
                // don't allow programs to use flatnames
06bc494ca11e Initial load
duke
parents:
diff changeset
  2127
                if (name == sym.name) return sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2128
            }
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2129
            else bestSoFar = bestOf(bestSoFar, sym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2130
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2131
        return (pack != null) ? pack : bestSoFar;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2132
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2133
06bc494ca11e Initial load
duke
parents:
diff changeset
  2134
    /** Find an identifier among the members of a given type `site'.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2135
     *  @param env       The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2136
     *  @param site      The type containing the symbol to be found.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2137
     *  @param name      The identifier's name.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2138
     *  @param kind      Indicates the possible symbol kinds
06bc494ca11e Initial load
duke
parents:
diff changeset
  2139
     *                   (a subset of VAL, TYP).
06bc494ca11e Initial load
duke
parents:
diff changeset
  2140
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2141
    Symbol findIdentInType(Env<AttrContext> env, Type site,
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2142
                           Name name, KindSelector kind) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2143
        Symbol bestSoFar = typeNotFound;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2144
        Symbol sym;
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2145
        if (kind.contains(KindSelector.VAL)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2146
            sym = findField(env, site, name, site.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2147
            if (sym.exists()) return sym;
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2148
            else bestSoFar = bestOf(bestSoFar, sym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2149
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2150
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2151
        if (kind.contains(KindSelector.TYP)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2152
            sym = findMemberType(env, site, name, site.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2153
            if (sym.exists()) return sym;
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2154
            else bestSoFar = bestOf(bestSoFar, sym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2155
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2156
        return bestSoFar;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2157
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2158
06bc494ca11e Initial load
duke
parents:
diff changeset
  2159
/* ***************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
  2160
 *  Access checking
06bc494ca11e Initial load
duke
parents:
diff changeset
  2161
 *  The following methods convert ResolveErrors to ErrorSymbols, issuing
06bc494ca11e Initial load
duke
parents:
diff changeset
  2162
 *  an error message in the process
06bc494ca11e Initial load
duke
parents:
diff changeset
  2163
 ****************************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
  2164
06bc494ca11e Initial load
duke
parents:
diff changeset
  2165
    /** If `sym' is a bad symbol: report error and return errSymbol
06bc494ca11e Initial load
duke
parents:
diff changeset
  2166
     *  else pass through unchanged,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2167
     *  additional arguments duplicate what has been used in trying to find the
13844
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 13438
diff changeset
  2168
     *  symbol {@literal (--> flyweight pattern)}. This improves performance since we
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2169
     *  expect misses to happen frequently.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2170
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  2171
     *  @param sym       The symbol that was found, or a ResolveError.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2172
     *  @param pos       The position to use for error reporting.
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2173
     *  @param location  The symbol the served as a context for this lookup
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2174
     *  @param site      The original type from where the selection took place.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2175
     *  @param name      The symbol's name.
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2176
     *  @param qualified Did we get here through a qualified expression resolution?
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2177
     *  @param argtypes  The invocation's value arguments,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2178
     *                   if we looked for a method.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2179
     *  @param typeargtypes  The invocation's type arguments,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2180
     *                   if we looked for a method.
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2181
     *  @param logResolveHelper helper class used to log resolve errors
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2182
     */
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2183
    Symbol accessInternal(Symbol sym,
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2184
                  DiagnosticPosition pos,
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2185
                  Symbol location,
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2186
                  Type site,
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2187
                  Name name,
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2188
                  boolean qualified,
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2189
                  List<Type> argtypes,
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2190
                  List<Type> typeargtypes,
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2191
                  LogResolveHelper logResolveHelper) {
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2192
        if (sym.kind.isOverloadError()) {
24400
f05df7f58567 8037379: fix for JDK-8029569 doesn't cover all possible cases
vromero
parents: 24396
diff changeset
  2193
            ResolveError errSym = (ResolveError)sym.baseSymbol();
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2194
            sym = errSym.access(name, qualified ? site.tsym : syms.noSymbol);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2195
            argtypes = logResolveHelper.getArgumentTypes(errSym, sym, name, argtypes);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2196
            if (logResolveHelper.resolveDiagnosticNeeded(site, argtypes, typeargtypes)) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2197
                logResolveError(errSym, pos, location, site, name, argtypes, typeargtypes);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2198
            }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2199
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2200
        return sym;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2201
    }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2202
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2203
    /**
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2204
     * Variant of the generalized access routine, to be used for generating method
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2205
     * resolution diagnostics
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2206
     */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2207
    Symbol accessMethod(Symbol sym,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2208
                  DiagnosticPosition pos,
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2209
                  Symbol location,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2210
                  Type site,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2211
                  Name name,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2212
                  boolean qualified,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2213
                  List<Type> argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2214
                  List<Type> typeargtypes) {
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2215
        return accessInternal(sym, pos, location, site, name, qualified, argtypes, typeargtypes, methodLogResolveHelper);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2216
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2217
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2218
    /** Same as original accessMethod(), but without location.
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2219
     */
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2220
    Symbol accessMethod(Symbol sym,
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2221
                  DiagnosticPosition pos,
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2222
                  Type site,
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2223
                  Name name,
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2224
                  boolean qualified,
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2225
                  List<Type> argtypes,
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2226
                  List<Type> typeargtypes) {
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2227
        return accessMethod(sym, pos, site.tsym, site, name, qualified, argtypes, typeargtypes);
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2228
    }
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2229
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2230
    /**
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2231
     * Variant of the generalized access routine, to be used for generating variable,
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2232
     * type resolution diagnostics
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2233
     */
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2234
    Symbol accessBase(Symbol sym,
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2235
                  DiagnosticPosition pos,
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2236
                  Symbol location,
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2237
                  Type site,
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2238
                  Name name,
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2239
                  boolean qualified) {
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2240
        return accessInternal(sym, pos, location, site, name, qualified, List.<Type>nil(), null, basicLogResolveHelper);
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2241
    }
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2242
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2243
    /** Same as original accessBase(), but without location.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2244
     */
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2245
    Symbol accessBase(Symbol sym,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2246
                  DiagnosticPosition pos,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2247
                  Type site,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2248
                  Name name,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2249
                  boolean qualified) {
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2250
        return accessBase(sym, pos, site.tsym, site, name, qualified);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2251
    }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2252
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2253
    interface LogResolveHelper {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2254
        boolean resolveDiagnosticNeeded(Type site, List<Type> argtypes, List<Type> typeargtypes);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2255
        List<Type> getArgumentTypes(ResolveError errSym, Symbol accessedSym, Name name, List<Type> argtypes);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2256
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2257
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2258
    LogResolveHelper basicLogResolveHelper = new LogResolveHelper() {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2259
        public boolean resolveDiagnosticNeeded(Type site, List<Type> argtypes, List<Type> typeargtypes) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2260
            return !site.isErroneous();
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2261
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2262
        public List<Type> getArgumentTypes(ResolveError errSym, Symbol accessedSym, Name name, List<Type> argtypes) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2263
            return argtypes;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2264
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2265
    };
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2266
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2267
    LogResolveHelper methodLogResolveHelper = new LogResolveHelper() {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2268
        public boolean resolveDiagnosticNeeded(Type site, List<Type> argtypes, List<Type> typeargtypes) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2269
            return !site.isErroneous() &&
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2270
                        !Type.isErroneous(argtypes) &&
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2271
                        (typeargtypes == null || !Type.isErroneous(typeargtypes));
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2272
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2273
        public List<Type> getArgumentTypes(ResolveError errSym, Symbol accessedSym, Name name, List<Type> argtypes) {
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2274
            return (syms.operatorNames.contains(name)) ?
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2275
                    argtypes :
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19652
diff changeset
  2276
                    Type.map(argtypes, new ResolveDeferredRecoveryMap(AttrMode.SPECULATIVE, accessedSym, currentResolutionContext.step));
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2277
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2278
    };
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2279
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19652
diff changeset
  2280
    class ResolveDeferredRecoveryMap extends DeferredAttr.RecoveryDeferredTypeMap {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19652
diff changeset
  2281
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19652
diff changeset
  2282
        public ResolveDeferredRecoveryMap(AttrMode mode, Symbol msym, MethodResolutionPhase step) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19652
diff changeset
  2283
            deferredAttr.super(mode, msym, step);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19652
diff changeset
  2284
        }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19652
diff changeset
  2285
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19652
diff changeset
  2286
        @Override
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19652
diff changeset
  2287
        protected Type typeOf(DeferredType dt) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19652
diff changeset
  2288
            Type res = super.typeOf(dt);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19652
diff changeset
  2289
            if (!res.isErroneous()) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19652
diff changeset
  2290
                switch (TreeInfo.skipParens(dt.tree).getTag()) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19652
diff changeset
  2291
                    case LAMBDA:
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19652
diff changeset
  2292
                    case REFERENCE:
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19652
diff changeset
  2293
                        return dt;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19652
diff changeset
  2294
                    case CONDEXPR:
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19652
diff changeset
  2295
                        return res == Type.recoveryType ?
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19652
diff changeset
  2296
                                dt : res;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19652
diff changeset
  2297
                }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19652
diff changeset
  2298
            }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19652
diff changeset
  2299
            return res;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19652
diff changeset
  2300
        }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19652
diff changeset
  2301
    }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19652
diff changeset
  2302
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2303
    /** Check that sym is not an abstract method.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2304
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2305
    void checkNonAbstract(DiagnosticPosition pos, Symbol sym) {
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2306
        if ((sym.flags() & ABSTRACT) != 0 && (sym.flags() & DEFAULT) == 0)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2307
            log.error(pos, "abstract.cant.be.accessed.directly",
06bc494ca11e Initial load
duke
parents:
diff changeset
  2308
                      kindName(sym), sym, sym.location());
06bc494ca11e Initial load
duke
parents:
diff changeset
  2309
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2310
06bc494ca11e Initial load
duke
parents:
diff changeset
  2311
/* ***************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
  2312
 *  Name resolution
06bc494ca11e Initial load
duke
parents:
diff changeset
  2313
 *  Naming conventions are as for symbol lookup
06bc494ca11e Initial load
duke
parents:
diff changeset
  2314
 *  Unlike the find... methods these methods will report access errors
06bc494ca11e Initial load
duke
parents:
diff changeset
  2315
 ****************************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
  2316
06bc494ca11e Initial load
duke
parents:
diff changeset
  2317
    /** Resolve an unqualified (non-method) identifier.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2318
     *  @param pos       The position to use for error reporting.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2319
     *  @param env       The environment current at the identifier use.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2320
     *  @param name      The identifier's name.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2321
     *  @param kind      The set of admissible symbol kinds for the identifier.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2322
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2323
    Symbol resolveIdent(DiagnosticPosition pos, Env<AttrContext> env,
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2324
                        Name name, KindSelector kind) {
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2325
        return accessBase(
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2326
            findIdent(env, name, kind),
06bc494ca11e Initial load
duke
parents:
diff changeset
  2327
            pos, env.enclClass.sym.type, name, false);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2328
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2329
06bc494ca11e Initial load
duke
parents:
diff changeset
  2330
    /** Resolve an unqualified method identifier.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2331
     *  @param pos       The position to use for error reporting.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2332
     *  @param env       The environment current at the method invocation.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2333
     *  @param name      The identifier's name.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2334
     *  @param argtypes  The types of the invocation's value arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2335
     *  @param typeargtypes  The types of the invocation's type arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2336
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2337
    Symbol resolveMethod(DiagnosticPosition pos,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2338
                         Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2339
                         Name name,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2340
                         List<Type> argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2341
                         List<Type> typeargtypes) {
16805
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  2342
        return lookupMethod(env, pos, env.enclClass.sym, resolveMethodCheck,
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  2343
                new BasicLookupHelper(name, env.enclClass.sym.type, argtypes, typeargtypes) {
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  2344
                    @Override
18668
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  2345
                    Symbol doLookup(Env<AttrContext> env, MethodResolutionPhase phase) {
16805
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  2346
                        return findFun(env, name, argtypes, typeargtypes,
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  2347
                                phase.isBoxingRequired(),
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  2348
                                phase.isVarargsRequired());
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  2349
                    }});
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
  2350
    }
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
  2351
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2352
    /** Resolve a qualified method identifier
06bc494ca11e Initial load
duke
parents:
diff changeset
  2353
     *  @param pos       The position to use for error reporting.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2354
     *  @param env       The environment current at the method invocation.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2355
     *  @param site      The type of the qualifying expression, in which
06bc494ca11e Initial load
duke
parents:
diff changeset
  2356
     *                   identifier is searched.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2357
     *  @param name      The identifier's name.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2358
     *  @param argtypes  The types of the invocation's value arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2359
     *  @param typeargtypes  The types of the invocation's type arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2360
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2361
    Symbol resolveQualifiedMethod(DiagnosticPosition pos, Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2362
                                  Type site, Name name, List<Type> argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2363
                                  List<Type> typeargtypes) {
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2364
        return resolveQualifiedMethod(pos, env, site.tsym, site, name, argtypes, typeargtypes);
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2365
    }
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2366
    Symbol resolveQualifiedMethod(DiagnosticPosition pos, Env<AttrContext> env,
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2367
                                  Symbol location, Type site, Name name, List<Type> argtypes,
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2368
                                  List<Type> typeargtypes) {
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2369
        return resolveQualifiedMethod(new MethodResolutionContext(), pos, env, location, site, name, argtypes, typeargtypes);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2370
    }
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2371
    private Symbol resolveQualifiedMethod(MethodResolutionContext resolveContext,
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2372
                                  DiagnosticPosition pos, Env<AttrContext> env,
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2373
                                  Symbol location, Type site, Name name, List<Type> argtypes,
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2374
                                  List<Type> typeargtypes) {
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2375
        return lookupMethod(env, pos, location, resolveContext, new BasicLookupHelper(name, site, argtypes, typeargtypes) {
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2376
            @Override
18668
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  2377
            Symbol doLookup(Env<AttrContext> env, MethodResolutionPhase phase) {
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2378
                return findMethod(env, site, name, argtypes, typeargtypes,
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2379
                        phase.isBoxingRequired(),
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2380
                        phase.isVarargsRequired(), false);
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2381
            }
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2382
            @Override
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2383
            Symbol access(Env<AttrContext> env, DiagnosticPosition pos, Symbol location, Symbol sym) {
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2384
                if (sym.kind.isOverloadError()) {
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2385
                    sym = super.access(env, pos, location, sym);
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2386
                } else if (allowMethodHandles) {
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2387
                    MethodSymbol msym = (MethodSymbol)sym;
18389
a425d0819f36 8016569: javac, add new flag for polymorphic method signatures
vromero
parents: 18378
diff changeset
  2388
                    if ((msym.flags() & SIGNATURE_POLYMORPHIC) != 0) {
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2389
                        return findPolymorphicSignatureInstance(env, sym, argtypes);
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2390
                    }
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2391
                }
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2392
                return sym;
6592
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6356
diff changeset
  2393
            }
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2394
        });
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2395
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2396
6592
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6356
diff changeset
  2397
    /** Find or create an implicit method of exactly the given type (after erasure).
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6356
diff changeset
  2398
     *  Searches in a side table, not the main scope of the site.
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6356
diff changeset
  2399
     *  This emulates the lookup process required by JSR 292 in JVM.
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6356
diff changeset
  2400
     *  @param env       Attribution environment
12335
4725d88691dd 7133185: Update 292 overload resolution logic to match JLS
mcimadamore
parents: 12334
diff changeset
  2401
     *  @param spMethod  signature polymorphic method - i.e. MH.invokeExact
4725d88691dd 7133185: Update 292 overload resolution logic to match JLS
mcimadamore
parents: 12334
diff changeset
  2402
     *  @param argtypes  The required argument types
6592
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6356
diff changeset
  2403
     */
12335
4725d88691dd 7133185: Update 292 overload resolution logic to match JLS
mcimadamore
parents: 12334
diff changeset
  2404
    Symbol findPolymorphicSignatureInstance(Env<AttrContext> env,
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2405
                                            final Symbol spMethod,
8036
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 8032
diff changeset
  2406
                                            List<Type> argtypes) {
6592
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6356
diff changeset
  2407
        Type mtype = infer.instantiatePolymorphicSignatureInstance(env,
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2408
                (MethodSymbol)spMethod, currentResolutionContext, argtypes);
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  2409
        for (Symbol sym : polymorphicSignatureScope.getSymbolsByName(spMethod.name)) {
12335
4725d88691dd 7133185: Update 292 overload resolution logic to match JLS
mcimadamore
parents: 12334
diff changeset
  2410
            if (types.isSameType(mtype, sym.type)) {
4725d88691dd 7133185: Update 292 overload resolution logic to match JLS
mcimadamore
parents: 12334
diff changeset
  2411
               return sym;
6592
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6356
diff changeset
  2412
            }
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6356
diff changeset
  2413
        }
12335
4725d88691dd 7133185: Update 292 overload resolution logic to match JLS
mcimadamore
parents: 12334
diff changeset
  2414
4725d88691dd 7133185: Update 292 overload resolution logic to match JLS
mcimadamore
parents: 12334
diff changeset
  2415
        // create the desired method
4725d88691dd 7133185: Update 292 overload resolution logic to match JLS
mcimadamore
parents: 12334
diff changeset
  2416
        long flags = ABSTRACT | HYPOTHETICAL | spMethod.flags() & Flags.AccessFlags;
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2417
        Symbol msym = new MethodSymbol(flags, spMethod.name, mtype, spMethod.owner) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2418
            @Override
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2419
            public Symbol baseSymbol() {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2420
                return spMethod;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2421
            }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2422
        };
12335
4725d88691dd 7133185: Update 292 overload resolution logic to match JLS
mcimadamore
parents: 12334
diff changeset
  2423
        polymorphicSignatureScope.enter(msym);
4725d88691dd 7133185: Update 292 overload resolution logic to match JLS
mcimadamore
parents: 12334
diff changeset
  2424
        return msym;
6592
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6356
diff changeset
  2425
    }
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6356
diff changeset
  2426
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2427
    /** Resolve a qualified method identifier, throw a fatal error if not
06bc494ca11e Initial load
duke
parents:
diff changeset
  2428
     *  found.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2429
     *  @param pos       The position to use for error reporting.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2430
     *  @param env       The environment current at the method invocation.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2431
     *  @param site      The type of the qualifying expression, in which
06bc494ca11e Initial load
duke
parents:
diff changeset
  2432
     *                   identifier is searched.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2433
     *  @param name      The identifier's name.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2434
     *  @param argtypes  The types of the invocation's value arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2435
     *  @param typeargtypes  The types of the invocation's type arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2436
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2437
    public MethodSymbol resolveInternalMethod(DiagnosticPosition pos, Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2438
                                        Type site, Name name,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2439
                                        List<Type> argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2440
                                        List<Type> typeargtypes) {
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2441
        MethodResolutionContext resolveContext = new MethodResolutionContext();
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2442
        resolveContext.internalResolution = true;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2443
        Symbol sym = resolveQualifiedMethod(resolveContext, pos, env, site.tsym,
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2444
                site, name, argtypes, typeargtypes);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2445
        if (sym.kind == MTH) return (MethodSymbol)sym;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2446
        else throw new FatalError(
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2447
                 diags.fragment("fatal.err.cant.locate.meth",
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2448
                                name));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2449
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2450
06bc494ca11e Initial load
duke
parents:
diff changeset
  2451
    /** Resolve constructor.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2452
     *  @param pos       The position to use for error reporting.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2453
     *  @param env       The environment current at the constructor invocation.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2454
     *  @param site      The type of class for which a constructor is searched.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2455
     *  @param argtypes  The types of the constructor invocation's value
06bc494ca11e Initial load
duke
parents:
diff changeset
  2456
     *                   arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2457
     *  @param typeargtypes  The types of the constructor invocation's type
06bc494ca11e Initial load
duke
parents:
diff changeset
  2458
     *                   arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2459
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2460
    Symbol resolveConstructor(DiagnosticPosition pos,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2461
                              Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2462
                              Type site,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2463
                              List<Type> argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2464
                              List<Type> typeargtypes) {
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2465
        return resolveConstructor(new MethodResolutionContext(), pos, env, site, argtypes, typeargtypes);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2466
    }
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2467
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2468
    private Symbol resolveConstructor(MethodResolutionContext resolveContext,
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2469
                              final DiagnosticPosition pos,
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2470
                              Env<AttrContext> env,
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2471
                              Type site,
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2472
                              List<Type> argtypes,
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2473
                              List<Type> typeargtypes) {
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2474
        return lookupMethod(env, pos, site.tsym, resolveContext, new BasicLookupHelper(names.init, site, argtypes, typeargtypes) {
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2475
            @Override
18668
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  2476
            Symbol doLookup(Env<AttrContext> env, MethodResolutionPhase phase) {
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2477
                return findConstructor(pos, env, site, argtypes, typeargtypes,
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2478
                        phase.isBoxingRequired(),
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2479
                        phase.isVarargsRequired());
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2480
            }
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2481
        });
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2482
    }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2483
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2484
    /** Resolve a constructor, throw a fatal error if not found.
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2485
     *  @param pos       The position to use for error reporting.
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2486
     *  @param env       The environment current at the method invocation.
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2487
     *  @param site      The type to be constructed.
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2488
     *  @param argtypes  The types of the invocation's value arguments.
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2489
     *  @param typeargtypes  The types of the invocation's type arguments.
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2490
     */
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2491
    public MethodSymbol resolveInternalConstructor(DiagnosticPosition pos, Env<AttrContext> env,
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2492
                                        Type site,
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2493
                                        List<Type> argtypes,
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2494
                                        List<Type> typeargtypes) {
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2495
        MethodResolutionContext resolveContext = new MethodResolutionContext();
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2496
        resolveContext.internalResolution = true;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2497
        Symbol sym = resolveConstructor(resolveContext, pos, env, site, argtypes, typeargtypes);
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2498
        if (sym.kind == MTH) return (MethodSymbol)sym;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2499
        else throw new FatalError(
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2500
                 diags.fragment("fatal.err.cant.locate.ctor", site));
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2501
    }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2502
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2503
    Symbol findConstructor(DiagnosticPosition pos, Env<AttrContext> env,
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2504
                              Type site, List<Type> argtypes,
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2505
                              List<Type> typeargtypes,
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2506
                              boolean allowBoxing,
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2507
                              boolean useVarargs) {
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2508
        Symbol sym = findMethod(env, site,
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2509
                                    names.init, argtypes,
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2510
                                    typeargtypes, allowBoxing,
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2511
                                    useVarargs, false);
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2512
        chk.checkDeprecated(pos, env.info.scope.owner, sym);
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2513
        return sym;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2514
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2515
5321
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  2516
    /** Resolve constructor using diamond inference.
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  2517
     *  @param pos       The position to use for error reporting.
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  2518
     *  @param env       The environment current at the constructor invocation.
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  2519
     *  @param site      The type of class for which a constructor is searched.
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  2520
     *                   The scope of this class has been touched in attribution.
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  2521
     *  @param argtypes  The types of the constructor invocation's value
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  2522
     *                   arguments.
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  2523
     *  @param typeargtypes  The types of the constructor invocation's type
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  2524
     *                   arguments.
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  2525
     */
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  2526
    Symbol resolveDiamond(DiagnosticPosition pos,
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  2527
                              Env<AttrContext> env,
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  2528
                              Type site,
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  2529
                              List<Type> argtypes,
6344
f190fb7fdd2d 6975275: diamond implementation needs some cleanup
mcimadamore
parents: 6151
diff changeset
  2530
                              List<Type> typeargtypes) {
16805
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  2531
        return lookupMethod(env, pos, site.tsym, resolveMethodCheck,
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  2532
                new BasicLookupHelper(names.init, site, argtypes, typeargtypes) {
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  2533
                    @Override
18668
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  2534
                    Symbol doLookup(Env<AttrContext> env, MethodResolutionPhase phase) {
16805
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  2535
                        return findDiamond(env, site, argtypes, typeargtypes,
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  2536
                                phase.isBoxingRequired(),
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  2537
                                phase.isVarargsRequired());
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  2538
                    }
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  2539
                    @Override
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  2540
                    Symbol access(Env<AttrContext> env, DiagnosticPosition pos, Symbol location, Symbol sym) {
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2541
                        if (sym.kind.isOverloadError()) {
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2542
                            if (sym.kind != WRONG_MTH &&
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2543
                                sym.kind != WRONG_MTHS) {
19917
ebb957ba7b07 8023545: Misleading error message when using diamond operator with private constructor
vromero
parents: 19914
diff changeset
  2544
                                sym = super.access(env, pos, location, sym);
ebb957ba7b07 8023545: Misleading error message when using diamond operator with private constructor
vromero
parents: 19914
diff changeset
  2545
                            } else {
ebb957ba7b07 8023545: Misleading error message when using diamond operator with private constructor
vromero
parents: 19914
diff changeset
  2546
                                final JCDiagnostic details = sym.kind == WRONG_MTH ?
24400
f05df7f58567 8037379: fix for JDK-8029569 doesn't cover all possible cases
vromero
parents: 24396
diff changeset
  2547
                                                ((InapplicableSymbolError)sym.baseSymbol()).errCandidate().snd :
19917
ebb957ba7b07 8023545: Misleading error message when using diamond operator with private constructor
vromero
parents: 19914
diff changeset
  2548
                                                null;
28331
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  2549
                                sym = new DiamondError(sym, currentResolutionContext);
19917
ebb957ba7b07 8023545: Misleading error message when using diamond operator with private constructor
vromero
parents: 19914
diff changeset
  2550
                                sym = accessMethod(sym, pos, site, names.init, true, argtypes, typeargtypes);
ebb957ba7b07 8023545: Misleading error message when using diamond operator with private constructor
vromero
parents: 19914
diff changeset
  2551
                                env.info.pendingResolutionPhase = currentResolutionContext.step;
ebb957ba7b07 8023545: Misleading error message when using diamond operator with private constructor
vromero
parents: 19914
diff changeset
  2552
                            }
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2553
                        }
16805
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  2554
                        return sym;
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  2555
                    }});
5321
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  2556
    }
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  2557
12078
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2558
    /** This method scans all the constructor symbol in a given class scope -
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2559
     *  assuming that the original scope contains a constructor of the kind:
13844
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 13438
diff changeset
  2560
     *  {@code Foo(X x, Y y)}, where X,Y are class type-variables declared in Foo,
12078
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2561
     *  a method check is executed against the modified constructor type:
13844
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 13438
diff changeset
  2562
     *  {@code <X,Y>Foo<X,Y>(X x, Y y)}. This is crucial in order to enable diamond
12078
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2563
     *  inference. The inferred return type of the synthetic constructor IS
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2564
     *  the inferred type for the diamond operator.
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2565
     */
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2566
    private Symbol findDiamond(Env<AttrContext> env,
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2567
                              Type site,
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2568
                              List<Type> argtypes,
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2569
                              List<Type> typeargtypes,
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2570
                              boolean allowBoxing,
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2571
                              boolean useVarargs) {
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2572
        Symbol bestSoFar = methodNotFound;
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  2573
        for (final Symbol sym : site.tsym.members().getSymbolsByName(names.init)) {
12078
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2574
            //- System.out.println(" e " + e.sym);
14051
9097cec96212 7188968: New instance creation expression using diamond is checked twice
mcimadamore
parents: 14048
diff changeset
  2575
            if (sym.kind == MTH &&
9097cec96212 7188968: New instance creation expression using diamond is checked twice
mcimadamore
parents: 14048
diff changeset
  2576
                (sym.flags_field & SYNTHETIC) == 0) {
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  2577
                    List<Type> oldParams = sym.type.hasTag(FORALL) ?
14051
9097cec96212 7188968: New instance creation expression using diamond is checked twice
mcimadamore
parents: 14048
diff changeset
  2578
                            ((ForAll)sym.type).tvars :
12078
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2579
                            List.<Type>nil();
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2580
                    Type constrType = new ForAll(site.tsym.type.getTypeArguments().appendList(oldParams),
24396
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 24393
diff changeset
  2581
                                                 types.createMethodTypeWithReturn(sym.type.asMethodType(), site));
14051
9097cec96212 7188968: New instance creation expression using diamond is checked twice
mcimadamore
parents: 14048
diff changeset
  2582
                    MethodSymbol newConstr = new MethodSymbol(sym.flags(), names.init, constrType, site.tsym) {
9097cec96212 7188968: New instance creation expression using diamond is checked twice
mcimadamore
parents: 14048
diff changeset
  2583
                        @Override
9097cec96212 7188968: New instance creation expression using diamond is checked twice
mcimadamore
parents: 14048
diff changeset
  2584
                        public Symbol baseSymbol() {
9097cec96212 7188968: New instance creation expression using diamond is checked twice
mcimadamore
parents: 14048
diff changeset
  2585
                            return sym;
9097cec96212 7188968: New instance creation expression using diamond is checked twice
mcimadamore
parents: 14048
diff changeset
  2586
                        }
9097cec96212 7188968: New instance creation expression using diamond is checked twice
mcimadamore
parents: 14048
diff changeset
  2587
                    };
12078
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2588
                    bestSoFar = selectBest(env, site, argtypes, typeargtypes,
14051
9097cec96212 7188968: New instance creation expression using diamond is checked twice
mcimadamore
parents: 14048
diff changeset
  2589
                            newConstr,
12078
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2590
                            bestSoFar,
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2591
                            allowBoxing,
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2592
                            useVarargs,
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2593
                            false);
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2594
            }
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2595
        }
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2596
        return bestSoFar;
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2597
    }
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2598
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2599
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2600
06bc494ca11e Initial load
duke
parents:
diff changeset
  2601
    /** Resolve operator.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2602
     *  @param pos       The position to use for error reporting.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2603
     *  @param optag     The tag of the operation tree.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2604
     *  @param env       The environment current at the operation.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2605
     *  @param argtypes  The types of the operands.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2606
     */
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10816
diff changeset
  2607
    Symbol resolveOperator(DiagnosticPosition pos, JCTree.Tag optag,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2608
                           Env<AttrContext> env, List<Type> argtypes) {
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2609
        MethodResolutionContext prevResolutionContext = currentResolutionContext;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2610
        try {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2611
            currentResolutionContext = new MethodResolutionContext();
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2612
            Name name = treeinfo.operatorName(optag);
17804
6bed8a263318 8014649: Regression: bug in Resolve.resolveOperator
mcimadamore
parents: 17582
diff changeset
  2613
            return lookupMethod(env, pos, syms.predefClass, currentResolutionContext,
6bed8a263318 8014649: Regression: bug in Resolve.resolveOperator
mcimadamore
parents: 17582
diff changeset
  2614
                    new BasicLookupHelper(name, syms.predefClass.type, argtypes, null, BOX) {
6bed8a263318 8014649: Regression: bug in Resolve.resolveOperator
mcimadamore
parents: 17582
diff changeset
  2615
                @Override
18668
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  2616
                Symbol doLookup(Env<AttrContext> env, MethodResolutionPhase phase) {
17804
6bed8a263318 8014649: Regression: bug in Resolve.resolveOperator
mcimadamore
parents: 17582
diff changeset
  2617
                    return findMethod(env, site, name, argtypes, typeargtypes,
6bed8a263318 8014649: Regression: bug in Resolve.resolveOperator
mcimadamore
parents: 17582
diff changeset
  2618
                            phase.isBoxingRequired(),
6bed8a263318 8014649: Regression: bug in Resolve.resolveOperator
mcimadamore
parents: 17582
diff changeset
  2619
                            phase.isVarargsRequired(), true);
6bed8a263318 8014649: Regression: bug in Resolve.resolveOperator
mcimadamore
parents: 17582
diff changeset
  2620
                }
6bed8a263318 8014649: Regression: bug in Resolve.resolveOperator
mcimadamore
parents: 17582
diff changeset
  2621
                @Override
6bed8a263318 8014649: Regression: bug in Resolve.resolveOperator
mcimadamore
parents: 17582
diff changeset
  2622
                Symbol access(Env<AttrContext> env, DiagnosticPosition pos, Symbol location, Symbol sym) {
6bed8a263318 8014649: Regression: bug in Resolve.resolveOperator
mcimadamore
parents: 17582
diff changeset
  2623
                    return accessMethod(sym, pos, env.enclClass.sym.type, name,
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2624
                          false, argtypes, null);
17804
6bed8a263318 8014649: Regression: bug in Resolve.resolveOperator
mcimadamore
parents: 17582
diff changeset
  2625
                }
6bed8a263318 8014649: Regression: bug in Resolve.resolveOperator
mcimadamore
parents: 17582
diff changeset
  2626
            });
6bed8a263318 8014649: Regression: bug in Resolve.resolveOperator
mcimadamore
parents: 17582
diff changeset
  2627
        } finally {
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2628
            currentResolutionContext = prevResolutionContext;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2629
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2630
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2631
06bc494ca11e Initial load
duke
parents:
diff changeset
  2632
    /** Resolve operator.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2633
     *  @param pos       The position to use for error reporting.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2634
     *  @param optag     The tag of the operation tree.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2635
     *  @param env       The environment current at the operation.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2636
     *  @param arg       The type of the operand.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2637
     */
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10816
diff changeset
  2638
    Symbol resolveUnaryOperator(DiagnosticPosition pos, JCTree.Tag optag, Env<AttrContext> env, Type arg) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2639
        return resolveOperator(pos, optag, env, List.of(arg));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2640
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2641
06bc494ca11e Initial load
duke
parents:
diff changeset
  2642
    /** Resolve binary operator.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2643
     *  @param pos       The position to use for error reporting.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2644
     *  @param optag     The tag of the operation tree.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2645
     *  @param env       The environment current at the operation.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2646
     *  @param left      The types of the left operand.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2647
     *  @param right     The types of the right operand.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2648
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2649
    Symbol resolveBinaryOperator(DiagnosticPosition pos,
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10816
diff changeset
  2650
                                 JCTree.Tag optag,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2651
                                 Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2652
                                 Type left,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2653
                                 Type right) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2654
        return resolveOperator(pos, optag, env, List.of(left, right));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2655
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2656
21718
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2657
    Symbol getMemberReference(DiagnosticPosition pos,
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2658
            Env<AttrContext> env,
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2659
            JCMemberReference referenceTree,
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2660
            Type site,
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2661
            Name name) {
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2662
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2663
        site = types.capture(site);
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2664
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2665
        ReferenceLookupHelper lookupHelper = makeReferenceLookupHelper(
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2666
                referenceTree, site, name, List.<Type>nil(), null, VARARITY);
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2667
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2668
        Env<AttrContext> newEnv = env.dup(env.tree, env.info.dup());
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2669
        Symbol sym = lookupMethod(newEnv, env.tree.pos(), site.tsym,
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2670
                nilMethodCheck, lookupHelper);
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2671
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2672
        env.info.pendingResolutionPhase = newEnv.info.pendingResolutionPhase;
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2673
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2674
        return sym;
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2675
    }
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2676
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2677
    ReferenceLookupHelper makeReferenceLookupHelper(JCMemberReference referenceTree,
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2678
                                  Type site,
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2679
                                  Name name,
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2680
                                  List<Type> argtypes,
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2681
                                  List<Type> typeargtypes,
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2682
                                  MethodResolutionPhase maxPhase) {
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2683
        ReferenceLookupHelper result;
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2684
        if (!name.equals(names.init)) {
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2685
            //method reference
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2686
            result =
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2687
                    new MethodReferenceLookupHelper(referenceTree, name, site, argtypes, typeargtypes, maxPhase);
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2688
        } else {
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2689
            if (site.hasTag(ARRAY)) {
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2690
                //array constructor reference
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2691
                result =
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2692
                        new ArrayConstructorReferenceLookupHelper(referenceTree, site, argtypes, typeargtypes, maxPhase);
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2693
            } else {
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2694
                //class constructor reference
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2695
                result =
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2696
                        new ConstructorReferenceLookupHelper(referenceTree, site, argtypes, typeargtypes, maxPhase);
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2697
            }
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2698
        }
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2699
        return result;
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2700
    }
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2701
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2702
    Symbol resolveMemberReferenceByArity(Env<AttrContext> env,
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2703
                                  JCMemberReference referenceTree,
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2704
                                  Type site,
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2705
                                  Name name,
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2706
                                  List<Type> argtypes,
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2707
                                  InferenceContext inferenceContext) {
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2708
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2709
        boolean isStaticSelector = TreeInfo.isStaticSelector(referenceTree.expr, names);
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2710
        site = types.capture(site);
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2711
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2712
        ReferenceLookupHelper boundLookupHelper = makeReferenceLookupHelper(
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2713
                referenceTree, site, name, argtypes, null, VARARITY);
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2714
        //step 1 - bound lookup
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2715
        Env<AttrContext> boundEnv = env.dup(env.tree, env.info.dup());
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2716
        Symbol boundSym = lookupMethod(boundEnv, env.tree.pos(), site.tsym,
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2717
                arityMethodCheck, boundLookupHelper);
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2718
        if (isStaticSelector &&
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2719
            !name.equals(names.init) &&
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2720
            !boundSym.isStatic() &&
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2721
            !boundSym.kind.isOverloadError()) {
21718
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2722
            boundSym = methodNotFound;
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2723
        }
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2724
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2725
        //step 2 - unbound lookup
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2726
        Symbol unboundSym = methodNotFound;
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2727
        ReferenceLookupHelper unboundLookupHelper = null;
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2728
        Env<AttrContext> unboundEnv = env.dup(env.tree, env.info.dup());
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2729
        if (isStaticSelector) {
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2730
            unboundLookupHelper = boundLookupHelper.unboundLookup(inferenceContext);
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2731
            unboundSym = lookupMethod(unboundEnv, env.tree.pos(), site.tsym,
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2732
                    arityMethodCheck, unboundLookupHelper);
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2733
            if (unboundSym.isStatic() &&
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2734
                !unboundSym.kind.isOverloadError()) {
21718
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2735
                unboundSym = methodNotFound;
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2736
            }
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2737
        }
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2738
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2739
        //merge results
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2740
        Symbol bestSym = choose(boundSym, unboundSym);
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2741
        env.info.pendingResolutionPhase = bestSym == unboundSym ?
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2742
                unboundEnv.info.pendingResolutionPhase :
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2743
                boundEnv.info.pendingResolutionPhase;
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2744
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2745
        return bestSym;
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2746
    }
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2747
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2748
    /**
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2749
     * Resolution of member references is typically done as a single
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2750
     * overload resolution step, where the argument types A are inferred from
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2751
     * the target functional descriptor.
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2752
     *
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2753
     * If the member reference is a method reference with a type qualifier,
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2754
     * a two-step lookup process is performed. The first step uses the
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2755
     * expected argument list A, while the second step discards the first
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2756
     * type from A (which is treated as a receiver type).
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2757
     *
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2758
     * There are two cases in which inference is performed: (i) if the member
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2759
     * reference is a constructor reference and the qualifier type is raw - in
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2760
     * which case diamond inference is used to infer a parameterization for the
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2761
     * type qualifier; (ii) if the member reference is an unbound reference
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2762
     * where the type qualifier is raw - in that case, during the unbound lookup
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2763
     * the receiver argument type is used to infer an instantiation for the raw
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2764
     * qualifier type.
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2765
     *
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2766
     * When a multi-step resolution process is exploited, it is an error
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2767
     * if two candidates are found (ambiguity).
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2768
     *
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2769
     * This routine returns a pair (T,S), where S is the member reference symbol,
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2770
     * and T is the type of the class in which S is defined. This is necessary as
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2771
     * the type T might be dynamically inferred (i.e. if constructor reference
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2772
     * has a raw qualifier).
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2773
     */
21718
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2774
    Pair<Symbol, ReferenceLookupHelper> resolveMemberReference(Env<AttrContext> env,
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2775
                                  JCMemberReference referenceTree,
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2776
                                  Type site,
21718
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2777
                                  Name name,
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2778
                                  List<Type> argtypes,
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2779
                                  List<Type> typeargtypes,
18910
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
  2780
                                  MethodCheck methodCheck,
21718
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2781
                                  InferenceContext inferenceContext,
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2782
                                  AttrMode mode) {
15360
450af2a9e3c9 8005854: Add support for array constructor references
mcimadamore
parents: 15038
diff changeset
  2783
19129
b1409ae675f8 8020843: javac crashes on accessibility check with method reference with typevar receiver
mcimadamore
parents: 18914
diff changeset
  2784
        site = types.capture(site);
21718
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2785
        ReferenceLookupHelper boundLookupHelper = makeReferenceLookupHelper(
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2786
                referenceTree, site, name, argtypes, typeargtypes, VARARITY);
15360
450af2a9e3c9 8005854: Add support for array constructor references
mcimadamore
parents: 15038
diff changeset
  2787
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2788
        //step 1 - bound lookup
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2789
        Env<AttrContext> boundEnv = env.dup(env.tree, env.info.dup());
21718
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2790
        Symbol origBoundSym;
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2791
        boolean staticErrorForBound = false;
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2792
        MethodResolutionContext boundSearchResolveContext = new MethodResolutionContext();
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2793
        boundSearchResolveContext.methodCheck = methodCheck;
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2794
        Symbol boundSym = origBoundSym = lookupMethod(boundEnv, env.tree.pos(),
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2795
                site.tsym, boundSearchResolveContext, boundLookupHelper);
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2796
        SearchResultKind boundSearchResultKind = SearchResultKind.NOT_APPLICABLE_MATCH;
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2797
        boolean isStaticSelector = TreeInfo.isStaticSelector(referenceTree.expr, names);
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2798
        boolean shouldCheckForStaticness = isStaticSelector &&
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2799
                referenceTree.getMode() == ReferenceMode.INVOKE;
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2800
        if (boundSym.kind != WRONG_MTHS && boundSym.kind != WRONG_MTH) {
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2801
            if (shouldCheckForStaticness) {
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2802
                if (!boundSym.isStatic()) {
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2803
                    staticErrorForBound = true;
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2804
                    if (hasAnotherApplicableMethod(
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2805
                            boundSearchResolveContext, boundSym, true)) {
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2806
                        boundSearchResultKind = SearchResultKind.BAD_MATCH_MORE_SPECIFIC;
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2807
                    } else {
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2808
                        boundSearchResultKind = SearchResultKind.BAD_MATCH;
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2809
                        if (!boundSym.kind.isOverloadError()) {
21718
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2810
                            boundSym = methodWithCorrectStaticnessNotFound;
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2811
                        }
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2812
                    }
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2813
                } else if (!boundSym.kind.isOverloadError()) {
21718
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2814
                    boundSearchResultKind = SearchResultKind.GOOD_MATCH;
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2815
                }
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2816
            }
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2817
        }
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2818
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2819
        //step 2 - unbound lookup
21718
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2820
        Symbol origUnboundSym = null;
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2821
        Symbol unboundSym = methodNotFound;
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2822
        ReferenceLookupHelper unboundLookupHelper = null;
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2823
        Env<AttrContext> unboundEnv = env.dup(env.tree, env.info.dup());
21718
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2824
        SearchResultKind unboundSearchResultKind = SearchResultKind.NOT_APPLICABLE_MATCH;
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2825
        boolean staticErrorForUnbound = false;
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2826
        if (isStaticSelector) {
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2827
            unboundLookupHelper = boundLookupHelper.unboundLookup(inferenceContext);
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2828
            MethodResolutionContext unboundSearchResolveContext =
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2829
                    new MethodResolutionContext();
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2830
            unboundSearchResolveContext.methodCheck = methodCheck;
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2831
            unboundSym = origUnboundSym = lookupMethod(unboundEnv, env.tree.pos(),
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2832
                    site.tsym, unboundSearchResolveContext, unboundLookupHelper);
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2833
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2834
            if (unboundSym.kind != WRONG_MTH && unboundSym.kind != WRONG_MTHS) {
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2835
                if (shouldCheckForStaticness) {
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2836
                    if (unboundSym.isStatic()) {
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2837
                        staticErrorForUnbound = true;
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2838
                        if (hasAnotherApplicableMethod(
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2839
                                unboundSearchResolveContext, unboundSym, false)) {
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2840
                            unboundSearchResultKind = SearchResultKind.BAD_MATCH_MORE_SPECIFIC;
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2841
                        } else {
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2842
                            unboundSearchResultKind = SearchResultKind.BAD_MATCH;
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2843
                            if (!unboundSym.kind.isOverloadError()) {
21718
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2844
                                unboundSym = methodWithCorrectStaticnessNotFound;
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2845
                            }
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2846
                        }
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2847
                    } else if (!unboundSym.kind.isOverloadError()) {
21718
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2848
                        unboundSearchResultKind = SearchResultKind.GOOD_MATCH;
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2849
                    }
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2850
                }
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2851
            }
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2852
        }
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2853
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2854
        //merge results
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2855
        Pair<Symbol, ReferenceLookupHelper> res;
19129
b1409ae675f8 8020843: javac crashes on accessibility check with method reference with typevar receiver
mcimadamore
parents: 18914
diff changeset
  2856
        Symbol bestSym = choose(boundSym, unboundSym);
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2857
        if (!bestSym.kind.isOverloadError() &&
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2858
            (staticErrorForBound || staticErrorForUnbound)) {
21718
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2859
            if (staticErrorForBound) {
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2860
                boundSym = methodWithCorrectStaticnessNotFound;
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2861
            }
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2862
            if (staticErrorForUnbound) {
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2863
                unboundSym = methodWithCorrectStaticnessNotFound;
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2864
            }
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2865
            bestSym = choose(boundSym, unboundSym);
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2866
        }
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2867
        if (bestSym == methodWithCorrectStaticnessNotFound && mode == AttrMode.CHECK) {
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2868
            Symbol symToPrint = origBoundSym;
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2869
            String errorFragmentToPrint = "non-static.cant.be.ref";
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2870
            if (staticErrorForBound && staticErrorForUnbound) {
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2871
                if (unboundSearchResultKind == SearchResultKind.BAD_MATCH_MORE_SPECIFIC) {
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2872
                    symToPrint = origUnboundSym;
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2873
                    errorFragmentToPrint = "static.method.in.unbound.lookup";
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2874
                }
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2875
            } else {
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2876
                if (!staticErrorForBound) {
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2877
                    symToPrint = origUnboundSym;
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2878
                    errorFragmentToPrint = "static.method.in.unbound.lookup";
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2879
                }
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2880
            }
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2881
            log.error(referenceTree.expr.pos(), "invalid.mref",
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2882
                Kinds.kindName(referenceTree.getMode()),
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2883
                diags.fragment(errorFragmentToPrint,
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2884
                Kinds.kindName(symToPrint), symToPrint));
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2885
        }
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2886
        res = new Pair<>(bestSym,
19129
b1409ae675f8 8020843: javac crashes on accessibility check with method reference with typevar receiver
mcimadamore
parents: 18914
diff changeset
  2887
                bestSym == unboundSym ? unboundLookupHelper : boundLookupHelper);
b1409ae675f8 8020843: javac crashes on accessibility check with method reference with typevar receiver
mcimadamore
parents: 18914
diff changeset
  2888
        env.info.pendingResolutionPhase = bestSym == unboundSym ?
b1409ae675f8 8020843: javac crashes on accessibility check with method reference with typevar receiver
mcimadamore
parents: 18914
diff changeset
  2889
                unboundEnv.info.pendingResolutionPhase :
b1409ae675f8 8020843: javac crashes on accessibility check with method reference with typevar receiver
mcimadamore
parents: 18914
diff changeset
  2890
                boundEnv.info.pendingResolutionPhase;
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2891
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2892
        return res;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2893
    }
21718
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2894
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2895
    enum SearchResultKind {
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2896
        GOOD_MATCH,                 //type I
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2897
        BAD_MATCH_MORE_SPECIFIC,    //type II
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2898
        BAD_MATCH,                  //type III
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2899
        NOT_APPLICABLE_MATCH        //type IV
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2900
    }
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2901
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2902
    boolean hasAnotherApplicableMethod(MethodResolutionContext resolutionContext,
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2903
            Symbol bestSoFar, boolean staticMth) {
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2904
        for (Candidate c : resolutionContext.candidates) {
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2905
            if (resolutionContext.step != c.step ||
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2906
                !c.isApplicable() ||
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2907
                c.sym == bestSoFar) {
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2908
                continue;
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2909
            } else {
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2910
                if (c.sym.isStatic() == staticMth) {
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2911
                    return true;
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2912
                }
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2913
            }
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2914
        }
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2915
        return false;
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2916
    }
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2917
19129
b1409ae675f8 8020843: javac crashes on accessibility check with method reference with typevar receiver
mcimadamore
parents: 18914
diff changeset
  2918
    //where
21718
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2919
        private Symbol choose(Symbol boundSym, Symbol unboundSym) {
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2920
            if (lookupSuccess(boundSym) && lookupSuccess(unboundSym)) {
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2921
                return ambiguityError(boundSym, unboundSym);
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2922
            } else if (lookupSuccess(boundSym) ||
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2923
                    (canIgnore(unboundSym) && !canIgnore(boundSym))) {
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2924
                return boundSym;
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2925
            } else if (lookupSuccess(unboundSym) ||
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2926
                    (canIgnore(boundSym) && !canIgnore(unboundSym))) {
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2927
                return unboundSym;
19129
b1409ae675f8 8020843: javac crashes on accessibility check with method reference with typevar receiver
mcimadamore
parents: 18914
diff changeset
  2928
            } else {
21718
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2929
                return boundSym;
19129
b1409ae675f8 8020843: javac crashes on accessibility check with method reference with typevar receiver
mcimadamore
parents: 18914
diff changeset
  2930
            }
b1409ae675f8 8020843: javac crashes on accessibility check with method reference with typevar receiver
mcimadamore
parents: 18914
diff changeset
  2931
        }
b1409ae675f8 8020843: javac crashes on accessibility check with method reference with typevar receiver
mcimadamore
parents: 18914
diff changeset
  2932
b1409ae675f8 8020843: javac crashes on accessibility check with method reference with typevar receiver
mcimadamore
parents: 18914
diff changeset
  2933
        private boolean lookupSuccess(Symbol s) {
16294
0c291a3cd60d 8007462: Fix provisional applicability for method references
mcimadamore
parents: 15706
diff changeset
  2934
            return s.kind == MTH || s.kind == AMBIGUOUS;
0c291a3cd60d 8007462: Fix provisional applicability for method references
mcimadamore
parents: 15706
diff changeset
  2935
        }
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2936
19129
b1409ae675f8 8020843: javac crashes on accessibility check with method reference with typevar receiver
mcimadamore
parents: 18914
diff changeset
  2937
        private boolean canIgnore(Symbol s) {
b1409ae675f8 8020843: javac crashes on accessibility check with method reference with typevar receiver
mcimadamore
parents: 18914
diff changeset
  2938
            switch (s.kind) {
b1409ae675f8 8020843: javac crashes on accessibility check with method reference with typevar receiver
mcimadamore
parents: 18914
diff changeset
  2939
                case ABSENT_MTH:
b1409ae675f8 8020843: javac crashes on accessibility check with method reference with typevar receiver
mcimadamore
parents: 18914
diff changeset
  2940
                    return true;
b1409ae675f8 8020843: javac crashes on accessibility check with method reference with typevar receiver
mcimadamore
parents: 18914
diff changeset
  2941
                case WRONG_MTH:
b1409ae675f8 8020843: javac crashes on accessibility check with method reference with typevar receiver
mcimadamore
parents: 18914
diff changeset
  2942
                    InapplicableSymbolError errSym =
24400
f05df7f58567 8037379: fix for JDK-8029569 doesn't cover all possible cases
vromero
parents: 24396
diff changeset
  2943
                            (InapplicableSymbolError)s.baseSymbol();
19129
b1409ae675f8 8020843: javac crashes on accessibility check with method reference with typevar receiver
mcimadamore
parents: 18914
diff changeset
  2944
                    return new Template(MethodCheckDiag.ARITY_MISMATCH.regex())
b1409ae675f8 8020843: javac crashes on accessibility check with method reference with typevar receiver
mcimadamore
parents: 18914
diff changeset
  2945
                            .matches(errSym.errCandidate().snd);
b1409ae675f8 8020843: javac crashes on accessibility check with method reference with typevar receiver
mcimadamore
parents: 18914
diff changeset
  2946
                case WRONG_MTHS:
b1409ae675f8 8020843: javac crashes on accessibility check with method reference with typevar receiver
mcimadamore
parents: 18914
diff changeset
  2947
                    InapplicableSymbolsError errSyms =
24400
f05df7f58567 8037379: fix for JDK-8029569 doesn't cover all possible cases
vromero
parents: 24396
diff changeset
  2948
                            (InapplicableSymbolsError)s.baseSymbol();
19129
b1409ae675f8 8020843: javac crashes on accessibility check with method reference with typevar receiver
mcimadamore
parents: 18914
diff changeset
  2949
                    return errSyms.filterCandidates(errSyms.mapCandidates()).isEmpty();
21718
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2950
                case WRONG_STATICNESS:
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2951
                    return false;
19129
b1409ae675f8 8020843: javac crashes on accessibility check with method reference with typevar receiver
mcimadamore
parents: 18914
diff changeset
  2952
                default:
b1409ae675f8 8020843: javac crashes on accessibility check with method reference with typevar receiver
mcimadamore
parents: 18914
diff changeset
  2953
                    return false;
b1409ae675f8 8020843: javac crashes on accessibility check with method reference with typevar receiver
mcimadamore
parents: 18914
diff changeset
  2954
            }
b1409ae675f8 8020843: javac crashes on accessibility check with method reference with typevar receiver
mcimadamore
parents: 18914
diff changeset
  2955
        }
b1409ae675f8 8020843: javac crashes on accessibility check with method reference with typevar receiver
mcimadamore
parents: 18914
diff changeset
  2956
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2957
    /**
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2958
     * Helper for defining custom method-like lookup logic; a lookup helper
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2959
     * provides hooks for (i) the actual lookup logic and (ii) accessing the
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2960
     * lookup result (this step might result in compiler diagnostics to be generated)
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2961
     */
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2962
    abstract class LookupHelper {
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2963
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2964
        /** name of the symbol to lookup */
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2965
        Name name;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2966
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2967
        /** location in which the lookup takes place */
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2968
        Type site;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2969
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2970
        /** actual types used during the lookup */
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2971
        List<Type> argtypes;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2972
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2973
        /** type arguments used during the lookup */
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2974
        List<Type> typeargtypes;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2975
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2976
        /** Max overload resolution phase handled by this helper */
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2977
        MethodResolutionPhase maxPhase;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2978
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2979
        LookupHelper(Name name, Type site, List<Type> argtypes, List<Type> typeargtypes, MethodResolutionPhase maxPhase) {
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2980
            this.name = name;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2981
            this.site = site;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2982
            this.argtypes = argtypes;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2983
            this.typeargtypes = typeargtypes;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2984
            this.maxPhase = maxPhase;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2985
        }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2986
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2987
        /**
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2988
         * Should lookup stop at given phase with given result
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2989
         */
26533
8fba085fbdbf 8055514: Wrong, confusing error when non-static varargs referenced in static context
mcimadamore
parents: 26266
diff changeset
  2990
        final boolean shouldStop(Symbol sym, MethodResolutionPhase phase) {
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2991
            return phase.ordinal() > maxPhase.ordinal() ||
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2992
                !sym.kind.isOverloadError() || sym.kind == AMBIGUOUS;
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2993
        }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2994
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2995
        /**
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2996
         * Search for a symbol under a given overload resolution phase - this method
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2997
         * is usually called several times, once per each overload resolution phase
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2998
         */
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2999
        abstract Symbol lookup(Env<AttrContext> env, MethodResolutionPhase phase);
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3000
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3001
        /**
18668
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  3002
         * Dump overload resolution info
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  3003
         */
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  3004
        void debug(DiagnosticPosition pos, Symbol sym) {
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  3005
            //do nothing
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  3006
        }
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  3007
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  3008
        /**
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3009
         * Validate the result of the lookup
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3010
         */
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3011
        abstract Symbol access(Env<AttrContext> env, DiagnosticPosition pos, Symbol location, Symbol sym);
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3012
    }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3013
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3014
    abstract class BasicLookupHelper extends LookupHelper {
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3015
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3016
        BasicLookupHelper(Name name, Type site, List<Type> argtypes, List<Type> typeargtypes) {
17804
6bed8a263318 8014649: Regression: bug in Resolve.resolveOperator
mcimadamore
parents: 17582
diff changeset
  3017
            this(name, site, argtypes, typeargtypes, MethodResolutionPhase.VARARITY);
6bed8a263318 8014649: Regression: bug in Resolve.resolveOperator
mcimadamore
parents: 17582
diff changeset
  3018
        }
6bed8a263318 8014649: Regression: bug in Resolve.resolveOperator
mcimadamore
parents: 17582
diff changeset
  3019
6bed8a263318 8014649: Regression: bug in Resolve.resolveOperator
mcimadamore
parents: 17582
diff changeset
  3020
        BasicLookupHelper(Name name, Type site, List<Type> argtypes, List<Type> typeargtypes, MethodResolutionPhase maxPhase) {
6bed8a263318 8014649: Regression: bug in Resolve.resolveOperator
mcimadamore
parents: 17582
diff changeset
  3021
            super(name, site, argtypes, typeargtypes, maxPhase);
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3022
        }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3023
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3024
        @Override
18668
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  3025
        final Symbol lookup(Env<AttrContext> env, MethodResolutionPhase phase) {
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  3026
            Symbol sym = doLookup(env, phase);
15038
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  3027
            if (sym.kind == AMBIGUOUS) {
22161
d79fb23b5dca 8029569: internal javac cast exception when resolving varargs ambiguity
vromero
parents: 21718
diff changeset
  3028
                AmbiguityError a_err = (AmbiguityError)sym.baseSymbol();
15038
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  3029
                sym = a_err.mergeAbstracts(site);
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  3030
            }
18668
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  3031
            return sym;
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  3032
        }
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  3033
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  3034
        abstract Symbol doLookup(Env<AttrContext> env, MethodResolutionPhase phase);
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  3035
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  3036
        @Override
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  3037
        Symbol access(Env<AttrContext> env, DiagnosticPosition pos, Symbol location, Symbol sym) {
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  3038
            if (sym.kind.isOverloadError()) {
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3039
                //if nothing is found return the 'first' error
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3040
                sym = accessMethod(sym, pos, location, site, name, true, argtypes, typeargtypes);
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3041
            }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3042
            return sym;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3043
        }
18668
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  3044
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  3045
        @Override
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  3046
        void debug(DiagnosticPosition pos, Symbol sym) {
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  3047
            reportVerboseResolutionDiagnostic(pos, name, site, argtypes, typeargtypes, sym);
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  3048
        }
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3049
    }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3050
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3051
    /**
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3052
     * Helper class for member reference lookup. A reference lookup helper
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3053
     * defines the basic logic for member reference lookup; a method gives
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3054
     * access to an 'unbound' helper used to perform an unbound member
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3055
     * reference lookup.
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3056
     */
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3057
    abstract class ReferenceLookupHelper extends LookupHelper {
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3058
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3059
        /** The member reference tree */
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3060
        JCMemberReference referenceTree;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3061
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3062
        ReferenceLookupHelper(JCMemberReference referenceTree, Name name, Type site,
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3063
                List<Type> argtypes, List<Type> typeargtypes, MethodResolutionPhase maxPhase) {
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3064
            super(name, site, argtypes, typeargtypes, maxPhase);
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3065
            this.referenceTree = referenceTree;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3066
        }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3067
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3068
        /**
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3069
         * Returns an unbound version of this lookup helper. By default, this
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3070
         * method returns an dummy lookup helper.
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3071
         */
18910
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
  3072
        ReferenceLookupHelper unboundLookup(InferenceContext inferenceContext) {
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3073
            //dummy loopkup helper that always return 'methodNotFound'
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3074
            return new ReferenceLookupHelper(referenceTree, name, site, argtypes, typeargtypes, maxPhase) {
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3075
                @Override
18910
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
  3076
                ReferenceLookupHelper unboundLookup(InferenceContext inferenceContext) {
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3077
                    return this;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3078
                }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3079
                @Override
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3080
                Symbol lookup(Env<AttrContext> env, MethodResolutionPhase phase) {
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3081
                    return methodNotFound;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3082
                }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3083
                @Override
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3084
                ReferenceKind referenceKind(Symbol sym) {
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3085
                    Assert.error();
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3086
                    return null;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3087
                }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3088
            };
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3089
        }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3090
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3091
        /**
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3092
         * Get the kind of the member reference
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3093
         */
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3094
        abstract JCMemberReference.ReferenceKind referenceKind(Symbol sym);
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3095
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3096
        Symbol access(Env<AttrContext> env, DiagnosticPosition pos, Symbol location, Symbol sym) {
15038
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  3097
            if (sym.kind == AMBIGUOUS) {
22161
d79fb23b5dca 8029569: internal javac cast exception when resolving varargs ambiguity
vromero
parents: 21718
diff changeset
  3098
                AmbiguityError a_err = (AmbiguityError)sym.baseSymbol();
15038
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  3099
                sym = a_err.mergeAbstracts(site);
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  3100
            }
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3101
            //skip error reporting
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3102
            return sym;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3103
        }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3104
    }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3105
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3106
    /**
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3107
     * Helper class for method reference lookup. The lookup logic is based
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3108
     * upon Resolve.findMethod; in certain cases, this helper class has a
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3109
     * corresponding unbound helper class (see UnboundMethodReferenceLookupHelper).
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3110
     * In such cases, non-static lookup results are thrown away.
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3111
     */
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3112
    class MethodReferenceLookupHelper extends ReferenceLookupHelper {
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3113
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3114
        MethodReferenceLookupHelper(JCMemberReference referenceTree, Name name, Type site,
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3115
                List<Type> argtypes, List<Type> typeargtypes, MethodResolutionPhase maxPhase) {
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3116
            super(referenceTree, name, site, argtypes, typeargtypes, maxPhase);
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3117
        }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3118
16323
c4409c235642 8008537: Missing method reference lookup error when unbound search finds a static method
mcimadamore
parents: 16322
diff changeset
  3119
        @Override
c4409c235642 8008537: Missing method reference lookup error when unbound search finds a static method
mcimadamore
parents: 16322
diff changeset
  3120
        final Symbol lookup(Env<AttrContext> env, MethodResolutionPhase phase) {
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3121
            return findMethod(env, site, name, argtypes, typeargtypes,
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3122
                    phase.isBoxingRequired(), phase.isVarargsRequired(), syms.operatorNames.contains(name));
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3123
        }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3124
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3125
        @Override
18910
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
  3126
        ReferenceLookupHelper unboundLookup(InferenceContext inferenceContext) {
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3127
            if (TreeInfo.isStaticSelector(referenceTree.expr, names) &&
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3128
                    argtypes.nonEmpty() &&
18910
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
  3129
                    (argtypes.head.hasTag(NONE) ||
23132
00a059740d87 8035891: javac, rename method asFree() in InferenceContext to asUndetVar() which reflects better it's purpose
vromero
parents: 23105
diff changeset
  3130
                    types.isSubtypeUnchecked(inferenceContext.asUndetVar(argtypes.head), site))) {
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3131
                return new UnboundMethodReferenceLookupHelper(referenceTree, name,
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3132
                        site, argtypes, typeargtypes, maxPhase);
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3133
            } else {
18910
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
  3134
                return super.unboundLookup(inferenceContext);
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3135
            }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3136
        }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3137
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3138
        @Override
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3139
        ReferenceKind referenceKind(Symbol sym) {
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3140
            if (sym.isStatic()) {
14724
b542db73539a 8004101: Add checks for method reference well-formedness
mcimadamore
parents: 14547
diff changeset
  3141
                return ReferenceKind.STATIC;
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3142
            } else {
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3143
                Name selName = TreeInfo.name(referenceTree.getQualifierExpression());
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3144
                return selName != null && selName == names._super ?
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3145
                        ReferenceKind.SUPER :
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3146
                        ReferenceKind.BOUND;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3147
            }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3148
        }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3149
    }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3150
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3151
    /**
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3152
     * Helper class for unbound method reference lookup. Essentially the same
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3153
     * as the basic method reference lookup helper; main difference is that static
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3154
     * lookup results are thrown away. If qualifier type is raw, an attempt to
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3155
     * infer a parameterized type is made using the first actual argument (that
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3156
     * would otherwise be ignored during the lookup).
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3157
     */
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3158
    class UnboundMethodReferenceLookupHelper extends MethodReferenceLookupHelper {
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3159
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3160
        UnboundMethodReferenceLookupHelper(JCMemberReference referenceTree, Name name, Type site,
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3161
                List<Type> argtypes, List<Type> typeargtypes, MethodResolutionPhase maxPhase) {
16294
0c291a3cd60d 8007462: Fix provisional applicability for method references
mcimadamore
parents: 15706
diff changeset
  3162
            super(referenceTree, name, site, argtypes.tail, typeargtypes, maxPhase);
16805
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  3163
            if (site.isRaw() && !argtypes.head.hasTag(NONE)) {
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  3164
                Type asSuperSite = types.asSuper(argtypes.head, site.tsym);
27849
df6cd9799fcd 8063052: Inference chokes on wildcard derived from method reference
vromero
parents: 27842
diff changeset
  3165
                this.site = types.capture(asSuperSite);
16294
0c291a3cd60d 8007462: Fix provisional applicability for method references
mcimadamore
parents: 15706
diff changeset
  3166
            }
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3167
        }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3168
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3169
        @Override
18910
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
  3170
        ReferenceLookupHelper unboundLookup(InferenceContext inferenceContext) {
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3171
            return this;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3172
        }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3173
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3174
        @Override
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3175
        ReferenceKind referenceKind(Symbol sym) {
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3176
            return ReferenceKind.UNBOUND;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3177
        }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3178
    }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3179
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3180
    /**
15360
450af2a9e3c9 8005854: Add support for array constructor references
mcimadamore
parents: 15038
diff changeset
  3181
     * Helper class for array constructor lookup; an array constructor lookup
450af2a9e3c9 8005854: Add support for array constructor references
mcimadamore
parents: 15038
diff changeset
  3182
     * is simulated by looking up a method that returns the array type specified
450af2a9e3c9 8005854: Add support for array constructor references
mcimadamore
parents: 15038
diff changeset
  3183
     * as qualifier, and that accepts a single int parameter (size of the array).
450af2a9e3c9 8005854: Add support for array constructor references
mcimadamore
parents: 15038
diff changeset
  3184
     */
450af2a9e3c9 8005854: Add support for array constructor references
mcimadamore
parents: 15038
diff changeset
  3185
    class ArrayConstructorReferenceLookupHelper extends ReferenceLookupHelper {
450af2a9e3c9 8005854: Add support for array constructor references
mcimadamore
parents: 15038
diff changeset
  3186
450af2a9e3c9 8005854: Add support for array constructor references
mcimadamore
parents: 15038
diff changeset
  3187
        ArrayConstructorReferenceLookupHelper(JCMemberReference referenceTree, Type site, List<Type> argtypes,
450af2a9e3c9 8005854: Add support for array constructor references
mcimadamore
parents: 15038
diff changeset
  3188
                List<Type> typeargtypes, MethodResolutionPhase maxPhase) {
450af2a9e3c9 8005854: Add support for array constructor references
mcimadamore
parents: 15038
diff changeset
  3189
            super(referenceTree, names.init, site, argtypes, typeargtypes, maxPhase);
450af2a9e3c9 8005854: Add support for array constructor references
mcimadamore
parents: 15038
diff changeset
  3190
        }
450af2a9e3c9 8005854: Add support for array constructor references
mcimadamore
parents: 15038
diff changeset
  3191
450af2a9e3c9 8005854: Add support for array constructor references
mcimadamore
parents: 15038
diff changeset
  3192
        @Override
450af2a9e3c9 8005854: Add support for array constructor references
mcimadamore
parents: 15038
diff changeset
  3193
        protected Symbol lookup(Env<AttrContext> env, MethodResolutionPhase phase) {
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  3194
            WriteableScope sc = WriteableScope.create(syms.arrayClass);
15360
450af2a9e3c9 8005854: Add support for array constructor references
mcimadamore
parents: 15038
diff changeset
  3195
            MethodSymbol arrayConstr = new MethodSymbol(PUBLIC, name, null, site.tsym);
18646
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18389
diff changeset
  3196
            arrayConstr.type = new MethodType(List.<Type>of(syms.intType), site, List.<Type>nil(), syms.methodClass);
15360
450af2a9e3c9 8005854: Add support for array constructor references
mcimadamore
parents: 15038
diff changeset
  3197
            sc.enter(arrayConstr);
450af2a9e3c9 8005854: Add support for array constructor references
mcimadamore
parents: 15038
diff changeset
  3198
            return findMethodInScope(env, site, name, argtypes, typeargtypes, sc, methodNotFound, phase.isBoxingRequired(), phase.isVarargsRequired(), false, false);
450af2a9e3c9 8005854: Add support for array constructor references
mcimadamore
parents: 15038
diff changeset
  3199
        }
450af2a9e3c9 8005854: Add support for array constructor references
mcimadamore
parents: 15038
diff changeset
  3200
450af2a9e3c9 8005854: Add support for array constructor references
mcimadamore
parents: 15038
diff changeset
  3201
        @Override
450af2a9e3c9 8005854: Add support for array constructor references
mcimadamore
parents: 15038
diff changeset
  3202
        ReferenceKind referenceKind(Symbol sym) {
450af2a9e3c9 8005854: Add support for array constructor references
mcimadamore
parents: 15038
diff changeset
  3203
            return ReferenceKind.ARRAY_CTOR;
450af2a9e3c9 8005854: Add support for array constructor references
mcimadamore
parents: 15038
diff changeset
  3204
        }
450af2a9e3c9 8005854: Add support for array constructor references
mcimadamore
parents: 15038
diff changeset
  3205
    }
450af2a9e3c9 8005854: Add support for array constructor references
mcimadamore
parents: 15038
diff changeset
  3206
450af2a9e3c9 8005854: Add support for array constructor references
mcimadamore
parents: 15038
diff changeset
  3207
    /**
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3208
     * Helper class for constructor reference lookup. The lookup logic is based
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3209
     * upon either Resolve.findMethod or Resolve.findDiamond - depending on
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3210
     * whether the constructor reference needs diamond inference (this is the case
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3211
     * if the qualifier type is raw). A special erroneous symbol is returned
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3212
     * if the lookup returns the constructor of an inner class and there's no
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3213
     * enclosing instance in scope.
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3214
     */
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3215
    class ConstructorReferenceLookupHelper extends ReferenceLookupHelper {
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3216
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3217
        boolean needsInference;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3218
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3219
        ConstructorReferenceLookupHelper(JCMemberReference referenceTree, Type site, List<Type> argtypes,
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3220
                List<Type> typeargtypes, MethodResolutionPhase maxPhase) {
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3221
            super(referenceTree, names.init, site, argtypes, typeargtypes, maxPhase);
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3222
            if (site.isRaw()) {
26781
a786b07c7b91 8048614: Add TypeMetadata to contain type annotations and other type information
emc
parents: 26533
diff changeset
  3223
                this.site = new ClassType(site.getEnclosingType(), site.tsym.type.getTypeArguments(), site.tsym, site.getMetadata());
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3224
                needsInference = true;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3225
            }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3226
        }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3227
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3228
        @Override
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3229
        protected Symbol lookup(Env<AttrContext> env, MethodResolutionPhase phase) {
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3230
            Symbol sym = needsInference ?
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3231
                findDiamond(env, site, argtypes, typeargtypes, phase.isBoxingRequired(), phase.isVarargsRequired()) :
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3232
                findMethod(env, site, name, argtypes, typeargtypes,
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3233
                        phase.isBoxingRequired(), phase.isVarargsRequired(), syms.operatorNames.contains(name));
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3234
            return sym.kind != MTH ||
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3235
                          site.getEnclosingType().hasTag(NONE) ||
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3236
                          hasEnclosingInstance(env, site) ?
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  3237
                          sym : new InvalidSymbolError(MISSING_ENCL, sym, null) {
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3238
                    @Override
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3239
                    JCDiagnostic getDiagnostic(DiagnosticType dkind, DiagnosticPosition pos, Symbol location, Type site, Name name, List<Type> argtypes, List<Type> typeargtypes) {
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3240
                       return diags.create(dkind, log.currentSource(), pos,
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3241
                            "cant.access.inner.cls.constr", site.tsym.name, argtypes, site.getEnclosingType());
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3242
                    }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3243
                };
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3244
        }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3245
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3246
        @Override
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3247
        ReferenceKind referenceKind(Symbol sym) {
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3248
            return site.getEnclosingType().hasTag(NONE) ?
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3249
                    ReferenceKind.TOPLEVEL : ReferenceKind.IMPLICIT_INNER;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3250
        }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3251
    }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3252
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3253
    /**
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3254
     * Main overload resolution routine. On each overload resolution step, a
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3255
     * lookup helper class is used to perform the method/constructor lookup;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3256
     * at the end of the lookup, the helper is used to validate the results
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3257
     * (this last step might trigger overload resolution diagnostics).
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3258
     */
16805
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  3259
    Symbol lookupMethod(Env<AttrContext> env, DiagnosticPosition pos, Symbol location, MethodCheck methodCheck, LookupHelper lookupHelper) {
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  3260
        MethodResolutionContext resolveContext = new MethodResolutionContext();
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  3261
        resolveContext.methodCheck = methodCheck;
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  3262
        return lookupMethod(env, pos, location, resolveContext, lookupHelper);
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3263
    }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3264
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3265
    Symbol lookupMethod(Env<AttrContext> env, DiagnosticPosition pos, Symbol location,
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3266
            MethodResolutionContext resolveContext, LookupHelper lookupHelper) {
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3267
        MethodResolutionContext prevResolutionContext = currentResolutionContext;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3268
        try {
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3269
            Symbol bestSoFar = methodNotFound;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3270
            currentResolutionContext = resolveContext;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3271
            for (MethodResolutionPhase phase : methodResolutionSteps) {
25445
603f0c93d5c9 8011044: Remove support for 1.5 and earlier source and target options
ntoda
parents: 25443
diff changeset
  3272
                if (lookupHelper.shouldStop(bestSoFar, phase))
603f0c93d5c9 8011044: Remove support for 1.5 and earlier source and target options
ntoda
parents: 25443
diff changeset
  3273
                    break;
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3274
                MethodResolutionPhase prevPhase = currentResolutionContext.step;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3275
                Symbol prevBest = bestSoFar;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3276
                currentResolutionContext.step = phase;
18668
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  3277
                Symbol sym = lookupHelper.lookup(env, phase);
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  3278
                lookupHelper.debug(pos, sym);
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  3279
                bestSoFar = phase.mergeResults(bestSoFar, sym);
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3280
                env.info.pendingResolutionPhase = (prevBest == bestSoFar) ? prevPhase : phase;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3281
            }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3282
            return lookupHelper.access(env, pos, location, bestSoFar);
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3283
        } finally {
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3284
            currentResolutionContext = prevResolutionContext;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3285
        }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3286
    }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3287
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3288
    /**
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3289
     * Resolve `c.name' where name == this or name == super.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3290
     * @param pos           The position to use for error reporting.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3291
     * @param env           The environment current at the expression.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3292
     * @param c             The qualifier.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3293
     * @param name          The identifier's name.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3294
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3295
    Symbol resolveSelf(DiagnosticPosition pos,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3296
                       Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3297
                       TypeSymbol c,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3298
                       Name name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3299
        Env<AttrContext> env1 = env;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3300
        boolean staticOnly = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3301
        while (env1.outer != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3302
            if (isStatic(env1)) staticOnly = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3303
            if (env1.enclClass.sym == c) {
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  3304
                Symbol sym = env1.info.scope.findFirst(name);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3305
                if (sym != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3306
                    if (staticOnly) sym = new StaticError(sym);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  3307
                    return accessBase(sym, pos, env.enclClass.sym.type,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3308
                                  name, true);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3309
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3310
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3311
            if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3312
            env1 = env1.outer;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3313
        }
23105
1d1ea26a10ae 8030855: Default methods should be visible under source previous to 8
vromero
parents: 22442
diff changeset
  3314
        if (c.isInterface() &&
1d1ea26a10ae 8030855: Default methods should be visible under source previous to 8
vromero
parents: 22442
diff changeset
  3315
            name == names._super && !isStatic(env) &&
1d1ea26a10ae 8030855: Default methods should be visible under source previous to 8
vromero
parents: 22442
diff changeset
  3316
            types.isDirectSuperInterface(c, env.enclClass.sym)) {
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3317
            //this might be a default super call if one of the superinterfaces is 'c'
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3318
            for (Type t : pruneInterfaces(env.enclClass.type)) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3319
                if (t.tsym == c) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3320
                    env.info.defaultSuperCallSite = t;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3321
                    return new VarSymbol(0, names._super,
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3322
                            types.asSuper(env.enclClass.type, c), env.enclClass.sym);
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3323
                }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3324
            }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3325
            //find a direct superinterface that is a subtype of 'c'
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3326
            for (Type i : types.interfaces(env.enclClass.type)) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3327
                if (i.tsym.isSubClass(c, types) && i.tsym != c) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3328
                    log.error(pos, "illegal.default.super.call", c,
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3329
                            diags.fragment("redundant.supertype", c, i));
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3330
                    return syms.errSymbol;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3331
                }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3332
            }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3333
            Assert.error();
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3334
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3335
        log.error(pos, "not.encl.class", c);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3336
        return syms.errSymbol;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3337
    }
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3338
    //where
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3339
    private List<Type> pruneInterfaces(Type t) {
20249
93f8eae31092 6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents: 19940
diff changeset
  3340
        ListBuffer<Type> result = new ListBuffer<>();
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3341
        for (Type t1 : types.interfaces(t)) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3342
            boolean shouldAdd = true;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3343
            for (Type t2 : types.interfaces(t)) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3344
                if (t1 != t2 && types.isSubtypeNoCapture(t2, t1)) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3345
                    shouldAdd = false;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3346
                }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3347
            }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3348
            if (shouldAdd) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3349
                result.append(t1);
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3350
            }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3351
        }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3352
        return result.toList();
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3353
    }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3354
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3355
06bc494ca11e Initial load
duke
parents:
diff changeset
  3356
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  3357
     * Resolve `c.this' for an enclosing class c that contains the
06bc494ca11e Initial load
duke
parents:
diff changeset
  3358
     * named member.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3359
     * @param pos           The position to use for error reporting.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3360
     * @param env           The environment current at the expression.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3361
     * @param member        The member that must be contained in the result.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3362
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3363
    Symbol resolveSelfContaining(DiagnosticPosition pos,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3364
                                 Env<AttrContext> env,
8622
4f032629a0fd 6541876: "Enclosing Instance" error new in 1.6
mcimadamore
parents: 8430
diff changeset
  3365
                                 Symbol member,
4f032629a0fd 6541876: "Enclosing Instance" error new in 1.6
mcimadamore
parents: 8430
diff changeset
  3366
                                 boolean isSuperCall) {
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3367
        Symbol sym = resolveSelfContainingInternal(env, member, isSuperCall);
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3368
        if (sym == null) {
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3369
            log.error(pos, "encl.class.required", member);
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3370
            return syms.errSymbol;
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3371
        } else {
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3372
            return accessBase(sym, pos, env.enclClass.sym.type, sym.name, true);
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3373
        }
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3374
    }
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3375
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3376
    boolean hasEnclosingInstance(Env<AttrContext> env, Type type) {
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3377
        Symbol encl = resolveSelfContainingInternal(env, type.tsym, false);
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  3378
        return encl != null && !encl.kind.isOverloadError();
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3379
    }
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3380
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3381
    private Symbol resolveSelfContainingInternal(Env<AttrContext> env,
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3382
                                 Symbol member,
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3383
                                 boolean isSuperCall) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3384
        Name name = names._this;
8622
4f032629a0fd 6541876: "Enclosing Instance" error new in 1.6
mcimadamore
parents: 8430
diff changeset
  3385
        Env<AttrContext> env1 = isSuperCall ? env.outer : env;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3386
        boolean staticOnly = false;
8622
4f032629a0fd 6541876: "Enclosing Instance" error new in 1.6
mcimadamore
parents: 8430
diff changeset
  3387
        if (env1 != null) {
4f032629a0fd 6541876: "Enclosing Instance" error new in 1.6
mcimadamore
parents: 8430
diff changeset
  3388
            while (env1 != null && env1.outer != null) {
4f032629a0fd 6541876: "Enclosing Instance" error new in 1.6
mcimadamore
parents: 8430
diff changeset
  3389
                if (isStatic(env1)) staticOnly = true;
4f032629a0fd 6541876: "Enclosing Instance" error new in 1.6
mcimadamore
parents: 8430
diff changeset
  3390
                if (env1.enclClass.sym.isSubClass(member.owner, types)) {
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  3391
                    Symbol sym = env1.info.scope.findFirst(name);
8622
4f032629a0fd 6541876: "Enclosing Instance" error new in 1.6
mcimadamore
parents: 8430
diff changeset
  3392
                    if (sym != null) {
4f032629a0fd 6541876: "Enclosing Instance" error new in 1.6
mcimadamore
parents: 8430
diff changeset
  3393
                        if (staticOnly) sym = new StaticError(sym);
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3394
                        return sym;
8622
4f032629a0fd 6541876: "Enclosing Instance" error new in 1.6
mcimadamore
parents: 8430
diff changeset
  3395
                    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3396
                }
8622
4f032629a0fd 6541876: "Enclosing Instance" error new in 1.6
mcimadamore
parents: 8430
diff changeset
  3397
                if ((env1.enclClass.sym.flags() & STATIC) != 0)
4f032629a0fd 6541876: "Enclosing Instance" error new in 1.6
mcimadamore
parents: 8430
diff changeset
  3398
                    staticOnly = true;
4f032629a0fd 6541876: "Enclosing Instance" error new in 1.6
mcimadamore
parents: 8430
diff changeset
  3399
                env1 = env1.outer;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3400
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3401
        }
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3402
        return null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3403
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3404
06bc494ca11e Initial load
duke
parents:
diff changeset
  3405
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  3406
     * Resolve an appropriate implicit this instance for t's container.
9303
eae35c201e19 7032975: API files in javax.annotation.processing need to be updated for references to JLS
jjh
parents: 9302
diff changeset
  3407
     * JLS 8.8.5.1 and 15.9.2
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3408
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3409
    Type resolveImplicitThis(DiagnosticPosition pos, Env<AttrContext> env, Type t) {
8622
4f032629a0fd 6541876: "Enclosing Instance" error new in 1.6
mcimadamore
parents: 8430
diff changeset
  3410
        return resolveImplicitThis(pos, env, t, false);
4f032629a0fd 6541876: "Enclosing Instance" error new in 1.6
mcimadamore
parents: 8430
diff changeset
  3411
    }
4f032629a0fd 6541876: "Enclosing Instance" error new in 1.6
mcimadamore
parents: 8430
diff changeset
  3412
4f032629a0fd 6541876: "Enclosing Instance" error new in 1.6
mcimadamore
parents: 8430
diff changeset
  3413
    Type resolveImplicitThis(DiagnosticPosition pos, Env<AttrContext> env, Type t, boolean isSuperCall) {
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  3414
        Type thisType = (t.tsym.owner.kind.matches(KindSelector.VAL_MTH)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3415
                         ? resolveSelf(pos, env, t.getEnclosingType().tsym, names._this)
8622
4f032629a0fd 6541876: "Enclosing Instance" error new in 1.6
mcimadamore
parents: 8430
diff changeset
  3416
                         : resolveSelfContaining(pos, env, t.tsym, isSuperCall)).type;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3417
        if (env.info.isSelfCall && thisType.tsym == env.enclClass.sym)
06bc494ca11e Initial load
duke
parents:
diff changeset
  3418
            log.error(pos, "cant.ref.before.ctor.called", "this");
06bc494ca11e Initial load
duke
parents:
diff changeset
  3419
        return thisType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3420
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3421
06bc494ca11e Initial load
duke
parents:
diff changeset
  3422
/* ***************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
  3423
 *  ResolveError classes, indicating error situations when accessing symbols
06bc494ca11e Initial load
duke
parents:
diff changeset
  3424
 ****************************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
  3425
12082
32bdf6ef6388 7151070: NullPointerException in Resolve.isAccessible
mcimadamore
parents: 12078
diff changeset
  3426
    //used by TransTypes when checking target type of synthetic cast
32bdf6ef6388 7151070: NullPointerException in Resolve.isAccessible
mcimadamore
parents: 12078
diff changeset
  3427
    public void logAccessErrorInternal(Env<AttrContext> env, JCTree tree, Type type) {
32bdf6ef6388 7151070: NullPointerException in Resolve.isAccessible
mcimadamore
parents: 12078
diff changeset
  3428
        AccessError error = new AccessError(env, env.enclClass.type, type.tsym);
32bdf6ef6388 7151070: NullPointerException in Resolve.isAccessible
mcimadamore
parents: 12078
diff changeset
  3429
        logResolveError(error, tree.pos(), env.enclClass.sym, env.enclClass.type, null, null, null);
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3430
    }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3431
    //where
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3432
    private void logResolveError(ResolveError error,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3433
            DiagnosticPosition pos,
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  3434
            Symbol location,
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3435
            Type site,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3436
            Name name,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3437
            List<Type> argtypes,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3438
            List<Type> typeargtypes) {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3439
        JCDiagnostic d = error.getDiagnostic(JCDiagnostic.DiagnosticType.ERROR,
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  3440
                pos, location, site, name, argtypes, typeargtypes);
6356
af24929939ca 6976747: JCDiagnostic: replace "boolean mandatory" with new "Set<JCDiagnostic.Flag>"
jjg
parents: 6344
diff changeset
  3441
        if (d != null) {
af24929939ca 6976747: JCDiagnostic: replace "boolean mandatory" with new "Set<JCDiagnostic.Flag>"
jjg
parents: 6344
diff changeset
  3442
            d.setFlag(DiagnosticFlag.RESOLVE_ERROR);
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3443
            log.report(d);
6356
af24929939ca 6976747: JCDiagnostic: replace "boolean mandatory" with new "Set<JCDiagnostic.Flag>"
jjg
parents: 6344
diff changeset
  3444
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3445
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3446
1534
e923a41e84cc 6758789: Some method resolution diagnostic should be improved
mcimadamore
parents: 1533
diff changeset
  3447
    private final LocalizedString noArgs = new LocalizedString("compiler.misc.no.args");
e923a41e84cc 6758789: Some method resolution diagnostic should be improved
mcimadamore
parents: 1533
diff changeset
  3448
e923a41e84cc 6758789: Some method resolution diagnostic should be improved
mcimadamore
parents: 1533
diff changeset
  3449
    public Object methodArguments(List<Type> argtypes) {
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  3450
        if (argtypes == null || argtypes.isEmpty()) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  3451
            return noArgs;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  3452
        } else {
20249
93f8eae31092 6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents: 19940
diff changeset
  3453
            ListBuffer<Object> diagArgs = new ListBuffer<>();
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  3454
            for (Type t : argtypes) {
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14062
diff changeset
  3455
                if (t.hasTag(DEFERRED)) {
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  3456
                    diagArgs.append(((DeferredAttr.DeferredType)t).tree);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  3457
                } else {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  3458
                    diagArgs.append(t);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  3459
                }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  3460
            }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  3461
            return diagArgs;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  3462
        }
1534
e923a41e84cc 6758789: Some method resolution diagnostic should be improved
mcimadamore
parents: 1533
diff changeset
  3463
    }
e923a41e84cc 6758789: Some method resolution diagnostic should be improved
mcimadamore
parents: 1533
diff changeset
  3464
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3465
    /**
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3466
     * Root class for resolution errors. Subclass of ResolveError
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3467
     * represent a different kinds of resolution error - as such they must
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3468
     * specify how they map into concrete compiler diagnostics.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3469
     */
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3470
    abstract class ResolveError extends Symbol {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3471
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3472
        /** The name of the kind of error, for debugging only. */
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3473
        final String debugName;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3474
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  3475
        ResolveError(Kind kind, String debugName) {
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3476
            super(kind, 0, null, null, null);
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3477
            this.debugName = debugName;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3478
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3479
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
  3480
        @Override @DefinedBy(Api.LANGUAGE_MODEL)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3481
        public <R, P> R accept(ElementVisitor<R, P> v, P p) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3482
            throw new AssertionError();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3483
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3484
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3485
        @Override
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3486
        public String toString() {
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3487
            return debugName;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3488
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3489
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3490
        @Override
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3491
        public boolean exists() {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3492
            return false;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3493
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3494
21718
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  3495
        @Override
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  3496
        public boolean isStatic() {
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  3497
            return false;
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  3498
        }
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  3499
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3500
        /**
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3501
         * Create an external representation for this erroneous symbol to be
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3502
         * used during attribution - by default this returns the symbol of a
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3503
         * brand new error type which stores the original type found
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3504
         * during resolution.
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3505
         *
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3506
         * @param name     the name used during resolution
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3507
         * @param location the location from which the symbol is accessed
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3508
         */
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3509
        protected Symbol access(Name name, TypeSymbol location) {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3510
            return types.createErrorType(name, location, syms.errSymbol.type).tsym;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3511
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3512
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3513
        /**
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3514
         * Create a diagnostic representing this resolution error.
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3515
         *
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3516
         * @param dkind     The kind of the diagnostic to be created (e.g error).
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3517
         * @param pos       The position to be used for error reporting.
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3518
         * @param site      The original type from where the selection took place.
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3519
         * @param name      The name of the symbol to be resolved.
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3520
         * @param argtypes  The invocation's value arguments,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3521
         *                  if we looked for a method.
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3522
         * @param typeargtypes  The invocation's type arguments,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3523
         *                      if we looked for a method.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3524
         */
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3525
        abstract JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3526
                DiagnosticPosition pos,
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  3527
                Symbol location,
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3528
                Type site,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3529
                Name name,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3530
                List<Type> argtypes,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3531
                List<Type> typeargtypes);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3532
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3533
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3534
    /**
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3535
     * This class is the root class of all resolution errors caused by
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3536
     * an invalid symbol being found during resolution.
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3537
     */
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3538
    abstract class InvalidSymbolError extends ResolveError {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3539
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3540
        /** The invalid symbol found during resolution */
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3541
        Symbol sym;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3542
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  3543
        InvalidSymbolError(Kind kind, Symbol sym, String debugName) {
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3544
            super(kind, debugName);
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3545
            this.sym = sym;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3546
        }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3547
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3548
        @Override
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3549
        public boolean exists() {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3550
            return true;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3551
        }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3552
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3553
        @Override
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3554
        public String toString() {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3555
             return super.toString() + " wrongSym=" + sym;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3556
        }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3557
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3558
        @Override
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3559
        public Symbol access(Name name, TypeSymbol location) {
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  3560
            if (!sym.kind.isOverloadError() && sym.kind.matches(KindSelector.TYP))
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3561
                return types.createErrorType(name, location, sym.type).tsym;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3562
            else
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3563
                return sym;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3564
        }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3565
    }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3566
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3567
    /**
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3568
     * InvalidSymbolError error class indicating that a symbol matching a
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3569
     * given name does not exists in a given site.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3570
     */
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3571
    class SymbolNotFoundError extends ResolveError {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3572
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  3573
        SymbolNotFoundError(Kind kind) {
21718
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  3574
            this(kind, "symbol not found error");
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  3575
        }
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  3576
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  3577
        SymbolNotFoundError(Kind kind, String debugName) {
21718
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  3578
            super(kind, debugName);
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3579
        }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3580
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3581
        @Override
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3582
        JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3583
                DiagnosticPosition pos,
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  3584
                Symbol location,
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3585
                Type site,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3586
                Name name,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3587
                List<Type> argtypes,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3588
                List<Type> typeargtypes) {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3589
            argtypes = argtypes == null ? List.<Type>nil() : argtypes;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3590
            typeargtypes = typeargtypes == null ? List.<Type>nil() : typeargtypes;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3591
            if (name == names.error)
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3592
                return null;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3593
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  3594
            if (syms.operatorNames.contains(name)) {
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  3595
                boolean isUnaryOp = argtypes.size() == 1;
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  3596
                String key = argtypes.size() == 1 ?
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  3597
                    "operator.cant.be.applied" :
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  3598
                    "operator.cant.be.applied.1";
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  3599
                Type first = argtypes.head;
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  3600
                Type second = !isUnaryOp ? argtypes.tail.head : null;
6151
dd513881e71d 6957438: improve code for generating warning messages containing option names
jjg
parents: 5857
diff changeset
  3601
                return diags.create(dkind, log.currentSource(), pos,
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  3602
                        key, name, first, second);
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3603
            }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3604
            boolean hasLocation = false;
8239
d2c934e951e2 7014715: javac returns different error code for certain failure(s)
mcimadamore
parents: 8237
diff changeset
  3605
            if (location == null) {
d2c934e951e2 7014715: javac returns different error code for certain failure(s)
mcimadamore
parents: 8237
diff changeset
  3606
                location = site.tsym;
d2c934e951e2 7014715: javac returns different error code for certain failure(s)
mcimadamore
parents: 8237
diff changeset
  3607
            }
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  3608
            if (!location.name.isEmpty()) {
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  3609
                if (location.kind == PCK && !site.tsym.exists()) {
6151
dd513881e71d 6957438: improve code for generating warning messages containing option names
jjg
parents: 5857
diff changeset
  3610
                    return diags.create(dkind, log.currentSource(), pos,
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  3611
                        "doesnt.exist", location);
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3612
                }
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  3613
                hasLocation = !location.name.equals(names._this) &&
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  3614
                        !location.name.equals(names._super);
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3615
            }
21718
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  3616
            boolean isConstructor = (kind == ABSENT_MTH || kind == WRONG_STATICNESS) &&
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  3617
                    name == names.init;
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  3618
            KindName kindname = isConstructor ? KindName.CONSTRUCTOR : kind.absentKind();
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3619
            Name idname = isConstructor ? site.tsym.name : name;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3620
            String errKey = getErrorKey(kindname, typeargtypes.nonEmpty(), hasLocation);
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3621
            if (hasLocation) {
6151
dd513881e71d 6957438: improve code for generating warning messages containing option names
jjg
parents: 5857
diff changeset
  3622
                return diags.create(dkind, log.currentSource(), pos,
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3623
                        errKey, kindname, idname, //symbol kindname, name
14953
89a34297b198 8004099: Bad compiler diagnostic generated when poly expression is passed to non-existent method
mcimadamore
parents: 14801
diff changeset
  3624
                        typeargtypes, args(argtypes), //type parameters and arguments (if any)
8239
d2c934e951e2 7014715: javac returns different error code for certain failure(s)
mcimadamore
parents: 8237
diff changeset
  3625
                        getLocationDiag(location, site)); //location kindname, type
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3626
            }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3627
            else {
6151
dd513881e71d 6957438: improve code for generating warning messages containing option names
jjg
parents: 5857
diff changeset
  3628
                return diags.create(dkind, log.currentSource(), pos,
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3629
                        errKey, kindname, idname, //symbol kindname, name
14953
89a34297b198 8004099: Bad compiler diagnostic generated when poly expression is passed to non-existent method
mcimadamore
parents: 14801
diff changeset
  3630
                        typeargtypes, args(argtypes)); //type parameters and arguments (if any)
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3631
            }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3632
        }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3633
        //where
14953
89a34297b198 8004099: Bad compiler diagnostic generated when poly expression is passed to non-existent method
mcimadamore
parents: 14801
diff changeset
  3634
        private Object args(List<Type> args) {
89a34297b198 8004099: Bad compiler diagnostic generated when poly expression is passed to non-existent method
mcimadamore
parents: 14801
diff changeset
  3635
            return args.isEmpty() ? args : methodArguments(args);
89a34297b198 8004099: Bad compiler diagnostic generated when poly expression is passed to non-existent method
mcimadamore
parents: 14801
diff changeset
  3636
        }
89a34297b198 8004099: Bad compiler diagnostic generated when poly expression is passed to non-existent method
mcimadamore
parents: 14801
diff changeset
  3637
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3638
        private String getErrorKey(KindName kindname, boolean hasTypeArgs, boolean hasLocation) {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3639
            String key = "cant.resolve";
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3640
            String suffix = hasLocation ? ".location" : "";
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3641
            switch (kindname) {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3642
                case METHOD:
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3643
                case CONSTRUCTOR: {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3644
                    suffix += ".args";
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3645
                    suffix += hasTypeArgs ? ".params" : "";
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3646
                }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3647
            }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3648
            return key + suffix;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3649
        }
8239
d2c934e951e2 7014715: javac returns different error code for certain failure(s)
mcimadamore
parents: 8237
diff changeset
  3650
        private JCDiagnostic getLocationDiag(Symbol location, Type site) {
d2c934e951e2 7014715: javac returns different error code for certain failure(s)
mcimadamore
parents: 8237
diff changeset
  3651
            if (location.kind == VAR) {
d2c934e951e2 7014715: javac returns different error code for certain failure(s)
mcimadamore
parents: 8237
diff changeset
  3652
                return diags.fragment("location.1",
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  3653
                    kindName(location),
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  3654
                    location,
8239
d2c934e951e2 7014715: javac returns different error code for certain failure(s)
mcimadamore
parents: 8237
diff changeset
  3655
                    location.type);
d2c934e951e2 7014715: javac returns different error code for certain failure(s)
mcimadamore
parents: 8237
diff changeset
  3656
            } else {
d2c934e951e2 7014715: javac returns different error code for certain failure(s)
mcimadamore
parents: 8237
diff changeset
  3657
                return diags.fragment("location",
d2c934e951e2 7014715: javac returns different error code for certain failure(s)
mcimadamore
parents: 8237
diff changeset
  3658
                    typeKindName(site),
d2c934e951e2 7014715: javac returns different error code for certain failure(s)
mcimadamore
parents: 8237
diff changeset
  3659
                    site,
d2c934e951e2 7014715: javac returns different error code for certain failure(s)
mcimadamore
parents: 8237
diff changeset
  3660
                    null);
d2c934e951e2 7014715: javac returns different error code for certain failure(s)
mcimadamore
parents: 8237
diff changeset
  3661
            }
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  3662
        }
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3663
    }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3664
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3665
    /**
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3666
     * InvalidSymbolError error class indicating that a given symbol
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3667
     * (either a method, a constructor or an operand) is not applicable
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3668
     * given an actual arguments/type argument list.
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3669
     */
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  3670
    class InapplicableSymbolError extends ResolveError {
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3671
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3672
        protected MethodResolutionContext resolveContext;
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3673
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3674
        InapplicableSymbolError(MethodResolutionContext context) {
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3675
            this(WRONG_MTH, "inapplicable symbol error", context);
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3676
        }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3677
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  3678
        protected InapplicableSymbolError(Kind kind, String debugName, MethodResolutionContext context) {
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  3679
            super(kind, debugName);
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3680
            this.resolveContext = context;
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3681
        }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3682
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3683
        @Override
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3684
        public String toString() {
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  3685
            return super.toString();
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  3686
        }
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  3687
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  3688
        @Override
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  3689
        public boolean exists() {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  3690
            return true;
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3691
        }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3692
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3693
        @Override
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3694
        JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3695
                DiagnosticPosition pos,
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  3696
                Symbol location,
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3697
                Type site,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3698
                Name name,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3699
                List<Type> argtypes,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3700
                List<Type> typeargtypes) {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3701
            if (name == names.error)
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3702
                return null;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3703
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  3704
            if (syms.operatorNames.contains(name)) {
8237
d5ef8db7ad09 5017953: spurious cascaded diagnostics when name not found
mcimadamore
parents: 8236
diff changeset
  3705
                boolean isUnaryOp = argtypes.size() == 1;
d5ef8db7ad09 5017953: spurious cascaded diagnostics when name not found
mcimadamore
parents: 8236
diff changeset
  3706
                String key = argtypes.size() == 1 ?
d5ef8db7ad09 5017953: spurious cascaded diagnostics when name not found
mcimadamore
parents: 8236
diff changeset
  3707
                    "operator.cant.be.applied" :
d5ef8db7ad09 5017953: spurious cascaded diagnostics when name not found
mcimadamore
parents: 8236
diff changeset
  3708
                    "operator.cant.be.applied.1";
d5ef8db7ad09 5017953: spurious cascaded diagnostics when name not found
mcimadamore
parents: 8236
diff changeset
  3709
                Type first = argtypes.head;
d5ef8db7ad09 5017953: spurious cascaded diagnostics when name not found
mcimadamore
parents: 8236
diff changeset
  3710
                Type second = !isUnaryOp ? argtypes.tail.head : null;
d5ef8db7ad09 5017953: spurious cascaded diagnostics when name not found
mcimadamore
parents: 8236
diff changeset
  3711
                return diags.create(dkind, log.currentSource(), pos,
d5ef8db7ad09 5017953: spurious cascaded diagnostics when name not found
mcimadamore
parents: 8236
diff changeset
  3712
                        key, name, first, second);
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3713
            }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3714
            else {
18913
a29ea2022929 8020286: Wrong diagnostic after compaction
mcimadamore
parents: 18911
diff changeset
  3715
                Pair<Symbol, JCDiagnostic> c = errCandidate();
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  3716
                if (compactMethodDiags) {
28331
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  3717
                    JCDiagnostic simpleDiag =
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  3718
                        MethodResolutionDiagHelper.rewrite(diags, pos, log.currentSource(), dkind, c.snd);
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  3719
                    if (simpleDiag != null) {
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  3720
                        return simpleDiag;
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  3721
                    }
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  3722
                }
18913
a29ea2022929 8020286: Wrong diagnostic after compaction
mcimadamore
parents: 18911
diff changeset
  3723
                Symbol ws = c.fst.asMemberOf(site, types);
6151
dd513881e71d 6957438: improve code for generating warning messages containing option names
jjg
parents: 5857
diff changeset
  3724
                return diags.create(dkind, log.currentSource(), pos,
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3725
                          "cant.apply.symbol",
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3726
                          kindName(ws),
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3727
                          ws.name == names.init ? ws.owner.name : ws.name,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3728
                          methodArguments(ws.type.getParameterTypes()),
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3729
                          methodArguments(argtypes),
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3730
                          kindName(ws.owner),
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3731
                          ws.owner.type,
18913
a29ea2022929 8020286: Wrong diagnostic after compaction
mcimadamore
parents: 18911
diff changeset
  3732
                          c.snd);
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3733
            }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3734
        }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3735
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3736
        @Override
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3737
        public Symbol access(Name name, TypeSymbol location) {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3738
            return types.createErrorType(name, location, syms.errSymbol.type).tsym;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3739
        }
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  3740
18913
a29ea2022929 8020286: Wrong diagnostic after compaction
mcimadamore
parents: 18911
diff changeset
  3741
        protected Pair<Symbol, JCDiagnostic> errCandidate() {
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3742
            Candidate bestSoFar = null;
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3743
            for (Candidate c : resolveContext.candidates) {
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3744
                if (c.isApplicable()) continue;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3745
                bestSoFar = c;
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  3746
            }
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3747
            Assert.checkNonNull(bestSoFar);
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22161
diff changeset
  3748
            return new Pair<>(bestSoFar.sym, bestSoFar.details);
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  3749
        }
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3750
    }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3751
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3752
    /**
28331
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  3753
     * ResolveError error class indicating that a symbol (either methods, constructors or operand)
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  3754
     * is not applicable given an actual arguments/type argument list.
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3755
     */
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  3756
    class InapplicableSymbolsError extends InapplicableSymbolError {
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
  3757
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3758
        InapplicableSymbolsError(MethodResolutionContext context) {
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3759
            super(WRONG_MTHS, "inapplicable symbols", context);
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3760
        }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3761
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3762
        @Override
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3763
        JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3764
                DiagnosticPosition pos,
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  3765
                Symbol location,
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3766
                Type site,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3767
                Name name,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3768
                List<Type> argtypes,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3769
                List<Type> typeargtypes) {
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  3770
            Map<Symbol, JCDiagnostic> candidatesMap = mapCandidates();
19129
b1409ae675f8 8020843: javac crashes on accessibility check with method reference with typevar receiver
mcimadamore
parents: 18914
diff changeset
  3771
            Map<Symbol, JCDiagnostic> filteredCandidates = compactMethodDiags ?
b1409ae675f8 8020843: javac crashes on accessibility check with method reference with typevar receiver
mcimadamore
parents: 18914
diff changeset
  3772
                    filterCandidates(candidatesMap) :
b1409ae675f8 8020843: javac crashes on accessibility check with method reference with typevar receiver
mcimadamore
parents: 18914
diff changeset
  3773
                    mapCandidates();
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  3774
            if (filteredCandidates.isEmpty()) {
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  3775
                filteredCandidates = candidatesMap;
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  3776
            }
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  3777
            boolean truncatedDiag = candidatesMap.size() != filteredCandidates.size();
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  3778
            if (filteredCandidates.size() > 1) {
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
  3779
                JCDiagnostic err = diags.create(dkind,
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  3780
                        null,
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  3781
                        truncatedDiag ?
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  3782
                            EnumSet.of(DiagnosticFlag.COMPRESSED) :
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  3783
                            EnumSet.noneOf(DiagnosticFlag.class),
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
  3784
                        log.currentSource(),
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
  3785
                        pos,
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
  3786
                        "cant.apply.symbols",
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  3787
                        name == names.init ? KindName.CONSTRUCTOR : kind.absentKind(),
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3788
                        name == names.init ? site.tsym.name : name,
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3789
                        methodArguments(argtypes));
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  3790
                return new JCDiagnostic.MultilineDiagnostic(err, candidateDetails(filteredCandidates, site));
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  3791
            } else if (filteredCandidates.size() == 1) {
18913
a29ea2022929 8020286: Wrong diagnostic after compaction
mcimadamore
parents: 18911
diff changeset
  3792
                Map.Entry<Symbol, JCDiagnostic> _e =
a29ea2022929 8020286: Wrong diagnostic after compaction
mcimadamore
parents: 18911
diff changeset
  3793
                                filteredCandidates.entrySet().iterator().next();
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22161
diff changeset
  3794
                final Pair<Symbol, JCDiagnostic> p = new Pair<>(_e.getKey(), _e.getValue());
18913
a29ea2022929 8020286: Wrong diagnostic after compaction
mcimadamore
parents: 18911
diff changeset
  3795
                JCDiagnostic d = new InapplicableSymbolError(resolveContext) {
a29ea2022929 8020286: Wrong diagnostic after compaction
mcimadamore
parents: 18911
diff changeset
  3796
                    @Override
a29ea2022929 8020286: Wrong diagnostic after compaction
mcimadamore
parents: 18911
diff changeset
  3797
                    protected Pair<Symbol, JCDiagnostic> errCandidate() {
a29ea2022929 8020286: Wrong diagnostic after compaction
mcimadamore
parents: 18911
diff changeset
  3798
                        return p;
a29ea2022929 8020286: Wrong diagnostic after compaction
mcimadamore
parents: 18911
diff changeset
  3799
                    }
a29ea2022929 8020286: Wrong diagnostic after compaction
mcimadamore
parents: 18911
diff changeset
  3800
                }.getDiagnostic(dkind, pos,
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  3801
                    location, site, name, argtypes, typeargtypes);
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  3802
                if (truncatedDiag) {
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  3803
                    d.setFlag(DiagnosticFlag.COMPRESSED);
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  3804
                }
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  3805
                return d;
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
  3806
            } else {
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
  3807
                return new SymbolNotFoundError(ABSENT_MTH).getDiagnostic(dkind, pos,
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  3808
                    location, site, name, argtypes, typeargtypes);
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
  3809
            }
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
  3810
        }
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
  3811
        //where
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  3812
            private Map<Symbol, JCDiagnostic> mapCandidates() {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22161
diff changeset
  3813
                Map<Symbol, JCDiagnostic> candidates = new LinkedHashMap<>();
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  3814
                for (Candidate c : resolveContext.candidates) {
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  3815
                    if (c.isApplicable()) continue;
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  3816
                    candidates.put(c.sym, c.details);
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  3817
                }
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  3818
                return candidates;
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  3819
            }
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  3820
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  3821
            Map<Symbol, JCDiagnostic> filterCandidates(Map<Symbol, JCDiagnostic> candidatesMap) {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22161
diff changeset
  3822
                Map<Symbol, JCDiagnostic> candidates = new LinkedHashMap<>();
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  3823
                for (Map.Entry<Symbol, JCDiagnostic> _entry : candidatesMap.entrySet()) {
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  3824
                    JCDiagnostic d = _entry.getValue();
19129
b1409ae675f8 8020843: javac crashes on accessibility check with method reference with typevar receiver
mcimadamore
parents: 18914
diff changeset
  3825
                    if (!new Template(MethodCheckDiag.ARITY_MISMATCH.regex()).matches(d)) {
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  3826
                        candidates.put(_entry.getKey(), d);
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  3827
                    }
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  3828
                }
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  3829
                return candidates;
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  3830
            }
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  3831
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  3832
            private List<JCDiagnostic> candidateDetails(Map<Symbol, JCDiagnostic> candidatesMap, Type site) {
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  3833
                List<JCDiagnostic> details = List.nil();
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  3834
                for (Map.Entry<Symbol, JCDiagnostic> _entry : candidatesMap.entrySet()) {
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  3835
                    Symbol sym = _entry.getKey();
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  3836
                    JCDiagnostic detailDiag = diags.fragment("inapplicable.method",
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  3837
                            Kinds.kindName(sym),
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  3838
                            sym.location(site, types),
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  3839
                            sym.asMemberOf(site, types),
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  3840
                            _entry.getValue());
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  3841
                    details = details.prepend(detailDiag);
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  3842
                }
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  3843
                //typically members are visited in reverse order (see Scope)
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  3844
                //so we need to reverse the candidate list so that candidates
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  3845
                //conform to source order
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  3846
                return details;
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  3847
            }
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3848
    }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3849
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3850
    /**
28331
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  3851
     * DiamondError error class indicating that a constructor symbol is not applicable
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  3852
     * given an actual arguments/type argument list using diamond inference.
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  3853
     */
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  3854
    class DiamondError extends InapplicableSymbolError {
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  3855
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  3856
        Symbol sym;
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  3857
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  3858
        public DiamondError(Symbol sym, MethodResolutionContext context) {
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  3859
            super(sym.kind, "diamondError", context);
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  3860
            this.sym = sym;
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  3861
        }
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  3862
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  3863
        JCDiagnostic getDetails() {
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  3864
            return (sym.kind == WRONG_MTH) ?
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  3865
                    ((InapplicableSymbolError)sym.baseSymbol()).errCandidate().snd :
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  3866
                    null;
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  3867
        }
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  3868
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  3869
        @Override
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  3870
        JCDiagnostic getDiagnostic(DiagnosticType dkind, DiagnosticPosition pos,
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  3871
                Symbol location, Type site, Name name, List<Type> argtypes, List<Type> typeargtypes) {
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  3872
            JCDiagnostic details = getDetails();
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  3873
            if (details != null && compactMethodDiags) {
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  3874
                JCDiagnostic simpleDiag =
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  3875
                        MethodResolutionDiagHelper.rewrite(diags, pos, log.currentSource(), dkind, details);
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  3876
                if (simpleDiag != null) {
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  3877
                    return simpleDiag;
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  3878
                }
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  3879
            }
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  3880
            String key = details == null ?
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  3881
                "cant.apply.diamond" :
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  3882
                "cant.apply.diamond.1";
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  3883
            return diags.create(dkind, log.currentSource(), pos, key,
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  3884
                    diags.fragment("diamond", site.tsym), details);
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  3885
        }
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  3886
    }
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  3887
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  3888
    /**
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3889
     * An InvalidSymbolError error class indicating that a symbol is not
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3890
     * accessible from a given site
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3891
     */
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3892
    class AccessError extends InvalidSymbolError {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3893
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3894
        private Env<AttrContext> env;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3895
        private Type site;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3896
06bc494ca11e Initial load
duke
parents:
diff changeset
  3897
        AccessError(Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3898
            this(null, null, sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3899
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3900
06bc494ca11e Initial load
duke
parents:
diff changeset
  3901
        AccessError(Env<AttrContext> env, Type site, Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3902
            super(HIDDEN, sym, "access error");
06bc494ca11e Initial load
duke
parents:
diff changeset
  3903
            this.env = env;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3904
            this.site = site;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3905
            if (debugResolve)
06bc494ca11e Initial load
duke
parents:
diff changeset
  3906
                log.error("proc.messager", sym + " @ " + site + " is inaccessible.");
06bc494ca11e Initial load
duke
parents:
diff changeset
  3907
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3908
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3909
        @Override
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3910
        public boolean exists() {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3911
            return false;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3912
        }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3913
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3914
        @Override
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3915
        JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3916
                DiagnosticPosition pos,
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  3917
                Symbol location,
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3918
                Type site,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3919
                Name name,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3920
                List<Type> argtypes,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3921
                List<Type> typeargtypes) {
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14062
diff changeset
  3922
            if (sym.owner.type.hasTag(ERROR))
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3923
                return null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3924
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3925
            if (sym.name == names.init && sym.owner != site.tsym) {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3926
                return new SymbolNotFoundError(ABSENT_MTH).getDiagnostic(dkind,
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  3927
                        pos, location, site, name, argtypes, typeargtypes);
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3928
            }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3929
            else if ((sym.flags() & PUBLIC) != 0
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3930
                || (env != null && this.site != null
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3931
                    && !isAccessible(env, this.site))) {
6151
dd513881e71d 6957438: improve code for generating warning messages containing option names
jjg
parents: 5857
diff changeset
  3932
                return diags.create(dkind, log.currentSource(),
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3933
                        pos, "not.def.access.class.intf.cant.access",
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3934
                    sym, sym.location());
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3935
            }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3936
            else if ((sym.flags() & (PRIVATE | PROTECTED)) != 0) {
6151
dd513881e71d 6957438: improve code for generating warning messages containing option names
jjg
parents: 5857
diff changeset
  3937
                return diags.create(dkind, log.currentSource(),
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3938
                        pos, "report.access", sym,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3939
                        asFlagSet(sym.flags() & (PRIVATE | PROTECTED)),
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3940
                        sym.location());
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3941
            }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3942
            else {
6151
dd513881e71d 6957438: improve code for generating warning messages containing option names
jjg
parents: 5857
diff changeset
  3943
                return diags.create(dkind, log.currentSource(),
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3944
                        pos, "not.def.public.cant.access", sym, sym.location());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3945
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3946
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3947
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3948
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3949
    /**
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3950
     * InvalidSymbolError error class indicating that an instance member
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3951
     * has erroneously been accessed from a static context.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3952
     */
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3953
    class StaticError extends InvalidSymbolError {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3954
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3955
        StaticError(Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3956
            super(STATICERR, sym, "static error");
06bc494ca11e Initial load
duke
parents:
diff changeset
  3957
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3958
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3959
        @Override
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3960
        JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3961
                DiagnosticPosition pos,
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  3962
                Symbol location,
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3963
                Type site,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3964
                Name name,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3965
                List<Type> argtypes,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3966
                List<Type> typeargtypes) {
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14062
diff changeset
  3967
            Symbol errSym = ((sym.kind == TYP && sym.type.hasTag(CLASS))
939
38e24969c7e9 6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents: 735
diff changeset
  3968
                ? types.erasure(sym.type).tsym
38e24969c7e9 6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents: 735
diff changeset
  3969
                : sym);
6151
dd513881e71d 6957438: improve code for generating warning messages containing option names
jjg
parents: 5857
diff changeset
  3970
            return diags.create(dkind, log.currentSource(), pos,
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3971
                    "non-static.cant.be.ref", kindName(sym), errSym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3972
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3973
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3974
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3975
    /**
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3976
     * InvalidSymbolError error class indicating that a pair of symbols
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3977
     * (either methods, constructors or operands) are ambiguous
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3978
     * given an actual arguments/type argument list.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3979
     */
15038
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  3980
    class AmbiguityError extends ResolveError {
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3981
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3982
        /** The other maximally specific symbol */
15038
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  3983
        List<Symbol> ambiguousSyms = List.nil();
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  3984
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  3985
        @Override
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  3986
        public boolean exists() {
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  3987
            return true;
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  3988
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3989
06bc494ca11e Initial load
duke
parents:
diff changeset
  3990
        AmbiguityError(Symbol sym1, Symbol sym2) {
15038
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  3991
            super(AMBIGUOUS, "ambiguity error");
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  3992
            ambiguousSyms = flatten(sym2).appendList(flatten(sym1));
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  3993
        }
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  3994
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  3995
        private List<Symbol> flatten(Symbol sym) {
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  3996
            if (sym.kind == AMBIGUOUS) {
22161
d79fb23b5dca 8029569: internal javac cast exception when resolving varargs ambiguity
vromero
parents: 21718
diff changeset
  3997
                return ((AmbiguityError)sym.baseSymbol()).ambiguousSyms;
15038
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  3998
            } else {
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  3999
                return List.of(sym);
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4000
            }
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4001
        }
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4002
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4003
        AmbiguityError addAmbiguousSymbol(Symbol s) {
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4004
            ambiguousSyms = ambiguousSyms.prepend(s);
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4005
            return this;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4006
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4007
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4008
        @Override
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4009
        JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4010
                DiagnosticPosition pos,
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  4011
                Symbol location,
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4012
                Type site,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4013
                Name name,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4014
                List<Type> argtypes,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4015
                List<Type> typeargtypes) {
15038
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4016
            List<Symbol> diagSyms = ambiguousSyms.reverse();
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4017
            Symbol s1 = diagSyms.head;
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4018
            Symbol s2 = diagSyms.tail.head;
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4019
            Name sname = s1.name;
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4020
            if (sname == names.init) sname = s1.owner.name;
6151
dd513881e71d 6957438: improve code for generating warning messages containing option names
jjg
parents: 5857
diff changeset
  4021
            return diags.create(dkind, log.currentSource(),
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4022
                      pos, "ref.ambiguous", sname,
15038
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4023
                      kindName(s1),
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4024
                      s1,
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4025
                      s1.location(site, types),
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4026
                      kindName(s2),
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4027
                      s2,
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4028
                      s2.location(site, types));
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4029
        }
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4030
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4031
        /**
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4032
         * If multiple applicable methods are found during overload and none of them
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4033
         * is more specific than the others, attempt to merge their signatures.
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4034
         */
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4035
        Symbol mergeAbstracts(Type site) {
18668
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  4036
            List<Symbol> ambiguousInOrder = ambiguousSyms.reverse();
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  4037
            for (Symbol s : ambiguousInOrder) {
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  4038
                Type mt = types.memberType(site, s);
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  4039
                boolean found = true;
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  4040
                List<Type> allThrown = mt.getThrownTypes();
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  4041
                for (Symbol s2 : ambiguousInOrder) {
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  4042
                    Type mt2 = types.memberType(site, s2);
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  4043
                    if ((s2.flags() & ABSTRACT) == 0 ||
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  4044
                        !types.overrideEquivalent(mt, mt2) ||
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  4045
                        !types.isSameTypes(s.erasure(types).getParameterTypes(),
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  4046
                                       s2.erasure(types).getParameterTypes())) {
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  4047
                        //ambiguity cannot be resolved
15038
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4048
                        return this;
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4049
                    }
18668
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  4050
                    Type mst = mostSpecificReturnType(mt, mt2);
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  4051
                    if (mst == null || mst != mt) {
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  4052
                        found = false;
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  4053
                        break;
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  4054
                    }
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  4055
                    allThrown = chk.intersect(allThrown, mt2.getThrownTypes());
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  4056
                }
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  4057
                if (found) {
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  4058
                    //all ambiguous methods were abstract and one method had
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  4059
                    //most specific return type then others
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  4060
                    return (allThrown == mt.getThrownTypes()) ?
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  4061
                            s : new MethodSymbol(
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  4062
                                s.flags(),
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  4063
                                s.name,
27842
edd6dd917166 8064803: Javac erroneously uses instantiated signatures when merging abstract most-specific methods
mcimadamore
parents: 27386
diff changeset
  4064
                                types.createMethodTypeWithThrown(s.type, allThrown),
18668
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  4065
                                s.owner);
15038
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4066
                }
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4067
            }
18668
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  4068
            return this;
15038
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4069
        }
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4070
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4071
        @Override
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4072
        protected Symbol access(Name name, TypeSymbol location) {
15362
f564dcca5525 8005964: Regression: difference in error recovery after ambiguity causes JCK test failure
mcimadamore
parents: 15360
diff changeset
  4073
            Symbol firstAmbiguity = ambiguousSyms.last();
f564dcca5525 8005964: Regression: difference in error recovery after ambiguity causes JCK test failure
mcimadamore
parents: 15360
diff changeset
  4074
            return firstAmbiguity.kind == TYP ?
f564dcca5525 8005964: Regression: difference in error recovery after ambiguity causes JCK test failure
mcimadamore
parents: 15360
diff changeset
  4075
                    types.createErrorType(name, location, firstAmbiguity.type).tsym :
f564dcca5525 8005964: Regression: difference in error recovery after ambiguity causes JCK test failure
mcimadamore
parents: 15360
diff changeset
  4076
                    firstAmbiguity;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4077
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4078
    }
1533
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  4079
16312
7fb570abcb54 8007461: Regression: bad overload resolution when inner class and outer class have method with same name
mcimadamore
parents: 16301
diff changeset
  4080
    class BadVarargsMethod extends ResolveError {
7fb570abcb54 8007461: Regression: bad overload resolution when inner class and outer class have method with same name
mcimadamore
parents: 16301
diff changeset
  4081
7fb570abcb54 8007461: Regression: bad overload resolution when inner class and outer class have method with same name
mcimadamore
parents: 16301
diff changeset
  4082
        ResolveError delegatedError;
7fb570abcb54 8007461: Regression: bad overload resolution when inner class and outer class have method with same name
mcimadamore
parents: 16301
diff changeset
  4083
7fb570abcb54 8007461: Regression: bad overload resolution when inner class and outer class have method with same name
mcimadamore
parents: 16301
diff changeset
  4084
        BadVarargsMethod(ResolveError delegatedError) {
7fb570abcb54 8007461: Regression: bad overload resolution when inner class and outer class have method with same name
mcimadamore
parents: 16301
diff changeset
  4085
            super(delegatedError.kind, "badVarargs");
7fb570abcb54 8007461: Regression: bad overload resolution when inner class and outer class have method with same name
mcimadamore
parents: 16301
diff changeset
  4086
            this.delegatedError = delegatedError;
7fb570abcb54 8007461: Regression: bad overload resolution when inner class and outer class have method with same name
mcimadamore
parents: 16301
diff changeset
  4087
        }
7fb570abcb54 8007461: Regression: bad overload resolution when inner class and outer class have method with same name
mcimadamore
parents: 16301
diff changeset
  4088
7fb570abcb54 8007461: Regression: bad overload resolution when inner class and outer class have method with same name
mcimadamore
parents: 16301
diff changeset
  4089
        @Override
16975
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  4090
        public Symbol baseSymbol() {
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  4091
            return delegatedError.baseSymbol();
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  4092
        }
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  4093
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  4094
        @Override
16312
7fb570abcb54 8007461: Regression: bad overload resolution when inner class and outer class have method with same name
mcimadamore
parents: 16301
diff changeset
  4095
        protected Symbol access(Name name, TypeSymbol location) {
7fb570abcb54 8007461: Regression: bad overload resolution when inner class and outer class have method with same name
mcimadamore
parents: 16301
diff changeset
  4096
            return delegatedError.access(name, location);
7fb570abcb54 8007461: Regression: bad overload resolution when inner class and outer class have method with same name
mcimadamore
parents: 16301
diff changeset
  4097
        }
7fb570abcb54 8007461: Regression: bad overload resolution when inner class and outer class have method with same name
mcimadamore
parents: 16301
diff changeset
  4098
7fb570abcb54 8007461: Regression: bad overload resolution when inner class and outer class have method with same name
mcimadamore
parents: 16301
diff changeset
  4099
        @Override
7fb570abcb54 8007461: Regression: bad overload resolution when inner class and outer class have method with same name
mcimadamore
parents: 16301
diff changeset
  4100
        public boolean exists() {
7fb570abcb54 8007461: Regression: bad overload resolution when inner class and outer class have method with same name
mcimadamore
parents: 16301
diff changeset
  4101
            return true;
7fb570abcb54 8007461: Regression: bad overload resolution when inner class and outer class have method with same name
mcimadamore
parents: 16301
diff changeset
  4102
        }
7fb570abcb54 8007461: Regression: bad overload resolution when inner class and outer class have method with same name
mcimadamore
parents: 16301
diff changeset
  4103
7fb570abcb54 8007461: Regression: bad overload resolution when inner class and outer class have method with same name
mcimadamore
parents: 16301
diff changeset
  4104
        @Override
7fb570abcb54 8007461: Regression: bad overload resolution when inner class and outer class have method with same name
mcimadamore
parents: 16301
diff changeset
  4105
        JCDiagnostic getDiagnostic(DiagnosticType dkind, DiagnosticPosition pos, Symbol location, Type site, Name name, List<Type> argtypes, List<Type> typeargtypes) {
7fb570abcb54 8007461: Regression: bad overload resolution when inner class and outer class have method with same name
mcimadamore
parents: 16301
diff changeset
  4106
            return delegatedError.getDiagnostic(dkind, pos, location, site, name, argtypes, typeargtypes);
7fb570abcb54 8007461: Regression: bad overload resolution when inner class and outer class have method with same name
mcimadamore
parents: 16301
diff changeset
  4107
        }
7fb570abcb54 8007461: Regression: bad overload resolution when inner class and outer class have method with same name
mcimadamore
parents: 16301
diff changeset
  4108
    }
7fb570abcb54 8007461: Regression: bad overload resolution when inner class and outer class have method with same name
mcimadamore
parents: 16301
diff changeset
  4109
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4110
    /**
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4111
     * Helper class for method resolution diagnostic simplification.
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4112
     * Certain resolution diagnostic are rewritten as simpler diagnostic
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4113
     * where the enclosing resolution diagnostic (i.e. 'inapplicable method')
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4114
     * is stripped away, as it doesn't carry additional info. The logic
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4115
     * for matching a given diagnostic is given in terms of a template
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4116
     * hierarchy: a diagnostic template can be specified programmatically,
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4117
     * so that only certain diagnostics are matched. Each templete is then
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4118
     * associated with a rewriter object that carries out the task of rewtiting
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4119
     * the diagnostic to a simpler one.
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4120
     */
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4121
    static class MethodResolutionDiagHelper {
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4122
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4123
        /**
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4124
         * A diagnostic rewriter transforms a method resolution diagnostic
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4125
         * into a simpler one
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4126
         */
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4127
        interface DiagnosticRewriter {
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4128
            JCDiagnostic rewriteDiagnostic(JCDiagnostic.Factory diags,
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4129
                    DiagnosticPosition preferedPos, DiagnosticSource preferredSource,
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4130
                    DiagnosticType preferredKind, JCDiagnostic d);
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4131
        }
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4132
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4133
        /**
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4134
         * A diagnostic template is made up of two ingredients: (i) a regular
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4135
         * expression for matching a diagnostic key and (ii) a list of sub-templates
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4136
         * for matching diagnostic arguments.
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4137
         */
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4138
        static class Template {
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4139
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4140
            /** regex used to match diag key */
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4141
            String regex;
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4142
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4143
            /** templates used to match diagnostic args */
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4144
            Template[] subTemplates;
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4145
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4146
            Template(String key, Template... subTemplates) {
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4147
                this.regex = key;
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4148
                this.subTemplates = subTemplates;
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4149
            }
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4150
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4151
            /**
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4152
             * Returns true if the regex matches the diagnostic key and if
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4153
             * all diagnostic arguments are matches by corresponding sub-templates.
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4154
             */
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4155
            boolean matches(Object o) {
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4156
                JCDiagnostic d = (JCDiagnostic)o;
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4157
                Object[] args = d.getArgs();
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4158
                if (!d.getCode().matches(regex) ||
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4159
                        subTemplates.length != d.getArgs().length) {
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4160
                    return false;
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4161
                }
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4162
                for (int i = 0; i < args.length ; i++) {
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4163
                    if (!subTemplates[i].matches(args[i])) {
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4164
                        return false;
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4165
                    }
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4166
                }
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4167
                return true;
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4168
            }
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4169
        }
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4170
28331
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4171
        /**
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4172
         * Common rewriter for all argument mismatch simplifications.
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4173
         */
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4174
        static class ArgMismatchRewriter implements DiagnosticRewriter {
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4175
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4176
            /** the index of the subdiagnostic to be used as primary. */
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4177
            int causeIndex;
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4178
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4179
            public ArgMismatchRewriter(int causeIndex) {
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4180
                this.causeIndex = causeIndex;
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4181
            }
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4182
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4183
            @Override
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4184
            public JCDiagnostic rewriteDiagnostic(JCDiagnostic.Factory diags,
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4185
                    DiagnosticPosition preferedPos, DiagnosticSource preferredSource,
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4186
                    DiagnosticType preferredKind, JCDiagnostic d) {
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4187
                JCDiagnostic cause = (JCDiagnostic)d.getArgs()[causeIndex];
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4188
                return diags.create(preferredKind, preferredSource, d.getDiagnosticPosition(),
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4189
                        "prob.found.req", cause);
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4190
            }
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4191
        }
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4192
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4193
        /** a dummy template that match any diagnostic argument */
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4194
        static final Template skip = new Template("") {
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4195
            @Override
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4196
            boolean matches(Object d) {
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4197
                return true;
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4198
            }
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4199
        };
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4200
28331
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4201
        /** template for matching inference-free arguments mismatch failures */
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4202
        static final Template argMismatchTemplate = new Template(MethodCheckDiag.ARG_MISMATCH.regex(), skip);
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4203
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4204
        /** template for matching inference related arguments mismatch failures */
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4205
        static final Template inferArgMismatchTemplate = new Template(MethodCheckDiag.ARG_MISMATCH.regex(), skip, skip) {
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4206
            @Override
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4207
            boolean matches(Object o) {
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4208
                if (!super.matches(o)) {
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4209
                    return false;
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4210
                }
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4211
                JCDiagnostic d = (JCDiagnostic)o;
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4212
                @SuppressWarnings("unchecked")
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4213
                List<Type> tvars = (List<Type>)d.getArgs()[0];
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4214
                return !containsAny(d, tvars);
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4215
            }
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4216
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4217
            BiPredicate<Object, List<Type>> containsPredicate = (o, ts) -> {
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4218
                if (o instanceof Type) {
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4219
                    return ((Type)o).containsAny(ts);
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4220
                } else if (o instanceof JCDiagnostic) {
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4221
                    return containsAny((JCDiagnostic)o, ts);
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4222
                } else {
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4223
                    return false;
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4224
                }
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4225
            };
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4226
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4227
            boolean containsAny(JCDiagnostic d, List<Type> ts) {
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4228
                return Stream.of(d.getArgs())
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4229
                        .anyMatch(o -> containsPredicate.test(o, ts));
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4230
            }
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4231
        };
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4232
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4233
        /** rewriter map used for method resolution simplification */
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22161
diff changeset
  4234
        static final Map<Template, DiagnosticRewriter> rewriters = new LinkedHashMap<>();
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4235
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4236
        static {
28331
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4237
            rewriters.put(argMismatchTemplate, new ArgMismatchRewriter(0));
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4238
            rewriters.put(inferArgMismatchTemplate, new ArgMismatchRewriter(1));
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4239
        }
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4240
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4241
        /**
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4242
         * Main entry point for diagnostic rewriting - given a diagnostic, see if any templates matches it,
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4243
         * and rewrite it accordingly.
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4244
         */
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4245
        static JCDiagnostic rewrite(JCDiagnostic.Factory diags, DiagnosticPosition pos, DiagnosticSource source,
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4246
                                    DiagnosticType dkind, JCDiagnostic d) {
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4247
            for (Map.Entry<Template, DiagnosticRewriter> _entry : rewriters.entrySet()) {
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4248
                if (_entry.getKey().matches(d)) {
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4249
                    JCDiagnostic simpleDiag =
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4250
                            _entry.getValue().rewriteDiagnostic(diags, pos, source, dkind, d);
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4251
                    simpleDiag.setFlag(DiagnosticFlag.COMPRESSED);
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4252
                    return simpleDiag;
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4253
                }
28331
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4254
            }
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4255
            return null;
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4256
        }
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4257
    }
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4258
1533
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  4259
    enum MethodResolutionPhase {
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  4260
        BASIC(false, false),
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  4261
        BOX(true, false),
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  4262
        VARARITY(true, true) {
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  4263
            @Override
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  4264
            public Symbol mergeResults(Symbol bestSoFar, Symbol sym) {
26533
8fba085fbdbf 8055514: Wrong, confusing error when non-static varargs referenced in static context
mcimadamore
parents: 26266
diff changeset
  4265
                //Check invariants (see {@code LookupHelper.shouldStop})
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  4266
                Assert.check(bestSoFar.kind.isOverloadError() && bestSoFar.kind != AMBIGUOUS);
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  4267
                if (!sym.kind.isOverloadError()) {
26533
8fba085fbdbf 8055514: Wrong, confusing error when non-static varargs referenced in static context
mcimadamore
parents: 26266
diff changeset
  4268
                    //varargs resolution successful
8fba085fbdbf 8055514: Wrong, confusing error when non-static varargs referenced in static context
mcimadamore
parents: 26266
diff changeset
  4269
                    return sym;
8fba085fbdbf 8055514: Wrong, confusing error when non-static varargs referenced in static context
mcimadamore
parents: 26266
diff changeset
  4270
                } else {
8fba085fbdbf 8055514: Wrong, confusing error when non-static varargs referenced in static context
mcimadamore
parents: 26266
diff changeset
  4271
                    //pick best error
8fba085fbdbf 8055514: Wrong, confusing error when non-static varargs referenced in static context
mcimadamore
parents: 26266
diff changeset
  4272
                    switch (bestSoFar.kind) {
8fba085fbdbf 8055514: Wrong, confusing error when non-static varargs referenced in static context
mcimadamore
parents: 26266
diff changeset
  4273
                        case WRONG_MTH:
8fba085fbdbf 8055514: Wrong, confusing error when non-static varargs referenced in static context
mcimadamore
parents: 26266
diff changeset
  4274
                        case WRONG_MTHS:
8fba085fbdbf 8055514: Wrong, confusing error when non-static varargs referenced in static context
mcimadamore
parents: 26266
diff changeset
  4275
                            //Override previous errors if they were caused by argument mismatch.
8fba085fbdbf 8055514: Wrong, confusing error when non-static varargs referenced in static context
mcimadamore
parents: 26266
diff changeset
  4276
                            //This generally means preferring current symbols - but we need to pay
8fba085fbdbf 8055514: Wrong, confusing error when non-static varargs referenced in static context
mcimadamore
parents: 26266
diff changeset
  4277
                            //attention to the fact that the varargs lookup returns 'less' candidates
8fba085fbdbf 8055514: Wrong, confusing error when non-static varargs referenced in static context
mcimadamore
parents: 26266
diff changeset
  4278
                            //than the previous rounds, and adjust that accordingly.
8fba085fbdbf 8055514: Wrong, confusing error when non-static varargs referenced in static context
mcimadamore
parents: 26266
diff changeset
  4279
                            switch (sym.kind) {
8fba085fbdbf 8055514: Wrong, confusing error when non-static varargs referenced in static context
mcimadamore
parents: 26266
diff changeset
  4280
                                case WRONG_MTH:
8fba085fbdbf 8055514: Wrong, confusing error when non-static varargs referenced in static context
mcimadamore
parents: 26266
diff changeset
  4281
                                    //if the previous round matched more than one method, return that
8fba085fbdbf 8055514: Wrong, confusing error when non-static varargs referenced in static context
mcimadamore
parents: 26266
diff changeset
  4282
                                    //result instead
8fba085fbdbf 8055514: Wrong, confusing error when non-static varargs referenced in static context
mcimadamore
parents: 26266
diff changeset
  4283
                                    return bestSoFar.kind == WRONG_MTHS ?
8fba085fbdbf 8055514: Wrong, confusing error when non-static varargs referenced in static context
mcimadamore
parents: 26266
diff changeset
  4284
                                            bestSoFar : sym;
8fba085fbdbf 8055514: Wrong, confusing error when non-static varargs referenced in static context
mcimadamore
parents: 26266
diff changeset
  4285
                                case ABSENT_MTH:
8fba085fbdbf 8055514: Wrong, confusing error when non-static varargs referenced in static context
mcimadamore
parents: 26266
diff changeset
  4286
                                    //do not override erroneous symbol if the arity lookup did not
8fba085fbdbf 8055514: Wrong, confusing error when non-static varargs referenced in static context
mcimadamore
parents: 26266
diff changeset
  4287
                                    //match any method
8fba085fbdbf 8055514: Wrong, confusing error when non-static varargs referenced in static context
mcimadamore
parents: 26266
diff changeset
  4288
                                    return bestSoFar;
8fba085fbdbf 8055514: Wrong, confusing error when non-static varargs referenced in static context
mcimadamore
parents: 26266
diff changeset
  4289
                                case WRONG_MTHS:
8fba085fbdbf 8055514: Wrong, confusing error when non-static varargs referenced in static context
mcimadamore
parents: 26266
diff changeset
  4290
                                default:
8fba085fbdbf 8055514: Wrong, confusing error when non-static varargs referenced in static context
mcimadamore
parents: 26266
diff changeset
  4291
                                    //safe to override
8fba085fbdbf 8055514: Wrong, confusing error when non-static varargs referenced in static context
mcimadamore
parents: 26266
diff changeset
  4292
                                    return sym;
8fba085fbdbf 8055514: Wrong, confusing error when non-static varargs referenced in static context
mcimadamore
parents: 26266
diff changeset
  4293
                            }
8fba085fbdbf 8055514: Wrong, confusing error when non-static varargs referenced in static context
mcimadamore
parents: 26266
diff changeset
  4294
                        default:
8fba085fbdbf 8055514: Wrong, confusing error when non-static varargs referenced in static context
mcimadamore
parents: 26266
diff changeset
  4295
                            //otherwise, return first error
8fba085fbdbf 8055514: Wrong, confusing error when non-static varargs referenced in static context
mcimadamore
parents: 26266
diff changeset
  4296
                            return bestSoFar;
8fba085fbdbf 8055514: Wrong, confusing error when non-static varargs referenced in static context
mcimadamore
parents: 26266
diff changeset
  4297
                    }
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  4298
                }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  4299
            }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  4300
        };
1533
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  4301
14801
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents: 14724
diff changeset
  4302
        final boolean isBoxingRequired;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents: 14724
diff changeset
  4303
        final boolean isVarargsRequired;
1533
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  4304
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  4305
        MethodResolutionPhase(boolean isBoxingRequired, boolean isVarargsRequired) {
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  4306
           this.isBoxingRequired = isBoxingRequired;
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  4307
           this.isVarargsRequired = isVarargsRequired;
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  4308
        }
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  4309
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  4310
        public boolean isBoxingRequired() {
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  4311
            return isBoxingRequired;
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  4312
        }
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  4313
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  4314
        public boolean isVarargsRequired() {
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  4315
            return isVarargsRequired;
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  4316
        }
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  4317
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  4318
        public Symbol mergeResults(Symbol prev, Symbol sym) {
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  4319
            return sym;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  4320
        }
1533
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  4321
    }
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  4322
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  4323
    final List<MethodResolutionPhase> methodResolutionSteps = List.of(BASIC, BOX, VARARITY);
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  4324
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4325
    /**
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4326
     * A resolution context is used to keep track of intermediate results of
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4327
     * overload resolution, such as list of method that are not applicable
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4328
     * (used to generate more precise diagnostics) and so on. Resolution contexts
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4329
     * can be nested - this means that when each overload resolution routine should
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4330
     * work within the resolution context it created.
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4331
     */
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4332
    class MethodResolutionContext {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4333
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4334
        private List<Candidate> candidates = List.nil();
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4335
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  4336
        MethodResolutionPhase step = null;
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4337
16805
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  4338
        MethodCheck methodCheck = resolveMethodCheck;
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  4339
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4340
        private boolean internalResolution = false;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  4341
        private DeferredAttr.AttrMode attrMode = DeferredAttr.AttrMode.SPECULATIVE;
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
  4342
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4343
        void addInapplicableCandidate(Symbol sym, JCDiagnostic details) {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4344
            Candidate c = new Candidate(currentResolutionContext.step, sym, details, null);
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  4345
            candidates = candidates.append(c);
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4346
        }
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4347
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4348
        void addApplicableCandidate(Symbol sym, Type mtype) {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4349
            Candidate c = new Candidate(currentResolutionContext.step, sym, null, mtype);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4350
            candidates = candidates.append(c);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4351
        }
10816
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
  4352
15706
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
  4353
        DeferredAttrContext deferredAttrContext(Symbol sym, InferenceContext inferenceContext, ResultInfo pendingResult, Warner warn) {
23132
00a059740d87 8035891: javac, rename method asFree() in InferenceContext to asUndetVar() which reflects better it's purpose
vromero
parents: 23105
diff changeset
  4354
            DeferredAttrContext parent = (pendingResult == null)
00a059740d87 8035891: javac, rename method asFree() in InferenceContext to asUndetVar() which reflects better it's purpose
vromero
parents: 23105
diff changeset
  4355
                ? deferredAttr.emptyDeferredAttrContext
00a059740d87 8035891: javac, rename method asFree() in InferenceContext to asUndetVar() which reflects better it's purpose
vromero
parents: 23105
diff changeset
  4356
                : pendingResult.checkContext.deferredAttrContext();
00a059740d87 8035891: javac, rename method asFree() in InferenceContext to asUndetVar() which reflects better it's purpose
vromero
parents: 23105
diff changeset
  4357
            return deferredAttr.new DeferredAttrContext(attrMode, sym, step,
00a059740d87 8035891: javac, rename method asFree() in InferenceContext to asUndetVar() which reflects better it's purpose
vromero
parents: 23105
diff changeset
  4358
                    inferenceContext, parent, warn);
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
  4359
        }
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
  4360
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4361
        /**
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4362
         * This class represents an overload resolution candidate. There are two
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4363
         * kinds of candidates: applicable methods and inapplicable methods;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4364
         * applicable methods have a pointer to the instantiated method type,
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4365
         * while inapplicable candidates contain further details about the
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4366
         * reason why the method has been considered inapplicable.
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4367
         */
16301
b6fd735ea78e 6563143: javac should issue a warning for overriding equals without hashCode
vromero
parents: 16294
diff changeset
  4368
        @SuppressWarnings("overrides")
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4369
        class Candidate {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4370
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4371
            final MethodResolutionPhase step;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4372
            final Symbol sym;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4373
            final JCDiagnostic details;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4374
            final Type mtype;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4375
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4376
            private Candidate(MethodResolutionPhase step, Symbol sym, JCDiagnostic details, Type mtype) {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4377
                this.step = step;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4378
                this.sym = sym;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4379
                this.details = details;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4380
                this.mtype = mtype;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4381
            }
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4382
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4383
            @Override
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4384
            public boolean equals(Object o) {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4385
                if (o instanceof Candidate) {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4386
                    Symbol s1 = this.sym;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4387
                    Symbol s2 = ((Candidate)o).sym;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4388
                    if  ((s1 != s2 &&
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  4389
                            (s1.overrides(s2, s1.owner.type.tsym, types, false) ||
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  4390
                            (s2.overrides(s1, s2.owner.type.tsym, types, false)))) ||
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  4391
                            ((s1.isConstructor() || s2.isConstructor()) && s1.owner != s2.owner))
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4392
                        return true;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4393
                }
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4394
                return false;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4395
            }
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4396
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4397
            boolean isApplicable() {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4398
                return mtype != null;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4399
            }
1533
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  4400
        }
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  4401
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  4402
        DeferredAttr.AttrMode attrMode() {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  4403
            return attrMode;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  4404
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  4405
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  4406
        boolean internal() {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  4407
            return internalResolution;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  4408
        }
1533
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  4409
    }
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4410
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4411
    MethodResolutionContext currentResolutionContext = null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4412
}