src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java
author jlahoda
Mon, 21 Oct 2019 15:38:26 +0200
changeset 58713 ad69fd32778e
parent 58350 e23e560afbcb
permissions -rw-r--r--
8226585: Improve javac messages for using a preview API Summary: Avoiding deprecation for removal for APIs associated with preview features, the features are marked with an annotation, and errors/warnings are produced for them based on the annotation. Reviewed-by: erikj, mcimadamore, alanb Contributed-by: joe.darcy@oracle.com, jan.lahoda@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
58350
e23e560afbcb 8231414: Mark various instance fields of javac internal types as transient
darcy
parents: 57963
diff changeset
     2
 * Copyright (c) 1999, 2019, 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
10816
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
    28
import com.sun.tools.javac.api.Formattable.LocalizedString;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import com.sun.tools.javac.code.*;
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
    30
import com.sun.tools.javac.code.Scope.WriteableScope;
48054
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47942
diff changeset
    31
import com.sun.tools.javac.code.Source.Feature;
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;
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
    39
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
    40
import com.sun.tools.javac.comp.Resolve.MethodResolutionDiagHelper.Template;
28456
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
    41
import com.sun.tools.javac.comp.Resolve.ReferenceLookupResult.StaticKind;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
import com.sun.tools.javac.jvm.*;
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
    43
import com.sun.tools.javac.main.Option;
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44504
diff changeset
    44
import com.sun.tools.javac.resources.CompilerProperties.Errors;
43272
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
    45
import com.sun.tools.javac.resources.CompilerProperties.Fragments;
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 51047
diff changeset
    46
import com.sun.tools.javac.resources.CompilerProperties.Warnings;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
import com.sun.tools.javac.tree.*;
10816
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
    48
import com.sun.tools.javac.tree.JCTree.*;
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
    49
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
    50
import com.sun.tools.javac.tree.JCTree.JCPolyExpression.*;
10816
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
    51
import com.sun.tools.javac.util.*;
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
    52
import com.sun.tools.javac.util.DefinedBy.Api;
10816
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
    53
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
    54
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
    55
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticType;
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 51047
diff changeset
    56
import com.sun.tools.javac.util.JCDiagnostic.Warning;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
10816
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
    58
import java.util.Arrays;
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
    59
import java.util.Collection;
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
    60
import java.util.EnumSet;
43138
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
    61
import java.util.HashSet;
14052
8b839ae9074b 8000233: Fix issues in recent push
mcimadamore
parents: 14051
diff changeset
    62
import java.util.Iterator;
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
    63
import java.util.LinkedHashMap;
10816
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
    64
import java.util.Map;
43138
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
    65
import java.util.Set;
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
    66
import java.util.function.BiFunction;
28331
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
    67
import java.util.function.BiPredicate;
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 51047
diff changeset
    68
import java.util.function.Consumer;
43272
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
    69
import java.util.function.Function;
43138
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
    70
import java.util.function.Predicate;
28331
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
    71
import java.util.stream.Stream;
10816
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
    72
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
    73
import javax.lang.model.element.ElementVisitor;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
import static com.sun.tools.javac.code.Flags.*;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10816
diff changeset
    76
import static com.sun.tools.javac.code.Flags.BLOCK;
28456
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
    77
import static com.sun.tools.javac.code.Flags.STATIC;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
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
    79
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
    80
import static com.sun.tools.javac.code.TypeTag.*;
10816
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
    81
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
    82
import static com.sun.tools.javac.tree.JCTree.Tag.*;
43272
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
    83
import static com.sun.tools.javac.util.Iterators.createCompoundIterator;
1533
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
    84
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
/** Helper class for name resolution, used mostly by the attribution phase.
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5846
diff changeset
    87
 *  <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
    88
 *  If you write code that depends on this, you do so at your own risk.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
 *  This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
 *  deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
public class Resolve {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22161
diff changeset
    93
    protected static final Context.Key<Resolve> resolveKey = new Context.Key<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 1257
diff changeset
    95
    Names names;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
    Log log;
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
    Symtab syms;
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
    98
    Attr attr;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
    99
    DeferredAttr deferredAttr;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
    Check chk;
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
    Infer infer;
24604
7f68545b5128 8041422: Split javac ClassReader into ClassReader+ClassFinder
jjg
parents: 24404
diff changeset
   102
    ClassFinder finder;
43138
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
   103
    ModuleFinder moduleFinder;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
    Types types;
1040
c0f5acfd9d15 6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents: 939
diff changeset
   105
    JCDiagnostic.Factory diags;
24404
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
   106
    public final boolean allowFunctionalInterfaceMostSpecific;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36278
diff changeset
   107
    public final boolean allowModules;
25435
928866794984 8049075: javac, wildcards and generic vararg method invocation not accepted
vromero
parents: 25279
diff changeset
   108
    public final boolean checkVarargsAccessAfterResolution;
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
   109
    private final boolean compactMethodDiags;
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   110
    private final boolean allowLocalVariableTypeInference;
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 51047
diff changeset
   111
    private final boolean allowYieldStatement;
10816
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   112
    final EnumSet<VerboseResolutionMode> verboseResolutionMode;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
   114
    WriteableScope polymorphicSignatureScope;
6592
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6356
diff changeset
   115
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   116
    protected Resolve(Context context) {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   117
        context.put(resolveKey, this);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   118
        syms = Symtab.instance(context);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   119
28456
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
   120
        varNotFound = new SymbolNotFoundError(ABSENT_VAR);
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
   121
        methodNotFound = new SymbolNotFoundError(ABSENT_MTH);
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
   122
        typeNotFound = new SymbolNotFoundError(ABSENT_TYP);
47297
a0116bcc65b7 8188144: regression in method reference type-checking
mcimadamore
parents: 47268
diff changeset
   123
        referenceNotFound = ReferenceLookupResult.error(methodNotFound);
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   124
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   125
        names = Names.instance(context);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   126
        log = Log.instance(context);
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   127
        attr = Attr.instance(context);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
   128
        deferredAttr = DeferredAttr.instance(context);
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   129
        chk = Check.instance(context);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   130
        infer = Infer.instance(context);
24604
7f68545b5128 8041422: Split javac ClassReader into ClassReader+ClassFinder
jjg
parents: 24404
diff changeset
   131
        finder = ClassFinder.instance(context);
43138
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
   132
        moduleFinder = ModuleFinder.instance(context);
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   133
        types = Types.instance(context);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   134
        diags = JCDiagnostic.Factory.instance(context);
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 51047
diff changeset
   135
        Preview preview = Preview.instance(context);
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   136
        Source source = Source.instance(context);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   137
        Options options = Options.instance(context);
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
   138
        compactMethodDiags = options.isSet(Option.XDIAGS, "compact") ||
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
   139
                options.isUnset(Option.XDIAGS) && options.isUnset("rawDiagnostics");
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   140
        verboseResolutionMode = VerboseResolutionMode.getVerboseResolutionMode(options);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   141
        Target target = Target.instance(context);
48054
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47942
diff changeset
   142
        allowFunctionalInterfaceMostSpecific = Feature.FUNCTIONAL_INTERFACE_MOST_SPECIFIC.allowedInSource(source);
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47942
diff changeset
   143
        allowLocalVariableTypeInference = Feature.LOCAL_VARIABLE_TYPE_INFERENCE.allowedInSource(source);
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 51047
diff changeset
   144
        allowYieldStatement = (!preview.isPreview(Feature.SWITCH_EXPRESSION) || preview.isEnabled()) &&
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 51047
diff changeset
   145
                Feature.SWITCH_EXPRESSION.allowedInSource(source);
25435
928866794984 8049075: javac, wildcards and generic vararg method invocation not accepted
vromero
parents: 25279
diff changeset
   146
        checkVarargsAccessAfterResolution =
48054
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47942
diff changeset
   147
                Feature.POST_APPLICABILITY_VARARGS_ACCESS_CHECK.allowedInSource(source);
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
   148
        polymorphicSignatureScope = WriteableScope.create(syms.noSymbol);
48054
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47942
diff changeset
   149
        allowModules = Feature.MODULES.allowedInSource(source);
12076
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
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   152
    /** error symbols, which are returned when resolution fails
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   153
     */
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   154
    private final SymbolNotFoundError varNotFound;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   155
    private final SymbolNotFoundError methodNotFound;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   156
    private final SymbolNotFoundError typeNotFound;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   157
28456
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
   158
    /** empty reference lookup result */
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
   159
    private final ReferenceLookupResult referenceNotFound;
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
   160
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   161
    public static Resolve instance(Context context) {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   162
        Resolve instance = context.get(resolveKey);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   163
        if (instance == null)
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   164
            instance = new Resolve(context);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   165
        return instance;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   166
    }
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   167
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
   168
    private static Symbol bestOf(Symbol s1,
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
   169
                                 Symbol s2) {
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
   170
        return s1.kind.betterThan(s2.kind) ? s1 : s2;
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
   171
    }
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
   172
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   173
    // <editor-fold defaultstate="collapsed" desc="Verbose resolution diagnostics support">
10816
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   174
    enum VerboseResolutionMode {
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   175
        SUCCESS("success"),
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   176
        FAILURE("failure"),
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   177
        APPLICABLE("applicable"),
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   178
        INAPPLICABLE("inapplicable"),
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   179
        DEFERRED_INST("deferred-inference"),
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   180
        PREDEF("predef"),
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   181
        OBJECT_INIT("object-init"),
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   182
        INTERNAL("internal");
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   183
14801
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents: 14724
diff changeset
   184
        final String opt;
10816
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   185
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   186
        private VerboseResolutionMode(String opt) {
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   187
            this.opt = opt;
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   188
        }
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   189
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   190
        static EnumSet<VerboseResolutionMode> getVerboseResolutionMode(Options opts) {
39599
3c7da4996d8c 8153407: javac, fold debug options
vromero
parents: 39092
diff changeset
   191
            String s = opts.get("debug.verboseResolution");
10816
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   192
            EnumSet<VerboseResolutionMode> res = EnumSet.noneOf(VerboseResolutionMode.class);
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   193
            if (s == null) return res;
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   194
            if (s.contains("all")) {
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   195
                res = EnumSet.allOf(VerboseResolutionMode.class);
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
            Collection<String> args = Arrays.asList(s.split(","));
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   198
            for (VerboseResolutionMode mode : values()) {
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   199
                if (args.contains(mode.opt)) {
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   200
                    res.add(mode);
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   201
                } else if (args.contains("-" + mode.opt)) {
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   202
                    res.remove(mode);
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   203
                }
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   204
            }
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   205
            return res;
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   206
        }
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   207
    }
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   208
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   209
    void reportVerboseResolutionDiagnostic(DiagnosticPosition dpos, Name name, Type site,
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   210
            List<Type> argtypes, List<Type> typeargtypes, Symbol bestSoFar) {
29774
9d438163db79 8075184: javac is mistakenly considering an missing enclosing instance error as an overload error
vromero
parents: 29554
diff changeset
   211
        boolean success = !bestSoFar.kind.isResolutionError();
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   212
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   213
        if (success && !verboseResolutionMode.contains(VerboseResolutionMode.SUCCESS)) {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   214
            return;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   215
        } else if (!success && !verboseResolutionMode.contains(VerboseResolutionMode.FAILURE)) {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   216
            return;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   217
        }
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   218
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   219
        if (bestSoFar.name == names.init &&
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   220
                bestSoFar.owner == syms.objectType.tsym &&
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   221
                !verboseResolutionMode.contains(VerboseResolutionMode.OBJECT_INIT)) {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   222
            return; //skip diags for Object constructor resolution
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   223
        } else if (site == syms.predefClass.type &&
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   224
                !verboseResolutionMode.contains(VerboseResolutionMode.PREDEF)) {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   225
            return; //skip spurious diags for predef symbols (i.e. operators)
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   226
        } else if (currentResolutionContext.internalResolution &&
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   227
                !verboseResolutionMode.contains(VerboseResolutionMode.INTERNAL)) {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   228
            return;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   229
        }
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   230
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   231
        int pos = 0;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   232
        int mostSpecificPos = -1;
20249
93f8eae31092 6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents: 19940
diff changeset
   233
        ListBuffer<JCDiagnostic> subDiags = new ListBuffer<>();
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   234
        for (Candidate c : currentResolutionContext.candidates) {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   235
            if (currentResolutionContext.step != c.step ||
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   236
                    (c.isApplicable() && !verboseResolutionMode.contains(VerboseResolutionMode.APPLICABLE)) ||
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   237
                    (!c.isApplicable() && !verboseResolutionMode.contains(VerboseResolutionMode.INAPPLICABLE))) {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   238
                continue;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   239
            } else {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   240
                subDiags.append(c.isApplicable() ?
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   241
                        getVerboseApplicableCandidateDiag(pos, c.sym, c.mtype) :
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   242
                        getVerboseInapplicableCandidateDiag(pos, c.sym, c.details));
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   243
                if (c.sym == bestSoFar)
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   244
                    mostSpecificPos = pos;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   245
                pos++;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   246
            }
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   247
        }
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   248
        String key = success ? "verbose.resolve.multi" : "verbose.resolve.multi.1";
29554
6d7957bd6866 8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents: 29292
diff changeset
   249
        List<Type> argtypes2 = argtypes.map(deferredAttr.new RecoveryDeferredTypeMap(AttrMode.SPECULATIVE, bestSoFar, currentResolutionContext.step));
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   250
        JCDiagnostic main = diags.note(log.currentSource(), dpos, key, name,
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   251
                site.tsym, mostSpecificPos, currentResolutionContext.step,
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
   252
                methodArguments(argtypes2),
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
   253
                methodArguments(typeargtypes));
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   254
        JCDiagnostic d = new JCDiagnostic.MultilineDiagnostic(main, subDiags.toList());
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   255
        log.report(d);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   258
    JCDiagnostic getVerboseApplicableCandidateDiag(int pos, Symbol sym, Type inst) {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   259
        JCDiagnostic subDiag = null;
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14062
diff changeset
   260
        if (sym.type.hasTag(FORALL)) {
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44504
diff changeset
   261
            subDiag = diags.fragment(Fragments.PartialInstSig(inst));
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   262
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   264
        String key = subDiag == null ?
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   265
                "applicable.method.found" :
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   266
                "applicable.method.found.1";
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
   267
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   268
        return diags.fragment(key, pos, sym, subDiag);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   271
    JCDiagnostic getVerboseInapplicableCandidateDiag(int pos, Symbol sym, JCDiagnostic subDiag) {
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44504
diff changeset
   272
        return diags.fragment(Fragments.NotApplicableMethodFound(pos, sym, subDiag));
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   273
    }
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   274
    // </editor-fold>
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
/* ************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
 * Identifier resolution
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
 *************************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
    /** An environment is "static" if its static level is greater than
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
     *  the one of its outer environment
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
     */
14056
0ea78d6e0b7b 7198582: (java) Minor refactor of JavacParser
ksrini
parents: 14052
diff changeset
   283
    protected static boolean isStatic(Env<AttrContext> env) {
22440
d40c30326317 8028389: NullPointerException compiling annotation values that have bodies
alundblad
parents: 22167
diff changeset
   284
        return env.outer != null && env.info.staticLevel > env.outer.info.staticLevel;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
    /** An environment is an "initializer" if it is a constructor or
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
     *  an instance initializer.
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
    static boolean isInitializer(Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
        Symbol owner = env.info.scope.owner;
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
        return owner.isConstructor() ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
            owner.owner.kind == TYP &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
            (owner.kind == VAR ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
             owner.kind == MTH && (owner.flags() & BLOCK) != 0) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
            (owner.flags() & STATIC) == 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
    /** Is class accessible in given evironment?
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
     *  @param env    The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
     *  @param c      The class whose accessibility is checked.
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
    public boolean isAccessible(Env<AttrContext> env, TypeSymbol c) {
7209
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   304
        return isAccessible(env, c, false);
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   305
    }
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   306
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   307
    public boolean isAccessible(Env<AttrContext> env, TypeSymbol c, boolean checkInner) {
32951
a098b538296c 8075799: Extraneous access checks implemented by javac
sadayapalam
parents: 32949
diff changeset
   308
a098b538296c 8075799: Extraneous access checks implemented by javac
sadayapalam
parents: 32949
diff changeset
   309
        /* 15.9.5.1: Note that it is possible for the signature of the anonymous constructor
a098b538296c 8075799: Extraneous access checks implemented by javac
sadayapalam
parents: 32949
diff changeset
   310
           to refer to an inaccessible type
a098b538296c 8075799: Extraneous access checks implemented by javac
sadayapalam
parents: 32949
diff changeset
   311
        */
a098b538296c 8075799: Extraneous access checks implemented by javac
sadayapalam
parents: 32949
diff changeset
   312
        if (env.enclMethod != null && (env.enclMethod.mods.flags & ANONCONSTR) != 0)
a098b538296c 8075799: Extraneous access checks implemented by javac
sadayapalam
parents: 32949
diff changeset
   313
            return true;
a098b538296c 8075799: Extraneous access checks implemented by javac
sadayapalam
parents: 32949
diff changeset
   314
43865
532c50fea155 8174243: incorrect error message for nested service provider
vromero
parents: 43272
diff changeset
   315
        if (env.info.visitingServiceImplementation &&
532c50fea155 8174243: incorrect error message for nested service provider
vromero
parents: 43272
diff changeset
   316
            env.toplevel.modle == c.packge().modle) {
532c50fea155 8174243: incorrect error message for nested service provider
vromero
parents: 43272
diff changeset
   317
            return true;
532c50fea155 8174243: incorrect error message for nested service provider
vromero
parents: 43272
diff changeset
   318
        }
532c50fea155 8174243: incorrect error message for nested service provider
vromero
parents: 43272
diff changeset
   319
7209
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   320
        boolean isAccessible = false;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
        switch ((short)(c.flags() & AccessFlags)) {
7209
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   322
            case PRIVATE:
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.enclClass.sym.outermostClass() ==
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   325
                    c.owner.outermostClass();
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   326
                break;
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   327
            case 0:
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   328
                isAccessible =
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   329
                    env.toplevel.packge == c.owner // fast special case
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   330
                    ||
32951
a098b538296c 8075799: Extraneous access checks implemented by javac
sadayapalam
parents: 32949
diff changeset
   331
                    env.toplevel.packge == c.packge();
7209
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   332
                break;
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   333
            default: // error recovery
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36278
diff changeset
   334
                isAccessible = true;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36278
diff changeset
   335
                break;
7209
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   336
            case PUBLIC:
43131
7769e4891d5e 8172411: -XDnoModules must be removed
jlahoda
parents: 42974
diff changeset
   337
                if (allowModules) {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36278
diff changeset
   338
                    ModuleSymbol currModule = env.toplevel.modle;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36278
diff changeset
   339
                    currModule.complete();
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36278
diff changeset
   340
                    PackageSymbol p = c.packge();
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36278
diff changeset
   341
                    isAccessible =
44451
1619c00af9df 8177311: Denied access when named module accesses unreferences package from the unnamed module
jlahoda
parents: 43866
diff changeset
   342
                        currModule == p.modle ||
1619c00af9df 8177311: Denied access when named module accesses unreferences package from the unnamed module
jlahoda
parents: 43866
diff changeset
   343
                        currModule.visiblePackages.get(p.fullname) == p ||
1619c00af9df 8177311: Denied access when named module accesses unreferences package from the unnamed module
jlahoda
parents: 43866
diff changeset
   344
                        p == syms.rootPackage ||
1619c00af9df 8177311: Denied access when named module accesses unreferences package from the unnamed module
jlahoda
parents: 43866
diff changeset
   345
                        (p.modle == syms.unnamedModule && currModule.readModules.contains(p.modle));
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36278
diff changeset
   346
                } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36278
diff changeset
   347
                    isAccessible = true;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36278
diff changeset
   348
                }
7209
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   349
                break;
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   350
            case PROTECTED:
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   351
                isAccessible =
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   352
                    env.toplevel.packge == c.owner // fast special case
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   353
                    ||
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   354
                    env.toplevel.packge == c.packge()
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   355
                    ||
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   356
                    isInnerSubClass(env.enclClass.sym, c.owner);
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   357
                break;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   358
        }
7209
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   359
        return (checkInner == false || c.type.getEnclosingType() == Type.noType) ?
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   360
            isAccessible :
7637
467bc8fc514e 6990134: minor (but red) findbugs warnings
jjg
parents: 7635
diff changeset
   361
            isAccessible && isAccessible(env, c.type.getEnclosingType(), checkInner);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   362
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   363
    //where
06bc494ca11e Initial load
duke
parents:
diff changeset
   364
        /** Is given class a subclass of given base class, or an inner class
06bc494ca11e Initial load
duke
parents:
diff changeset
   365
         *  of a subclass?
06bc494ca11e Initial load
duke
parents:
diff changeset
   366
         *  Return null if no such class exists.
06bc494ca11e Initial load
duke
parents:
diff changeset
   367
         *  @param c     The class which is the subclass or is contained in it.
06bc494ca11e Initial load
duke
parents:
diff changeset
   368
         *  @param base  The base class
06bc494ca11e Initial load
duke
parents:
diff changeset
   369
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   370
        private boolean isInnerSubClass(ClassSymbol c, Symbol base) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   371
            while (c != null && !c.isSubClass(base, types)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   372
                c = c.owner.enclClass();
06bc494ca11e Initial load
duke
parents:
diff changeset
   373
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   374
            return c != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   375
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   376
06bc494ca11e Initial load
duke
parents:
diff changeset
   377
    boolean isAccessible(Env<AttrContext> env, Type t) {
7209
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   378
        return isAccessible(env, t, false);
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   379
    }
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   380
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   381
    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
   382
        return (t.hasTag(ARRAY))
24612
75dc732b45af 8042338: Refactor Types.upperBound to treat wildcards and variables separately
dlsmith
parents: 24604
diff changeset
   383
            ? isAccessible(env, types.cvarUpperBound(types.elemtype(t)))
7209
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   384
            : isAccessible(env, t.tsym, checkInner);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   385
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   386
17998
02a5abb2ae2f 7179353: try-with-resources fails to compile with generic exception parameters
vromero
parents: 17804
diff changeset
   387
    /** Is symbol accessible as a member of given type in given environment?
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   388
     *  @param env    The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
   389
     *  @param site   The type of which the tested symbol is regarded
06bc494ca11e Initial load
duke
parents:
diff changeset
   390
     *                as a member.
06bc494ca11e Initial load
duke
parents:
diff changeset
   391
     *  @param sym    The symbol.
06bc494ca11e Initial load
duke
parents:
diff changeset
   392
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   393
    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
   394
        return isAccessible(env, site, sym, false);
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   395
    }
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   396
    public boolean isAccessible(Env<AttrContext> env, Type site, Symbol sym, boolean checkInner) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   397
        if (sym.name == names.init && sym.owner != site.tsym) return false;
32951
a098b538296c 8075799: Extraneous access checks implemented by javac
sadayapalam
parents: 32949
diff changeset
   398
a098b538296c 8075799: Extraneous access checks implemented by javac
sadayapalam
parents: 32949
diff changeset
   399
        /* 15.9.5.1: Note that it is possible for the signature of the anonymous constructor
a098b538296c 8075799: Extraneous access checks implemented by javac
sadayapalam
parents: 32949
diff changeset
   400
           to refer to an inaccessible type
a098b538296c 8075799: Extraneous access checks implemented by javac
sadayapalam
parents: 32949
diff changeset
   401
        */
a098b538296c 8075799: Extraneous access checks implemented by javac
sadayapalam
parents: 32949
diff changeset
   402
        if (env.enclMethod != null && (env.enclMethod.mods.flags & ANONCONSTR) != 0)
a098b538296c 8075799: Extraneous access checks implemented by javac
sadayapalam
parents: 32949
diff changeset
   403
            return true;
a098b538296c 8075799: Extraneous access checks implemented by javac
sadayapalam
parents: 32949
diff changeset
   404
43865
532c50fea155 8174243: incorrect error message for nested service provider
vromero
parents: 43272
diff changeset
   405
        if (env.info.visitingServiceImplementation &&
532c50fea155 8174243: incorrect error message for nested service provider
vromero
parents: 43272
diff changeset
   406
            env.toplevel.modle == sym.packge().modle) {
532c50fea155 8174243: incorrect error message for nested service provider
vromero
parents: 43272
diff changeset
   407
            return true;
532c50fea155 8174243: incorrect error message for nested service provider
vromero
parents: 43272
diff changeset
   408
        }
532c50fea155 8174243: incorrect error message for nested service provider
vromero
parents: 43272
diff changeset
   409
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   410
        switch ((short)(sym.flags() & AccessFlags)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   411
        case PRIVATE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   412
            return
06bc494ca11e Initial load
duke
parents:
diff changeset
   413
                (env.enclClass.sym == sym.owner // fast special case
06bc494ca11e Initial load
duke
parents:
diff changeset
   414
                 ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   415
                 env.enclClass.sym.outermostClass() ==
06bc494ca11e Initial load
duke
parents:
diff changeset
   416
                 sym.owner.outermostClass())
06bc494ca11e Initial load
duke
parents:
diff changeset
   417
                &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   418
                sym.isInheritedIn(site.tsym, types);
06bc494ca11e Initial load
duke
parents:
diff changeset
   419
        case 0:
06bc494ca11e Initial load
duke
parents:
diff changeset
   420
            return
06bc494ca11e Initial load
duke
parents:
diff changeset
   421
                (env.toplevel.packge == sym.owner.owner // fast special case
06bc494ca11e Initial load
duke
parents:
diff changeset
   422
                 ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   423
                 env.toplevel.packge == sym.packge())
06bc494ca11e Initial load
duke
parents:
diff changeset
   424
                &&
7209
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   425
                isAccessible(env, site, checkInner)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   426
                &&
2511
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   427
                sym.isInheritedIn(site.tsym, types)
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   428
                &&
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   429
                notOverriddenIn(site, sym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   430
        case PROTECTED:
06bc494ca11e Initial load
duke
parents:
diff changeset
   431
            return
06bc494ca11e Initial load
duke
parents:
diff changeset
   432
                (env.toplevel.packge == sym.owner.owner // fast special case
06bc494ca11e Initial load
duke
parents:
diff changeset
   433
                 ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   434
                 env.toplevel.packge == sym.packge()
06bc494ca11e Initial load
duke
parents:
diff changeset
   435
                 ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   436
                 isProtectedAccessible(sym, env.enclClass.sym, site)
06bc494ca11e Initial load
duke
parents:
diff changeset
   437
                 ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   438
                 // OK to select instance method or field from 'super' or type name
06bc494ca11e Initial load
duke
parents:
diff changeset
   439
                 // (but type names should be disallowed elsewhere!)
06bc494ca11e Initial load
duke
parents:
diff changeset
   440
                 env.info.selectSuper && (sym.flags() & STATIC) == 0 && sym.kind != TYP)
06bc494ca11e Initial load
duke
parents:
diff changeset
   441
                &&
7209
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   442
                isAccessible(env, site, checkInner)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   443
                &&
2511
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   444
                notOverriddenIn(site, sym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   445
        default: // this case includes erroneous combinations as well
7209
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   446
            return isAccessible(env, site, checkInner) && notOverriddenIn(site, sym);
2511
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   447
        }
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   448
    }
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   449
    //where
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   450
    /* `sym' is accessible only if not overridden by
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   451
     * another symbol which is a member of `site'
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   452
     * (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
   453
     * 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
   454
     * cannot be overridden (e.g. MH.invokeExact(Object[])).
2511
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   455
     */
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   456
    private boolean notOverriddenIn(Type site, Symbol sym) {
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   457
        if (sym.kind != MTH || sym.isConstructor() || sym.isStatic())
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   458
            return true;
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   459
        else {
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   460
            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
   461
            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
   462
                    !types.isSubSignature(types.memberType(site, s2), types.memberType(site, sym)));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   463
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   464
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   465
    //where
06bc494ca11e Initial load
duke
parents:
diff changeset
   466
        /** Is given protected symbol accessible if it is selected from given site
06bc494ca11e Initial load
duke
parents:
diff changeset
   467
         *  and the selection takes place in given class?
06bc494ca11e Initial load
duke
parents:
diff changeset
   468
         *  @param sym     The symbol with protected access
06bc494ca11e Initial load
duke
parents:
diff changeset
   469
         *  @param c       The class where the access takes place
06bc494ca11e Initial load
duke
parents:
diff changeset
   470
         *  @site          The type of the qualifier
06bc494ca11e Initial load
duke
parents:
diff changeset
   471
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   472
        private
06bc494ca11e Initial load
duke
parents:
diff changeset
   473
        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
   474
            Type newSite = site.hasTag(TYPEVAR) ? site.getUpperBound() : site;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   475
            while (c != null &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   476
                   !(c.isSubClass(sym.owner, types) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   477
                     (c.flags() & INTERFACE) == 0 &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   478
                     // In JLS 2e 6.6.2.1, the subclass restriction applies
06bc494ca11e Initial load
duke
parents:
diff changeset
   479
                     // only to instance fields and methods -- types are excluded
06bc494ca11e Initial load
duke
parents:
diff changeset
   480
                     // 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
   481
                     ((sym.flags() & STATIC) != 0 || sym.kind == TYP || newSite.tsym.isSubClass(c, types))))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   482
                c = c.owner.enclClass();
06bc494ca11e Initial load
duke
parents:
diff changeset
   483
            return c != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   484
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   485
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   486
    /**
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   487
     * Performs a recursive scan of a type looking for accessibility problems
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   488
     * from current attribution environment
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   489
     */
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   490
    void checkAccessibleType(Env<AttrContext> env, Type t) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   491
        accessibilityChecker.visit(t, env);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   492
    }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   493
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   494
    /**
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   495
     * Accessibility type-visitor
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   496
     */
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   497
    Types.SimpleVisitor<Void, Env<AttrContext>> accessibilityChecker =
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   498
            new Types.SimpleVisitor<Void, Env<AttrContext>>() {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   499
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   500
        void visit(List<Type> ts, Env<AttrContext> env) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   501
            for (Type t : ts) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   502
                visit(t, env);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   503
            }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   504
        }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   505
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   506
        public Void visitType(Type t, Env<AttrContext> env) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   507
            return null;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   508
        }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   509
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   510
        @Override
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   511
        public Void visitArrayType(ArrayType t, Env<AttrContext> env) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   512
            visit(t.elemtype, env);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   513
            return null;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   514
        }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   515
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   516
        @Override
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   517
        public Void visitClassType(ClassType t, Env<AttrContext> env) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   518
            visit(t.getTypeArguments(), env);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   519
            if (!isAccessible(env, t, true)) {
43138
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
   520
                accessBase(new AccessError(env, null, t.tsym), env.tree.pos(), env.enclClass.sym, t, t.tsym.name, true);
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   521
            }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   522
            return null;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   523
        }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   524
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   525
        @Override
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   526
        public Void visitWildcardType(WildcardType t, Env<AttrContext> env) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   527
            visit(t.type, env);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   528
            return null;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   529
        }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   530
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   531
        @Override
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   532
        public Void visitMethodType(MethodType t, Env<AttrContext> env) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   533
            visit(t.getParameterTypes(), env);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   534
            visit(t.getReturnType(), env);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   535
            visit(t.getThrownTypes(), env);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   536
            return null;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   537
        }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   538
    };
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   539
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   540
    /** 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
   541
     *  given type arguments and argument types. If successful, return
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   542
     *  the method's instantiated type, else return null.
06bc494ca11e Initial load
duke
parents:
diff changeset
   543
     *  The instantiation will take into account an additional leading
06bc494ca11e Initial load
duke
parents:
diff changeset
   544
     *  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
   545
     *  of an under determined site. In this case, we treat site as an additional
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   546
     *  parameter and the parameters of the class containing the method as
06bc494ca11e Initial load
duke
parents:
diff changeset
   547
     *  additional type variables that get instantiated.
06bc494ca11e Initial load
duke
parents:
diff changeset
   548
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   549
     *  @param env         The current environment
06bc494ca11e Initial load
duke
parents:
diff changeset
   550
     *  @param site        The type of which the method is a member.
06bc494ca11e Initial load
duke
parents:
diff changeset
   551
     *  @param m           The method symbol.
06bc494ca11e Initial load
duke
parents:
diff changeset
   552
     *  @param argtypes    The invocation's given value arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
   553
     *  @param typeargtypes    The invocation's given type arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
   554
     *  @param allowBoxing Allow boxing conversions of arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
   555
     *  @param useVarargs Box trailing arguments into an array for varargs.
06bc494ca11e Initial load
duke
parents:
diff changeset
   556
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   557
    Type rawInstantiate(Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
   558
                        Type site,
06bc494ca11e Initial load
duke
parents:
diff changeset
   559
                        Symbol m,
12915
28cf1e0dafdc 7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents: 12335
diff changeset
   560
                        ResultInfo resultInfo,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   561
                        List<Type> argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
   562
                        List<Type> typeargtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
   563
                        boolean allowBoxing,
06bc494ca11e Initial load
duke
parents:
diff changeset
   564
                        boolean useVarargs,
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
   565
                        Warner warn) throws Infer.InferenceException {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   566
        Type mt = types.memberType(site, m);
06bc494ca11e Initial load
duke
parents:
diff changeset
   567
        // tvars is the list of formal type variables for which type arguments
06bc494ca11e Initial load
duke
parents:
diff changeset
   568
        // need to inferred.
12915
28cf1e0dafdc 7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents: 12335
diff changeset
   569
        List<Type> tvars = List.nil();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   570
        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
   571
        if (!mt.hasTag(FORALL) && typeargtypes.nonEmpty()) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   572
            // 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
   573
            // 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
   574
        } else if (mt.hasTag(FORALL) && typeargtypes.nonEmpty()) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   575
            ForAll pmt = (ForAll) mt;
06bc494ca11e Initial load
duke
parents:
diff changeset
   576
            if (typeargtypes.length() != pmt.tvars.length())
41943
a4ee110842fb 8169093: Generics, javac not matching actual and formal arguments.
mcimadamore
parents: 41932
diff changeset
   577
                 // not enough args
49876
ac916fea6ec7 8202157: remove the use of string keys at InapplicableMethodException
vromero
parents: 49872
diff changeset
   578
                throw new InapplicableMethodException(diags.fragment(Fragments.WrongNumberTypeArgs(Integer.toString(pmt.tvars.length()))));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   579
            // Check type arguments are within bounds
06bc494ca11e Initial load
duke
parents:
diff changeset
   580
            List<Type> formals = pmt.tvars;
06bc494ca11e Initial load
duke
parents:
diff changeset
   581
            List<Type> actuals = typeargtypes;
06bc494ca11e Initial load
duke
parents:
diff changeset
   582
            while (formals.nonEmpty() && actuals.nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   583
                List<Type> bounds = types.subst(types.getBounds((TypeVar)formals.head),
06bc494ca11e Initial load
duke
parents:
diff changeset
   584
                                                pmt.tvars, typeargtypes);
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
   585
                for (; bounds.nonEmpty(); bounds = bounds.tail) {
49876
ac916fea6ec7 8202157: remove the use of string keys at InapplicableMethodException
vromero
parents: 49872
diff changeset
   586
                    if (!types.isSubtypeUnchecked(actuals.head, bounds.head, warn)) {
ac916fea6ec7 8202157: remove the use of string keys at InapplicableMethodException
vromero
parents: 49872
diff changeset
   587
                        throw new InapplicableMethodException(diags.fragment(Fragments.ExplicitParamDoNotConformToBounds(actuals.head, bounds)));
ac916fea6ec7 8202157: remove the use of string keys at InapplicableMethodException
vromero
parents: 49872
diff changeset
   588
                    }
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
   589
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   590
                formals = formals.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   591
                actuals = actuals.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   592
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   593
            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
   594
        } else if (mt.hasTag(FORALL)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   595
            ForAll pmt = (ForAll) mt;
06bc494ca11e Initial load
duke
parents:
diff changeset
   596
            List<Type> tvars1 = types.newInstances(pmt.tvars);
06bc494ca11e Initial load
duke
parents:
diff changeset
   597
            tvars = tvars.appendList(tvars1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   598
            mt = types.subst(pmt.qtype, pmt.tvars, tvars1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   599
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   600
06bc494ca11e Initial load
duke
parents:
diff changeset
   601
        // 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
   602
        boolean instNeeded = tvars.tail != null; /*inlined: tvars.nonEmpty()*/
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   603
        for (List<Type> l = argtypes;
06bc494ca11e Initial load
duke
parents:
diff changeset
   604
             l.tail != null/*inlined: l.nonEmpty()*/ && !instNeeded;
06bc494ca11e Initial load
duke
parents:
diff changeset
   605
             l = l.tail) {
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14062
diff changeset
   606
            if (l.head.hasTag(FORALL)) instNeeded = true;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   607
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   608
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
   609
        if (instNeeded) {
12335
4725d88691dd 7133185: Update 292 overload resolution logic to match JLS
mcimadamore
parents: 12334
diff changeset
   610
            return infer.instantiateMethod(env,
5489
e7af65bf7577 6730476: invalid "unchecked generic array" warning
mcimadamore
parents: 5321
diff changeset
   611
                                    tvars,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   612
                                    (MethodType)mt,
12915
28cf1e0dafdc 7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents: 12335
diff changeset
   613
                                    resultInfo,
24226
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23132
diff changeset
   614
                                    (MethodSymbol)m,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   615
                                    argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
   616
                                    allowBoxing,
06bc494ca11e Initial load
duke
parents:
diff changeset
   617
                                    useVarargs,
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
   618
                                    currentResolutionContext,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   619
                                    warn);
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
   620
        }
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
   621
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19652
diff changeset
   622
        DeferredAttr.DeferredAttrContext dc = currentResolutionContext.deferredAttrContext(m, infer.emptyContext, resultInfo, warn);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19652
diff changeset
   623
        currentResolutionContext.methodCheck.argumentsAcceptable(env, dc,
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   624
                                argtypes, mt.getParameterTypes(), warn);
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19652
diff changeset
   625
        dc.complete();
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
   626
        return mt;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   627
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   628
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
   629
    Type checkMethod(Env<AttrContext> env,
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
   630
                     Type site,
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
   631
                     Symbol m,
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
   632
                     ResultInfo resultInfo,
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
   633
                     List<Type> argtypes,
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
   634
                     List<Type> typeargtypes,
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
   635
                     Warner warn) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
   636
        MethodResolutionContext prevContext = currentResolutionContext;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
   637
        try {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
   638
            currentResolutionContext = new MethodResolutionContext();
32709
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32545
diff changeset
   639
            currentResolutionContext.attrMode = (resultInfo.pt == Infer.anyPoly) ?
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32545
diff changeset
   640
                    AttrMode.SPECULATIVE : DeferredAttr.AttrMode.CHECK;
18910
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   641
            if (env.tree.hasTag(JCTree.Tag.REFERENCE)) {
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   642
                //method/constructor references need special check class
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   643
                //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
   644
                //during an unsticking round)
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   645
                currentResolutionContext.methodCheck =
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   646
                        new MethodReferenceCheck(resultInfo.checkContext.inferenceContext());
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   647
            }
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
   648
            MethodResolutionPhase step = currentResolutionContext.step = env.info.pendingResolutionPhase;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
   649
            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
   650
                    step.isBoxingRequired(), step.isVarargsRequired(), warn);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
   651
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
   652
        finally {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
   653
            currentResolutionContext = prevContext;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
   654
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
   655
    }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
   656
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   657
    /** Same but returns null instead throwing a NoInstanceException
06bc494ca11e Initial load
duke
parents:
diff changeset
   658
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   659
    Type instantiate(Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
   660
                     Type site,
06bc494ca11e Initial load
duke
parents:
diff changeset
   661
                     Symbol m,
12915
28cf1e0dafdc 7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents: 12335
diff changeset
   662
                     ResultInfo resultInfo,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   663
                     List<Type> argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
   664
                     List<Type> typeargtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
   665
                     boolean allowBoxing,
06bc494ca11e Initial load
duke
parents:
diff changeset
   666
                     boolean useVarargs,
06bc494ca11e Initial load
duke
parents:
diff changeset
   667
                     Warner warn) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   668
        try {
12915
28cf1e0dafdc 7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents: 12335
diff changeset
   669
            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
   670
                                  allowBoxing, useVarargs, warn);
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
   671
        } catch (InapplicableMethodException ex) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   672
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   673
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   674
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   675
15037
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
     * 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
   678
     * 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
   679
     * 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
   680
     * 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
   681
     * this interfaces allows to easily add new pluggable method check routines.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   682
     */
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   683
    interface MethodCheck {
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   684
        /**
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   685
         * 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
   686
         * 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
   687
         * 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
   688
         * is assumed to be thrown.
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   689
         */
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   690
        void argumentsAcceptable(Env<AttrContext> env,
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   691
                                DeferredAttrContext deferredAttrContext,
8229
39266c1b1b0e 6313164: javac generates code that fails byte code verification for the varargs feature
mcimadamore
parents: 8228
diff changeset
   692
                                List<Type> argtypes,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   693
                                List<Type> formals,
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   694
                                Warner warn);
16805
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   695
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   696
        /**
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   697
         * 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
   698
         * most specific check.
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   699
         */
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents: 28456
diff changeset
   700
        MethodCheck mostSpecificCheck(List<Type> actuals);
11707
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   701
    }
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   702
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   703
    /**
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   704
     * Helper enum defining all method check diagnostics (used by resolveMethodCheck).
11707
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   705
     */
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   706
    enum MethodCheckDiag {
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   707
        /**
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   708
         * Actuals and formals differs in length.
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   709
         */
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   710
        ARITY_MISMATCH("arg.length.mismatch", "infer.arg.length.mismatch"),
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   711
        /**
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   712
         * An actual is incompatible with a formal.
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   713
         */
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   714
        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
   715
        /**
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   716
         * An actual is incompatible with the varargs element type.
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   717
         */
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   718
        VARARG_MISMATCH("varargs.argument.mismatch", "infer.varargs.argument.mismatch"),
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   719
        /**
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   720
         * The varargs element type is inaccessible.
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   721
         */
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   722
        INACCESSIBLE_VARARGS("inaccessible.varargs.type", "inaccessible.varargs.type");
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
        final String basicKey;
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   725
        final String inferKey;
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
        MethodCheckDiag(String basicKey, String inferKey) {
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   728
            this.basicKey = basicKey;
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   729
            this.inferKey = inferKey;
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   730
        }
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
   731
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
   732
        String regex() {
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
   733
            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
   734
        }
11707
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   735
    }
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   736
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   737
    /**
16805
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   738
     * 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
   739
     * of their formal parameter types.
11707
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   740
     */
16805
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   741
    MethodCheck nilMethodCheck = new MethodCheck() {
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   742
        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
   743
            //do nothing - method always applicable regardless of actuals
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   744
        }
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   745
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents: 28456
diff changeset
   746
        public MethodCheck mostSpecificCheck(List<Type> actuals) {
16805
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   747
            return this;
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   748
        }
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   749
    };
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   750
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   751
    /**
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   752
     * 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
   753
     * 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
   754
     * 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
   755
     */
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   756
    abstract class AbstractMethodCheck implements MethodCheck {
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   757
        @Override
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   758
        public void argumentsAcceptable(final Env<AttrContext> env,
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   759
                                    DeferredAttrContext deferredAttrContext,
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   760
                                    List<Type> argtypes,
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   761
                                    List<Type> formals,
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   762
                                    Warner warn) {
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   763
            //should we expand formals?
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   764
            boolean useVarargs = deferredAttrContext.phase.isVarargsRequired();
34854
d90aa0a83be4 8145466: javac: No line numbers in compilation error
sadayapalam
parents: 34566
diff changeset
   765
            JCTree callTree = treeForDiagnostics(env);
d90aa0a83be4 8145466: javac: No line numbers in compilation error
sadayapalam
parents: 34566
diff changeset
   766
            List<JCExpression> trees = TreeInfo.args(callTree);
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   767
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   768
            //inference context used during this method check
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   769
            InferenceContext inferenceContext = deferredAttrContext.inferenceContext;
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   770
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   771
            Type varargsFormal = useVarargs ? formals.last() : null;
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   772
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   773
            if (varargsFormal == null &&
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   774
                    argtypes.size() != formals.size()) {
34854
d90aa0a83be4 8145466: javac: No line numbers in compilation error
sadayapalam
parents: 34566
diff changeset
   775
                reportMC(callTree, MethodCheckDiag.ARITY_MISMATCH, inferenceContext); // not enough 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
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   778
            while (argtypes.nonEmpty() && formals.head != varargsFormal) {
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
   779
                DiagnosticPosition pos = trees != null ? trees.head : null;
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
   780
                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
   781
                argtypes = argtypes.tail;
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   782
                formals = formals.tail;
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
   783
                trees = trees != null ? trees.tail : trees;
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
   784
            }
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   785
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   786
            if (formals.head != varargsFormal) {
34854
d90aa0a83be4 8145466: javac: No line numbers in compilation error
sadayapalam
parents: 34566
diff changeset
   787
                reportMC(callTree, MethodCheckDiag.ARITY_MISMATCH, inferenceContext); // not enough args
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   788
            }
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   789
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   790
            if (useVarargs) {
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   791
                //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
   792
                //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
   793
                final Type elt = types.elemtype(varargsFormal);
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   794
                while (argtypes.nonEmpty()) {
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
   795
                    DiagnosticPosition pos = trees != null ? trees.head : null;
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
   796
                    checkArg(pos, true, argtypes.head, elt, deferredAttrContext, warn);
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   797
                    argtypes = argtypes.tail;
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
   798
                    trees = trees != null ? trees.tail : trees;
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 14045
diff changeset
   799
                }
8229
39266c1b1b0e 6313164: javac generates code that fails byte code verification for the varargs feature
mcimadamore
parents: 8228
diff changeset
   800
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   801
        }
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   802
34854
d90aa0a83be4 8145466: javac: No line numbers in compilation error
sadayapalam
parents: 34566
diff changeset
   803
            // where
d90aa0a83be4 8145466: javac: No line numbers in compilation error
sadayapalam
parents: 34566
diff changeset
   804
            private JCTree treeForDiagnostics(Env<AttrContext> env) {
d90aa0a83be4 8145466: javac: No line numbers in compilation error
sadayapalam
parents: 34566
diff changeset
   805
                return env.info.preferredTreeForDiagnostics != null ? env.info.preferredTreeForDiagnostics : env.tree;
d90aa0a83be4 8145466: javac: No line numbers in compilation error
sadayapalam
parents: 34566
diff changeset
   806
            }
d90aa0a83be4 8145466: javac: No line numbers in compilation error
sadayapalam
parents: 34566
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
         * 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
   810
         */
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
   811
        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
   812
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
   813
        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
   814
            boolean inferDiag = inferenceContext != infer.emptyContext;
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   815
            if (inferDiag && (!diag.inferKey.equals(diag.basicKey))) {
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   816
                Object[] args2 = new Object[args.length + 1];
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   817
                System.arraycopy(args, 0, args2, 1, args.length);
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   818
                args2[0] = inferenceContext.inferenceVars();
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   819
                args = args2;
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   820
            }
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
   821
            String key = inferDiag ? diag.inferKey : diag.basicKey;
49872
0798eab12791 8201281: Truncated error message with Incompatible : null
vromero
parents: 49579
diff changeset
   822
            throw inferDiag ?
0798eab12791 8201281: Truncated error message with Incompatible : null
vromero
parents: 49579
diff changeset
   823
                infer.error(diags.create(DiagnosticType.FRAGMENT, log.currentSource(), pos, key, args)) :
50592
dddf078c242f 8203838: javac performance regression in 11+11
mcimadamore
parents: 50539
diff changeset
   824
                methodCheckFailure.setMessage(diags.create(DiagnosticType.FRAGMENT, log.currentSource(), pos, key, args));
dddf078c242f 8203838: javac performance regression in 11+11
mcimadamore
parents: 50539
diff changeset
   825
        }
dddf078c242f 8203838: javac performance regression in 11+11
mcimadamore
parents: 50539
diff changeset
   826
dddf078c242f 8203838: javac performance regression in 11+11
mcimadamore
parents: 50539
diff changeset
   827
        /**
dddf078c242f 8203838: javac performance regression in 11+11
mcimadamore
parents: 50539
diff changeset
   828
         * To eliminate the overhead associated with allocating an exception object in such an
dddf078c242f 8203838: javac performance regression in 11+11
mcimadamore
parents: 50539
diff changeset
   829
         * hot execution path, we use flyweight pattern - and share the same exception instance
dddf078c242f 8203838: javac performance regression in 11+11
mcimadamore
parents: 50539
diff changeset
   830
         * across multiple method check failures.
dddf078c242f 8203838: javac performance regression in 11+11
mcimadamore
parents: 50539
diff changeset
   831
         */
dddf078c242f 8203838: javac performance regression in 11+11
mcimadamore
parents: 50539
diff changeset
   832
        class SharedInapplicableMethodException extends InapplicableMethodException {
dddf078c242f 8203838: javac performance regression in 11+11
mcimadamore
parents: 50539
diff changeset
   833
            private static final long serialVersionUID = 0;
dddf078c242f 8203838: javac performance regression in 11+11
mcimadamore
parents: 50539
diff changeset
   834
dddf078c242f 8203838: javac performance regression in 11+11
mcimadamore
parents: 50539
diff changeset
   835
            SharedInapplicableMethodException() {
dddf078c242f 8203838: javac performance regression in 11+11
mcimadamore
parents: 50539
diff changeset
   836
                super(null);
dddf078c242f 8203838: javac performance regression in 11+11
mcimadamore
parents: 50539
diff changeset
   837
            }
dddf078c242f 8203838: javac performance regression in 11+11
mcimadamore
parents: 50539
diff changeset
   838
dddf078c242f 8203838: javac performance regression in 11+11
mcimadamore
parents: 50539
diff changeset
   839
            SharedInapplicableMethodException setMessage(JCDiagnostic details) {
dddf078c242f 8203838: javac performance regression in 11+11
mcimadamore
parents: 50539
diff changeset
   840
                this.diagnostic = details;
dddf078c242f 8203838: javac performance regression in 11+11
mcimadamore
parents: 50539
diff changeset
   841
                return this;
dddf078c242f 8203838: javac performance regression in 11+11
mcimadamore
parents: 50539
diff changeset
   842
            }
dddf078c242f 8203838: javac performance regression in 11+11
mcimadamore
parents: 50539
diff changeset
   843
        }
dddf078c242f 8203838: javac performance regression in 11+11
mcimadamore
parents: 50539
diff changeset
   844
dddf078c242f 8203838: javac performance regression in 11+11
mcimadamore
parents: 50539
diff changeset
   845
        SharedInapplicableMethodException methodCheckFailure = new SharedInapplicableMethodException();
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   846
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents: 28456
diff changeset
   847
        public MethodCheck mostSpecificCheck(List<Type> actuals) {
16805
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   848
            return nilMethodCheck;
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   849
        }
24226
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23132
diff changeset
   850
16805
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   851
    }
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
     * 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
   855
     * supplied conforms to the method signature.
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   856
     */
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   857
    MethodCheck arityMethodCheck = new AbstractMethodCheck() {
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   858
        @Override
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
   859
        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
   860
            //do nothing - actual always compatible to formals
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   861
        }
24226
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23132
diff changeset
   862
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23132
diff changeset
   863
        @Override
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23132
diff changeset
   864
        public String toString() {
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23132
diff changeset
   865
            return "arityMethodCheck";
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23132
diff changeset
   866
        }
16805
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   867
    };
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   868
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   869
    /**
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   870
     * 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
   871
     * 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
   872
     * 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
   873
     *
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   874
     * 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
   875
     * 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
   876
     * 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
   877
     * 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
   878
     *
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   879
     * 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
   880
     * 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
   881
     * 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
   882
     * 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
   883
     * 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
   884
     *
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   885
     * 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
   886
     */
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   887
    MethodCheck resolveMethodCheck = new AbstractMethodCheck() {
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   888
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   889
        @Override
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
   890
        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
   891
            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
   892
            mresult.check(pos, actual);
16805
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   893
        }
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   894
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   895
        @Override
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   896
        public void argumentsAcceptable(final Env<AttrContext> env,
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   897
                                    DeferredAttrContext deferredAttrContext,
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   898
                                    List<Type> argtypes,
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   899
                                    List<Type> formals,
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   900
                                    Warner warn) {
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   901
            super.argumentsAcceptable(env, deferredAttrContext, argtypes, formals, warn);
29958
3463809fbb52 8075520: Varargs access check mishandles capture variables
dlsmith
parents: 29776
diff changeset
   902
            // should we check varargs element type accessibility?
25435
928866794984 8049075: javac, wildcards and generic vararg method invocation not accepted
vromero
parents: 25279
diff changeset
   903
            if (deferredAttrContext.phase.isVarargsRequired()) {
29958
3463809fbb52 8075520: Varargs access check mishandles capture variables
dlsmith
parents: 29776
diff changeset
   904
                if (deferredAttrContext.mode == AttrMode.CHECK || !checkVarargsAccessAfterResolution) {
3463809fbb52 8075520: Varargs access check mishandles capture variables
dlsmith
parents: 29776
diff changeset
   905
                    varargsAccessible(env, types.elemtype(formals.last()), deferredAttrContext.inferenceContext);
25435
928866794984 8049075: javac, wildcards and generic vararg method invocation not accepted
vromero
parents: 25279
diff changeset
   906
                }
16805
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   907
            }
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   908
        }
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   909
29958
3463809fbb52 8075520: Varargs access check mishandles capture variables
dlsmith
parents: 29776
diff changeset
   910
        /**
3463809fbb52 8075520: Varargs access check mishandles capture variables
dlsmith
parents: 29776
diff changeset
   911
         * Test that the runtime array element type corresponding to 't' is accessible.  't' should be the
3463809fbb52 8075520: Varargs access check mishandles capture variables
dlsmith
parents: 29776
diff changeset
   912
         * varargs element type of either the method invocation type signature (after inference completes)
3463809fbb52 8075520: Varargs access check mishandles capture variables
dlsmith
parents: 29776
diff changeset
   913
         * or the method declaration signature (before inference completes).
3463809fbb52 8075520: Varargs access check mishandles capture variables
dlsmith
parents: 29776
diff changeset
   914
         */
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   915
        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
   916
            if (inferenceContext.free(t)) {
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42824
diff changeset
   917
                inferenceContext.addFreeTypeListener(List.of(t),
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42824
diff changeset
   918
                        solvedContext -> varargsAccessible(env, solvedContext.asInstType(t), solvedContext));
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   919
            } else {
29958
3463809fbb52 8075520: Varargs access check mishandles capture variables
dlsmith
parents: 29776
diff changeset
   920
                if (!isAccessible(env, types.erasure(t))) {
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   921
                    Symbol location = env.enclClass.sym;
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
   922
                    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
   923
                }
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   924
            }
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   925
        }
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   926
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   927
        private ResultInfo methodCheckResult(final boolean varargsCheck, Type to,
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   928
                final DeferredAttr.DeferredAttrContext deferredAttrContext, Warner rsWarner) {
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   929
            CheckContext checkContext = new MethodCheckContext(!deferredAttrContext.phase.isBoxingRequired(), deferredAttrContext, rsWarner) {
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   930
                MethodCheckDiag methodDiag = varargsCheck ?
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   931
                                 MethodCheckDiag.VARARG_MISMATCH : MethodCheckDiag.ARG_MISMATCH;
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   932
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   933
                @Override
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   934
                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
   935
                    reportMC(pos, methodDiag, deferredAttrContext.inferenceContext, details);
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   936
                }
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   937
            };
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   938
            return new MethodResultInfo(to, checkContext);
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   939
        }
16805
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   940
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   941
        @Override
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents: 28456
diff changeset
   942
        public MethodCheck mostSpecificCheck(List<Type> actuals) {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents: 28456
diff changeset
   943
            return new MostSpecificCheck(actuals);
16805
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
   944
        }
24226
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23132
diff changeset
   945
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23132
diff changeset
   946
        @Override
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23132
diff changeset
   947
        public String toString() {
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23132
diff changeset
   948
            return "resolveMethodCheck";
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23132
diff changeset
   949
        }
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
   950
    };
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   951
21718
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
   952
    /**
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
   953
     * This class handles method reference applicability checks; since during
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
   954
     * 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
   955
     * 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
   956
     * extended so that inference variables are 'opened' as needed.
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
   957
     */
18910
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   958
    class MethodReferenceCheck extends AbstractMethodCheck {
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   959
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   960
        InferenceContext pendingInferenceContext;
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   961
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   962
        MethodReferenceCheck(InferenceContext pendingInferenceContext) {
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   963
            this.pendingInferenceContext = pendingInferenceContext;
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   964
        }
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   965
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   966
        @Override
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   967
        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
   968
            ResultInfo mresult = methodCheckResult(varargs, formal, deferredAttrContext, warn);
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   969
            mresult.check(pos, actual);
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   970
        }
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   971
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   972
        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
   973
                final DeferredAttr.DeferredAttrContext deferredAttrContext, Warner rsWarner) {
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   974
            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
   975
                MethodCheckDiag methodDiag = varargsCheck ?
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   976
                                 MethodCheckDiag.VARARG_MISMATCH : MethodCheckDiag.ARG_MISMATCH;
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   977
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   978
                @Override
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   979
                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
   980
                    found = pendingInferenceContext.asUndetVar(found);
24226
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23132
diff changeset
   981
                    if (found.hasTag(UNDETVAR) && req.isPrimitive()) {
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23132
diff changeset
   982
                        req = types.boxedClass(req).type;
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23132
diff changeset
   983
                    }
18910
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   984
                    return super.compatible(found, req, warn);
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   985
                }
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   986
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   987
                @Override
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   988
                public void report(DiagnosticPosition pos, JCDiagnostic details) {
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   989
                    reportMC(pos, methodDiag, deferredAttrContext.inferenceContext, details);
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   990
                }
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   991
            };
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   992
            return new MethodResultInfo(to, checkContext);
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   993
        }
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   994
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   995
        @Override
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents: 28456
diff changeset
   996
        public MethodCheck mostSpecificCheck(List<Type> actuals) {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents: 28456
diff changeset
   997
            return new MostSpecificCheck(actuals);
18910
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
   998
        }
24226
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23132
diff changeset
   999
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23132
diff changeset
  1000
        @Override
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23132
diff changeset
  1001
        public String toString() {
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23132
diff changeset
  1002
            return "MethodReferenceCheck";
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23132
diff changeset
  1003
        }
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22161
diff changeset
  1004
    }
18910
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
  1005
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
  1006
    /**
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
  1007
     * 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
  1008
     * context might contain inference variables.
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
  1009
     */
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
  1010
    abstract class MethodCheckContext implements CheckContext {
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
  1011
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
  1012
        boolean strict;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  1013
        DeferredAttrContext deferredAttrContext;
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
  1014
        Warner rsWarner;
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
  1015
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
  1016
        public MethodCheckContext(boolean strict, DeferredAttrContext deferredAttrContext, Warner rsWarner) {
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
  1017
           this.strict = strict;
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
  1018
           this.deferredAttrContext = deferredAttrContext;
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
  1019
           this.rsWarner = rsWarner;
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
  1020
        }
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
  1021
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
  1022
        public boolean compatible(Type found, Type req, Warner warn) {
25279
a665d5cbb8c3 8044546: Crash on faulty reduce/lambda
vromero
parents: 25008
diff changeset
  1023
            InferenceContext inferenceContext = deferredAttrContext.inferenceContext;
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
  1024
            return strict ?
25279
a665d5cbb8c3 8044546: Crash on faulty reduce/lambda
vromero
parents: 25008
diff changeset
  1025
                    types.isSubtypeUnchecked(inferenceContext.asUndetVar(found), inferenceContext.asUndetVar(req), warn) :
a665d5cbb8c3 8044546: Crash on faulty reduce/lambda
vromero
parents: 25008
diff changeset
  1026
                    types.isConvertible(inferenceContext.asUndetVar(found), inferenceContext.asUndetVar(req), warn);
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
  1027
        }
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
  1028
13438
83729994273a 7175911: Simplify error reporting API in Check.CheckContext interface
mcimadamore
parents: 12915
diff changeset
  1029
        public void report(DiagnosticPosition pos, JCDiagnostic details) {
49876
ac916fea6ec7 8202157: remove the use of string keys at InapplicableMethodException
vromero
parents: 49872
diff changeset
  1030
            throw new InapplicableMethodException(details);
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
  1031
        }
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
  1032
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
  1033
        public Warner checkWarner(DiagnosticPosition pos, Type found, Type req) {
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
  1034
            return rsWarner;
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
  1035
        }
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 14045
diff changeset
  1036
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 14045
diff changeset
  1037
        public InferenceContext inferenceContext() {
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
  1038
            return deferredAttrContext.inferenceContext;
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 14045
diff changeset
  1039
        }
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  1040
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  1041
        public DeferredAttrContext deferredAttrContext() {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  1042
            return deferredAttrContext;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  1043
        }
24226
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23132
diff changeset
  1044
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23132
diff changeset
  1045
        @Override
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23132
diff changeset
  1046
        public String toString() {
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23132
diff changeset
  1047
            return "MethodCheckContext";
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23132
diff changeset
  1048
        }
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
  1049
    }
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
  1050
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
  1051
    /**
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
  1052
     * 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
  1053
     * for deferred types goes through special path.
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
  1054
     */
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  1055
    class MethodResultInfo extends ResultInfo {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  1056
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
  1057
        public MethodResultInfo(Type pt, CheckContext checkContext) {
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  1058
            attr.super(KindSelector.VAL, pt, checkContext);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  1059
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  1060
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  1061
        @Override
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  1062
        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
  1063
            if (found.hasTag(DEFERRED)) {
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  1064
                DeferredType dt = (DeferredType)found;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  1065
                return dt.check(this);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  1066
            } else {
28336
9a6ae3c3efb5 8066871: java.lang.VerifyError: Bad local variable type - local final String
mcimadamore
parents: 28331
diff changeset
  1067
                Type uResult = U(found);
24226
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23132
diff changeset
  1068
                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
  1069
                        types.capture(uResult) :
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23132
diff changeset
  1070
                        checkContext.inferenceContext()
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23132
diff changeset
  1071
                            .cachedCapture(pos.getTree(), uResult, true);
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 23132
diff changeset
  1072
                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
  1073
            }
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  1074
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  1075
18911
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1076
        /**
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1077
         * javac has a long-standing 'simplification' (see 6391995):
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1078
         * given an actual argument type, the method check is performed
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1079
         * on its upper bound. This leads to inconsistencies when an
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1080
         * argument type is checked against itself. For example, given
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1081
         * 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
  1082
         * so we need to guard against that.
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1083
         */
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1084
        private Type U(Type found) {
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1085
            return found == pt ?
24612
75dc732b45af 8042338: Refactor Types.upperBound to treat wildcards and variables separately
dlsmith
parents: 24604
diff changeset
  1086
                    found : types.cvarUpperBound(found);
18911
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1087
        }
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1088
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  1089
        @Override
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  1090
        protected MethodResultInfo dup(Type newPt) {
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
  1091
            return new MethodResultInfo(newPt, checkContext);
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  1092
        }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  1093
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  1094
        @Override
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  1095
        protected ResultInfo dup(CheckContext newContext) {
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
  1096
            return new MethodResultInfo(pt, newContext);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  1097
        }
32709
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32545
diff changeset
  1098
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32545
diff changeset
  1099
        @Override
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32545
diff changeset
  1100
        protected ResultInfo dup(Type newPt, CheckContext newContext) {
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32545
diff changeset
  1101
            return new MethodResultInfo(newPt, newContext);
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32545
diff changeset
  1102
        }
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
  1103
    }
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
  1104
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1105
    /**
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1106
     * 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
  1107
     * 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
  1108
     * 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
  1109
     * argument types A.
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1110
     */
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1111
    class MostSpecificCheck implements MethodCheck {
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1112
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1113
        List<Type> actuals;
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1114
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents: 28456
diff changeset
  1115
        MostSpecificCheck(List<Type> actuals) {
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1116
            this.actuals = actuals;
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1117
        }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1118
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1119
        @Override
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1120
        public void argumentsAcceptable(final Env<AttrContext> env,
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1121
                                    DeferredAttrContext deferredAttrContext,
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1122
                                    List<Type> formals1,
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1123
                                    List<Type> formals2,
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1124
                                    Warner warn) {
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1125
            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
  1126
            while (formals2.nonEmpty()) {
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1127
                ResultInfo mresult = methodCheckResult(formals2.head, deferredAttrContext, warn, actuals.head);
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1128
                mresult.check(null, formals1.head);
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1129
                formals1 = formals1.tail;
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1130
                formals2 = formals2.tail;
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1131
                actuals = actuals.isEmpty() ? actuals : actuals.tail;
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1132
            }
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
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1135
       /**
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1136
        * 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
  1137
        */
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1138
        ResultInfo methodCheckResult(Type to, DeferredAttr.DeferredAttrContext deferredAttrContext,
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1139
               Warner rsWarner, Type actual) {
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  1140
            return attr.new ResultInfo(KindSelector.VAL, to,
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents: 28456
diff changeset
  1141
                   new MostSpecificCheckContext(deferredAttrContext, rsWarner, actual));
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1142
        }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1143
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1144
        /**
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1145
         * 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
  1146
         * 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
  1147
         * a full blown structural analysis is carried out.
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1148
         */
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1149
        class MostSpecificCheckContext extends MethodCheckContext {
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
            Type actual;
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1152
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents: 28456
diff changeset
  1153
            public MostSpecificCheckContext(DeferredAttrContext deferredAttrContext, Warner rsWarner, Type actual) {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents: 28456
diff changeset
  1154
                super(true, deferredAttrContext, rsWarner);
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1155
                this.actual = actual;
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
            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
  1159
                if (allowFunctionalInterfaceMostSpecific &&
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1160
                        unrelatedFunctionalInterfaces(found, req) &&
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1161
                        (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
  1162
                    DeferredType dt = (DeferredType) actual;
32709
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32545
diff changeset
  1163
                    JCTree speculativeTree = dt.speculativeTree(deferredAttrContext);
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32545
diff changeset
  1164
                    if (speculativeTree != deferredAttr.stuckTree) {
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32545
diff changeset
  1165
                        return functionalInterfaceMostSpecific(found, req, speculativeTree);
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1166
                    }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1167
                }
29146
7115a5967ed1 8039214: Inference should not map capture variables to their upper bounds
dlsmith
parents: 29051
diff changeset
  1168
                return compatibleBySubtyping(found, req);
7115a5967ed1 8039214: Inference should not map capture variables to their upper bounds
dlsmith
parents: 29051
diff changeset
  1169
            }
7115a5967ed1 8039214: Inference should not map capture variables to their upper bounds
dlsmith
parents: 29051
diff changeset
  1170
7115a5967ed1 8039214: Inference should not map capture variables to their upper bounds
dlsmith
parents: 29051
diff changeset
  1171
            private boolean compatibleBySubtyping(Type found, Type req) {
7115a5967ed1 8039214: Inference should not map capture variables to their upper bounds
dlsmith
parents: 29051
diff changeset
  1172
                if (!strict && found.isPrimitive() != req.isPrimitive()) {
7115a5967ed1 8039214: Inference should not map capture variables to their upper bounds
dlsmith
parents: 29051
diff changeset
  1173
                    found = found.isPrimitive() ? types.boxedClass(found).type : types.unboxedType(found);
7115a5967ed1 8039214: Inference should not map capture variables to their upper bounds
dlsmith
parents: 29051
diff changeset
  1174
                }
7115a5967ed1 8039214: Inference should not map capture variables to their upper bounds
dlsmith
parents: 29051
diff changeset
  1175
                return types.isSubtypeNoCapture(found, deferredAttrContext.inferenceContext.asUndetVar(req));
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1176
            }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1177
24404
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1178
            /** 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
  1179
            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
  1180
                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
  1181
                       types.isFunctionalInterface(s.tsym) &&
38914
2f652b30f1b0 8037804: Implement specified test for related functional interface types
dlsmith
parents: 38913
diff changeset
  1182
                       unrelatedInterfaces(t, s);
2f652b30f1b0 8037804: Implement specified test for related functional interface types
dlsmith
parents: 38913
diff changeset
  1183
            }
2f652b30f1b0 8037804: Implement specified test for related functional interface types
dlsmith
parents: 38913
diff changeset
  1184
2f652b30f1b0 8037804: Implement specified test for related functional interface types
dlsmith
parents: 38913
diff changeset
  1185
            /** Whether {@code t} and {@code s} are unrelated interface types; recurs on intersections. **/
2f652b30f1b0 8037804: Implement specified test for related functional interface types
dlsmith
parents: 38913
diff changeset
  1186
            private boolean unrelatedInterfaces(Type t, Type s) {
2f652b30f1b0 8037804: Implement specified test for related functional interface types
dlsmith
parents: 38913
diff changeset
  1187
                if (t.isCompound()) {
2f652b30f1b0 8037804: Implement specified test for related functional interface types
dlsmith
parents: 38913
diff changeset
  1188
                    for (Type ti : types.interfaces(t)) {
2f652b30f1b0 8037804: Implement specified test for related functional interface types
dlsmith
parents: 38913
diff changeset
  1189
                        if (!unrelatedInterfaces(ti, s)) {
2f652b30f1b0 8037804: Implement specified test for related functional interface types
dlsmith
parents: 38913
diff changeset
  1190
                            return false;
2f652b30f1b0 8037804: Implement specified test for related functional interface types
dlsmith
parents: 38913
diff changeset
  1191
                        }
2f652b30f1b0 8037804: Implement specified test for related functional interface types
dlsmith
parents: 38913
diff changeset
  1192
                    }
2f652b30f1b0 8037804: Implement specified test for related functional interface types
dlsmith
parents: 38913
diff changeset
  1193
                    return true;
2f652b30f1b0 8037804: Implement specified test for related functional interface types
dlsmith
parents: 38913
diff changeset
  1194
                } else if (s.isCompound()) {
2f652b30f1b0 8037804: Implement specified test for related functional interface types
dlsmith
parents: 38913
diff changeset
  1195
                    for (Type si : types.interfaces(s)) {
2f652b30f1b0 8037804: Implement specified test for related functional interface types
dlsmith
parents: 38913
diff changeset
  1196
                        if (!unrelatedInterfaces(t, si)) {
2f652b30f1b0 8037804: Implement specified test for related functional interface types
dlsmith
parents: 38913
diff changeset
  1197
                            return false;
2f652b30f1b0 8037804: Implement specified test for related functional interface types
dlsmith
parents: 38913
diff changeset
  1198
                        }
2f652b30f1b0 8037804: Implement specified test for related functional interface types
dlsmith
parents: 38913
diff changeset
  1199
                    }
2f652b30f1b0 8037804: Implement specified test for related functional interface types
dlsmith
parents: 38913
diff changeset
  1200
                    return true;
2f652b30f1b0 8037804: Implement specified test for related functional interface types
dlsmith
parents: 38913
diff changeset
  1201
                } else {
2f652b30f1b0 8037804: Implement specified test for related functional interface types
dlsmith
parents: 38913
diff changeset
  1202
                    return types.asSuper(t, s.tsym) == null && types.asSuper(s, t.tsym) == null;
2f652b30f1b0 8037804: Implement specified test for related functional interface types
dlsmith
parents: 38913
diff changeset
  1203
                }
24404
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1204
            }
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1205
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1206
            /** Parameters {@code t} and {@code s} are unrelated functional interface types. */
29146
7115a5967ed1 8039214: Inference should not map capture variables to their upper bounds
dlsmith
parents: 29051
diff changeset
  1207
            private boolean functionalInterfaceMostSpecific(Type t, Type s, JCTree tree) {
38913
5b15a5ed3b0d 8144767: Fix handling of capture variables in most-specific test
dlsmith
parents: 36996
diff changeset
  1208
                Type tDesc = types.findDescriptorType(types.capture(t));
5b15a5ed3b0d 8144767: Fix handling of capture variables in most-specific test
dlsmith
parents: 36996
diff changeset
  1209
                Type tDescNoCapture = types.findDescriptorType(t);
34995
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1210
                Type sDesc = types.findDescriptorType(s);
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1211
                final List<Type> tTypeParams = tDesc.getTypeArguments();
38913
5b15a5ed3b0d 8144767: Fix handling of capture variables in most-specific test
dlsmith
parents: 36996
diff changeset
  1212
                final List<Type> tTypeParamsNoCapture = tDescNoCapture.getTypeArguments();
34995
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1213
                final List<Type> sTypeParams = sDesc.getTypeArguments();
38913
5b15a5ed3b0d 8144767: Fix handling of capture variables in most-specific test
dlsmith
parents: 36996
diff changeset
  1214
5b15a5ed3b0d 8144767: Fix handling of capture variables in most-specific test
dlsmith
parents: 36996
diff changeset
  1215
                // compare type parameters
5b15a5ed3b0d 8144767: Fix handling of capture variables in most-specific test
dlsmith
parents: 36996
diff changeset
  1216
                if (tDesc.hasTag(FORALL) && !types.hasSameBounds((ForAll) tDesc, (ForAll) tDescNoCapture)) {
5b15a5ed3b0d 8144767: Fix handling of capture variables in most-specific test
dlsmith
parents: 36996
diff changeset
  1217
                    return false;
5b15a5ed3b0d 8144767: Fix handling of capture variables in most-specific test
dlsmith
parents: 36996
diff changeset
  1218
                }
5b15a5ed3b0d 8144767: Fix handling of capture variables in most-specific test
dlsmith
parents: 36996
diff changeset
  1219
                // can't use Types.hasSameBounds on sDesc because bounds may have ivars
34995
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1220
                List<Type> tIter = tTypeParams;
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1221
                List<Type> sIter = sTypeParams;
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1222
                while (tIter.nonEmpty() && sIter.nonEmpty()) {
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1223
                    Type tBound = tIter.head.getUpperBound();
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1224
                    Type sBound = types.subst(sIter.head.getUpperBound(), sTypeParams, tTypeParams);
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1225
                    if (tBound.containsAny(tTypeParams) && inferenceContext().free(sBound)) {
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1226
                        return false;
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1227
                    }
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1228
                    if (!types.isSameType(tBound, inferenceContext().asUndetVar(sBound))) {
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1229
                        return false;
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1230
                    }
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1231
                    tIter = tIter.tail;
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1232
                    sIter = sIter.tail;
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1233
                }
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1234
                if (!tIter.isEmpty() || !sIter.isEmpty()) {
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1235
                    return false;
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1236
                }
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1237
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1238
                // compare parameters
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1239
                List<Type> tParams = tDesc.getParameterTypes();
38913
5b15a5ed3b0d 8144767: Fix handling of capture variables in most-specific test
dlsmith
parents: 36996
diff changeset
  1240
                List<Type> tParamsNoCapture = tDescNoCapture.getParameterTypes();
34995
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1241
                List<Type> sParams = sDesc.getParameterTypes();
38913
5b15a5ed3b0d 8144767: Fix handling of capture variables in most-specific test
dlsmith
parents: 36996
diff changeset
  1242
                while (tParams.nonEmpty() && tParamsNoCapture.nonEmpty() && sParams.nonEmpty()) {
34995
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1243
                    Type tParam = tParams.head;
38913
5b15a5ed3b0d 8144767: Fix handling of capture variables in most-specific test
dlsmith
parents: 36996
diff changeset
  1244
                    Type tParamNoCapture = types.subst(tParamsNoCapture.head, tTypeParamsNoCapture, tTypeParams);
34995
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1245
                    Type sParam = types.subst(sParams.head, sTypeParams, tTypeParams);
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1246
                    if (tParam.containsAny(tTypeParams) && inferenceContext().free(sParam)) {
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1247
                        return false;
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1248
                    }
38913
5b15a5ed3b0d 8144767: Fix handling of capture variables in most-specific test
dlsmith
parents: 36996
diff changeset
  1249
                    if (!types.isSubtype(inferenceContext().asUndetVar(sParam), tParam)) {
5b15a5ed3b0d 8144767: Fix handling of capture variables in most-specific test
dlsmith
parents: 36996
diff changeset
  1250
                        return false;
5b15a5ed3b0d 8144767: Fix handling of capture variables in most-specific test
dlsmith
parents: 36996
diff changeset
  1251
                    }
5b15a5ed3b0d 8144767: Fix handling of capture variables in most-specific test
dlsmith
parents: 36996
diff changeset
  1252
                    if (!types.isSameType(tParamNoCapture, inferenceContext().asUndetVar(sParam))) {
34995
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1253
                        return false;
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1254
                    }
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1255
                    tParams = tParams.tail;
38913
5b15a5ed3b0d 8144767: Fix handling of capture variables in most-specific test
dlsmith
parents: 36996
diff changeset
  1256
                    tParamsNoCapture = tParamsNoCapture.tail;
34995
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1257
                    sParams = sParams.tail;
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1258
                }
38913
5b15a5ed3b0d 8144767: Fix handling of capture variables in most-specific test
dlsmith
parents: 36996
diff changeset
  1259
                if (!tParams.isEmpty() || !tParamsNoCapture.isEmpty() || !sParams.isEmpty()) {
34995
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1260
                    return false;
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1261
                }
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1262
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1263
                // compare returns
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1264
                Type tRet = tDesc.getReturnType();
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1265
                Type sRet = types.subst(sDesc.getReturnType(), sTypeParams, tTypeParams);
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1266
                if (tRet.containsAny(tTypeParams) && inferenceContext().free(sRet)) {
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1267
                    return false;
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1268
                }
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1269
                MostSpecificFunctionReturnChecker msc = new MostSpecificFunctionReturnChecker(tRet, sRet);
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1270
                msc.scan(tree);
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1271
                return msc.result;
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1272
            }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1273
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1274
            /**
24404
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1275
             * 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
  1276
             * 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
  1277
             */
34995
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1278
            class MostSpecificFunctionReturnChecker extends DeferredAttr.PolyScanner {
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1279
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1280
                final Type tRet;
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1281
                final Type sRet;
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1282
                boolean result;
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1283
24404
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1284
                /** Parameters {@code t} and {@code s} are unrelated functional interface types. */
34995
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1285
                MostSpecificFunctionReturnChecker(Type tRet, Type sRet) {
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1286
                    this.tRet = tRet;
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1287
                    this.sRet = sRet;
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1288
                    result = true;
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1289
                }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1290
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1291
                @Override
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1292
                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
  1293
                    result &= false;
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1294
                }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1295
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1296
                @Override
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1297
                public void visitConditional(JCConditional tree) {
32709
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32545
diff changeset
  1298
                    scan(asExpr(tree.truepart));
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32545
diff changeset
  1299
                    scan(asExpr(tree.falsepart));
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1300
                }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1301
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1302
                @Override
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1303
                public void visitReference(JCMemberReference tree) {
34995
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1304
                    if (sRet.hasTag(VOID)) {
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1305
                        result &= true;
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1306
                    } else if (tRet.hasTag(VOID)) {
24404
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1307
                        result &= false;
34995
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1308
                    } else if (tRet.isPrimitive() != sRet.isPrimitive()) {
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1309
                        boolean retValIsPrimitive =
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1310
                                tree.refPolyKind == PolyKind.STANDALONE &&
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1311
                                tree.sym.type.getReturnType().isPrimitive();
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1312
                        result &= (retValIsPrimitive == tRet.isPrimitive()) &&
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1313
                                  (retValIsPrimitive != sRet.isPrimitive());
24404
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1314
                    } else {
34995
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1315
                        result &= compatibleBySubtyping(tRet, sRet);
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1316
                    }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1317
                }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1318
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1319
                @Override
32709
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32545
diff changeset
  1320
                public void visitParens(JCParens tree) {
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32545
diff changeset
  1321
                    scan(asExpr(tree.expr));
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32545
diff changeset
  1322
                }
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32545
diff changeset
  1323
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32545
diff changeset
  1324
                @Override
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1325
                public void visitLambda(JCLambda tree) {
34995
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1326
                    if (sRet.hasTag(VOID)) {
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1327
                        result &= true;
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1328
                    } else if (tRet.hasTag(VOID)) {
24404
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1329
                        result &= false;
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1330
                    } else {
34995
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1331
                        List<JCExpression> lambdaResults = lambdaResults(tree);
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1332
                        if (!lambdaResults.isEmpty() && unrelatedFunctionalInterfaces(tRet, sRet)) {
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1333
                            for (JCExpression expr : lambdaResults) {
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1334
                                result &= functionalInterfaceMostSpecific(tRet, sRet, expr);
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1335
                            }
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1336
                        } else if (!lambdaResults.isEmpty() && tRet.isPrimitive() != sRet.isPrimitive()) {
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1337
                            for (JCExpression expr : lambdaResults) {
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1338
                                boolean retValIsPrimitive = expr.isStandalone() && expr.type.isPrimitive();
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1339
                                result &= (retValIsPrimitive == tRet.isPrimitive()) &&
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1340
                                        (retValIsPrimitive != sRet.isPrimitive());
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1341
                            }
34992
eaba62b5d8e2 8037789: Surprising more-specific results for lambda bodies with no return expressions
dlsmith
parents: 34854
diff changeset
  1342
                        } else {
34995
e4e19f521ee7 8143852: Implement type variable renaming for functional interface most specific test
dlsmith
parents: 34992
diff changeset
  1343
                            result &= compatibleBySubtyping(tRet, sRet);
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1344
                        }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1345
                    }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1346
                }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1347
                //where
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1348
24404
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1349
                private List<JCExpression> lambdaResults(JCLambda lambda) {
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1350
                    if (lambda.getBodyKind() == JCTree.JCLambda.BodyKind.EXPRESSION) {
32709
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32545
diff changeset
  1351
                        return List.of(asExpr((JCExpression) lambda.body));
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1352
                    } else {
24404
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1353
                        final ListBuffer<JCExpression> buffer = new ListBuffer<>();
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1354
                        DeferredAttr.LambdaReturnScanner lambdaScanner =
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1355
                                new DeferredAttr.LambdaReturnScanner() {
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1356
                                    @Override
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1357
                                    public void visitReturn(JCReturn tree) {
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1358
                                        if (tree.expr != null) {
32709
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32545
diff changeset
  1359
                                            buffer.append(asExpr(tree.expr));
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1360
                                        }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1361
                                    }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1362
                                };
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1363
                        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
  1364
                        return buffer.toList();
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1365
                    }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1366
                }
32709
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32545
diff changeset
  1367
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32545
diff changeset
  1368
                private JCExpression asExpr(JCExpression expr) {
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32545
diff changeset
  1369
                    if (expr.type.hasTag(DEFERRED)) {
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32545
diff changeset
  1370
                        JCTree speculativeTree = ((DeferredType)expr.type).speculativeTree(deferredAttrContext);
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32545
diff changeset
  1371
                        if (speculativeTree != deferredAttr.stuckTree) {
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32545
diff changeset
  1372
                            expr = (JCExpression)speculativeTree;
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32545
diff changeset
  1373
                        }
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32545
diff changeset
  1374
                    }
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32545
diff changeset
  1375
                    return expr;
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32545
diff changeset
  1376
                }
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1377
            }
24404
cf534ffbc9d8 8034223: Most-specific should not have any special treatment for boxed vs. unboxed types
dlsmith
parents: 24400
diff changeset
  1378
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1379
        }
16805
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  1380
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents: 28456
diff changeset
  1381
        public MethodCheck mostSpecificCheck(List<Type> actuals) {
16805
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  1382
            Assert.error("Cannot get here!");
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  1383
            return null;
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  1384
        }
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1385
    }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1386
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
  1387
    public static class InapplicableMethodException extends RuntimeException {
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
  1388
        private static final long serialVersionUID = 0;
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
  1389
58350
e23e560afbcb 8231414: Mark various instance fields of javac internal types as transient
darcy
parents: 57963
diff changeset
  1390
        transient JCDiagnostic diagnostic;
49876
ac916fea6ec7 8202157: remove the use of string keys at InapplicableMethodException
vromero
parents: 49872
diff changeset
  1391
ac916fea6ec7 8202157: remove the use of string keys at InapplicableMethodException
vromero
parents: 49872
diff changeset
  1392
        InapplicableMethodException(JCDiagnostic diag) {
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
  1393
            this.diagnostic = diag;
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
  1394
        }
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
  1395
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
  1396
        public JCDiagnostic getDiagnostic() {
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
  1397
            return diagnostic;
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
  1398
        }
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
  1399
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1400
06bc494ca11e Initial load
duke
parents:
diff changeset
  1401
/* ***************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
  1402
 *  Symbol lookup
06bc494ca11e Initial load
duke
parents:
diff changeset
  1403
 *  the following naming conventions for arguments are used
06bc494ca11e Initial load
duke
parents:
diff changeset
  1404
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
  1405
 *       env      is the environment where the symbol was mentioned
06bc494ca11e Initial load
duke
parents:
diff changeset
  1406
 *       site     is the type of which the symbol is a member
06bc494ca11e Initial load
duke
parents:
diff changeset
  1407
 *       name     is the symbol's name
06bc494ca11e Initial load
duke
parents:
diff changeset
  1408
 *                if no arguments are given
06bc494ca11e Initial load
duke
parents:
diff changeset
  1409
 *       argtypes are the value arguments, if we search for a method
06bc494ca11e Initial load
duke
parents:
diff changeset
  1410
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
  1411
 *  If no symbol was found, a ResolveError detailing the problem is returned.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1412
 ****************************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
  1413
06bc494ca11e Initial load
duke
parents:
diff changeset
  1414
    /** Find field. Synthetic fields are always skipped.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1415
     *  @param env     The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1416
     *  @param site    The original type from where the selection takes place.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1417
     *  @param name    The name of the field.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1418
     *  @param c       The class to search for the field. This is always
06bc494ca11e Initial load
duke
parents:
diff changeset
  1419
     *                 a superclass or implemented interface of site's class.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1420
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1421
    Symbol findField(Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1422
                     Type site,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1423
                     Name name,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1424
                     TypeSymbol c) {
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14062
diff changeset
  1425
        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
  1426
            c = c.type.getUpperBound().tsym;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1427
        Symbol bestSoFar = varNotFound;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1428
        Symbol sym;
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1429
        for (Symbol s : c.members().getSymbolsByName(name)) {
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1430
            if (s.kind == VAR && (s.flags_field & SYNTHETIC) == 0) {
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1431
                return isAccessible(env, site, s)
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1432
                    ? s : new AccessError(env, site, s);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1433
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1434
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1435
        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
  1436
        if (st != null && (st.hasTag(CLASS) || st.hasTag(TYPEVAR))) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1437
            sym = findField(env, site, name, st.tsym);
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  1438
            bestSoFar = bestOf(bestSoFar, sym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1439
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1440
        for (List<Type> l = types.interfaces(c.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1441
             bestSoFar.kind != AMBIGUOUS && l.nonEmpty();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1442
             l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1443
            sym = findField(env, site, name, l.head.tsym);
18378
00267721fe3a 6360970: javac erroneously accept ambiguous field reference
mcimadamore
parents: 17998
diff changeset
  1444
            if (bestSoFar.exists() && sym.exists() &&
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1445
                sym.owner != bestSoFar.owner)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1446
                bestSoFar = new AmbiguityError(bestSoFar, sym);
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  1447
            else
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  1448
                bestSoFar = bestOf(bestSoFar, sym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1449
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1450
        return bestSoFar;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1451
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1452
06bc494ca11e Initial load
duke
parents:
diff changeset
  1453
    /** Resolve a field identifier, throw a fatal error if not found.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1454
     *  @param pos       The position to use for error reporting.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1455
     *  @param env       The environment current at the method invocation.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1456
     *  @param site      The type of the qualifying expression, in which
06bc494ca11e Initial load
duke
parents:
diff changeset
  1457
     *                   identifier is searched.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1458
     *  @param name      The identifier's name.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1459
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1460
    public VarSymbol resolveInternalField(DiagnosticPosition pos, Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1461
                                          Type site, Name name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1462
        Symbol sym = findField(env, site, name, site.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1463
        if (sym.kind == VAR) return (VarSymbol)sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1464
        else throw new FatalError(
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44504
diff changeset
  1465
                 diags.fragment(Fragments.FatalErrCantLocateField(name)));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1466
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1467
06bc494ca11e Initial load
duke
parents:
diff changeset
  1468
    /** Find unqualified variable or field with given name.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1469
     *  Synthetic fields always skipped.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1470
     *  @param env     The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1471
     *  @param name    The name of the variable or field.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1472
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1473
    Symbol findVar(Env<AttrContext> env, Name name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1474
        Symbol bestSoFar = varNotFound;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1475
        Env<AttrContext> env1 = env;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1476
        boolean staticOnly = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1477
        while (env1.outer != null) {
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1478
            Symbol sym = null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1479
            if (isStatic(env1)) staticOnly = true;
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1480
            for (Symbol s : env1.info.scope.getSymbolsByName(name)) {
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1481
                if (s.kind == VAR && (s.flags_field & SYNTHETIC) == 0) {
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1482
                    sym = s;
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1483
                    break;
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1484
                }
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1485
            }
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1486
            if (sym == null) {
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1487
                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
  1488
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1489
            if (sym.exists()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1490
                if (staticOnly &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1491
                    sym.kind == VAR &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1492
                    sym.owner.kind == TYP &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1493
                    (sym.flags() & STATIC) == 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1494
                    return new StaticError(sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1495
                else
06bc494ca11e Initial load
duke
parents:
diff changeset
  1496
                    return sym;
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  1497
            } else {
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  1498
                bestSoFar = bestOf(bestSoFar, sym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1499
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1500
06bc494ca11e Initial load
duke
parents:
diff changeset
  1501
            if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1502
            env1 = env1.outer;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1503
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1504
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1505
        Symbol sym = findField(env, syms.predefClass.type, name, syms.predefClass);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1506
        if (sym.exists())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1507
            return sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1508
        if (bestSoFar.exists())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1509
            return bestSoFar;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1510
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
  1511
        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
  1512
        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
  1513
            for (Symbol currentSymbol : sc.getSymbolsByName(name)) {
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1514
                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
  1515
                    continue;
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  1516
                // invariant: sym.kind == Symbol.Kind.VAR
29774
9d438163db79 8075184: javac is mistakenly considering an missing enclosing instance error as an overload error
vromero
parents: 29554
diff changeset
  1517
                if (!bestSoFar.kind.isResolutionError() &&
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  1518
                    currentSymbol.owner != bestSoFar.owner)
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1519
                    return new AmbiguityError(bestSoFar, currentSymbol);
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  1520
                else if (!bestSoFar.kind.betterThan(VAR)) {
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1521
                    origin = sc.getOrigin(currentSymbol).owner;
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1522
                    bestSoFar = isAccessible(env, origin.type, currentSymbol)
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1523
                        ? 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
  1524
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1525
            }
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
  1526
            if (bestSoFar.exists()) break;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1527
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1528
        if (bestSoFar.kind == VAR && bestSoFar.owner.type != origin.type)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1529
            return bestSoFar.clone(origin);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1530
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
  1531
            return bestSoFar;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1532
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1533
06bc494ca11e Initial load
duke
parents:
diff changeset
  1534
    Warner noteWarner = new Warner();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1535
06bc494ca11e Initial load
duke
parents:
diff changeset
  1536
    /** Select the best method for a call site among two choices.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1537
     *  @param env              The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1538
     *  @param site             The original type from where the
06bc494ca11e Initial load
duke
parents:
diff changeset
  1539
     *                          selection takes place.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1540
     *  @param argtypes         The invocation's value arguments,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1541
     *  @param typeargtypes     The invocation's type arguments,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1542
     *  @param sym              Proposed new best match.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1543
     *  @param bestSoFar        Previously found best match.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1544
     *  @param allowBoxing Allow boxing conversions of arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1545
     *  @param useVarargs Box trailing arguments into an array for varargs.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1546
     */
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
  1547
    @SuppressWarnings("fallthrough")
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1548
    Symbol selectBest(Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1549
                      Type site,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1550
                      List<Type> argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1551
                      List<Type> typeargtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1552
                      Symbol sym,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1553
                      Symbol bestSoFar,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1554
                      boolean allowBoxing,
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents: 28456
diff changeset
  1555
                      boolean useVarargs) {
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  1556
        if (sym.kind == ERR ||
47942
06652ffab955 8185451: Misleading 'cannot be accessed from outside package' diagnostic for inconsistent varargs override
vromero
parents: 47298
diff changeset
  1557
                (site.tsym != sym.owner && !sym.isInheritedIn(site.tsym, types)) ||
06652ffab955 8185451: Misleading 'cannot be accessed from outside package' diagnostic for inconsistent varargs override
vromero
parents: 47298
diff changeset
  1558
                !notOverriddenIn(site, sym)) {
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  1559
            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
  1560
        } else if (useVarargs && (sym.flags() & VARARGS) == 0) {
29774
9d438163db79 8075184: javac is mistakenly considering an missing enclosing instance error as an overload error
vromero
parents: 29554
diff changeset
  1561
            return bestSoFar.kind.isResolutionError() ?
24400
f05df7f58567 8037379: fix for JDK-8029569 doesn't cover all possible cases
vromero
parents: 24396
diff changeset
  1562
                    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
  1563
                    bestSoFar;
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  1564
        }
29774
9d438163db79 8075184: javac is mistakenly considering an missing enclosing instance error as an overload error
vromero
parents: 29554
diff changeset
  1565
        Assert.check(!sym.kind.isResolutionError());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1566
        try {
32545
9c4c47640408 8073594: javac, before calling rawInstantiate from selectBest the warner should be cleared out
vromero
parents: 32544
diff changeset
  1567
            types.noWarnings.clear();
12915
28cf1e0dafdc 7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents: 12335
diff changeset
  1568
            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
  1569
                               allowBoxing, useVarargs, types.noWarnings);
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents: 28456
diff changeset
  1570
            currentResolutionContext.addApplicableCandidate(sym, mt);
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
  1571
        } catch (InapplicableMethodException ex) {
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents: 28456
diff changeset
  1572
            currentResolutionContext.addInapplicableCandidate(sym, ex.getDiagnostic());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1573
            switch (bestSoFar.kind) {
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  1574
                case ABSENT_MTH:
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  1575
                    return new InapplicableSymbolError(currentResolutionContext);
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  1576
                case WRONG_MTH:
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  1577
                    bestSoFar = new InapplicableSymbolsError(currentResolutionContext);
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  1578
                default:
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  1579
                    return bestSoFar;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1580
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1581
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1582
        if (!isAccessible(env, site, sym)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1583
            return (bestSoFar.kind == ABSENT_MTH)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1584
                ? new AccessError(env, site, sym)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1585
                : bestSoFar;
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1586
        }
29774
9d438163db79 8075184: javac is mistakenly considering an missing enclosing instance error as an overload error
vromero
parents: 29554
diff changeset
  1587
        return (bestSoFar.kind.isResolutionError() && bestSoFar.kind != AMBIGUOUS)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1588
            ? sym
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents: 28456
diff changeset
  1589
            : mostSpecific(argtypes, sym, bestSoFar, env, site, useVarargs);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1590
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1591
06bc494ca11e Initial load
duke
parents:
diff changeset
  1592
    /* Return the most specific of the two methods for a call,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1593
     *  given that both are accessible and applicable.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1594
     *  @param m1               A new candidate for most specific.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1595
     *  @param m2               The previous most specific candidate.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1596
     *  @param env              The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1597
     *  @param site             The original type from where the selection
06bc494ca11e Initial load
duke
parents:
diff changeset
  1598
     *                          takes place.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1599
     *  @param allowBoxing Allow boxing conversions of arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1600
     *  @param useVarargs Box trailing arguments into an array for varargs.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1601
     */
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1602
    Symbol mostSpecific(List<Type> argtypes, Symbol m1,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1603
                        Symbol m2,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1604
                        Env<AttrContext> env,
2511
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
  1605
                        final Type site,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1606
                        boolean useVarargs) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1607
        switch (m2.kind) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1608
        case MTH:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1609
            if (m1 == m2) return m1;
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1610
            boolean m1SignatureMoreSpecific =
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents: 28456
diff changeset
  1611
                    signatureMoreSpecific(argtypes, env, site, m1, m2, useVarargs);
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1612
            boolean m2SignatureMoreSpecific =
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents: 28456
diff changeset
  1613
                    signatureMoreSpecific(argtypes, env, site, m2, m1, useVarargs);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1614
            if (m1SignatureMoreSpecific && m2SignatureMoreSpecific) {
7623
66d98f7ba8c7 5088429: varargs overloading problem
mcimadamore
parents: 7209
diff changeset
  1615
                Type mt1 = types.memberType(site, m1);
66d98f7ba8c7 5088429: varargs overloading problem
mcimadamore
parents: 7209
diff changeset
  1616
                Type mt2 = types.memberType(site, m2);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1617
                if (!types.overrideEquivalent(mt1, mt2))
8228
4e6ee38974b2 6910550: javac 1.5.0_17 fails with incorrect error message
mcimadamore
parents: 8045
diff changeset
  1618
                    return ambiguityError(m1, m2);
4e6ee38974b2 6910550: javac 1.5.0_17 fails with incorrect error message
mcimadamore
parents: 8045
diff changeset
  1619
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1620
                // same signature; select (a) the non-bridge method, or
06bc494ca11e Initial load
duke
parents:
diff changeset
  1621
                // (b) the one that overrides the other, or (c) the concrete
06bc494ca11e Initial load
duke
parents:
diff changeset
  1622
                // 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
  1623
                if ((m1.flags() & BRIDGE) != (m2.flags() & BRIDGE))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1624
                    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
  1625
48459
a5f815d1060b 8194932: no ambuguity error is emitted if classfile contains two identical methods with different return types
mcimadamore
parents: 48054
diff changeset
  1626
                if (m1.baseSymbol() == m2.baseSymbol()) {
a5f815d1060b 8194932: no ambuguity error is emitted if classfile contains two identical methods with different return types
mcimadamore
parents: 48054
diff changeset
  1627
                    // this is the same imported symbol which has been cloned twice.
a5f815d1060b 8194932: no ambuguity error is emitted if classfile contains two identical methods with different return types
mcimadamore
parents: 48054
diff changeset
  1628
                    // Return the first one (either will do).
a5f815d1060b 8194932: no ambuguity error is emitted if classfile contains two identical methods with different return types
mcimadamore
parents: 48054
diff changeset
  1629
                    return m1;
a5f815d1060b 8194932: no ambuguity error is emitted if classfile contains two identical methods with different return types
mcimadamore
parents: 48054
diff changeset
  1630
                }
a5f815d1060b 8194932: no ambuguity error is emitted if classfile contains two identical methods with different return types
mcimadamore
parents: 48054
diff changeset
  1631
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1632
                // if one overrides or hides the other, use it
06bc494ca11e Initial load
duke
parents:
diff changeset
  1633
                TypeSymbol m1Owner = (TypeSymbol)m1.owner;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1634
                TypeSymbol m2Owner = (TypeSymbol)m2.owner;
48459
a5f815d1060b 8194932: no ambuguity error is emitted if classfile contains two identical methods with different return types
mcimadamore
parents: 48054
diff changeset
  1635
                // the two owners can never be the same if the target methods are compiled from source,
a5f815d1060b 8194932: no ambuguity error is emitted if classfile contains two identical methods with different return types
mcimadamore
parents: 48054
diff changeset
  1636
                // but we need to protect against cases where the methods are defined in some classfile
a5f815d1060b 8194932: no ambuguity error is emitted if classfile contains two identical methods with different return types
mcimadamore
parents: 48054
diff changeset
  1637
                // and make sure we issue an ambiguity error accordingly (by skipping the logic below).
a5f815d1060b 8194932: no ambuguity error is emitted if classfile contains two identical methods with different return types
mcimadamore
parents: 48054
diff changeset
  1638
                if (m1Owner != m2Owner) {
a5f815d1060b 8194932: no ambuguity error is emitted if classfile contains two identical methods with different return types
mcimadamore
parents: 48054
diff changeset
  1639
                    if (types.asSuper(m1Owner.type, m2Owner) != null &&
a5f815d1060b 8194932: no ambuguity error is emitted if classfile contains two identical methods with different return types
mcimadamore
parents: 48054
diff changeset
  1640
                        ((m1.owner.flags_field & INTERFACE) == 0 ||
a5f815d1060b 8194932: no ambuguity error is emitted if classfile contains two identical methods with different return types
mcimadamore
parents: 48054
diff changeset
  1641
                         (m2.owner.flags_field & INTERFACE) != 0) &&
a5f815d1060b 8194932: no ambuguity error is emitted if classfile contains two identical methods with different return types
mcimadamore
parents: 48054
diff changeset
  1642
                        m1.overrides(m2, m1Owner, types, false))
a5f815d1060b 8194932: no ambuguity error is emitted if classfile contains two identical methods with different return types
mcimadamore
parents: 48054
diff changeset
  1643
                        return m1;
a5f815d1060b 8194932: no ambuguity error is emitted if classfile contains two identical methods with different return types
mcimadamore
parents: 48054
diff changeset
  1644
                    if (types.asSuper(m2Owner.type, m1Owner) != null &&
a5f815d1060b 8194932: no ambuguity error is emitted if classfile contains two identical methods with different return types
mcimadamore
parents: 48054
diff changeset
  1645
                        ((m2.owner.flags_field & INTERFACE) == 0 ||
a5f815d1060b 8194932: no ambuguity error is emitted if classfile contains two identical methods with different return types
mcimadamore
parents: 48054
diff changeset
  1646
                         (m1.owner.flags_field & INTERFACE) != 0) &&
a5f815d1060b 8194932: no ambuguity error is emitted if classfile contains two identical methods with different return types
mcimadamore
parents: 48054
diff changeset
  1647
                        m2.overrides(m1, m2Owner, types, false))
a5f815d1060b 8194932: no ambuguity error is emitted if classfile contains two identical methods with different return types
mcimadamore
parents: 48054
diff changeset
  1648
                        return m2;
a5f815d1060b 8194932: no ambuguity error is emitted if classfile contains two identical methods with different return types
mcimadamore
parents: 48054
diff changeset
  1649
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1650
                boolean m1Abstract = (m1.flags() & ABSTRACT) != 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1651
                boolean m2Abstract = (m2.flags() & ABSTRACT) != 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1652
                if (m1Abstract && !m2Abstract) return m2;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1653
                if (m2Abstract && !m1Abstract) return m1;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1654
                // both abstract or both concrete
15038
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  1655
                return ambiguityError(m1, m2);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1656
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1657
            if (m1SignatureMoreSpecific) return m1;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1658
            if (m2SignatureMoreSpecific) return m2;
8228
4e6ee38974b2 6910550: javac 1.5.0_17 fails with incorrect error message
mcimadamore
parents: 8045
diff changeset
  1659
            return ambiguityError(m1, m2);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1660
        case AMBIGUOUS:
24393
381ade32a034 8041663: Sensitive dependence on location of nested interface
jlahoda
parents: 24293
diff changeset
  1661
            //compare m1 to ambiguous methods in m2
22161
d79fb23b5dca 8029569: internal javac cast exception when resolving varargs ambiguity
vromero
parents: 21718
diff changeset
  1662
            AmbiguityError e = (AmbiguityError)m2.baseSymbol();
24393
381ade32a034 8041663: Sensitive dependence on location of nested interface
jlahoda
parents: 24293
diff changeset
  1663
            boolean m1MoreSpecificThanAnyAmbiguous = true;
381ade32a034 8041663: Sensitive dependence on location of nested interface
jlahoda
parents: 24293
diff changeset
  1664
            boolean allAmbiguousMoreSpecificThanM1 = true;
15038
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  1665
            for (Symbol s : e.ambiguousSyms) {
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents: 28456
diff changeset
  1666
                Symbol moreSpecific = mostSpecific(argtypes, m1, s, env, site, useVarargs);
24393
381ade32a034 8041663: Sensitive dependence on location of nested interface
jlahoda
parents: 24293
diff changeset
  1667
                m1MoreSpecificThanAnyAmbiguous &= moreSpecific == m1;
381ade32a034 8041663: Sensitive dependence on location of nested interface
jlahoda
parents: 24293
diff changeset
  1668
                allAmbiguousMoreSpecificThanM1 &= moreSpecific == s;
15038
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  1669
            }
24393
381ade32a034 8041663: Sensitive dependence on location of nested interface
jlahoda
parents: 24293
diff changeset
  1670
            if (m1MoreSpecificThanAnyAmbiguous)
381ade32a034 8041663: Sensitive dependence on location of nested interface
jlahoda
parents: 24293
diff changeset
  1671
                return m1;
381ade32a034 8041663: Sensitive dependence on location of nested interface
jlahoda
parents: 24293
diff changeset
  1672
            //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
  1673
            //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
  1674
            if (!allAmbiguousMoreSpecificThanM1)
381ade32a034 8041663: Sensitive dependence on location of nested interface
jlahoda
parents: 24293
diff changeset
  1675
                e.addAmbiguousSymbol(m1);
381ade32a034 8041663: Sensitive dependence on location of nested interface
jlahoda
parents: 24293
diff changeset
  1676
            return e;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1677
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1678
            throw new AssertionError();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1679
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1680
    }
7623
66d98f7ba8c7 5088429: varargs overloading problem
mcimadamore
parents: 7209
diff changeset
  1681
    //where
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents: 28456
diff changeset
  1682
    private boolean signatureMoreSpecific(List<Type> actuals, Env<AttrContext> env, Type site, Symbol m1, Symbol m2, boolean useVarargs) {
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1683
        noteWarner.clear();
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1684
        int maxLength = Math.max(
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1685
                            Math.max(m1.type.getParameterTypes().length(), actuals.length()),
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1686
                            m2.type.getParameterTypes().length());
16805
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  1687
        MethodResolutionContext prevResolutionContext = currentResolutionContext;
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  1688
        try {
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  1689
            currentResolutionContext = new MethodResolutionContext();
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  1690
            currentResolutionContext.step = prevResolutionContext.step;
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  1691
            currentResolutionContext.methodCheck =
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents: 28456
diff changeset
  1692
                    prevResolutionContext.methodCheck.mostSpecificCheck(actuals);
16805
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  1693
            Type mst = instantiate(env, site, m2, null,
24293
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24226
diff changeset
  1694
                    adjustArgs(types.cvarLowerBounds(types.memberType(site, m1).getParameterTypes()), m1, maxLength, useVarargs), null,
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents: 28456
diff changeset
  1695
                    false, useVarargs, noteWarner);
16805
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  1696
            return mst != null &&
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  1697
                    !noteWarner.hasLint(Lint.LintCategory.UNCHECKED);
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  1698
        } finally {
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  1699
            currentResolutionContext = prevResolutionContext;
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  1700
        }
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1701
    }
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19652
diff changeset
  1702
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19652
diff changeset
  1703
    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
  1704
        if ((msym.flags() & VARARGS) != 0 && allowVarargs) {
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1705
            Type varargsElem = types.elemtype(args.last());
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1706
            if (varargsElem == null) {
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1707
                Assert.error("Bad varargs = " + args.last() + " " + msym);
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1708
            }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1709
            List<Type> newArgs = args.reverse().tail.prepend(varargsElem).reverse();
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1710
            while (newArgs.length() < length) {
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1711
                newArgs = newArgs.append(newArgs.last());
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1712
            }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1713
            return newArgs;
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1714
        } else {
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15362
diff changeset
  1715
            return args;
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
  1716
        }
7623
66d98f7ba8c7 5088429: varargs overloading problem
mcimadamore
parents: 7209
diff changeset
  1717
    }
66d98f7ba8c7 5088429: varargs overloading problem
mcimadamore
parents: 7209
diff changeset
  1718
    //where
8228
4e6ee38974b2 6910550: javac 1.5.0_17 fails with incorrect error message
mcimadamore
parents: 8045
diff changeset
  1719
    Symbol ambiguityError(Symbol m1, Symbol m2) {
4e6ee38974b2 6910550: javac 1.5.0_17 fails with incorrect error message
mcimadamore
parents: 8045
diff changeset
  1720
        if (((m1.flags() | m2.flags()) & CLASH) != 0) {
4e6ee38974b2 6910550: javac 1.5.0_17 fails with incorrect error message
mcimadamore
parents: 8045
diff changeset
  1721
            return (m1.flags() & CLASH) == 0 ? m1 : m2;
4e6ee38974b2 6910550: javac 1.5.0_17 fails with incorrect error message
mcimadamore
parents: 8045
diff changeset
  1722
        } else {
4e6ee38974b2 6910550: javac 1.5.0_17 fails with incorrect error message
mcimadamore
parents: 8045
diff changeset
  1723
            return new AmbiguityError(m1, m2);
4e6ee38974b2 6910550: javac 1.5.0_17 fails with incorrect error message
mcimadamore
parents: 8045
diff changeset
  1724
        }
4e6ee38974b2 6910550: javac 1.5.0_17 fails with incorrect error message
mcimadamore
parents: 8045
diff changeset
  1725
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1726
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  1727
    Symbol findMethodInScope(Env<AttrContext> env,
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1728
            Type site,
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1729
            Name name,
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1730
            List<Type> argtypes,
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1731
            List<Type> typeargtypes,
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1732
            Scope sc,
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1733
            Symbol bestSoFar,
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1734
            boolean allowBoxing,
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1735
            boolean useVarargs,
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1736
            boolean abstractok) {
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1737
        for (Symbol s : sc.getSymbolsByName(name, new LookupFilter(abstractok))) {
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1738
            bestSoFar = selectBest(env, site, argtypes, typeargtypes, s,
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents: 28456
diff changeset
  1739
                    bestSoFar, allowBoxing, useVarargs);
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1740
        }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1741
        return bestSoFar;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1742
    }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1743
    //where
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1744
        class LookupFilter implements Filter<Symbol> {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1745
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1746
            boolean abstractOk;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1747
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1748
            LookupFilter(boolean abstractOk) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1749
                this.abstractOk = abstractOk;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1750
            }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1751
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1752
            public boolean accepts(Symbol s) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1753
                long flags = s.flags();
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1754
                return s.kind == MTH &&
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1755
                        (flags & SYNTHETIC) == 0 &&
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1756
                        (abstractOk ||
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1757
                        (flags & DEFAULT) != 0 ||
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1758
                        (flags & ABSTRACT) == 0);
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1759
            }
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22161
diff changeset
  1760
        }
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1761
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1762
    /** Find best qualified method matching given name, type and value
06bc494ca11e Initial load
duke
parents:
diff changeset
  1763
     *  arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1764
     *  @param env       The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1765
     *  @param site      The original type from where the selection
06bc494ca11e Initial load
duke
parents:
diff changeset
  1766
     *                   takes place.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1767
     *  @param name      The method's name.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1768
     *  @param argtypes  The method's value arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1769
     *  @param typeargtypes The method's type arguments
06bc494ca11e Initial load
duke
parents:
diff changeset
  1770
     *  @param allowBoxing Allow boxing conversions of arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1771
     *  @param useVarargs Box trailing arguments into an array for varargs.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1772
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1773
    Symbol findMethod(Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1774
                      Type site,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1775
                      Name name,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1776
                      List<Type> argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1777
                      List<Type> typeargtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1778
                      boolean allowBoxing,
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents: 28456
diff changeset
  1779
                      boolean useVarargs) {
5736
ee0850472ca1 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 5321
diff changeset
  1780
        Symbol bestSoFar = methodNotFound;
10816
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
  1781
        bestSoFar = findMethod(env,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1782
                          site,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1783
                          name,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1784
                          argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1785
                          typeargtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1786
                          site.tsym.type,
5736
ee0850472ca1 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 5321
diff changeset
  1787
                          bestSoFar,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1788
                          allowBoxing,
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents: 28456
diff changeset
  1789
                          useVarargs);
10816
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
  1790
        return bestSoFar;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1791
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1792
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
  1793
    private Symbol findMethod(Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1794
                              Type site,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1795
                              Name name,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1796
                              List<Type> argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1797
                              List<Type> typeargtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1798
                              Type intype,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1799
                              Symbol bestSoFar,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1800
                              boolean allowBoxing,
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents: 28456
diff changeset
  1801
                              boolean useVarargs) {
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1802
        @SuppressWarnings({"unchecked","rawtypes"})
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1803
        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
  1804
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1805
        InterfaceLookupPhase iphase = InterfaceLookupPhase.ABSTRACT_OK;
14045
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1806
        for (TypeSymbol s : superclasses(intype)) {
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  1807
            bestSoFar = findMethodInScope(env, site, name, argtypes, typeargtypes,
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents: 28456
diff changeset
  1808
                    s.members(), bestSoFar, allowBoxing, useVarargs, true);
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1809
            if (name == names.init) return bestSoFar;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1810
            iphase = (iphase == null) ? null : iphase.update(s, this);
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1811
            if (iphase != null) {
14045
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1812
                for (Type itype : types.interfaces(s.type)) {
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1813
                    itypes[iphase.ordinal()] = types.union(types.closure(itype), itypes[iphase.ordinal()]);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1814
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1815
            }
14045
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1816
        }
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1817
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  1818
        Symbol concrete = bestSoFar.kind.isValid() &&
14045
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1819
                (bestSoFar.flags() & ABSTRACT) == 0 ?
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1820
                bestSoFar : methodNotFound;
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1821
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1822
        for (InterfaceLookupPhase iphase2 : InterfaceLookupPhase.values()) {
14045
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1823
            //keep searching for abstract methods
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1824
            for (Type itype : itypes[iphase2.ordinal()]) {
14045
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1825
                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
  1826
                if (iphase2 == InterfaceLookupPhase.DEFAULT_OK &&
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1827
                        (itype.tsym.flags() & DEFAULT) == 0) continue;
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  1828
                bestSoFar = findMethodInScope(env, site, name, argtypes, typeargtypes,
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents: 28456
diff changeset
  1829
                        itype.tsym.members(), bestSoFar, allowBoxing, useVarargs, true);
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1830
                if (concrete != bestSoFar &&
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  1831
                    concrete.kind.isValid() &&
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  1832
                    bestSoFar.kind.isValid() &&
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1833
                        types.isSubSignature(concrete.type, bestSoFar.type)) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1834
                    //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
  1835
                    //most specific ends up comparing abstract methods that might have
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1836
                    //been implemented by some concrete method in a subclass and,
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1837
                    //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
  1838
                    //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
  1839
                    //to explicitly call that out (see CR 6178365)
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1840
                    bestSoFar = concrete;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1841
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1842
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1843
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1844
        return bestSoFar;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1845
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1846
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1847
    enum InterfaceLookupPhase {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1848
        ABSTRACT_OK() {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1849
            @Override
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1850
            InterfaceLookupPhase update(Symbol s, Resolve rs) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1851
                //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
  1852
                //(as concrete classes are expected to implement all abstracts coming
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1853
                //from superinterfaces)
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1854
                if ((s.flags() & (ABSTRACT | INTERFACE | ENUM)) != 0) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1855
                    return this;
23105
1d1ea26a10ae 8030855: Default methods should be visible under source previous to 8
vromero
parents: 22442
diff changeset
  1856
                } else {
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1857
                    return DEFAULT_OK;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1858
                }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1859
            }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1860
        },
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1861
        DEFAULT_OK() {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1862
            @Override
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1863
            InterfaceLookupPhase update(Symbol s, Resolve rs) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1864
                return this;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1865
            }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1866
        };
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1867
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1868
        abstract InterfaceLookupPhase update(Symbol s, Resolve rs);
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1869
    }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  1870
14045
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1871
    /**
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1872
     * Return an Iterable object to scan the superclasses of a given type.
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1873
     * 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
  1874
     * access more supertypes than strictly needed (as this could trigger completion
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1875
     * errors if some of the not-needed supertypes are missing/ill-formed).
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1876
     */
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1877
    Iterable<TypeSymbol> superclasses(final Type intype) {
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42824
diff changeset
  1878
        return () -> new Iterator<TypeSymbol>() {
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42824
diff changeset
  1879
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42824
diff changeset
  1880
            List<TypeSymbol> seen = List.nil();
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42824
diff changeset
  1881
            TypeSymbol currentSym = symbolFor(intype);
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42824
diff changeset
  1882
            TypeSymbol prevSym = null;
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42824
diff changeset
  1883
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42824
diff changeset
  1884
            public boolean hasNext() {
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42824
diff changeset
  1885
                if (currentSym == syms.noSymbol) {
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42824
diff changeset
  1886
                    currentSym = symbolFor(types.supertype(prevSym.type));
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42824
diff changeset
  1887
                }
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42824
diff changeset
  1888
                return currentSym != null;
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42824
diff changeset
  1889
            }
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42824
diff changeset
  1890
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42824
diff changeset
  1891
            public TypeSymbol next() {
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42824
diff changeset
  1892
                prevSym = currentSym;
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42824
diff changeset
  1893
                currentSym = syms.noSymbol;
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42824
diff changeset
  1894
                Assert.check(prevSym != null || prevSym != syms.noSymbol);
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42824
diff changeset
  1895
                return prevSym;
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42824
diff changeset
  1896
            }
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42824
diff changeset
  1897
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42824
diff changeset
  1898
            public void remove() {
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42824
diff changeset
  1899
                throw new UnsupportedOperationException();
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42824
diff changeset
  1900
            }
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42824
diff changeset
  1901
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42824
diff changeset
  1902
            TypeSymbol symbolFor(Type t) {
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42824
diff changeset
  1903
                if (!t.hasTag(CLASS) &&
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42824
diff changeset
  1904
                        !t.hasTag(TYPEVAR)) {
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42824
diff changeset
  1905
                    return null;
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42824
diff changeset
  1906
                }
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42824
diff changeset
  1907
                t = types.skipTypeVars(t, false);
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42824
diff changeset
  1908
                if (seen.contains(t.tsym)) {
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42824
diff changeset
  1909
                    //degenerate case in which we have a circular
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42824
diff changeset
  1910
                    //class hierarchy - because of ill-formed classfiles
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42824
diff changeset
  1911
                    return null;
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42824
diff changeset
  1912
                }
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42824
diff changeset
  1913
                seen = seen.prepend(t.tsym);
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42824
diff changeset
  1914
                return t.tsym;
14045
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1915
            }
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1916
        };
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1917
    }
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1918
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1919
    /** Find unqualified method matching given name, type and value arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1920
     *  @param env       The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1921
     *  @param name      The method's name.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1922
     *  @param argtypes  The method's value arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1923
     *  @param typeargtypes  The method's type arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1924
     *  @param allowBoxing Allow boxing conversions of arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1925
     *  @param useVarargs Box trailing arguments into an array for varargs.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1926
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1927
    Symbol findFun(Env<AttrContext> env, Name name,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1928
                   List<Type> argtypes, List<Type> typeargtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1929
                   boolean allowBoxing, boolean useVarargs) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1930
        Symbol bestSoFar = methodNotFound;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1931
        Env<AttrContext> env1 = env;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1932
        boolean staticOnly = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1933
        while (env1.outer != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1934
            if (isStatic(env1)) staticOnly = true;
34854
d90aa0a83be4 8145466: javac: No line numbers in compilation error
sadayapalam
parents: 34566
diff changeset
  1935
            Assert.check(env1.info.preferredTreeForDiagnostics == null);
d90aa0a83be4 8145466: javac: No line numbers in compilation error
sadayapalam
parents: 34566
diff changeset
  1936
            env1.info.preferredTreeForDiagnostics = env.tree;
d90aa0a83be4 8145466: javac: No line numbers in compilation error
sadayapalam
parents: 34566
diff changeset
  1937
            try {
d90aa0a83be4 8145466: javac: No line numbers in compilation error
sadayapalam
parents: 34566
diff changeset
  1938
                Symbol sym = findMethod(
d90aa0a83be4 8145466: javac: No line numbers in compilation error
sadayapalam
parents: 34566
diff changeset
  1939
                    env1, env1.enclClass.sym.type, name, argtypes, typeargtypes,
d90aa0a83be4 8145466: javac: No line numbers in compilation error
sadayapalam
parents: 34566
diff changeset
  1940
                    allowBoxing, useVarargs);
d90aa0a83be4 8145466: javac: No line numbers in compilation error
sadayapalam
parents: 34566
diff changeset
  1941
                if (sym.exists()) {
d90aa0a83be4 8145466: javac: No line numbers in compilation error
sadayapalam
parents: 34566
diff changeset
  1942
                    if (staticOnly &&
d90aa0a83be4 8145466: javac: No line numbers in compilation error
sadayapalam
parents: 34566
diff changeset
  1943
                        sym.kind == MTH &&
d90aa0a83be4 8145466: javac: No line numbers in compilation error
sadayapalam
parents: 34566
diff changeset
  1944
                        sym.owner.kind == TYP &&
d90aa0a83be4 8145466: javac: No line numbers in compilation error
sadayapalam
parents: 34566
diff changeset
  1945
                        (sym.flags() & STATIC) == 0) return new StaticError(sym);
d90aa0a83be4 8145466: javac: No line numbers in compilation error
sadayapalam
parents: 34566
diff changeset
  1946
                    else return sym;
d90aa0a83be4 8145466: javac: No line numbers in compilation error
sadayapalam
parents: 34566
diff changeset
  1947
                } else {
d90aa0a83be4 8145466: javac: No line numbers in compilation error
sadayapalam
parents: 34566
diff changeset
  1948
                    bestSoFar = bestOf(bestSoFar, sym);
d90aa0a83be4 8145466: javac: No line numbers in compilation error
sadayapalam
parents: 34566
diff changeset
  1949
                }
d90aa0a83be4 8145466: javac: No line numbers in compilation error
sadayapalam
parents: 34566
diff changeset
  1950
            } finally {
d90aa0a83be4 8145466: javac: No line numbers in compilation error
sadayapalam
parents: 34566
diff changeset
  1951
                env1.info.preferredTreeForDiagnostics = null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1952
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1953
            if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1954
            env1 = env1.outer;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1955
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1956
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1957
        Symbol sym = findMethod(env, syms.predefClass.type, name, argtypes,
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents: 28456
diff changeset
  1958
                                typeargtypes, allowBoxing, useVarargs);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1959
        if (sym.exists())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1960
            return sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1961
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1962
        for (Symbol currentSym : env.toplevel.namedImportScope.getSymbolsByName(name)) {
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1963
            Symbol origin = env.toplevel.namedImportScope.getOrigin(currentSym).owner;
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1964
            if (currentSym.kind == MTH) {
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1965
                if (currentSym.owner.type != origin.type)
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1966
                    currentSym = currentSym.clone(origin);
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1967
                if (!isAccessible(env, origin.type, currentSym))
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1968
                    currentSym = new AccessError(env, origin.type, currentSym);
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1969
                bestSoFar = selectBest(env, origin.type,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1970
                                       argtypes, typeargtypes,
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1971
                                       currentSym, bestSoFar,
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents: 28456
diff changeset
  1972
                                       allowBoxing, useVarargs);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1973
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1974
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1975
        if (bestSoFar.exists())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1976
            return bestSoFar;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1977
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1978
        for (Symbol currentSym : env.toplevel.starImportScope.getSymbolsByName(name)) {
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1979
            Symbol origin = env.toplevel.starImportScope.getOrigin(currentSym).owner;
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1980
            if (currentSym.kind == MTH) {
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1981
                if (currentSym.owner.type != origin.type)
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1982
                    currentSym = currentSym.clone(origin);
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1983
                if (!isAccessible(env, origin.type, currentSym))
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1984
                    currentSym = new AccessError(env, origin.type, currentSym);
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1985
                bestSoFar = selectBest(env, origin.type,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1986
                                       argtypes, typeargtypes,
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  1987
                                       currentSym, bestSoFar,
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents: 28456
diff changeset
  1988
                                       allowBoxing, useVarargs);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1989
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1990
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1991
        return bestSoFar;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1992
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1993
06bc494ca11e Initial load
duke
parents:
diff changeset
  1994
    /** Load toplevel or member class with given fully qualified name and
06bc494ca11e Initial load
duke
parents:
diff changeset
  1995
     *  verify that it is accessible.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1996
     *  @param env       The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1997
     *  @param name      The fully qualified name of the class to be loaded.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1998
     */
43272
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  1999
    Symbol loadClass(Env<AttrContext> env, Name name, RecoveryLoadClass recoveryLoadClass) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2000
        try {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36278
diff changeset
  2001
            ClassSymbol c = finder.loadClass(env.toplevel.modle, name);
43138
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  2002
            return isAccessible(env, c) ? c : new AccessError(env, null, c);
24604
7f68545b5128 8041422: Split javac ClassReader into ClassReader+ClassFinder
jjg
parents: 24404
diff changeset
  2003
        } catch (ClassFinder.BadClassFile err) {
50374
2d0647b9ac18 8198378: javac crash when checking imports and a broken class is present
jlahoda
parents: 50287
diff changeset
  2004
            return new BadClassFileError(err);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2005
        } catch (CompletionFailure ex) {
42824
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 41943
diff changeset
  2006
            Symbol candidate = recoveryLoadClass.loadClass(env, name);
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 41943
diff changeset
  2007
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 41943
diff changeset
  2008
            if (candidate != null) {
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 41943
diff changeset
  2009
                return candidate;
41931
d7c9720c4223 8166538: Improve error reporting for compiling against unexported package
jlahoda
parents: 41526
diff changeset
  2010
            }
42824
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 41943
diff changeset
  2011
41931
d7c9720c4223 8166538: Improve error reporting for compiling against unexported package
jlahoda
parents: 41526
diff changeset
  2012
            return typeNotFound;
d7c9720c4223 8166538: Improve error reporting for compiling against unexported package
jlahoda
parents: 41526
diff changeset
  2013
        }
d7c9720c4223 8166538: Improve error reporting for compiling against unexported package
jlahoda
parents: 41526
diff changeset
  2014
    }
d7c9720c4223 8166538: Improve error reporting for compiling against unexported package
jlahoda
parents: 41526
diff changeset
  2015
43272
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2016
    public interface RecoveryLoadClass {
42824
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 41943
diff changeset
  2017
        Symbol loadClass(Env<AttrContext> env, Name name);
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 41943
diff changeset
  2018
    }
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 41943
diff changeset
  2019
43272
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2020
    private final RecoveryLoadClass noRecovery = (env, name) -> null;
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2021
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2022
    private final RecoveryLoadClass doRecoveryLoadClass = new RecoveryLoadClass() {
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2023
        @Override public Symbol loadClass(Env<AttrContext> env, Name name) {
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2024
            List<Name> candidates = Convert.classCandidates(name);
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2025
            return lookupInvisibleSymbol(env, name,
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2026
                                         n -> () -> createCompoundIterator(candidates,
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2027
                                                                           c -> syms.getClassesForName(c)
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2028
                                                                                    .iterator()),
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2029
                                         (ms, n) -> {
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2030
                for (Name candidate : candidates) {
43138
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  2031
                    try {
43272
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2032
                        return finder.loadClass(ms, candidate);
43138
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  2033
                    } catch (CompletionFailure cf) {
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  2034
                        //ignore
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  2035
                    }
43272
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2036
                }
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2037
                return null;
50539
7bf4f1b5e438 8193302: Javac AssertionError: typeSig ERROR on usage of @Generated
jlahoda
parents: 50374
diff changeset
  2038
            }, sym -> sym.kind == Kind.TYP, typeNotFound);
43138
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  2039
        }
42824
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 41943
diff changeset
  2040
    };
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 41943
diff changeset
  2041
43272
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2042
    private final RecoveryLoadClass namedImportScopeRecovery = (env, name) -> {
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2043
        Scope importScope = env.toplevel.namedImportScope;
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2044
        Symbol existing = importScope.findFirst(Convert.shortName(name),
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2045
                                                sym -> sym.kind == TYP && sym.flatName() == name);
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2046
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2047
        if (existing != null) {
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2048
            return new InvisibleSymbolError(env, true, existing);
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2049
        }
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2050
        return null;
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2051
    };
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2052
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2053
    private final RecoveryLoadClass starImportScopeRecovery = (env, name) -> {
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2054
        Scope importScope = env.toplevel.starImportScope;
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2055
        Symbol existing = importScope.findFirst(Convert.shortName(name),
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2056
                                                sym -> sym.kind == TYP && sym.flatName() == name);
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2057
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2058
        if (existing != null) {
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2059
            try {
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2060
                existing = finder.loadClass(existing.packge().modle, name);
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2061
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2062
                return new InvisibleSymbolError(env, true, existing);
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2063
            } catch (CompletionFailure cf) {
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2064
                //ignore
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2065
            }
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2066
        }
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2067
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2068
        return null;
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2069
    };
42824
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 41943
diff changeset
  2070
43138
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  2071
    Symbol lookupPackage(Env<AttrContext> env, Name name) {
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  2072
        PackageSymbol pack = syms.lookupPackage(env.toplevel.modle, name);
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  2073
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  2074
        if (allowModules && isImportOnDemand(env, name)) {
50539
7bf4f1b5e438 8193302: Javac AssertionError: typeSig ERROR on usage of @Generated
jlahoda
parents: 50374
diff changeset
  2075
            if (pack.members().isEmpty()) {
43272
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2076
                return lookupInvisibleSymbol(env, name, syms::getPackagesForName, syms::enterPackage, sym -> {
43138
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  2077
                    sym.complete();
50539
7bf4f1b5e438 8193302: Javac AssertionError: typeSig ERROR on usage of @Generated
jlahoda
parents: 50374
diff changeset
  2078
                    return !sym.members().isEmpty();
7bf4f1b5e438 8193302: Javac AssertionError: typeSig ERROR on usage of @Generated
jlahoda
parents: 50374
diff changeset
  2079
                }, pack);
43138
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  2080
            }
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  2081
        }
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  2082
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  2083
        return pack;
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  2084
    }
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  2085
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  2086
    private boolean isImportOnDemand(Env<AttrContext> env, Name name) {
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  2087
        if (!env.tree.hasTag(IMPORT))
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  2088
            return false;
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  2089
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  2090
        JCTree qualid = ((JCImport) env.tree).qualid;
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  2091
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  2092
        if (!qualid.hasTag(SELECT))
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  2093
            return false;
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  2094
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  2095
        if (TreeInfo.name(qualid) != names.asterisk)
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  2096
            return false;
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  2097
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  2098
        return TreeInfo.fullName(((JCFieldAccess) qualid).selected) == name;
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  2099
    }
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  2100
43272
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2101
    private <S extends Symbol> Symbol lookupInvisibleSymbol(Env<AttrContext> env,
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2102
                                                            Name name,
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2103
                                                            Function<Name, Iterable<S>> get,
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2104
                                                            BiFunction<ModuleSymbol, Name, S> load,
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2105
                                                            Predicate<S> validate,
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2106
                                                            Symbol defaultResult) {
43138
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  2107
        //even if a class/package cannot be found in the current module and among packages in modules
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  2108
        //it depends on that are exported for any or this module, the class/package may exist internally
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  2109
        //in some of these modules, or may exist in a module on which this module does not depend.
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  2110
        //Provide better diagnostic in such cases by looking for the class in any module:
43272
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2111
        Iterable<? extends S> candidates = get.apply(name);
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2112
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2113
        for (S sym : candidates) {
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2114
            if (validate.test(sym))
50539
7bf4f1b5e438 8193302: Javac AssertionError: typeSig ERROR on usage of @Generated
jlahoda
parents: 50374
diff changeset
  2115
                return createInvisibleSymbolError(env, sym);
43272
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2116
        }
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2117
43138
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  2118
        Set<ModuleSymbol> recoverableModules = new HashSet<>(syms.getAllModules());
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  2119
49579
fce4252d5227 8189765: Improve error reporting for compiling against package not visible due to modules
jlahoda
parents: 49197
diff changeset
  2120
        recoverableModules.add(syms.unnamedModule);
43138
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  2121
        recoverableModules.remove(env.toplevel.modle);
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  2122
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  2123
        for (ModuleSymbol ms : recoverableModules) {
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  2124
            //avoid overly eager completing classes from source-based modules, as those
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  2125
            //may not be completable with the current compiler settings:
43272
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2126
            if (ms.sourceLocation == null) {
43138
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  2127
                if (ms.classLocation == null) {
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  2128
                    ms = moduleFinder.findModule(ms);
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  2129
                }
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  2130
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  2131
                if (ms.kind != ERR) {
43272
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2132
                    S sym = load.apply(ms, name);
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2133
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2134
                    if (sym != null && validate.test(sym)) {
50539
7bf4f1b5e438 8193302: Javac AssertionError: typeSig ERROR on usage of @Generated
jlahoda
parents: 50374
diff changeset
  2135
                        return createInvisibleSymbolError(env, sym);
43272
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2136
                    }
43138
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  2137
                }
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  2138
            }
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  2139
        }
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  2140
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  2141
        return defaultResult;
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  2142
    }
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  2143
50539
7bf4f1b5e438 8193302: Javac AssertionError: typeSig ERROR on usage of @Generated
jlahoda
parents: 50374
diff changeset
  2144
    private Symbol createInvisibleSymbolError(Env<AttrContext> env, Symbol sym) {
43866
3195ea126044 8175007: Incorrect error messages for inaccessible classes in visible packages
jlahoda
parents: 43865
diff changeset
  2145
        if (symbolPackageVisible(env, sym)) {
3195ea126044 8175007: Incorrect error messages for inaccessible classes in visible packages
jlahoda
parents: 43865
diff changeset
  2146
            return new AccessError(env, null, sym);
3195ea126044 8175007: Incorrect error messages for inaccessible classes in visible packages
jlahoda
parents: 43865
diff changeset
  2147
        } else {
50539
7bf4f1b5e438 8193302: Javac AssertionError: typeSig ERROR on usage of @Generated
jlahoda
parents: 50374
diff changeset
  2148
            return new InvisibleSymbolError(env, false, sym);
43866
3195ea126044 8175007: Incorrect error messages for inaccessible classes in visible packages
jlahoda
parents: 43865
diff changeset
  2149
        }
3195ea126044 8175007: Incorrect error messages for inaccessible classes in visible packages
jlahoda
parents: 43865
diff changeset
  2150
    }
3195ea126044 8175007: Incorrect error messages for inaccessible classes in visible packages
jlahoda
parents: 43865
diff changeset
  2151
3195ea126044 8175007: Incorrect error messages for inaccessible classes in visible packages
jlahoda
parents: 43865
diff changeset
  2152
    private boolean symbolPackageVisible(Env<AttrContext> env, Symbol sym) {
3195ea126044 8175007: Incorrect error messages for inaccessible classes in visible packages
jlahoda
parents: 43865
diff changeset
  2153
        ModuleSymbol envMod = env.toplevel.modle;
3195ea126044 8175007: Incorrect error messages for inaccessible classes in visible packages
jlahoda
parents: 43865
diff changeset
  2154
        PackageSymbol symPack = sym.packge();
3195ea126044 8175007: Incorrect error messages for inaccessible classes in visible packages
jlahoda
parents: 43865
diff changeset
  2155
        return envMod == symPack.modle ||
3195ea126044 8175007: Incorrect error messages for inaccessible classes in visible packages
jlahoda
parents: 43865
diff changeset
  2156
               envMod.visiblePackages.containsKey(symPack.fullname);
3195ea126044 8175007: Incorrect error messages for inaccessible classes in visible packages
jlahoda
parents: 43865
diff changeset
  2157
    }
3195ea126044 8175007: Incorrect error messages for inaccessible classes in visible packages
jlahoda
parents: 43865
diff changeset
  2158
19652
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2159
    /**
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2160
     * 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
  2161
     * if none is found.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2162
     *  @param env       The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2163
     *  @param site      The original type from where the selection takes
06bc494ca11e Initial load
duke
parents:
diff changeset
  2164
     *                   place.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2165
     *  @param name      The type's name.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2166
     *  @param c         The class to search for the member type. This is
06bc494ca11e Initial load
duke
parents:
diff changeset
  2167
     *                   always a superclass or implemented interface of
06bc494ca11e Initial load
duke
parents:
diff changeset
  2168
     *                   site's class.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2169
     */
19652
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2170
    Symbol findImmediateMemberType(Env<AttrContext> env,
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2171
                                   Type site,
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2172
                                   Name name,
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2173
                                   TypeSymbol c) {
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  2174
        for (Symbol sym : c.members().getSymbolsByName(name)) {
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  2175
            if (sym.kind == TYP) {
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  2176
                return isAccessible(env, site, sym)
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  2177
                    ? sym
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  2178
                    : new AccessError(env, site, sym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2179
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2180
        }
19652
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2181
        return typeNotFound;
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2182
    }
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2183
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2184
    /** Find a member type inherited from a superclass or interface.
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2185
     *  @param env       The current environment.
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2186
     *  @param site      The original type from where the selection takes
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2187
     *                   place.
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2188
     *  @param name      The type's name.
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2189
     *  @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
  2190
     *                   always a superclass or implemented interface of
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2191
     *                   site's class.
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2192
     */
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2193
    Symbol findInheritedMemberType(Env<AttrContext> env,
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2194
                                   Type site,
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2195
                                   Name name,
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2196
                                   TypeSymbol c) {
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2197
        Symbol bestSoFar = typeNotFound;
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2198
        Symbol sym;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2199
        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
  2200
        if (st != null && st.hasTag(CLASS)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2201
            sym = findMemberType(env, site, name, st.tsym);
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2202
            bestSoFar = bestOf(bestSoFar, sym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2203
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2204
        for (List<Type> l = types.interfaces(c.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2205
             bestSoFar.kind != AMBIGUOUS && l.nonEmpty();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2206
             l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2207
            sym = findMemberType(env, site, name, l.head.tsym);
29774
9d438163db79 8075184: javac is mistakenly considering an missing enclosing instance error as an overload error
vromero
parents: 29554
diff changeset
  2208
            if (!bestSoFar.kind.isResolutionError() &&
9d438163db79 8075184: javac is mistakenly considering an missing enclosing instance error as an overload error
vromero
parents: 29554
diff changeset
  2209
                !sym.kind.isResolutionError() &&
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2210
                sym.owner != bestSoFar.owner)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2211
                bestSoFar = new AmbiguityError(bestSoFar, sym);
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2212
            else
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2213
                bestSoFar = bestOf(bestSoFar, sym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2214
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2215
        return bestSoFar;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2216
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2217
19652
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2218
    /** Find qualified member type.
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2219
     *  @param env       The current environment.
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2220
     *  @param site      The original type from where the selection takes
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2221
     *                   place.
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2222
     *  @param name      The type's name.
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2223
     *  @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
  2224
     *                   always a superclass or implemented interface of
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2225
     *                   site's class.
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2226
     */
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2227
    Symbol findMemberType(Env<AttrContext> env,
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2228
                          Type site,
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2229
                          Name name,
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2230
                          TypeSymbol c) {
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2231
        Symbol sym = findImmediateMemberType(env, site, name, c);
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2232
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2233
        if (sym != typeNotFound)
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2234
            return sym;
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2235
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2236
        return findInheritedMemberType(env, site, name, c);
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2237
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2238
    }
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2239
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2240
    /** Find a global type in given scope and load corresponding class.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2241
     *  @param env       The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2242
     *  @param scope     The scope in which to look for the type.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2243
     *  @param name      The type's name.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2244
     */
43272
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2245
    Symbol findGlobalType(Env<AttrContext> env, Scope scope, Name name, RecoveryLoadClass recoveryLoadClass) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2246
        Symbol bestSoFar = typeNotFound;
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  2247
        for (Symbol s : scope.getSymbolsByName(name)) {
43272
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2248
            Symbol sym = loadClass(env, s.flatName(), recoveryLoadClass);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2249
            if (bestSoFar.kind == TYP && sym.kind == TYP &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  2250
                bestSoFar != sym)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2251
                return new AmbiguityError(bestSoFar, sym);
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2252
            else
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2253
                bestSoFar = bestOf(bestSoFar, sym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2254
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2255
        return bestSoFar;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2256
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2257
19652
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2258
    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
  2259
        for (Symbol sym : env.info.scope.getSymbolsByName(name)) {
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  2260
            if (sym.kind == TYP) {
19652
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2261
                if (staticOnly &&
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  2262
                    sym.type.hasTag(TYPEVAR) &&
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  2263
                    sym.owner.kind == TYP)
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  2264
                    return new StaticError(sym);
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  2265
                return sym;
19652
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2266
            }
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2267
        }
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2268
        return typeNotFound;
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2269
    }
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2270
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2271
    /** Find an unqualified type symbol.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2272
     *  @param env       The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2273
     *  @param name      The type's name.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2274
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2275
    Symbol findType(Env<AttrContext> env, Name name) {
30404
952a476681b2 8078473: javac diamond finder crashes when used to build java.base module.
sadayapalam
parents: 29958
diff changeset
  2276
        if (name == names.empty)
952a476681b2 8078473: javac diamond finder crashes when used to build java.base module.
sadayapalam
parents: 29958
diff changeset
  2277
            return typeNotFound; // do not allow inadvertent "lookup" of anonymous types
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2278
        Symbol bestSoFar = typeNotFound;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2279
        Symbol sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2280
        boolean staticOnly = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2281
        for (Env<AttrContext> env1 = env; env1.outer != null; env1 = env1.outer) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2282
            if (isStatic(env1)) staticOnly = true;
19652
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2283
            // 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
  2284
            final Symbol tyvar = findTypeVar(env1, name, staticOnly);
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2285
            sym = findImmediateMemberType(env1, env1.enclClass.sym.type,
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2286
                                          name, env1.enclClass.sym);
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2287
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2288
            // 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
  2289
            // immediate member, OR the type variable is for a method.
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2290
            if (tyvar != typeNotFound) {
27386
784414cffd9a 8035259: javac, incorrect shadowing of classes vs type parameters
emc
parents: 27224
diff changeset
  2291
                if (env.baseClause || sym == typeNotFound ||
19652
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2292
                    (tyvar.kind == TYP && tyvar.exists() &&
27386
784414cffd9a 8035259: javac, incorrect shadowing of classes vs type parameters
emc
parents: 27224
diff changeset
  2293
                     tyvar.owner.kind == MTH)) {
19652
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2294
                    return tyvar;
27386
784414cffd9a 8035259: javac, incorrect shadowing of classes vs type parameters
emc
parents: 27224
diff changeset
  2295
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2296
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2297
19652
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2298
            // If the environment is a class def, finish up,
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2299
            // otherwise, do the entire findMemberType
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2300
            if (sym == typeNotFound)
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2301
                sym = findInheritedMemberType(env1, env1.enclClass.sym.type,
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2302
                                              name, env1.enclClass.sym);
1186c404a994 7118412: Shadowing of type-variables vs. member types
emc
parents: 19492
diff changeset
  2303
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2304
            if (staticOnly && sym.kind == TYP &&
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14062
diff changeset
  2305
                sym.type.hasTag(CLASS) &&
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14062
diff changeset
  2306
                sym.type.getEnclosingType().hasTag(CLASS) &&
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2307
                env1.enclClass.sym.type.isParameterized() &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  2308
                sym.type.getEnclosingType().isParameterized())
06bc494ca11e Initial load
duke
parents:
diff changeset
  2309
                return new StaticError(sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2310
            else if (sym.exists()) return sym;
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2311
            else bestSoFar = bestOf(bestSoFar, sym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2312
06bc494ca11e Initial load
duke
parents:
diff changeset
  2313
            JCClassDecl encl = env1.baseClause ? (JCClassDecl)env1.tree : env1.enclClass;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2314
            if ((encl.sym.flags() & STATIC) != 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2315
                staticOnly = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2316
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2317
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10816
diff changeset
  2318
        if (!env.tree.hasTag(IMPORT)) {
43272
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2319
            sym = findGlobalType(env, env.toplevel.namedImportScope, name, namedImportScopeRecovery);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2320
            if (sym.exists()) return sym;
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2321
            else bestSoFar = bestOf(bestSoFar, sym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2322
50287
64c880300d9b 8193717: Import resolution performance regression in JDK 9
jlahoda
parents: 50250
diff changeset
  2323
            sym = findGlobalType(env, env.toplevel.toplevelScope, name, noRecovery);
64c880300d9b 8193717: Import resolution performance regression in JDK 9
jlahoda
parents: 50250
diff changeset
  2324
            if (sym.exists()) return sym;
64c880300d9b 8193717: Import resolution performance regression in JDK 9
jlahoda
parents: 50250
diff changeset
  2325
            else bestSoFar = bestOf(bestSoFar, sym);
64c880300d9b 8193717: Import resolution performance regression in JDK 9
jlahoda
parents: 50250
diff changeset
  2326
43272
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2327
            sym = findGlobalType(env, env.toplevel.packge.members(), name, noRecovery);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2328
            if (sym.exists()) return sym;
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2329
            else bestSoFar = bestOf(bestSoFar, sym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2330
43272
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2331
            sym = findGlobalType(env, env.toplevel.starImportScope, name, starImportScopeRecovery);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2332
            if (sym.exists()) return sym;
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2333
            else bestSoFar = bestOf(bestSoFar, sym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2334
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2335
06bc494ca11e Initial load
duke
parents:
diff changeset
  2336
        return bestSoFar;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2337
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2338
06bc494ca11e Initial load
duke
parents:
diff changeset
  2339
    /** Find an unqualified identifier which matches a specified kind set.
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 51047
diff changeset
  2340
     *  @param pos       position on which report warnings, if any;
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 51047
diff changeset
  2341
     *                   null warnings should not be reported
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2342
     *  @param env       The current environment.
14541
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14446
diff changeset
  2343
     *  @param name      The identifier's name.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2344
     *  @param kind      Indicates the possible symbol kinds
06bc494ca11e Initial load
duke
parents:
diff changeset
  2345
     *                   (a subset of VAL, TYP, PCK).
06bc494ca11e Initial load
duke
parents:
diff changeset
  2346
     */
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 51047
diff changeset
  2347
    Symbol findIdent(DiagnosticPosition pos, Env<AttrContext> env, Name name, KindSelector kind) {
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 51047
diff changeset
  2348
        return checkRestrictedType(pos, findIdentInternal(env, name, kind), name);
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
  2349
    }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
  2350
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
  2351
    Symbol findIdentInternal(Env<AttrContext> env, Name name, KindSelector kind) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2352
        Symbol bestSoFar = typeNotFound;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2353
        Symbol sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2354
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2355
        if (kind.contains(KindSelector.VAL)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2356
            sym = findVar(env, name);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2357
            if (sym.exists()) return sym;
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2358
            else bestSoFar = bestOf(bestSoFar, sym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2359
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2360
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2361
        if (kind.contains(KindSelector.TYP)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2362
            sym = findType(env, name);
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2363
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2364
            if (sym.exists()) return sym;
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2365
            else bestSoFar = bestOf(bestSoFar, sym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2366
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2367
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2368
        if (kind.contains(KindSelector.PCK))
43138
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  2369
            return lookupPackage(env, name);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2370
        else return bestSoFar;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2371
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2372
06bc494ca11e Initial load
duke
parents:
diff changeset
  2373
    /** Find an identifier in a package which matches a specified kind set.
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 51047
diff changeset
  2374
     *  @param pos       position on which report warnings, if any;
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 51047
diff changeset
  2375
     *                   null warnings should not be reported
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2376
     *  @param env       The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2377
     *  @param name      The identifier's name.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2378
     *  @param kind      Indicates the possible symbol kinds
06bc494ca11e Initial load
duke
parents:
diff changeset
  2379
     *                   (a nonempty subset of TYP, PCK).
06bc494ca11e Initial load
duke
parents:
diff changeset
  2380
     */
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 51047
diff changeset
  2381
    Symbol findIdentInPackage(DiagnosticPosition pos,
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 51047
diff changeset
  2382
                              Env<AttrContext> env, TypeSymbol pck,
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2383
                              Name name, KindSelector kind) {
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 51047
diff changeset
  2384
        return checkRestrictedType(pos, findIdentInPackageInternal(env, pck, name, kind), name);
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
  2385
    }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
  2386
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
  2387
    Symbol findIdentInPackageInternal(Env<AttrContext> env, TypeSymbol pck,
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
  2388
                              Name name, KindSelector kind) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2389
        Name fullname = TypeSymbol.formFullName(name, pck);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2390
        Symbol bestSoFar = typeNotFound;
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2391
        if (kind.contains(KindSelector.TYP)) {
43272
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2392
            RecoveryLoadClass recoveryLoadClass =
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2393
                    allowModules && !kind.contains(KindSelector.PCK) &&
57963
ed7eb20871c5 8230105: -XDfind=diamond crashes
jlahoda
parents: 55306
diff changeset
  2394
                    !pck.exists() && !env.info.attributionMode.isSpeculative ?
43272
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2395
                        doRecoveryLoadClass : noRecovery;
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  2396
            Symbol sym = loadClass(env, fullname, recoveryLoadClass);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2397
            if (sym.exists()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2398
                // don't allow programs to use flatnames
06bc494ca11e Initial load
duke
parents:
diff changeset
  2399
                if (name == sym.name) return sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2400
            }
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2401
            else bestSoFar = bestOf(bestSoFar, sym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2402
        }
43138
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  2403
        if (kind.contains(KindSelector.PCK)) {
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  2404
            return lookupPackage(env, fullname);
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  2405
        }
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  2406
        return bestSoFar;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2407
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2408
06bc494ca11e Initial load
duke
parents:
diff changeset
  2409
    /** Find an identifier among the members of a given type `site'.
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 51047
diff changeset
  2410
     *  @param pos       position on which report warnings, if any;
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 51047
diff changeset
  2411
     *                   null warnings should not be reported
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2412
     *  @param env       The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2413
     *  @param site      The type containing the symbol to be found.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2414
     *  @param name      The identifier's name.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2415
     *  @param kind      Indicates the possible symbol kinds
06bc494ca11e Initial load
duke
parents:
diff changeset
  2416
     *                   (a subset of VAL, TYP).
06bc494ca11e Initial load
duke
parents:
diff changeset
  2417
     */
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 51047
diff changeset
  2418
    Symbol findIdentInType(DiagnosticPosition pos,
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 51047
diff changeset
  2419
                           Env<AttrContext> env, Type site,
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2420
                           Name name, KindSelector kind) {
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 51047
diff changeset
  2421
        return checkRestrictedType(pos, findIdentInTypeInternal(env, site, name, kind), name);
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
  2422
    }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
  2423
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
  2424
    Symbol findIdentInTypeInternal(Env<AttrContext> env, Type site,
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
  2425
                           Name name, KindSelector kind) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2426
        Symbol bestSoFar = typeNotFound;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2427
        Symbol sym;
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2428
        if (kind.contains(KindSelector.VAL)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2429
            sym = findField(env, site, name, site.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2430
            if (sym.exists()) return sym;
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2431
            else bestSoFar = bestOf(bestSoFar, sym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2432
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2433
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2434
        if (kind.contains(KindSelector.TYP)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2435
            sym = findMemberType(env, site, name, site.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2436
            if (sym.exists()) return sym;
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2437
            else bestSoFar = bestOf(bestSoFar, sym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2438
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2439
        return bestSoFar;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2440
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2441
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 51047
diff changeset
  2442
    private Symbol checkRestrictedType(DiagnosticPosition pos, Symbol bestSoFar, Name name) {
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 51047
diff changeset
  2443
        if (bestSoFar.kind == TYP || bestSoFar.kind == ABSENT_TYP) {
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 51047
diff changeset
  2444
            if (allowLocalVariableTypeInference && name.equals(names.var)) {
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 51047
diff changeset
  2445
                bestSoFar = new BadRestrictedTypeError(names.var);
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 51047
diff changeset
  2446
            } else if (name.equals(names.yield)) {
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 51047
diff changeset
  2447
                if (allowYieldStatement) {
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 51047
diff changeset
  2448
                    bestSoFar = new BadRestrictedTypeError(names.yield);
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 51047
diff changeset
  2449
                } else if (pos != null) {
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 51047
diff changeset
  2450
                    log.warning(pos, Warnings.IllegalRefToRestrictedType(names.yield));
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 51047
diff changeset
  2451
                }
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 51047
diff changeset
  2452
            }
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
  2453
        }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
  2454
        return bestSoFar;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
  2455
    }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
  2456
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2457
/* ***************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
  2458
 *  Access checking
06bc494ca11e Initial load
duke
parents:
diff changeset
  2459
 *  The following methods convert ResolveErrors to ErrorSymbols, issuing
06bc494ca11e Initial load
duke
parents:
diff changeset
  2460
 *  an error message in the process
06bc494ca11e Initial load
duke
parents:
diff changeset
  2461
 ****************************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
  2462
06bc494ca11e Initial load
duke
parents:
diff changeset
  2463
    /** If `sym' is a bad symbol: report error and return errSymbol
06bc494ca11e Initial load
duke
parents:
diff changeset
  2464
     *  else pass through unchanged,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2465
     *  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
  2466
     *  symbol {@literal (--> flyweight pattern)}. This improves performance since we
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2467
     *  expect misses to happen frequently.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2468
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  2469
     *  @param sym       The symbol that was found, or a ResolveError.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2470
     *  @param pos       The position to use for error reporting.
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2471
     *  @param location  The symbol the served as a context for this lookup
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2472
     *  @param site      The original type from where the selection took place.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2473
     *  @param name      The symbol's name.
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2474
     *  @param qualified Did we get here through a qualified expression resolution?
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2475
     *  @param argtypes  The invocation's value arguments,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2476
     *                   if we looked for a method.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2477
     *  @param typeargtypes  The invocation's type arguments,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2478
     *                   if we looked for a method.
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2479
     *  @param logResolveHelper helper class used to log resolve errors
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2480
     */
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2481
    Symbol accessInternal(Symbol sym,
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2482
                  DiagnosticPosition pos,
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2483
                  Symbol location,
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2484
                  Type site,
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2485
                  Name name,
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2486
                  boolean qualified,
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2487
                  List<Type> argtypes,
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2488
                  List<Type> typeargtypes,
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2489
                  LogResolveHelper logResolveHelper) {
29774
9d438163db79 8075184: javac is mistakenly considering an missing enclosing instance error as an overload error
vromero
parents: 29554
diff changeset
  2490
        if (sym.kind.isResolutionError()) {
24400
f05df7f58567 8037379: fix for JDK-8029569 doesn't cover all possible cases
vromero
parents: 24396
diff changeset
  2491
            ResolveError errSym = (ResolveError)sym.baseSymbol();
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2492
            sym = errSym.access(name, qualified ? site.tsym : syms.noSymbol);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2493
            argtypes = logResolveHelper.getArgumentTypes(errSym, sym, name, argtypes);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2494
            if (logResolveHelper.resolveDiagnosticNeeded(site, argtypes, typeargtypes)) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2495
                logResolveError(errSym, pos, location, site, name, argtypes, typeargtypes);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2496
            }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2497
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2498
        return sym;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2499
    }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2500
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2501
    /**
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2502
     * 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
  2503
     * resolution diagnostics
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2504
     */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2505
    Symbol accessMethod(Symbol sym,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2506
                  DiagnosticPosition pos,
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2507
                  Symbol location,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2508
                  Type site,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2509
                  Name name,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2510
                  boolean qualified,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2511
                  List<Type> argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2512
                  List<Type> typeargtypes) {
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2513
        return accessInternal(sym, pos, location, site, name, qualified, argtypes, typeargtypes, methodLogResolveHelper);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2514
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2515
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2516
    /** Same as original accessMethod(), but without location.
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2517
     */
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2518
    Symbol accessMethod(Symbol sym,
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2519
                  DiagnosticPosition pos,
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2520
                  Type site,
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2521
                  Name name,
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2522
                  boolean qualified,
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2523
                  List<Type> argtypes,
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2524
                  List<Type> typeargtypes) {
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2525
        return accessMethod(sym, pos, site.tsym, site, name, qualified, argtypes, typeargtypes);
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2526
    }
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2527
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2528
    /**
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2529
     * 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
  2530
     * type resolution diagnostics
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2531
     */
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2532
    Symbol accessBase(Symbol sym,
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2533
                  DiagnosticPosition pos,
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2534
                  Symbol location,
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2535
                  Type site,
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2536
                  Name name,
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2537
                  boolean qualified) {
42828
cce89649f958 8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents: 42827
diff changeset
  2538
        return accessInternal(sym, pos, location, site, name, qualified, List.nil(), null, basicLogResolveHelper);
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2539
    }
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2540
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2541
    /** Same as original accessBase(), but without location.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2542
     */
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2543
    Symbol accessBase(Symbol sym,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2544
                  DiagnosticPosition pos,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2545
                  Type site,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2546
                  Name name,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2547
                  boolean qualified) {
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2548
        return accessBase(sym, pos, site.tsym, site, name, qualified);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2549
    }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2550
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2551
    interface LogResolveHelper {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2552
        boolean resolveDiagnosticNeeded(Type site, List<Type> argtypes, List<Type> typeargtypes);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2553
        List<Type> getArgumentTypes(ResolveError errSym, Symbol accessedSym, Name name, List<Type> argtypes);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2554
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2555
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2556
    LogResolveHelper basicLogResolveHelper = new LogResolveHelper() {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2557
        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
  2558
            return !site.isErroneous();
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2559
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2560
        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
  2561
            return argtypes;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2562
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2563
    };
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2564
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2565
    LogResolveHelper methodLogResolveHelper = new LogResolveHelper() {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2566
        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
  2567
            return !site.isErroneous() &&
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2568
                        !Type.isErroneous(argtypes) &&
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2569
                        (typeargtypes == null || !Type.isErroneous(typeargtypes));
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2570
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2571
        public List<Type> getArgumentTypes(ResolveError errSym, Symbol accessedSym, Name name, List<Type> argtypes) {
29554
6d7957bd6866 8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents: 29292
diff changeset
  2572
            return argtypes.map(new ResolveDeferredRecoveryMap(AttrMode.SPECULATIVE, accessedSym, currentResolutionContext.step));
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2573
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2574
    };
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2575
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19652
diff changeset
  2576
    class ResolveDeferredRecoveryMap extends DeferredAttr.RecoveryDeferredTypeMap {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19652
diff changeset
  2577
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19652
diff changeset
  2578
        public ResolveDeferredRecoveryMap(AttrMode mode, Symbol msym, MethodResolutionPhase step) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19652
diff changeset
  2579
            deferredAttr.super(mode, msym, step);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19652
diff changeset
  2580
        }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19652
diff changeset
  2581
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19652
diff changeset
  2582
        @Override
50898
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50675
diff changeset
  2583
        protected Type typeOf(DeferredType dt, Type pt) {
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50675
diff changeset
  2584
            Type res = super.typeOf(dt, pt);
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19652
diff changeset
  2585
            if (!res.isErroneous()) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19652
diff changeset
  2586
                switch (TreeInfo.skipParens(dt.tree).getTag()) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19652
diff changeset
  2587
                    case LAMBDA:
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19652
diff changeset
  2588
                    case REFERENCE:
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19652
diff changeset
  2589
                        return dt;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19652
diff changeset
  2590
                    case CONDEXPR:
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19652
diff changeset
  2591
                        return res == Type.recoveryType ?
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19652
diff changeset
  2592
                                dt : res;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19652
diff changeset
  2593
                }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19652
diff changeset
  2594
            }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19652
diff changeset
  2595
            return res;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19652
diff changeset
  2596
        }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19652
diff changeset
  2597
    }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19652
diff changeset
  2598
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2599
    /** Check that sym is not an abstract method.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2600
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2601
    void checkNonAbstract(DiagnosticPosition pos, Symbol sym) {
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  2602
        if ((sym.flags() & ABSTRACT) != 0 && (sym.flags() & DEFAULT) == 0)
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44504
diff changeset
  2603
            log.error(pos,
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44504
diff changeset
  2604
                      Errors.AbstractCantBeAccessedDirectly(kindName(sym),sym, sym.location()));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2605
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2606
06bc494ca11e Initial load
duke
parents:
diff changeset
  2607
/* ***************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
  2608
 *  Name resolution
06bc494ca11e Initial load
duke
parents:
diff changeset
  2609
 *  Naming conventions are as for symbol lookup
06bc494ca11e Initial load
duke
parents:
diff changeset
  2610
 *  Unlike the find... methods these methods will report access errors
06bc494ca11e Initial load
duke
parents:
diff changeset
  2611
 ****************************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
  2612
06bc494ca11e Initial load
duke
parents:
diff changeset
  2613
    /** Resolve an unqualified (non-method) identifier.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2614
     *  @param pos       The position to use for error reporting.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2615
     *  @param env       The environment current at the identifier use.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2616
     *  @param name      The identifier's name.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2617
     *  @param kind      The set of admissible symbol kinds for the identifier.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2618
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2619
    Symbol resolveIdent(DiagnosticPosition pos, Env<AttrContext> env,
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2620
                        Name name, KindSelector kind) {
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2621
        return accessBase(
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 51047
diff changeset
  2622
            findIdent(pos, env, name, kind),
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2623
            pos, env.enclClass.sym.type, name, false);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2624
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2625
06bc494ca11e Initial load
duke
parents:
diff changeset
  2626
    /** Resolve an unqualified method identifier.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2627
     *  @param pos       The position to use for error reporting.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2628
     *  @param env       The environment current at the method invocation.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2629
     *  @param name      The identifier's name.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2630
     *  @param argtypes  The types of the invocation's value arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2631
     *  @param typeargtypes  The types of the invocation's type arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2632
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2633
    Symbol resolveMethod(DiagnosticPosition pos,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2634
                         Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2635
                         Name name,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2636
                         List<Type> argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2637
                         List<Type> typeargtypes) {
16805
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  2638
        return lookupMethod(env, pos, env.enclClass.sym, resolveMethodCheck,
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  2639
                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
  2640
                    @Override
18668
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  2641
                    Symbol doLookup(Env<AttrContext> env, MethodResolutionPhase phase) {
16805
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  2642
                        return findFun(env, name, argtypes, typeargtypes,
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  2643
                                phase.isBoxingRequired(),
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  2644
                                phase.isVarargsRequired());
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  2645
                    }});
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
  2646
    }
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
  2647
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2648
    /** Resolve a qualified method identifier
06bc494ca11e Initial load
duke
parents:
diff changeset
  2649
     *  @param pos       The position to use for error reporting.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2650
     *  @param env       The environment current at the method invocation.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2651
     *  @param site      The type of the qualifying expression, in which
06bc494ca11e Initial load
duke
parents:
diff changeset
  2652
     *                   identifier is searched.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2653
     *  @param name      The identifier's name.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2654
     *  @param argtypes  The types of the invocation's value arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2655
     *  @param typeargtypes  The types of the invocation's type arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2656
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2657
    Symbol resolveQualifiedMethod(DiagnosticPosition pos, Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2658
                                  Type site, Name name, List<Type> argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2659
                                  List<Type> typeargtypes) {
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2660
        return resolveQualifiedMethod(pos, env, site.tsym, site, name, argtypes, typeargtypes);
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2661
    }
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2662
    Symbol resolveQualifiedMethod(DiagnosticPosition pos, Env<AttrContext> env,
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2663
                                  Symbol location, Type site, Name name, List<Type> argtypes,
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2664
                                  List<Type> typeargtypes) {
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2665
        return resolveQualifiedMethod(new MethodResolutionContext(), pos, env, location, site, name, argtypes, typeargtypes);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2666
    }
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2667
    private Symbol resolveQualifiedMethod(MethodResolutionContext resolveContext,
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2668
                                  DiagnosticPosition pos, Env<AttrContext> env,
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2669
                                  Symbol location, Type site, Name name, List<Type> argtypes,
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2670
                                  List<Type> typeargtypes) {
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2671
        return lookupMethod(env, pos, location, resolveContext, new BasicLookupHelper(name, site, argtypes, typeargtypes) {
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2672
            @Override
18668
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  2673
            Symbol doLookup(Env<AttrContext> env, MethodResolutionPhase phase) {
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2674
                return findMethod(env, site, name, argtypes, typeargtypes,
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2675
                        phase.isBoxingRequired(),
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents: 28456
diff changeset
  2676
                        phase.isVarargsRequired());
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2677
            }
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2678
            @Override
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2679
            Symbol access(Env<AttrContext> env, DiagnosticPosition pos, Symbol location, Symbol sym) {
29774
9d438163db79 8075184: javac is mistakenly considering an missing enclosing instance error as an overload error
vromero
parents: 29554
diff changeset
  2680
                if (sym.kind.isResolutionError()) {
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2681
                    sym = super.access(env, pos, location, sym);
51047
860a3648c494 8028563: Remove javac support for 6/1.6 source and target values
darcy
parents: 50898
diff changeset
  2682
                } else {
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2683
                    MethodSymbol msym = (MethodSymbol)sym;
18389
a425d0819f36 8016569: javac, add new flag for polymorphic method signatures
vromero
parents: 18378
diff changeset
  2684
                    if ((msym.flags() & SIGNATURE_POLYMORPHIC) != 0) {
50250
04f9bb270ab8 8203488: Remove error generation from TransTypes
mcimadamore
parents: 49876
diff changeset
  2685
                        env.info.pendingResolutionPhase = BASIC;
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2686
                        return findPolymorphicSignatureInstance(env, sym, argtypes);
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2687
                    }
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2688
                }
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2689
                return sym;
6592
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6356
diff changeset
  2690
            }
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2691
        });
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2692
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2693
6592
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6356
diff changeset
  2694
    /** 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
  2695
     *  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
  2696
     *  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
  2697
     *  @param env       Attribution environment
12335
4725d88691dd 7133185: Update 292 overload resolution logic to match JLS
mcimadamore
parents: 12334
diff changeset
  2698
     *  @param spMethod  signature polymorphic method - i.e. MH.invokeExact
4725d88691dd 7133185: Update 292 overload resolution logic to match JLS
mcimadamore
parents: 12334
diff changeset
  2699
     *  @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
  2700
     */
12335
4725d88691dd 7133185: Update 292 overload resolution logic to match JLS
mcimadamore
parents: 12334
diff changeset
  2701
    Symbol findPolymorphicSignatureInstance(Env<AttrContext> env,
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2702
                                            final Symbol spMethod,
8036
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 8032
diff changeset
  2703
                                            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
  2704
        Type mtype = infer.instantiatePolymorphicSignatureInstance(env,
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  2705
                (MethodSymbol)spMethod, currentResolutionContext, argtypes);
50250
04f9bb270ab8 8203488: Remove error generation from TransTypes
mcimadamore
parents: 49876
diff changeset
  2706
        return findPolymorphicSignatureInstance(spMethod, mtype);
04f9bb270ab8 8203488: Remove error generation from TransTypes
mcimadamore
parents: 49876
diff changeset
  2707
    }
04f9bb270ab8 8203488: Remove error generation from TransTypes
mcimadamore
parents: 49876
diff changeset
  2708
04f9bb270ab8 8203488: Remove error generation from TransTypes
mcimadamore
parents: 49876
diff changeset
  2709
    Symbol findPolymorphicSignatureInstance(final Symbol spMethod,
04f9bb270ab8 8203488: Remove error generation from TransTypes
mcimadamore
parents: 49876
diff changeset
  2710
                                            Type mtype) {
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  2711
        for (Symbol sym : polymorphicSignatureScope.getSymbolsByName(spMethod.name)) {
36995
e19153419efd 8149644: Integrate VarHandles
psandoz
parents: 36278
diff changeset
  2712
            // Check that there is already a method symbol for the method
e19153419efd 8149644: Integrate VarHandles
psandoz
parents: 36278
diff changeset
  2713
            // type and owner
e19153419efd 8149644: Integrate VarHandles
psandoz
parents: 36278
diff changeset
  2714
            if (types.isSameType(mtype, sym.type) &&
e19153419efd 8149644: Integrate VarHandles
psandoz
parents: 36278
diff changeset
  2715
                spMethod.owner == sym.owner) {
e19153419efd 8149644: Integrate VarHandles
psandoz
parents: 36278
diff changeset
  2716
                return sym;
6592
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6356
diff changeset
  2717
            }
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6356
diff changeset
  2718
        }
12335
4725d88691dd 7133185: Update 292 overload resolution logic to match JLS
mcimadamore
parents: 12334
diff changeset
  2719
36995
e19153419efd 8149644: Integrate VarHandles
psandoz
parents: 36278
diff changeset
  2720
        // Create the desired method
e19153419efd 8149644: Integrate VarHandles
psandoz
parents: 36278
diff changeset
  2721
        // Retain static modifier is to support invocations to
e19153419efd 8149644: Integrate VarHandles
psandoz
parents: 36278
diff changeset
  2722
        // MethodHandle.linkTo* methods
e19153419efd 8149644: Integrate VarHandles
psandoz
parents: 36278
diff changeset
  2723
        long flags = ABSTRACT | HYPOTHETICAL |
e19153419efd 8149644: Integrate VarHandles
psandoz
parents: 36278
diff changeset
  2724
                     spMethod.flags() & (Flags.AccessFlags | Flags.STATIC);
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2725
        Symbol msym = new MethodSymbol(flags, spMethod.name, mtype, spMethod.owner) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2726
            @Override
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2727
            public Symbol baseSymbol() {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2728
                return spMethod;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2729
            }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2730
        };
32949
7e0f345e64a8 8130506: javac AssertionError when invoking MethodHandle.invoke with lambda paramter
sadayapalam
parents: 32709
diff changeset
  2731
        if (!mtype.isErroneous()) { // Cache only if kosher.
7e0f345e64a8 8130506: javac AssertionError when invoking MethodHandle.invoke with lambda paramter
sadayapalam
parents: 32709
diff changeset
  2732
            polymorphicSignatureScope.enter(msym);
7e0f345e64a8 8130506: javac AssertionError when invoking MethodHandle.invoke with lambda paramter
sadayapalam
parents: 32709
diff changeset
  2733
        }
12335
4725d88691dd 7133185: Update 292 overload resolution logic to match JLS
mcimadamore
parents: 12334
diff changeset
  2734
        return msym;
6592
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6356
diff changeset
  2735
    }
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6356
diff changeset
  2736
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2737
    /** Resolve a qualified method identifier, throw a fatal error if not
06bc494ca11e Initial load
duke
parents:
diff changeset
  2738
     *  found.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2739
     *  @param pos       The position to use for error reporting.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2740
     *  @param env       The environment current at the method invocation.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2741
     *  @param site      The type of the qualifying expression, in which
06bc494ca11e Initial load
duke
parents:
diff changeset
  2742
     *                   identifier is searched.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2743
     *  @param name      The identifier's name.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2744
     *  @param argtypes  The types of the invocation's value arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2745
     *  @param typeargtypes  The types of the invocation's type arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2746
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2747
    public MethodSymbol resolveInternalMethod(DiagnosticPosition pos, Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2748
                                        Type site, Name name,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2749
                                        List<Type> argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2750
                                        List<Type> typeargtypes) {
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2751
        MethodResolutionContext resolveContext = new MethodResolutionContext();
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2752
        resolveContext.internalResolution = true;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2753
        Symbol sym = resolveQualifiedMethod(resolveContext, pos, env, site.tsym,
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2754
                site, name, argtypes, typeargtypes);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2755
        if (sym.kind == MTH) return (MethodSymbol)sym;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2756
        else throw new FatalError(
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44504
diff changeset
  2757
                 diags.fragment(Fragments.FatalErrCantLocateMeth(name)));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2758
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2759
06bc494ca11e Initial load
duke
parents:
diff changeset
  2760
    /** Resolve constructor.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2761
     *  @param pos       The position to use for error reporting.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2762
     *  @param env       The environment current at the constructor invocation.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2763
     *  @param site      The type of class for which a constructor is searched.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2764
     *  @param argtypes  The types of the constructor invocation's value
06bc494ca11e Initial load
duke
parents:
diff changeset
  2765
     *                   arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2766
     *  @param typeargtypes  The types of the constructor invocation's type
06bc494ca11e Initial load
duke
parents:
diff changeset
  2767
     *                   arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2768
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2769
    Symbol resolveConstructor(DiagnosticPosition pos,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2770
                              Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2771
                              Type site,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2772
                              List<Type> argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2773
                              List<Type> typeargtypes) {
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2774
        return resolveConstructor(new MethodResolutionContext(), pos, env, site, argtypes, typeargtypes);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2775
    }
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2776
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2777
    private Symbol resolveConstructor(MethodResolutionContext resolveContext,
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2778
                              final DiagnosticPosition pos,
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2779
                              Env<AttrContext> env,
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2780
                              Type site,
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2781
                              List<Type> argtypes,
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2782
                              List<Type> typeargtypes) {
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2783
        return lookupMethod(env, pos, site.tsym, resolveContext, new BasicLookupHelper(names.init, site, argtypes, typeargtypes) {
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2784
            @Override
18668
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  2785
            Symbol doLookup(Env<AttrContext> env, MethodResolutionPhase phase) {
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2786
                return findConstructor(pos, env, site, argtypes, typeargtypes,
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2787
                        phase.isBoxingRequired(),
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2788
                        phase.isVarargsRequired());
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2789
            }
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2790
        });
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2791
    }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2792
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2793
    /** Resolve a constructor, throw a fatal error if not found.
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2794
     *  @param pos       The position to use for error reporting.
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2795
     *  @param env       The environment current at the method invocation.
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2796
     *  @param site      The type to be constructed.
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2797
     *  @param argtypes  The types of the invocation's value arguments.
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2798
     *  @param typeargtypes  The types of the invocation's type arguments.
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2799
     */
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2800
    public MethodSymbol resolveInternalConstructor(DiagnosticPosition pos, Env<AttrContext> env,
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2801
                                        Type site,
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2802
                                        List<Type> argtypes,
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2803
                                        List<Type> typeargtypes) {
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2804
        MethodResolutionContext resolveContext = new MethodResolutionContext();
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2805
        resolveContext.internalResolution = true;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2806
        Symbol sym = resolveConstructor(resolveContext, pos, env, site, argtypes, typeargtypes);
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2807
        if (sym.kind == MTH) return (MethodSymbol)sym;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2808
        else throw new FatalError(
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44504
diff changeset
  2809
                 diags.fragment(Fragments.FatalErrCantLocateCtor(site)));
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2810
    }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2811
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2812
    Symbol findConstructor(DiagnosticPosition pos, Env<AttrContext> env,
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2813
                              Type site, List<Type> argtypes,
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2814
                              List<Type> typeargtypes,
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2815
                              boolean allowBoxing,
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2816
                              boolean useVarargs) {
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2817
        Symbol sym = findMethod(env, site,
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2818
                                    names.init, argtypes,
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2819
                                    typeargtypes, allowBoxing,
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents: 28456
diff changeset
  2820
                                    useVarargs);
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2821
        chk.checkDeprecated(pos, env.info.scope.owner, sym);
58713
ad69fd32778e 8226585: Improve javac messages for using a preview API
jlahoda
parents: 58350
diff changeset
  2822
        chk.checkPreview(pos, sym);
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2823
        return sym;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2824
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2825
5321
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  2826
    /** Resolve constructor using diamond inference.
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  2827
     *  @param pos       The position to use for error reporting.
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  2828
     *  @param env       The environment current at the constructor invocation.
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  2829
     *  @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
  2830
     *                   The scope of this class has been touched in attribution.
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  2831
     *  @param argtypes  The types of the constructor invocation's value
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  2832
     *                   arguments.
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  2833
     *  @param typeargtypes  The types of the constructor invocation's type
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  2834
     *                   arguments.
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  2835
     */
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  2836
    Symbol resolveDiamond(DiagnosticPosition pos,
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  2837
                              Env<AttrContext> env,
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  2838
                              Type site,
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  2839
                              List<Type> argtypes,
6344
f190fb7fdd2d 6975275: diamond implementation needs some cleanup
mcimadamore
parents: 6151
diff changeset
  2840
                              List<Type> typeargtypes) {
16805
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  2841
        return lookupMethod(env, pos, site.tsym, resolveMethodCheck,
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  2842
                new BasicLookupHelper(names.init, site, argtypes, typeargtypes) {
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  2843
                    @Override
18668
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  2844
                    Symbol doLookup(Env<AttrContext> env, MethodResolutionPhase phase) {
16805
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  2845
                        return findDiamond(env, site, argtypes, typeargtypes,
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  2846
                                phase.isBoxingRequired(),
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  2847
                                phase.isVarargsRequired());
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  2848
                    }
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  2849
                    @Override
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  2850
                    Symbol access(Env<AttrContext> env, DiagnosticPosition pos, Symbol location, Symbol sym) {
29774
9d438163db79 8075184: javac is mistakenly considering an missing enclosing instance error as an overload error
vromero
parents: 29554
diff changeset
  2851
                        if (sym.kind.isResolutionError()) {
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2852
                            if (sym.kind != WRONG_MTH &&
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2853
                                sym.kind != WRONG_MTHS) {
19917
ebb957ba7b07 8023545: Misleading error message when using diamond operator with private constructor
vromero
parents: 19914
diff changeset
  2854
                                sym = super.access(env, pos, location, sym);
ebb957ba7b07 8023545: Misleading error message when using diamond operator with private constructor
vromero
parents: 19914
diff changeset
  2855
                            } else {
ebb957ba7b07 8023545: Misleading error message when using diamond operator with private constructor
vromero
parents: 19914
diff changeset
  2856
                                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
  2857
                                                ((InapplicableSymbolError)sym.baseSymbol()).errCandidate().snd :
19917
ebb957ba7b07 8023545: Misleading error message when using diamond operator with private constructor
vromero
parents: 19914
diff changeset
  2858
                                                null;
28331
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  2859
                                sym = new DiamondError(sym, currentResolutionContext);
19917
ebb957ba7b07 8023545: Misleading error message when using diamond operator with private constructor
vromero
parents: 19914
diff changeset
  2860
                                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
  2861
                                env.info.pendingResolutionPhase = currentResolutionContext.step;
ebb957ba7b07 8023545: Misleading error message when using diamond operator with private constructor
vromero
parents: 19914
diff changeset
  2862
                            }
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2863
                        }
16805
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  2864
                        return sym;
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  2865
                    }});
5321
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  2866
    }
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  2867
12078
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2868
    /** 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
  2869
     *  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
  2870
     *  {@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
  2871
     *  a method check is executed against the modified constructor type:
13844
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 13438
diff changeset
  2872
     *  {@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
  2873
     *  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
  2874
     *  the inferred type for the diamond operator.
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2875
     */
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2876
    private Symbol findDiamond(Env<AttrContext> env,
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2877
                              Type site,
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2878
                              List<Type> argtypes,
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2879
                              List<Type> typeargtypes,
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2880
                              boolean allowBoxing,
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2881
                              boolean useVarargs) {
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2882
        Symbol bestSoFar = methodNotFound;
29776
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2883
        TypeSymbol tsym = site.tsym.isInterface() ? syms.objectType.tsym : site.tsym;
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2884
        for (final Symbol sym : tsym.members().getSymbolsByName(names.init)) {
12078
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2885
            //- System.out.println(" e " + e.sym);
14051
9097cec96212 7188968: New instance creation expression using diamond is checked twice
mcimadamore
parents: 14048
diff changeset
  2886
            if (sym.kind == MTH &&
9097cec96212 7188968: New instance creation expression using diamond is checked twice
mcimadamore
parents: 14048
diff changeset
  2887
                (sym.flags_field & SYNTHETIC) == 0) {
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  2888
                    List<Type> oldParams = sym.type.hasTag(FORALL) ?
14051
9097cec96212 7188968: New instance creation expression using diamond is checked twice
mcimadamore
parents: 14048
diff changeset
  2889
                            ((ForAll)sym.type).tvars :
42828
cce89649f958 8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents: 42827
diff changeset
  2890
                            List.nil();
12078
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2891
                    Type constrType = new ForAll(site.tsym.type.getTypeArguments().appendList(oldParams),
24396
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 24393
diff changeset
  2892
                                                 types.createMethodTypeWithReturn(sym.type.asMethodType(), site));
14051
9097cec96212 7188968: New instance creation expression using diamond is checked twice
mcimadamore
parents: 14048
diff changeset
  2893
                    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
  2894
                        @Override
9097cec96212 7188968: New instance creation expression using diamond is checked twice
mcimadamore
parents: 14048
diff changeset
  2895
                        public Symbol baseSymbol() {
9097cec96212 7188968: New instance creation expression using diamond is checked twice
mcimadamore
parents: 14048
diff changeset
  2896
                            return sym;
9097cec96212 7188968: New instance creation expression using diamond is checked twice
mcimadamore
parents: 14048
diff changeset
  2897
                        }
9097cec96212 7188968: New instance creation expression using diamond is checked twice
mcimadamore
parents: 14048
diff changeset
  2898
                    };
12078
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2899
                    bestSoFar = selectBest(env, site, argtypes, typeargtypes,
14051
9097cec96212 7188968: New instance creation expression using diamond is checked twice
mcimadamore
parents: 14048
diff changeset
  2900
                            newConstr,
12078
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2901
                            bestSoFar,
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2902
                            allowBoxing,
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents: 28456
diff changeset
  2903
                            useVarargs);
12078
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2904
            }
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2905
        }
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2906
        return bestSoFar;
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2907
    }
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2908
21718
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2909
    Symbol getMemberReference(DiagnosticPosition pos,
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2910
            Env<AttrContext> env,
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2911
            JCMemberReference referenceTree,
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2912
            Type site,
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2913
            Name name) {
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
        site = types.capture(site);
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
        ReferenceLookupHelper lookupHelper = makeReferenceLookupHelper(
42828
cce89649f958 8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents: 42827
diff changeset
  2918
                referenceTree, site, name, List.nil(), null, VARARITY);
21718
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2919
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2920
        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
  2921
        Symbol sym = lookupMethod(newEnv, env.tree.pos(), site.tsym,
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2922
                nilMethodCheck, lookupHelper);
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2923
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2924
        env.info.pendingResolutionPhase = newEnv.info.pendingResolutionPhase;
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2925
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2926
        return sym;
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2927
    }
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2928
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2929
    ReferenceLookupHelper makeReferenceLookupHelper(JCMemberReference referenceTree,
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2930
                                  Type site,
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2931
                                  Name name,
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2932
                                  List<Type> argtypes,
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2933
                                  List<Type> typeargtypes,
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2934
                                  MethodResolutionPhase maxPhase) {
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2935
        if (!name.equals(names.init)) {
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2936
            //method reference
28456
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  2937
            return new MethodReferenceLookupHelper(referenceTree, name, site, argtypes, typeargtypes, maxPhase);
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  2938
        } else if (site.hasTag(ARRAY)) {
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  2939
            //array constructor reference
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  2940
            return new ArrayConstructorReferenceLookupHelper(referenceTree, site, argtypes, typeargtypes, maxPhase);
21718
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2941
        } else {
28456
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  2942
            //class constructor reference
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  2943
            return new ConstructorReferenceLookupHelper(referenceTree, site, argtypes, typeargtypes, maxPhase);
21718
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2944
        }
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2945
    }
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2946
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2947
    /**
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2948
     * Resolution of member references is typically done as a single
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2949
     * overload resolution step, where the argument types A are inferred from
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2950
     * the target functional descriptor.
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2951
     *
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2952
     * If the member reference is a method reference with a type qualifier,
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2953
     * a two-step lookup process is performed. The first step uses the
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2954
     * expected argument list A, while the second step discards the first
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2955
     * type from A (which is treated as a receiver type).
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2956
     *
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2957
     * There are two cases in which inference is performed: (i) if the member
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2958
     * reference is a constructor reference and the qualifier type is raw - in
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2959
     * which case diamond inference is used to infer a parameterization for the
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2960
     * type qualifier; (ii) if the member reference is an unbound reference
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2961
     * where the type qualifier is raw - in that case, during the unbound lookup
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2962
     * the receiver argument type is used to infer an instantiation for the raw
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2963
     * qualifier type.
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2964
     *
28456
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  2965
     * When a multi-step resolution process is exploited, the process of picking
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  2966
     * the resulting symbol is delegated to an helper class {@link com.sun.tools.javac.comp.Resolve.ReferenceChooser}.
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2967
     *
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2968
     * This routine returns a pair (T,S), where S is the member reference symbol,
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2969
     * 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
  2970
     * the type T might be dynamically inferred (i.e. if constructor reference
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2971
     * has a raw qualifier).
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2972
     */
21718
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2973
    Pair<Symbol, ReferenceLookupHelper> resolveMemberReference(Env<AttrContext> env,
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2974
                                  JCMemberReference referenceTree,
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2975
                                  Type site,
21718
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2976
                                  Name name,
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2977
                                  List<Type> argtypes,
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2978
                                  List<Type> typeargtypes,
50250
04f9bb270ab8 8203488: Remove error generation from TransTypes
mcimadamore
parents: 49876
diff changeset
  2979
                                  Type descriptor,
18910
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
  2980
                                  MethodCheck methodCheck,
21718
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2981
                                  InferenceContext inferenceContext,
28456
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  2982
                                  ReferenceChooser referenceChooser) {
15360
450af2a9e3c9 8005854: Add support for array constructor references
mcimadamore
parents: 15038
diff changeset
  2983
29292
c10d63c667cd 8073842: Invalid method reference when referencing a method on a wildcard type
mcimadamore
parents: 29146
diff changeset
  2984
        //step 1 - bound lookup
21718
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2985
        ReferenceLookupHelper boundLookupHelper = makeReferenceLookupHelper(
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2986
                referenceTree, site, name, argtypes, typeargtypes, VARARITY);
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2987
        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
  2988
        MethodResolutionContext boundSearchResolveContext = new MethodResolutionContext();
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2989
        boundSearchResolveContext.methodCheck = methodCheck;
28456
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  2990
        Symbol boundSym = lookupMethod(boundEnv, env.tree.pos(),
21718
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2991
                site.tsym, boundSearchResolveContext, boundLookupHelper);
28456
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  2992
        ReferenceLookupResult boundRes = new ReferenceLookupResult(boundSym, boundSearchResolveContext);
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2993
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2994
        //step 2 - unbound lookup
21718
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  2995
        Symbol unboundSym = methodNotFound;
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  2996
        Env<AttrContext> unboundEnv = env.dup(env.tree, env.info.dup());
28456
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  2997
        ReferenceLookupHelper unboundLookupHelper = boundLookupHelper.unboundLookup(inferenceContext);
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  2998
        ReferenceLookupResult unboundRes = referenceNotFound;
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  2999
        if (unboundLookupHelper != null) {
21718
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  3000
            MethodResolutionContext unboundSearchResolveContext =
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  3001
                    new MethodResolutionContext();
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  3002
            unboundSearchResolveContext.methodCheck = methodCheck;
28456
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3003
            unboundSym = lookupMethod(unboundEnv, env.tree.pos(),
21718
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  3004
                    site.tsym, unboundSearchResolveContext, unboundLookupHelper);
28456
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3005
            unboundRes = new ReferenceLookupResult(unboundSym, unboundSearchResolveContext);
21718
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  3006
        }
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3007
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3008
        //merge results
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3009
        Pair<Symbol, ReferenceLookupHelper> res;
47297
a0116bcc65b7 8188144: regression in method reference type-checking
mcimadamore
parents: 47268
diff changeset
  3010
        ReferenceLookupResult bestRes = referenceChooser.result(boundRes, unboundRes);
a0116bcc65b7 8188144: regression in method reference type-checking
mcimadamore
parents: 47268
diff changeset
  3011
        res = new Pair<>(bestRes.sym,
a0116bcc65b7 8188144: regression in method reference type-checking
mcimadamore
parents: 47268
diff changeset
  3012
                bestRes == unboundRes ? unboundLookupHelper : boundLookupHelper);
a0116bcc65b7 8188144: regression in method reference type-checking
mcimadamore
parents: 47268
diff changeset
  3013
        env.info.pendingResolutionPhase = bestRes == unboundRes ?
19129
b1409ae675f8 8020843: javac crashes on accessibility check with method reference with typevar receiver
mcimadamore
parents: 18914
diff changeset
  3014
                unboundEnv.info.pendingResolutionPhase :
b1409ae675f8 8020843: javac crashes on accessibility check with method reference with typevar receiver
mcimadamore
parents: 18914
diff changeset
  3015
                boundEnv.info.pendingResolutionPhase;
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3016
50250
04f9bb270ab8 8203488: Remove error generation from TransTypes
mcimadamore
parents: 49876
diff changeset
  3017
        if (!res.fst.kind.isResolutionError()) {
04f9bb270ab8 8203488: Remove error generation from TransTypes
mcimadamore
parents: 49876
diff changeset
  3018
            //handle sigpoly method references
04f9bb270ab8 8203488: Remove error generation from TransTypes
mcimadamore
parents: 49876
diff changeset
  3019
            MethodSymbol msym = (MethodSymbol)res.fst;
04f9bb270ab8 8203488: Remove error generation from TransTypes
mcimadamore
parents: 49876
diff changeset
  3020
            if ((msym.flags() & SIGNATURE_POLYMORPHIC) != 0) {
04f9bb270ab8 8203488: Remove error generation from TransTypes
mcimadamore
parents: 49876
diff changeset
  3021
                env.info.pendingResolutionPhase = BASIC;
04f9bb270ab8 8203488: Remove error generation from TransTypes
mcimadamore
parents: 49876
diff changeset
  3022
                res = new Pair<>(findPolymorphicSignatureInstance(msym, descriptor), res.snd);
04f9bb270ab8 8203488: Remove error generation from TransTypes
mcimadamore
parents: 49876
diff changeset
  3023
            }
04f9bb270ab8 8203488: Remove error generation from TransTypes
mcimadamore
parents: 49876
diff changeset
  3024
        }
04f9bb270ab8 8203488: Remove error generation from TransTypes
mcimadamore
parents: 49876
diff changeset
  3025
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3026
        return res;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3027
    }
21718
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  3028
28456
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3029
    /**
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3030
     * This class is used to represent a method reference lookup result. It keeps track of two
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3031
     * things: (i) the symbol found during a method reference lookup and (ii) the static kind
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3032
     * of the lookup (see {@link com.sun.tools.javac.comp.Resolve.ReferenceLookupResult.StaticKind}).
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3033
     */
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3034
    static class ReferenceLookupResult {
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3035
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3036
        /**
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3037
         * Static kind associated with a method reference lookup. Erroneous lookups end up with
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3038
         * the UNDEFINED kind; successful lookups will end up with either STATIC, NON_STATIC,
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3039
         * depending on whether all applicable candidates are static or non-static methods,
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3040
         * respectively. If a successful lookup has both static and non-static applicable methods,
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3041
         * its kind is set to BOTH.
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3042
         */
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3043
        enum StaticKind {
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3044
            STATIC,
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3045
            NON_STATIC,
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3046
            BOTH,
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3047
            UNDEFINED;
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3048
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3049
            /**
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3050
             * Retrieve the static kind associated with a given (method) symbol.
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3051
             */
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3052
            static StaticKind from(Symbol s) {
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3053
                return s.isStatic() ?
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3054
                        STATIC : NON_STATIC;
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3055
            }
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3056
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3057
            /**
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3058
             * Merge two static kinds together.
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3059
             */
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3060
            static StaticKind reduce(StaticKind sk1, StaticKind sk2) {
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3061
                if (sk1 == UNDEFINED) {
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3062
                    return sk2;
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3063
                } else if (sk2 == UNDEFINED) {
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3064
                    return sk1;
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3065
                } else {
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3066
                    return sk1 == sk2 ? sk1 : BOTH;
21718
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  3067
                }
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  3068
            }
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  3069
        }
28456
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3070
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3071
        /** The static kind. */
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3072
        StaticKind staticKind;
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3073
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3074
        /** The lookup result. */
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3075
        Symbol sym;
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3076
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3077
        ReferenceLookupResult(Symbol sym, MethodResolutionContext resolutionContext) {
47297
a0116bcc65b7 8188144: regression in method reference type-checking
mcimadamore
parents: 47268
diff changeset
  3078
            this(sym, staticKind(sym, resolutionContext));
a0116bcc65b7 8188144: regression in method reference type-checking
mcimadamore
parents: 47268
diff changeset
  3079
        }
a0116bcc65b7 8188144: regression in method reference type-checking
mcimadamore
parents: 47268
diff changeset
  3080
a0116bcc65b7 8188144: regression in method reference type-checking
mcimadamore
parents: 47268
diff changeset
  3081
        private ReferenceLookupResult(Symbol sym, StaticKind staticKind) {
a0116bcc65b7 8188144: regression in method reference type-checking
mcimadamore
parents: 47268
diff changeset
  3082
            this.staticKind = staticKind;
28456
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3083
            this.sym = sym;
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3084
        }
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3085
47297
a0116bcc65b7 8188144: regression in method reference type-checking
mcimadamore
parents: 47268
diff changeset
  3086
        private static StaticKind staticKind(Symbol sym, MethodResolutionContext resolutionContext) {
28456
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3087
            switch (sym.kind) {
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3088
                case MTH:
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3089
                case AMBIGUOUS:
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3090
                    return resolutionContext.candidates.stream()
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3091
                            .filter(c -> c.isApplicable() && c.step == resolutionContext.step)
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3092
                            .map(c -> StaticKind.from(c.sym))
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3093
                            .reduce(StaticKind::reduce)
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3094
                            .orElse(StaticKind.UNDEFINED);
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3095
                default:
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3096
                    return StaticKind.UNDEFINED;
19129
b1409ae675f8 8020843: javac crashes on accessibility check with method reference with typevar receiver
mcimadamore
parents: 18914
diff changeset
  3097
            }
b1409ae675f8 8020843: javac crashes on accessibility check with method reference with typevar receiver
mcimadamore
parents: 18914
diff changeset
  3098
        }
b1409ae675f8 8020843: javac crashes on accessibility check with method reference with typevar receiver
mcimadamore
parents: 18914
diff changeset
  3099
28456
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3100
        /**
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3101
         * Does this result corresponds to a successful lookup (i.e. one where a method has been found?)
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3102
         */
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3103
        boolean isSuccess() {
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3104
            return staticKind != StaticKind.UNDEFINED;
16294
0c291a3cd60d 8007462: Fix provisional applicability for method references
mcimadamore
parents: 15706
diff changeset
  3105
        }
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3106
28456
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3107
        /**
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3108
         * Does this result have given static kind?
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3109
         */
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3110
        boolean hasKind(StaticKind sk) {
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3111
            return this.staticKind == sk;
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3112
        }
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3113
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3114
        /**
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3115
         * Error recovery helper: can this lookup result be ignored (for the purpose of returning
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3116
         * some 'better' result) ?
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3117
         */
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3118
        boolean canIgnore() {
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3119
            switch (sym.kind) {
19129
b1409ae675f8 8020843: javac crashes on accessibility check with method reference with typevar receiver
mcimadamore
parents: 18914
diff changeset
  3120
                case ABSENT_MTH:
b1409ae675f8 8020843: javac crashes on accessibility check with method reference with typevar receiver
mcimadamore
parents: 18914
diff changeset
  3121
                    return true;
b1409ae675f8 8020843: javac crashes on accessibility check with method reference with typevar receiver
mcimadamore
parents: 18914
diff changeset
  3122
                case WRONG_MTH:
b1409ae675f8 8020843: javac crashes on accessibility check with method reference with typevar receiver
mcimadamore
parents: 18914
diff changeset
  3123
                    InapplicableSymbolError errSym =
28456
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3124
                            (InapplicableSymbolError)sym.baseSymbol();
19129
b1409ae675f8 8020843: javac crashes on accessibility check with method reference with typevar receiver
mcimadamore
parents: 18914
diff changeset
  3125
                    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
  3126
                            .matches(errSym.errCandidate().snd);
b1409ae675f8 8020843: javac crashes on accessibility check with method reference with typevar receiver
mcimadamore
parents: 18914
diff changeset
  3127
                case WRONG_MTHS:
b1409ae675f8 8020843: javac crashes on accessibility check with method reference with typevar receiver
mcimadamore
parents: 18914
diff changeset
  3128
                    InapplicableSymbolsError errSyms =
28456
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3129
                            (InapplicableSymbolsError)sym.baseSymbol();
19129
b1409ae675f8 8020843: javac crashes on accessibility check with method reference with typevar receiver
mcimadamore
parents: 18914
diff changeset
  3130
                    return errSyms.filterCandidates(errSyms.mapCandidates()).isEmpty();
b1409ae675f8 8020843: javac crashes on accessibility check with method reference with typevar receiver
mcimadamore
parents: 18914
diff changeset
  3131
                default:
b1409ae675f8 8020843: javac crashes on accessibility check with method reference with typevar receiver
mcimadamore
parents: 18914
diff changeset
  3132
                    return false;
b1409ae675f8 8020843: javac crashes on accessibility check with method reference with typevar receiver
mcimadamore
parents: 18914
diff changeset
  3133
            }
b1409ae675f8 8020843: javac crashes on accessibility check with method reference with typevar receiver
mcimadamore
parents: 18914
diff changeset
  3134
        }
47297
a0116bcc65b7 8188144: regression in method reference type-checking
mcimadamore
parents: 47268
diff changeset
  3135
a0116bcc65b7 8188144: regression in method reference type-checking
mcimadamore
parents: 47268
diff changeset
  3136
        static ReferenceLookupResult error(Symbol sym) {
a0116bcc65b7 8188144: regression in method reference type-checking
mcimadamore
parents: 47268
diff changeset
  3137
            return new ReferenceLookupResult(sym, StaticKind.UNDEFINED);
a0116bcc65b7 8188144: regression in method reference type-checking
mcimadamore
parents: 47268
diff changeset
  3138
        }
28456
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3139
    }
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3140
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3141
    /**
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3142
     * This abstract class embodies the logic that converts one (bound lookup) or two (unbound lookup)
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3143
     * {@code ReferenceLookupResult} objects into a (@code Symbol), which is then regarded as the
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3144
     * result of method reference resolution.
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3145
     */
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3146
    abstract class ReferenceChooser {
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3147
        /**
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3148
         * Generate a result from a pair of lookup result objects. This method delegates to the
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3149
         * appropriate result generation routine.
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3150
         */
47297
a0116bcc65b7 8188144: regression in method reference type-checking
mcimadamore
parents: 47268
diff changeset
  3151
        ReferenceLookupResult result(ReferenceLookupResult boundRes, ReferenceLookupResult unboundRes) {
28456
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3152
            return unboundRes != referenceNotFound ?
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3153
                    unboundResult(boundRes, unboundRes) :
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3154
                    boundResult(boundRes);
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3155
        }
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3156
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3157
        /**
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3158
         * Generate a symbol from a given bound lookup result.
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3159
         */
47297
a0116bcc65b7 8188144: regression in method reference type-checking
mcimadamore
parents: 47268
diff changeset
  3160
        abstract ReferenceLookupResult boundResult(ReferenceLookupResult boundRes);
28456
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3161
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3162
        /**
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3163
         * Generate a symbol from a pair of bound/unbound lookup results.
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3164
         */
47297
a0116bcc65b7 8188144: regression in method reference type-checking
mcimadamore
parents: 47268
diff changeset
  3165
        abstract ReferenceLookupResult unboundResult(ReferenceLookupResult boundRes, ReferenceLookupResult unboundRes);
28456
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3166
    }
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3167
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3168
    /**
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3169
     * This chooser implements the selection strategy used during a full lookup; this logic
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3170
     * is described in JLS SE 8 (15.3.2).
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3171
     */
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3172
    ReferenceChooser basicReferenceChooser = new ReferenceChooser() {
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3173
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3174
        @Override
47297
a0116bcc65b7 8188144: regression in method reference type-checking
mcimadamore
parents: 47268
diff changeset
  3175
        ReferenceLookupResult boundResult(ReferenceLookupResult boundRes) {
28456
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3176
            return !boundRes.isSuccess() || boundRes.hasKind(StaticKind.NON_STATIC) ?
47297
a0116bcc65b7 8188144: regression in method reference type-checking
mcimadamore
parents: 47268
diff changeset
  3177
                    boundRes : //the search produces a non-static method
a0116bcc65b7 8188144: regression in method reference type-checking
mcimadamore
parents: 47268
diff changeset
  3178
                    ReferenceLookupResult.error(new BadMethodReferenceError(boundRes.sym, false));
28456
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3179
        }
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3180
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3181
        @Override
47297
a0116bcc65b7 8188144: regression in method reference type-checking
mcimadamore
parents: 47268
diff changeset
  3182
        ReferenceLookupResult unboundResult(ReferenceLookupResult boundRes, ReferenceLookupResult unboundRes) {
28456
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3183
            if (boundRes.hasKind(StaticKind.STATIC) &&
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3184
                    (!unboundRes.isSuccess() || unboundRes.hasKind(StaticKind.STATIC))) {
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3185
                //the first search produces a static method and no non-static method is applicable
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3186
                //during the second search
47297
a0116bcc65b7 8188144: regression in method reference type-checking
mcimadamore
parents: 47268
diff changeset
  3187
                return boundRes;
28456
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3188
            } else if (unboundRes.hasKind(StaticKind.NON_STATIC) &&
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3189
                    (!boundRes.isSuccess() || boundRes.hasKind(StaticKind.NON_STATIC))) {
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3190
                //the second search produces a non-static method and no static method is applicable
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3191
                //during the first search
47297
a0116bcc65b7 8188144: regression in method reference type-checking
mcimadamore
parents: 47268
diff changeset
  3192
                return unboundRes;
28456
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3193
            } else if (boundRes.isSuccess() && unboundRes.isSuccess()) {
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3194
                //both searches produce some result; ambiguity (error recovery)
47297
a0116bcc65b7 8188144: regression in method reference type-checking
mcimadamore
parents: 47268
diff changeset
  3195
                return ReferenceLookupResult.error(ambiguityError(boundRes.sym, unboundRes.sym));
28456
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3196
            } else if (boundRes.isSuccess() || unboundRes.isSuccess()) {
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3197
                //Both searches failed to produce a result with correct staticness (i.e. first search
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3198
                //produces an non-static method). Alternatively, a given search produced a result
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3199
                //with the right staticness, but the other search has applicable methods with wrong
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3200
                //staticness (error recovery)
47297
a0116bcc65b7 8188144: regression in method reference type-checking
mcimadamore
parents: 47268
diff changeset
  3201
                return ReferenceLookupResult.error(new BadMethodReferenceError(boundRes.isSuccess() ?
a0116bcc65b7 8188144: regression in method reference type-checking
mcimadamore
parents: 47268
diff changeset
  3202
                        boundRes.sym : unboundRes.sym, true));
28456
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3203
            } else {
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3204
                //both searches fail to produce a result - pick 'better' error using heuristics (error recovery)
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3205
                return (boundRes.canIgnore() && !unboundRes.canIgnore()) ?
47297
a0116bcc65b7 8188144: regression in method reference type-checking
mcimadamore
parents: 47268
diff changeset
  3206
                        unboundRes : boundRes;
28456
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3207
            }
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3208
        }
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3209
    };
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3210
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3211
    /**
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3212
     * This chooser implements the selection strategy used during an arity-based lookup; this logic
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3213
     * is described in JLS SE 8 (15.12.2.1).
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3214
     */
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3215
    ReferenceChooser structuralReferenceChooser = new ReferenceChooser() {
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3216
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3217
        @Override
47297
a0116bcc65b7 8188144: regression in method reference type-checking
mcimadamore
parents: 47268
diff changeset
  3218
        ReferenceLookupResult boundResult(ReferenceLookupResult boundRes) {
28456
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3219
            return (!boundRes.isSuccess() || !boundRes.hasKind(StaticKind.STATIC)) ?
47297
a0116bcc65b7 8188144: regression in method reference type-checking
mcimadamore
parents: 47268
diff changeset
  3220
                    boundRes : //the search has at least one applicable non-static method
a0116bcc65b7 8188144: regression in method reference type-checking
mcimadamore
parents: 47268
diff changeset
  3221
                    ReferenceLookupResult.error(new BadMethodReferenceError(boundRes.sym, false));
28456
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3222
        }
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3223
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3224
        @Override
47297
a0116bcc65b7 8188144: regression in method reference type-checking
mcimadamore
parents: 47268
diff changeset
  3225
        ReferenceLookupResult unboundResult(ReferenceLookupResult boundRes, ReferenceLookupResult unboundRes) {
28456
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3226
            if (boundRes.isSuccess() && !boundRes.hasKind(StaticKind.NON_STATIC)) {
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3227
                //the first serach has at least one applicable static method
47297
a0116bcc65b7 8188144: regression in method reference type-checking
mcimadamore
parents: 47268
diff changeset
  3228
                return boundRes;
28456
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3229
            } else if (unboundRes.isSuccess() && !unboundRes.hasKind(StaticKind.STATIC)) {
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3230
                //the second search has at least one applicable non-static method
47297
a0116bcc65b7 8188144: regression in method reference type-checking
mcimadamore
parents: 47268
diff changeset
  3231
                return unboundRes;
28456
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3232
            } else if (boundRes.isSuccess() || unboundRes.isSuccess()) {
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3233
                //either the first search produces a non-static method, or second search produces
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3234
                //a non-static method (error recovery)
47297
a0116bcc65b7 8188144: regression in method reference type-checking
mcimadamore
parents: 47268
diff changeset
  3235
                return ReferenceLookupResult.error(new BadMethodReferenceError(boundRes.isSuccess() ?
a0116bcc65b7 8188144: regression in method reference type-checking
mcimadamore
parents: 47268
diff changeset
  3236
                        boundRes.sym : unboundRes.sym, true));
28456
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3237
            } else {
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3238
                //both searches fail to produce a result - pick 'better' error using heuristics (error recovery)
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3239
                return (boundRes.canIgnore() && !unboundRes.canIgnore()) ?
47297
a0116bcc65b7 8188144: regression in method reference type-checking
mcimadamore
parents: 47268
diff changeset
  3240
                        unboundRes : boundRes;
28456
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3241
            }
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3242
        }
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3243
    };
19129
b1409ae675f8 8020843: javac crashes on accessibility check with method reference with typevar receiver
mcimadamore
parents: 18914
diff changeset
  3244
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3245
    /**
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3246
     * Helper for defining custom method-like lookup logic; a lookup helper
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3247
     * provides hooks for (i) the actual lookup logic and (ii) accessing the
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3248
     * lookup result (this step might result in compiler diagnostics to be generated)
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3249
     */
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3250
    abstract class LookupHelper {
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3251
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3252
        /** name of the symbol to lookup */
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3253
        Name name;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3254
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3255
        /** location in which the lookup takes place */
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3256
        Type site;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3257
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3258
        /** actual types used during the lookup */
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3259
        List<Type> argtypes;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3260
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3261
        /** type arguments used during the lookup */
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3262
        List<Type> typeargtypes;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3263
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3264
        /** Max overload resolution phase handled by this helper */
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3265
        MethodResolutionPhase maxPhase;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3266
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3267
        LookupHelper(Name name, Type site, List<Type> argtypes, List<Type> typeargtypes, MethodResolutionPhase maxPhase) {
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3268
            this.name = name;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3269
            this.site = site;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3270
            this.argtypes = argtypes;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3271
            this.typeargtypes = typeargtypes;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3272
            this.maxPhase = maxPhase;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3273
        }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3274
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3275
        /**
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3276
         * Should lookup stop at given phase with given result
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3277
         */
26533
8fba085fbdbf 8055514: Wrong, confusing error when non-static varargs referenced in static context
mcimadamore
parents: 26266
diff changeset
  3278
        final boolean shouldStop(Symbol sym, MethodResolutionPhase phase) {
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3279
            return phase.ordinal() > maxPhase.ordinal() ||
29774
9d438163db79 8075184: javac is mistakenly considering an missing enclosing instance error as an overload error
vromero
parents: 29554
diff changeset
  3280
                !sym.kind.isResolutionError() || sym.kind == AMBIGUOUS;
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3281
        }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3282
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3283
        /**
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3284
         * Search for a symbol under a given overload resolution phase - this method
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3285
         * is usually called several times, once per each overload resolution phase
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3286
         */
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3287
        abstract Symbol lookup(Env<AttrContext> env, MethodResolutionPhase phase);
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3288
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3289
        /**
18668
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  3290
         * Dump overload resolution info
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  3291
         */
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  3292
        void debug(DiagnosticPosition pos, Symbol sym) {
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  3293
            //do nothing
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  3294
        }
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  3295
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  3296
        /**
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3297
         * Validate the result of the lookup
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3298
         */
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3299
        abstract Symbol access(Env<AttrContext> env, DiagnosticPosition pos, Symbol location, Symbol sym);
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3300
    }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3301
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3302
    abstract class BasicLookupHelper extends LookupHelper {
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3303
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3304
        BasicLookupHelper(Name name, Type site, List<Type> argtypes, List<Type> typeargtypes) {
17804
6bed8a263318 8014649: Regression: bug in Resolve.resolveOperator
mcimadamore
parents: 17582
diff changeset
  3305
            this(name, site, argtypes, typeargtypes, MethodResolutionPhase.VARARITY);
6bed8a263318 8014649: Regression: bug in Resolve.resolveOperator
mcimadamore
parents: 17582
diff changeset
  3306
        }
6bed8a263318 8014649: Regression: bug in Resolve.resolveOperator
mcimadamore
parents: 17582
diff changeset
  3307
6bed8a263318 8014649: Regression: bug in Resolve.resolveOperator
mcimadamore
parents: 17582
diff changeset
  3308
        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
  3309
            super(name, site, argtypes, typeargtypes, maxPhase);
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3310
        }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3311
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3312
        @Override
18668
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  3313
        final Symbol lookup(Env<AttrContext> env, MethodResolutionPhase phase) {
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  3314
            Symbol sym = doLookup(env, phase);
15038
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  3315
            if (sym.kind == AMBIGUOUS) {
22161
d79fb23b5dca 8029569: internal javac cast exception when resolving varargs ambiguity
vromero
parents: 21718
diff changeset
  3316
                AmbiguityError a_err = (AmbiguityError)sym.baseSymbol();
15038
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  3317
                sym = a_err.mergeAbstracts(site);
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  3318
            }
18668
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  3319
            return sym;
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  3320
        }
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  3321
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  3322
        abstract Symbol doLookup(Env<AttrContext> env, MethodResolutionPhase phase);
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  3323
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  3324
        @Override
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  3325
        Symbol access(Env<AttrContext> env, DiagnosticPosition pos, Symbol location, Symbol sym) {
29774
9d438163db79 8075184: javac is mistakenly considering an missing enclosing instance error as an overload error
vromero
parents: 29554
diff changeset
  3326
            if (sym.kind.isResolutionError()) {
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3327
                //if nothing is found return the 'first' error
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3328
                sym = accessMethod(sym, pos, location, site, name, true, argtypes, typeargtypes);
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3329
            }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3330
            return sym;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3331
        }
18668
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  3332
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  3333
        @Override
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  3334
        void debug(DiagnosticPosition pos, Symbol sym) {
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  3335
            reportVerboseResolutionDiagnostic(pos, name, site, argtypes, typeargtypes, sym);
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  3336
        }
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3337
    }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3338
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3339
    /**
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3340
     * Helper class for member reference lookup. A reference lookup helper
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3341
     * defines the basic logic for member reference lookup; a method gives
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3342
     * access to an 'unbound' helper used to perform an unbound member
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3343
     * reference lookup.
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3344
     */
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3345
    abstract class ReferenceLookupHelper extends LookupHelper {
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3346
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3347
        /** The member reference tree */
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3348
        JCMemberReference referenceTree;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3349
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3350
        ReferenceLookupHelper(JCMemberReference referenceTree, Name name, Type site,
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3351
                List<Type> argtypes, List<Type> typeargtypes, MethodResolutionPhase maxPhase) {
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3352
            super(name, site, argtypes, typeargtypes, maxPhase);
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3353
            this.referenceTree = referenceTree;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3354
        }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3355
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3356
        /**
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3357
         * Returns an unbound version of this lookup helper. By default, this
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3358
         * method returns an dummy lookup helper.
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3359
         */
18910
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
  3360
        ReferenceLookupHelper unboundLookup(InferenceContext inferenceContext) {
28456
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3361
            return null;
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3362
        }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3363
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3364
        /**
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3365
         * Get the kind of the member reference
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3366
         */
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3367
        abstract JCMemberReference.ReferenceKind referenceKind(Symbol sym);
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3368
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3369
        Symbol access(Env<AttrContext> env, DiagnosticPosition pos, Symbol location, Symbol sym) {
15038
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  3370
            if (sym.kind == AMBIGUOUS) {
22161
d79fb23b5dca 8029569: internal javac cast exception when resolving varargs ambiguity
vromero
parents: 21718
diff changeset
  3371
                AmbiguityError a_err = (AmbiguityError)sym.baseSymbol();
15038
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  3372
                sym = a_err.mergeAbstracts(site);
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  3373
            }
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3374
            //skip error reporting
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3375
            return sym;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3376
        }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3377
    }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3378
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3379
    /**
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3380
     * Helper class for method reference lookup. The lookup logic is based
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3381
     * upon Resolve.findMethod; in certain cases, this helper class has a
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3382
     * corresponding unbound helper class (see UnboundMethodReferenceLookupHelper).
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3383
     * In such cases, non-static lookup results are thrown away.
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3384
     */
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3385
    class MethodReferenceLookupHelper extends ReferenceLookupHelper {
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3386
29292
c10d63c667cd 8073842: Invalid method reference when referencing a method on a wildcard type
mcimadamore
parents: 29146
diff changeset
  3387
        /** The original method reference lookup site. */
c10d63c667cd 8073842: Invalid method reference when referencing a method on a wildcard type
mcimadamore
parents: 29146
diff changeset
  3388
        Type originalSite;
c10d63c667cd 8073842: Invalid method reference when referencing a method on a wildcard type
mcimadamore
parents: 29146
diff changeset
  3389
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3390
        MethodReferenceLookupHelper(JCMemberReference referenceTree, Name name, Type site,
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3391
                List<Type> argtypes, List<Type> typeargtypes, MethodResolutionPhase maxPhase) {
29292
c10d63c667cd 8073842: Invalid method reference when referencing a method on a wildcard type
mcimadamore
parents: 29146
diff changeset
  3392
            super(referenceTree, name, types.skipTypeVars(site, true), argtypes, typeargtypes, maxPhase);
c10d63c667cd 8073842: Invalid method reference when referencing a method on a wildcard type
mcimadamore
parents: 29146
diff changeset
  3393
            this.originalSite = site;
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3394
        }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3395
16323
c4409c235642 8008537: Missing method reference lookup error when unbound search finds a static method
mcimadamore
parents: 16322
diff changeset
  3396
        @Override
c4409c235642 8008537: Missing method reference lookup error when unbound search finds a static method
mcimadamore
parents: 16322
diff changeset
  3397
        final Symbol lookup(Env<AttrContext> env, MethodResolutionPhase phase) {
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3398
            return findMethod(env, site, name, argtypes, typeargtypes,
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents: 28456
diff changeset
  3399
                    phase.isBoxingRequired(), phase.isVarargsRequired());
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3400
        }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3401
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3402
        @Override
18910
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
  3403
        ReferenceLookupHelper unboundLookup(InferenceContext inferenceContext) {
28456
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3404
            if (TreeInfo.isStaticSelector(referenceTree.expr, names)) {
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3405
                if (argtypes.nonEmpty() &&
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3406
                        (argtypes.head.hasTag(NONE) ||
42974
ae7decb33b43 8169091: Method reference T::methodName for generic type T does not compile any more
mcimadamore
parents: 42828
diff changeset
  3407
                        types.isSubtypeUnchecked(inferenceContext.asUndetVar(argtypes.head), originalSite))) {
28456
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3408
                    return new UnboundMethodReferenceLookupHelper(referenceTree, name,
29292
c10d63c667cd 8073842: Invalid method reference when referencing a method on a wildcard type
mcimadamore
parents: 29146
diff changeset
  3409
                            originalSite, argtypes, typeargtypes, maxPhase);
28456
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3410
                } else {
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3411
                    return new ReferenceLookupHelper(referenceTree, name, site, argtypes, typeargtypes, maxPhase) {
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3412
                        @Override
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3413
                        ReferenceLookupHelper unboundLookup(InferenceContext inferenceContext) {
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3414
                            return this;
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3415
                        }
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3416
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3417
                        @Override
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3418
                        Symbol lookup(Env<AttrContext> env, MethodResolutionPhase phase) {
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3419
                            return methodNotFound;
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3420
                        }
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3421
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3422
                        @Override
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3423
                        ReferenceKind referenceKind(Symbol sym) {
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3424
                            Assert.error();
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3425
                            return null;
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3426
                        }
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3427
                    };
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3428
                }
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3429
            } else {
18910
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
  3430
                return super.unboundLookup(inferenceContext);
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3431
            }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3432
        }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3433
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3434
        @Override
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3435
        ReferenceKind referenceKind(Symbol sym) {
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3436
            if (sym.isStatic()) {
14724
b542db73539a 8004101: Add checks for method reference well-formedness
mcimadamore
parents: 14547
diff changeset
  3437
                return ReferenceKind.STATIC;
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3438
            } else {
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3439
                Name selName = TreeInfo.name(referenceTree.getQualifierExpression());
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3440
                return selName != null && selName == names._super ?
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3441
                        ReferenceKind.SUPER :
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3442
                        ReferenceKind.BOUND;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3443
            }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3444
        }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3445
    }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3446
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3447
    /**
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3448
     * Helper class for unbound method reference lookup. Essentially the same
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3449
     * as the basic method reference lookup helper; main difference is that static
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3450
     * lookup results are thrown away. If qualifier type is raw, an attempt to
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3451
     * infer a parameterized type is made using the first actual argument (that
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3452
     * would otherwise be ignored during the lookup).
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3453
     */
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3454
    class UnboundMethodReferenceLookupHelper extends MethodReferenceLookupHelper {
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3455
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3456
        UnboundMethodReferenceLookupHelper(JCMemberReference referenceTree, Name name, Type site,
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3457
                List<Type> argtypes, List<Type> typeargtypes, MethodResolutionPhase maxPhase) {
16294
0c291a3cd60d 8007462: Fix provisional applicability for method references
mcimadamore
parents: 15706
diff changeset
  3458
            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
  3459
            if (site.isRaw() && !argtypes.head.hasTag(NONE)) {
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  3460
                Type asSuperSite = types.asSuper(argtypes.head, site.tsym);
29292
c10d63c667cd 8073842: Invalid method reference when referencing a method on a wildcard type
mcimadamore
parents: 29146
diff changeset
  3461
                this.site = types.skipTypeVars(asSuperSite, true);
16294
0c291a3cd60d 8007462: Fix provisional applicability for method references
mcimadamore
parents: 15706
diff changeset
  3462
            }
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3463
        }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3464
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3465
        @Override
18910
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18668
diff changeset
  3466
        ReferenceLookupHelper unboundLookup(InferenceContext inferenceContext) {
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3467
            return this;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3468
        }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3469
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3470
        @Override
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3471
        ReferenceKind referenceKind(Symbol sym) {
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3472
            return ReferenceKind.UNBOUND;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3473
        }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3474
    }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3475
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3476
    /**
15360
450af2a9e3c9 8005854: Add support for array constructor references
mcimadamore
parents: 15038
diff changeset
  3477
     * Helper class for array constructor lookup; an array constructor lookup
450af2a9e3c9 8005854: Add support for array constructor references
mcimadamore
parents: 15038
diff changeset
  3478
     * 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
  3479
     * 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
  3480
     */
450af2a9e3c9 8005854: Add support for array constructor references
mcimadamore
parents: 15038
diff changeset
  3481
    class ArrayConstructorReferenceLookupHelper extends ReferenceLookupHelper {
450af2a9e3c9 8005854: Add support for array constructor references
mcimadamore
parents: 15038
diff changeset
  3482
450af2a9e3c9 8005854: Add support for array constructor references
mcimadamore
parents: 15038
diff changeset
  3483
        ArrayConstructorReferenceLookupHelper(JCMemberReference referenceTree, Type site, List<Type> argtypes,
450af2a9e3c9 8005854: Add support for array constructor references
mcimadamore
parents: 15038
diff changeset
  3484
                List<Type> typeargtypes, MethodResolutionPhase maxPhase) {
450af2a9e3c9 8005854: Add support for array constructor references
mcimadamore
parents: 15038
diff changeset
  3485
            super(referenceTree, names.init, site, argtypes, typeargtypes, maxPhase);
450af2a9e3c9 8005854: Add support for array constructor references
mcimadamore
parents: 15038
diff changeset
  3486
        }
450af2a9e3c9 8005854: Add support for array constructor references
mcimadamore
parents: 15038
diff changeset
  3487
450af2a9e3c9 8005854: Add support for array constructor references
mcimadamore
parents: 15038
diff changeset
  3488
        @Override
450af2a9e3c9 8005854: Add support for array constructor references
mcimadamore
parents: 15038
diff changeset
  3489
        protected Symbol lookup(Env<AttrContext> env, MethodResolutionPhase phase) {
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  3490
            WriteableScope sc = WriteableScope.create(syms.arrayClass);
15360
450af2a9e3c9 8005854: Add support for array constructor references
mcimadamore
parents: 15038
diff changeset
  3491
            MethodSymbol arrayConstr = new MethodSymbol(PUBLIC, name, null, site.tsym);
42828
cce89649f958 8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents: 42827
diff changeset
  3492
            arrayConstr.type = new MethodType(List.of(syms.intType), site, List.nil(), syms.methodClass);
15360
450af2a9e3c9 8005854: Add support for array constructor references
mcimadamore
parents: 15038
diff changeset
  3493
            sc.enter(arrayConstr);
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents: 28456
diff changeset
  3494
            return findMethodInScope(env, site, name, argtypes, typeargtypes, sc, methodNotFound, phase.isBoxingRequired(), phase.isVarargsRequired(), false);
15360
450af2a9e3c9 8005854: Add support for array constructor references
mcimadamore
parents: 15038
diff changeset
  3495
        }
450af2a9e3c9 8005854: Add support for array constructor references
mcimadamore
parents: 15038
diff changeset
  3496
450af2a9e3c9 8005854: Add support for array constructor references
mcimadamore
parents: 15038
diff changeset
  3497
        @Override
450af2a9e3c9 8005854: Add support for array constructor references
mcimadamore
parents: 15038
diff changeset
  3498
        ReferenceKind referenceKind(Symbol sym) {
450af2a9e3c9 8005854: Add support for array constructor references
mcimadamore
parents: 15038
diff changeset
  3499
            return ReferenceKind.ARRAY_CTOR;
450af2a9e3c9 8005854: Add support for array constructor references
mcimadamore
parents: 15038
diff changeset
  3500
        }
450af2a9e3c9 8005854: Add support for array constructor references
mcimadamore
parents: 15038
diff changeset
  3501
    }
450af2a9e3c9 8005854: Add support for array constructor references
mcimadamore
parents: 15038
diff changeset
  3502
450af2a9e3c9 8005854: Add support for array constructor references
mcimadamore
parents: 15038
diff changeset
  3503
    /**
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3504
     * Helper class for constructor reference lookup. The lookup logic is based
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3505
     * upon either Resolve.findMethod or Resolve.findDiamond - depending on
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3506
     * whether the constructor reference needs diamond inference (this is the case
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3507
     * if the qualifier type is raw). A special erroneous symbol is returned
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3508
     * if the lookup returns the constructor of an inner class and there's no
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3509
     * enclosing instance in scope.
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3510
     */
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3511
    class ConstructorReferenceLookupHelper extends ReferenceLookupHelper {
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3512
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3513
        boolean needsInference;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3514
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3515
        ConstructorReferenceLookupHelper(JCMemberReference referenceTree, Type site, List<Type> argtypes,
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3516
                List<Type> typeargtypes, MethodResolutionPhase maxPhase) {
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3517
            super(referenceTree, names.init, site, argtypes, typeargtypes, maxPhase);
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3518
            if (site.isRaw()) {
26781
a786b07c7b91 8048614: Add TypeMetadata to contain type annotations and other type information
emc
parents: 26533
diff changeset
  3519
                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
  3520
                needsInference = true;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3521
            }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3522
        }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3523
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3524
        @Override
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3525
        protected Symbol lookup(Env<AttrContext> env, MethodResolutionPhase phase) {
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3526
            Symbol sym = needsInference ?
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3527
                findDiamond(env, site, argtypes, typeargtypes, phase.isBoxingRequired(), phase.isVarargsRequired()) :
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3528
                findMethod(env, site, name, argtypes, typeargtypes,
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents: 28456
diff changeset
  3529
                        phase.isBoxingRequired(), phase.isVarargsRequired());
33019
2dc64d1f5e18 8139245: compiler crashes with exception on int:new method reference and generic method inference
sadayapalam
parents: 32951
diff changeset
  3530
            return enclosingInstanceMissing(env, site) ? new BadConstructorReferenceError(sym) : sym;
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3531
        }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3532
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3533
        @Override
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3534
        ReferenceKind referenceKind(Symbol sym) {
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3535
            return site.getEnclosingType().hasTag(NONE) ?
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3536
                    ReferenceKind.TOPLEVEL : ReferenceKind.IMPLICIT_INNER;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3537
        }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3538
    }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3539
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3540
    /**
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3541
     * Main overload resolution routine. On each overload resolution step, a
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3542
     * lookup helper class is used to perform the method/constructor lookup;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3543
     * at the end of the lookup, the helper is used to validate the results
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3544
     * (this last step might trigger overload resolution diagnostics).
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3545
     */
16805
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  3546
    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
  3547
        MethodResolutionContext resolveContext = new MethodResolutionContext();
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  3548
        resolveContext.methodCheck = methodCheck;
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  3549
        return lookupMethod(env, pos, location, resolveContext, lookupHelper);
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3550
    }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3551
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3552
    Symbol lookupMethod(Env<AttrContext> env, DiagnosticPosition pos, Symbol location,
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3553
            MethodResolutionContext resolveContext, LookupHelper lookupHelper) {
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3554
        MethodResolutionContext prevResolutionContext = currentResolutionContext;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3555
        try {
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3556
            Symbol bestSoFar = methodNotFound;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3557
            currentResolutionContext = resolveContext;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3558
            for (MethodResolutionPhase phase : methodResolutionSteps) {
25445
603f0c93d5c9 8011044: Remove support for 1.5 and earlier source and target options
ntoda
parents: 25443
diff changeset
  3559
                if (lookupHelper.shouldStop(bestSoFar, phase))
603f0c93d5c9 8011044: Remove support for 1.5 and earlier source and target options
ntoda
parents: 25443
diff changeset
  3560
                    break;
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3561
                MethodResolutionPhase prevPhase = currentResolutionContext.step;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3562
                Symbol prevBest = bestSoFar;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3563
                currentResolutionContext.step = phase;
18668
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  3564
                Symbol sym = lookupHelper.lookup(env, phase);
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  3565
                lookupHelper.debug(pos, sym);
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  3566
                bestSoFar = phase.mergeResults(bestSoFar, sym);
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3567
                env.info.pendingResolutionPhase = (prevBest == bestSoFar) ? prevPhase : phase;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3568
            }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3569
            return lookupHelper.access(env, pos, location, bestSoFar);
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3570
        } finally {
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3571
            currentResolutionContext = prevResolutionContext;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3572
        }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3573
    }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3574
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  3575
    /**
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3576
     * Resolve `c.name' where name == this or name == super.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3577
     * @param pos           The position to use for error reporting.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3578
     * @param env           The environment current at the expression.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3579
     * @param c             The qualifier.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3580
     * @param name          The identifier's name.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3581
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3582
    Symbol resolveSelf(DiagnosticPosition pos,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3583
                       Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3584
                       TypeSymbol c,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3585
                       Name name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3586
        Env<AttrContext> env1 = env;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3587
        boolean staticOnly = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3588
        while (env1.outer != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3589
            if (isStatic(env1)) staticOnly = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3590
            if (env1.enclClass.sym == c) {
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  3591
                Symbol sym = env1.info.scope.findFirst(name);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3592
                if (sym != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3593
                    if (staticOnly) sym = new StaticError(sym);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  3594
                    return accessBase(sym, pos, env.enclClass.sym.type,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3595
                                  name, true);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3596
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3597
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3598
            if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3599
            env1 = env1.outer;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3600
        }
23105
1d1ea26a10ae 8030855: Default methods should be visible under source previous to 8
vromero
parents: 22442
diff changeset
  3601
        if (c.isInterface() &&
1d1ea26a10ae 8030855: Default methods should be visible under source previous to 8
vromero
parents: 22442
diff changeset
  3602
            name == names._super && !isStatic(env) &&
1d1ea26a10ae 8030855: Default methods should be visible under source previous to 8
vromero
parents: 22442
diff changeset
  3603
            types.isDirectSuperInterface(c, env.enclClass.sym)) {
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3604
            //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
  3605
            for (Type t : pruneInterfaces(env.enclClass.type)) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3606
                if (t.tsym == c) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3607
                    env.info.defaultSuperCallSite = t;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3608
                    return new VarSymbol(0, names._super,
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3609
                            types.asSuper(env.enclClass.type, c), env.enclClass.sym);
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3610
                }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3611
            }
39092
399acd370763 8058244: missing error in qualified default super call
vromero
parents: 38914
diff changeset
  3612
            //find a direct super type that is a subtype of 'c'
399acd370763 8058244: missing error in qualified default super call
vromero
parents: 38914
diff changeset
  3613
            for (Type i : types.directSupertypes(env.enclClass.type)) {
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3614
                if (i.tsym.isSubClass(c, types) && i.tsym != c) {
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44504
diff changeset
  3615
                    log.error(pos,
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44504
diff changeset
  3616
                              Errors.IllegalDefaultSuperCall(c,
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44504
diff changeset
  3617
                                                             Fragments.RedundantSupertype(c, i)));
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3618
                    return syms.errSymbol;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3619
                }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3620
            }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3621
            Assert.error();
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3622
        }
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44504
diff changeset
  3623
        log.error(pos, Errors.NotEnclClass(c));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3624
        return syms.errSymbol;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3625
    }
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3626
    //where
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3627
    private List<Type> pruneInterfaces(Type t) {
20249
93f8eae31092 6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents: 19940
diff changeset
  3628
        ListBuffer<Type> result = new ListBuffer<>();
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3629
        for (Type t1 : types.interfaces(t)) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3630
            boolean shouldAdd = true;
39092
399acd370763 8058244: missing error in qualified default super call
vromero
parents: 38914
diff changeset
  3631
            for (Type t2 : types.directSupertypes(t)) {
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3632
                if (t1 != t2 && types.isSubtypeNoCapture(t2, t1)) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3633
                    shouldAdd = false;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3634
                }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3635
            }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3636
            if (shouldAdd) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3637
                result.append(t1);
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3638
            }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3639
        }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3640
        return result.toList();
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3641
    }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14359
diff changeset
  3642
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3643
06bc494ca11e Initial load
duke
parents:
diff changeset
  3644
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  3645
     * Resolve `c.this' for an enclosing class c that contains the
06bc494ca11e Initial load
duke
parents:
diff changeset
  3646
     * named member.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3647
     * @param pos           The position to use for error reporting.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3648
     * @param env           The environment current at the expression.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3649
     * @param member        The member that must be contained in the result.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3650
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3651
    Symbol resolveSelfContaining(DiagnosticPosition pos,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3652
                                 Env<AttrContext> env,
8622
4f032629a0fd 6541876: "Enclosing Instance" error new in 1.6
mcimadamore
parents: 8430
diff changeset
  3653
                                 Symbol member,
4f032629a0fd 6541876: "Enclosing Instance" error new in 1.6
mcimadamore
parents: 8430
diff changeset
  3654
                                 boolean isSuperCall) {
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3655
        Symbol sym = resolveSelfContainingInternal(env, member, isSuperCall);
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3656
        if (sym == null) {
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44504
diff changeset
  3657
            log.error(pos, Errors.EnclClassRequired(member));
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3658
            return syms.errSymbol;
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3659
        } else {
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3660
            return accessBase(sym, pos, env.enclClass.sym.type, sym.name, true);
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3661
        }
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3662
    }
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3663
33019
2dc64d1f5e18 8139245: compiler crashes with exception on int:new method reference and generic method inference
sadayapalam
parents: 32951
diff changeset
  3664
    boolean enclosingInstanceMissing(Env<AttrContext> env, Type type) {
2dc64d1f5e18 8139245: compiler crashes with exception on int:new method reference and generic method inference
sadayapalam
parents: 32951
diff changeset
  3665
        if (type.hasTag(CLASS) && type.getEnclosingType().hasTag(CLASS)) {
2dc64d1f5e18 8139245: compiler crashes with exception on int:new method reference and generic method inference
sadayapalam
parents: 32951
diff changeset
  3666
            Symbol encl = resolveSelfContainingInternal(env, type.tsym, false);
2dc64d1f5e18 8139245: compiler crashes with exception on int:new method reference and generic method inference
sadayapalam
parents: 32951
diff changeset
  3667
            return encl == null || encl.kind.isResolutionError();
2dc64d1f5e18 8139245: compiler crashes with exception on int:new method reference and generic method inference
sadayapalam
parents: 32951
diff changeset
  3668
        }
2dc64d1f5e18 8139245: compiler crashes with exception on int:new method reference and generic method inference
sadayapalam
parents: 32951
diff changeset
  3669
        return false;
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3670
    }
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3671
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3672
    private Symbol resolveSelfContainingInternal(Env<AttrContext> env,
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3673
                                 Symbol member,
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3674
                                 boolean isSuperCall) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3675
        Name name = names._this;
8622
4f032629a0fd 6541876: "Enclosing Instance" error new in 1.6
mcimadamore
parents: 8430
diff changeset
  3676
        Env<AttrContext> env1 = isSuperCall ? env.outer : env;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3677
        boolean staticOnly = false;
8622
4f032629a0fd 6541876: "Enclosing Instance" error new in 1.6
mcimadamore
parents: 8430
diff changeset
  3678
        if (env1 != null) {
4f032629a0fd 6541876: "Enclosing Instance" error new in 1.6
mcimadamore
parents: 8430
diff changeset
  3679
            while (env1 != null && env1.outer != null) {
4f032629a0fd 6541876: "Enclosing Instance" error new in 1.6
mcimadamore
parents: 8430
diff changeset
  3680
                if (isStatic(env1)) staticOnly = true;
34561
79c436257611 8144673: Suspect message regarding suitable enclosing instance not being in scope
sadayapalam
parents: 33019
diff changeset
  3681
                if (env1.enclClass.sym.isSubClass(member.owner.enclClass(), types)) {
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25435
diff changeset
  3682
                    Symbol sym = env1.info.scope.findFirst(name);
8622
4f032629a0fd 6541876: "Enclosing Instance" error new in 1.6
mcimadamore
parents: 8430
diff changeset
  3683
                    if (sym != null) {
4f032629a0fd 6541876: "Enclosing Instance" error new in 1.6
mcimadamore
parents: 8430
diff changeset
  3684
                        if (staticOnly) sym = new StaticError(sym);
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3685
                        return sym;
8622
4f032629a0fd 6541876: "Enclosing Instance" error new in 1.6
mcimadamore
parents: 8430
diff changeset
  3686
                    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3687
                }
8622
4f032629a0fd 6541876: "Enclosing Instance" error new in 1.6
mcimadamore
parents: 8430
diff changeset
  3688
                if ((env1.enclClass.sym.flags() & STATIC) != 0)
4f032629a0fd 6541876: "Enclosing Instance" error new in 1.6
mcimadamore
parents: 8430
diff changeset
  3689
                    staticOnly = true;
4f032629a0fd 6541876: "Enclosing Instance" error new in 1.6
mcimadamore
parents: 8430
diff changeset
  3690
                env1 = env1.outer;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3691
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3692
        }
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3693
        return null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3694
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3695
06bc494ca11e Initial load
duke
parents:
diff changeset
  3696
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  3697
     * 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
  3698
     * JLS 8.8.5.1 and 15.9.2
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3699
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3700
    Type resolveImplicitThis(DiagnosticPosition pos, Env<AttrContext> env, Type t) {
8622
4f032629a0fd 6541876: "Enclosing Instance" error new in 1.6
mcimadamore
parents: 8430
diff changeset
  3701
        return resolveImplicitThis(pos, env, t, false);
4f032629a0fd 6541876: "Enclosing Instance" error new in 1.6
mcimadamore
parents: 8430
diff changeset
  3702
    }
4f032629a0fd 6541876: "Enclosing Instance" error new in 1.6
mcimadamore
parents: 8430
diff changeset
  3703
4f032629a0fd 6541876: "Enclosing Instance" error new in 1.6
mcimadamore
parents: 8430
diff changeset
  3704
    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
  3705
        Type thisType = (t.tsym.owner.kind.matches(KindSelector.VAL_MTH)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3706
                         ? resolveSelf(pos, env, t.getEnclosingType().tsym, names._this)
8622
4f032629a0fd 6541876: "Enclosing Instance" error new in 1.6
mcimadamore
parents: 8430
diff changeset
  3707
                         : resolveSelfContaining(pos, env, t.tsym, isSuperCall)).type;
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44504
diff changeset
  3708
        if (env.info.isSelfCall && thisType.tsym == env.enclClass.sym) {
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44504
diff changeset
  3709
            log.error(pos, Errors.CantRefBeforeCtorCalled("this"));
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44504
diff changeset
  3710
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3711
        return thisType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3712
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3713
06bc494ca11e Initial load
duke
parents:
diff changeset
  3714
/* ***************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
  3715
 *  ResolveError classes, indicating error situations when accessing symbols
06bc494ca11e Initial load
duke
parents:
diff changeset
  3716
 ****************************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
  3717
12082
32bdf6ef6388 7151070: NullPointerException in Resolve.isAccessible
mcimadamore
parents: 12078
diff changeset
  3718
    //used by TransTypes when checking target type of synthetic cast
32bdf6ef6388 7151070: NullPointerException in Resolve.isAccessible
mcimadamore
parents: 12078
diff changeset
  3719
    public void logAccessErrorInternal(Env<AttrContext> env, JCTree tree, Type type) {
32bdf6ef6388 7151070: NullPointerException in Resolve.isAccessible
mcimadamore
parents: 12078
diff changeset
  3720
        AccessError error = new AccessError(env, env.enclClass.type, type.tsym);
32bdf6ef6388 7151070: NullPointerException in Resolve.isAccessible
mcimadamore
parents: 12078
diff changeset
  3721
        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
  3722
    }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3723
    //where
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3724
    private void logResolveError(ResolveError error,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3725
            DiagnosticPosition pos,
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  3726
            Symbol location,
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3727
            Type site,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3728
            Name name,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3729
            List<Type> argtypes,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3730
            List<Type> typeargtypes) {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3731
        JCDiagnostic d = error.getDiagnostic(JCDiagnostic.DiagnosticType.ERROR,
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  3732
                pos, location, site, name, argtypes, typeargtypes);
6356
af24929939ca 6976747: JCDiagnostic: replace "boolean mandatory" with new "Set<JCDiagnostic.Flag>"
jjg
parents: 6344
diff changeset
  3733
        if (d != null) {
af24929939ca 6976747: JCDiagnostic: replace "boolean mandatory" with new "Set<JCDiagnostic.Flag>"
jjg
parents: 6344
diff changeset
  3734
            d.setFlag(DiagnosticFlag.RESOLVE_ERROR);
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3735
            log.report(d);
6356
af24929939ca 6976747: JCDiagnostic: replace "boolean mandatory" with new "Set<JCDiagnostic.Flag>"
jjg
parents: 6344
diff changeset
  3736
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3737
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3738
1534
e923a41e84cc 6758789: Some method resolution diagnostic should be improved
mcimadamore
parents: 1533
diff changeset
  3739
    private final LocalizedString noArgs = new LocalizedString("compiler.misc.no.args");
e923a41e84cc 6758789: Some method resolution diagnostic should be improved
mcimadamore
parents: 1533
diff changeset
  3740
e923a41e84cc 6758789: Some method resolution diagnostic should be improved
mcimadamore
parents: 1533
diff changeset
  3741
    public Object methodArguments(List<Type> argtypes) {
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  3742
        if (argtypes == null || argtypes.isEmpty()) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  3743
            return noArgs;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  3744
        } else {
20249
93f8eae31092 6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents: 19940
diff changeset
  3745
            ListBuffer<Object> diagArgs = new ListBuffer<>();
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  3746
            for (Type t : argtypes) {
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14062
diff changeset
  3747
                if (t.hasTag(DEFERRED)) {
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  3748
                    diagArgs.append(((DeferredAttr.DeferredType)t).tree);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  3749
                } else {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  3750
                    diagArgs.append(t);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  3751
                }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  3752
            }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  3753
            return diagArgs;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  3754
        }
1534
e923a41e84cc 6758789: Some method resolution diagnostic should be improved
mcimadamore
parents: 1533
diff changeset
  3755
    }
e923a41e84cc 6758789: Some method resolution diagnostic should be improved
mcimadamore
parents: 1533
diff changeset
  3756
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3757
    /**
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3758
     * Root class for resolution errors. Subclass of ResolveError
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3759
     * represent a different kinds of resolution error - as such they must
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3760
     * specify how they map into concrete compiler diagnostics.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3761
     */
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3762
    abstract class ResolveError extends Symbol {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3763
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3764
        /** The name of the kind of error, for debugging only. */
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3765
        final String debugName;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3766
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  3767
        ResolveError(Kind kind, String debugName) {
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3768
            super(kind, 0, null, null, null);
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3769
            this.debugName = debugName;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3770
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3771
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
  3772
        @Override @DefinedBy(Api.LANGUAGE_MODEL)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3773
        public <R, P> R accept(ElementVisitor<R, P> v, P p) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3774
            throw new AssertionError();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3775
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3776
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3777
        @Override
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3778
        public String toString() {
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3779
            return debugName;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3780
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3781
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3782
        @Override
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3783
        public boolean exists() {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3784
            return false;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3785
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3786
21718
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  3787
        @Override
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  3788
        public boolean isStatic() {
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  3789
            return false;
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  3790
        }
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  3791
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3792
        /**
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3793
         * Create an external representation for this erroneous symbol to be
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3794
         * used during attribution - by default this returns the symbol of a
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3795
         * brand new error type which stores the original type found
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3796
         * during resolution.
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3797
         *
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3798
         * @param name     the name used during resolution
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3799
         * @param location the location from which the symbol is accessed
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3800
         */
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3801
        protected Symbol access(Name name, TypeSymbol location) {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3802
            return types.createErrorType(name, location, syms.errSymbol.type).tsym;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3803
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3804
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3805
        /**
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3806
         * Create a diagnostic representing this resolution error.
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3807
         *
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3808
         * @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
  3809
         * @param pos       The position to be used for error reporting.
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3810
         * @param site      The original type from where the selection took place.
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3811
         * @param name      The name of the symbol to be resolved.
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3812
         * @param argtypes  The invocation's value arguments,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3813
         *                  if we looked for a method.
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3814
         * @param typeargtypes  The invocation's type arguments,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3815
         *                      if we looked for a method.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3816
         */
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3817
        abstract JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3818
                DiagnosticPosition pos,
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  3819
                Symbol location,
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3820
                Type site,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3821
                Name name,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3822
                List<Type> argtypes,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3823
                List<Type> typeargtypes);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3824
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3825
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3826
    /**
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3827
     * 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
  3828
     * an invalid symbol being found during resolution.
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3829
     */
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3830
    abstract class InvalidSymbolError extends ResolveError {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3831
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3832
        /** The invalid symbol found during resolution */
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3833
        Symbol sym;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3834
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  3835
        InvalidSymbolError(Kind kind, Symbol sym, String debugName) {
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3836
            super(kind, debugName);
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3837
            this.sym = sym;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3838
        }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3839
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3840
        @Override
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3841
        public boolean exists() {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3842
            return true;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3843
        }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3844
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3845
        @Override
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3846
        public String toString() {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3847
             return super.toString() + " wrongSym=" + sym;
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
        @Override
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3851
        public Symbol access(Name name, TypeSymbol location) {
29774
9d438163db79 8075184: javac is mistakenly considering an missing enclosing instance error as an overload error
vromero
parents: 29554
diff changeset
  3852
            if (!sym.kind.isResolutionError() && sym.kind.matches(KindSelector.TYP))
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3853
                return types.createErrorType(name, location, sym.type).tsym;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3854
            else
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3855
                return sym;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3856
        }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3857
    }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3858
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 51047
diff changeset
  3859
    class BadRestrictedTypeError extends ResolveError {
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 51047
diff changeset
  3860
        private final Name typeName;
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 51047
diff changeset
  3861
        BadRestrictedTypeError(Name typeName) {
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 51047
diff changeset
  3862
            super(Kind.BAD_RESTRICTED_TYPE, "bad var use");
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 51047
diff changeset
  3863
            this.typeName = typeName;
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
  3864
        }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
  3865
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
  3866
        @Override
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
  3867
        JCDiagnostic getDiagnostic(DiagnosticType dkind, DiagnosticPosition pos, Symbol location, Type site, Name name, List<Type> argtypes, List<Type> typeargtypes) {
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 51047
diff changeset
  3868
            return diags.create(dkind, log.currentSource(), pos, "illegal.ref.to.restricted.type", typeName);
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
  3869
        }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
  3870
    }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
  3871
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3872
    /**
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3873
     * InvalidSymbolError error class indicating that a symbol matching a
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3874
     * given name does not exists in a given site.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3875
     */
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3876
    class SymbolNotFoundError extends ResolveError {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3877
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  3878
        SymbolNotFoundError(Kind kind) {
21718
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  3879
            this(kind, "symbol not found error");
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  3880
        }
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  3881
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  3882
        SymbolNotFoundError(Kind kind, String debugName) {
21718
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21024
diff changeset
  3883
            super(kind, debugName);
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3884
        }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3885
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3886
        @Override
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3887
        JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3888
                DiagnosticPosition pos,
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  3889
                Symbol location,
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3890
                Type site,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3891
                Name name,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3892
                List<Type> argtypes,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3893
                List<Type> typeargtypes) {
42828
cce89649f958 8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents: 42827
diff changeset
  3894
            argtypes = argtypes == null ? List.nil() : argtypes;
cce89649f958 8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents: 42827
diff changeset
  3895
            typeargtypes = typeargtypes == null ? List.nil() : typeargtypes;
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3896
            if (name == names.error)
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3897
                return null;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3898
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3899
            boolean hasLocation = false;
8239
d2c934e951e2 7014715: javac returns different error code for certain failure(s)
mcimadamore
parents: 8237
diff changeset
  3900
            if (location == null) {
d2c934e951e2 7014715: javac returns different error code for certain failure(s)
mcimadamore
parents: 8237
diff changeset
  3901
                location = site.tsym;
d2c934e951e2 7014715: javac returns different error code for certain failure(s)
mcimadamore
parents: 8237
diff changeset
  3902
            }
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  3903
            if (!location.name.isEmpty()) {
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  3904
                if (location.kind == PCK && !site.tsym.exists()) {
6151
dd513881e71d 6957438: improve code for generating warning messages containing option names
jjg
parents: 5857
diff changeset
  3905
                    return diags.create(dkind, log.currentSource(), pos,
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  3906
                        "doesnt.exist", location);
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3907
                }
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  3908
                hasLocation = !location.name.equals(names._this) &&
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  3909
                        !location.name.equals(names._super);
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3910
            }
28456
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3911
            boolean isConstructor = name == names.init;
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  3912
            KindName kindname = isConstructor ? KindName.CONSTRUCTOR : kind.absentKind();
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3913
            Name idname = isConstructor ? site.tsym.name : name;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3914
            String errKey = getErrorKey(kindname, typeargtypes.nonEmpty(), hasLocation);
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3915
            if (hasLocation) {
6151
dd513881e71d 6957438: improve code for generating warning messages containing option names
jjg
parents: 5857
diff changeset
  3916
                return diags.create(dkind, log.currentSource(), pos,
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3917
                        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
  3918
                        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
  3919
                        getLocationDiag(location, site)); //location kindname, type
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3920
            }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3921
            else {
6151
dd513881e71d 6957438: improve code for generating warning messages containing option names
jjg
parents: 5857
diff changeset
  3922
                return diags.create(dkind, log.currentSource(), pos,
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3923
                        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
  3924
                        typeargtypes, args(argtypes)); //type parameters and arguments (if any)
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3925
            }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3926
        }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3927
        //where
14953
89a34297b198 8004099: Bad compiler diagnostic generated when poly expression is passed to non-existent method
mcimadamore
parents: 14801
diff changeset
  3928
        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
  3929
            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
  3930
        }
89a34297b198 8004099: Bad compiler diagnostic generated when poly expression is passed to non-existent method
mcimadamore
parents: 14801
diff changeset
  3931
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3932
        private String getErrorKey(KindName kindname, boolean hasTypeArgs, boolean hasLocation) {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3933
            String key = "cant.resolve";
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3934
            String suffix = hasLocation ? ".location" : "";
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3935
            switch (kindname) {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3936
                case METHOD:
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3937
                case CONSTRUCTOR: {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3938
                    suffix += ".args";
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3939
                    suffix += hasTypeArgs ? ".params" : "";
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3940
                }
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
            return key + suffix;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3943
        }
8239
d2c934e951e2 7014715: javac returns different error code for certain failure(s)
mcimadamore
parents: 8237
diff changeset
  3944
        private JCDiagnostic getLocationDiag(Symbol location, Type site) {
d2c934e951e2 7014715: javac returns different error code for certain failure(s)
mcimadamore
parents: 8237
diff changeset
  3945
            if (location.kind == VAR) {
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44504
diff changeset
  3946
                return diags.fragment(Fragments.Location1(kindName(location),
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44504
diff changeset
  3947
                                                          location,
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44504
diff changeset
  3948
                                                          location.type));
8239
d2c934e951e2 7014715: javac returns different error code for certain failure(s)
mcimadamore
parents: 8237
diff changeset
  3949
            } else {
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44504
diff changeset
  3950
                return diags.fragment(Fragments.Location(typeKindName(site),
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44504
diff changeset
  3951
                                      site,
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44504
diff changeset
  3952
                                      null));
8239
d2c934e951e2 7014715: javac returns different error code for certain failure(s)
mcimadamore
parents: 8237
diff changeset
  3953
            }
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  3954
        }
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3955
    }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3956
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3957
    /**
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3958
     * InvalidSymbolError error class indicating that a given symbol
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3959
     * (either a method, a constructor or an operand) is not applicable
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3960
     * given an actual arguments/type argument list.
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3961
     */
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  3962
    class InapplicableSymbolError extends ResolveError {
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3963
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3964
        protected MethodResolutionContext resolveContext;
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3965
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3966
        InapplicableSymbolError(MethodResolutionContext context) {
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3967
            this(WRONG_MTH, "inapplicable symbol error", context);
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3968
        }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3969
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  3970
        protected InapplicableSymbolError(Kind kind, String debugName, MethodResolutionContext context) {
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  3971
            super(kind, debugName);
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3972
            this.resolveContext = context;
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3973
        }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3974
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3975
        @Override
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3976
        public String toString() {
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  3977
            return super.toString();
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  3978
        }
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  3979
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  3980
        @Override
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  3981
        public boolean exists() {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  3982
            return true;
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3983
        }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3984
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3985
        @Override
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3986
        JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3987
                DiagnosticPosition pos,
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  3988
                Symbol location,
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3989
                Type site,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3990
                Name name,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3991
                List<Type> argtypes,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3992
                List<Type> typeargtypes) {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3993
            if (name == names.error)
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3994
                return null;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  3995
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents: 28456
diff changeset
  3996
            Pair<Symbol, JCDiagnostic> c = errCandidate();
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents: 28456
diff changeset
  3997
            if (compactMethodDiags) {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents: 28456
diff changeset
  3998
                JCDiagnostic simpleDiag =
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents: 28456
diff changeset
  3999
                    MethodResolutionDiagHelper.rewrite(diags, pos, log.currentSource(), dkind, c.snd);
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents: 28456
diff changeset
  4000
                if (simpleDiag != null) {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents: 28456
diff changeset
  4001
                    return simpleDiag;
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents: 28456
diff changeset
  4002
                }
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4003
            }
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents: 28456
diff changeset
  4004
            Symbol ws = c.fst.asMemberOf(site, types);
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents: 28456
diff changeset
  4005
            return diags.create(dkind, log.currentSource(), pos,
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents: 28456
diff changeset
  4006
                      "cant.apply.symbol",
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents: 28456
diff changeset
  4007
                      kindName(ws),
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents: 28456
diff changeset
  4008
                      ws.name == names.init ? ws.owner.name : ws.name,
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents: 28456
diff changeset
  4009
                      methodArguments(ws.type.getParameterTypes()),
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents: 28456
diff changeset
  4010
                      methodArguments(argtypes),
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents: 28456
diff changeset
  4011
                      kindName(ws.owner),
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents: 28456
diff changeset
  4012
                      ws.owner.type,
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents: 28456
diff changeset
  4013
                      c.snd);
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4014
        }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4015
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4016
        @Override
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4017
        public Symbol access(Name name, TypeSymbol location) {
50898
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50675
diff changeset
  4018
            Symbol sym = bestCandidate();
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50675
diff changeset
  4019
            return types.createErrorType(name, location, sym != null ? sym.type : syms.errSymbol.type).tsym;
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50675
diff changeset
  4020
        }
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50675
diff changeset
  4021
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50675
diff changeset
  4022
        protected Symbol bestCandidate() {
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50675
diff changeset
  4023
            return errCandidate().fst;
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4024
        }
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4025
18913
a29ea2022929 8020286: Wrong diagnostic after compaction
mcimadamore
parents: 18911
diff changeset
  4026
        protected Pair<Symbol, JCDiagnostic> errCandidate() {
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  4027
            Candidate bestSoFar = null;
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  4028
            for (Candidate c : resolveContext.candidates) {
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  4029
                if (c.isApplicable()) continue;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  4030
                bestSoFar = c;
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4031
            }
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  4032
            Assert.checkNonNull(bestSoFar);
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22161
diff changeset
  4033
            return new Pair<>(bestSoFar.sym, bestSoFar.details);
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4034
        }
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4035
    }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4036
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4037
    /**
28331
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4038
     * 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
  4039
     * is not applicable given an actual arguments/type argument list.
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4040
     */
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4041
    class InapplicableSymbolsError extends InapplicableSymbolError {
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
  4042
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  4043
        InapplicableSymbolsError(MethodResolutionContext context) {
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  4044
            super(WRONG_MTHS, "inapplicable symbols", context);
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4045
        }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4046
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4047
        @Override
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4048
        JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4049
                DiagnosticPosition pos,
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  4050
                Symbol location,
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4051
                Type site,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4052
                Name name,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4053
                List<Type> argtypes,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4054
                List<Type> typeargtypes) {
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4055
            Map<Symbol, JCDiagnostic> candidatesMap = mapCandidates();
19129
b1409ae675f8 8020843: javac crashes on accessibility check with method reference with typevar receiver
mcimadamore
parents: 18914
diff changeset
  4056
            Map<Symbol, JCDiagnostic> filteredCandidates = compactMethodDiags ?
b1409ae675f8 8020843: javac crashes on accessibility check with method reference with typevar receiver
mcimadamore
parents: 18914
diff changeset
  4057
                    filterCandidates(candidatesMap) :
b1409ae675f8 8020843: javac crashes on accessibility check with method reference with typevar receiver
mcimadamore
parents: 18914
diff changeset
  4058
                    mapCandidates();
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4059
            if (filteredCandidates.isEmpty()) {
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4060
                filteredCandidates = candidatesMap;
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4061
            }
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4062
            boolean truncatedDiag = candidatesMap.size() != filteredCandidates.size();
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4063
            if (filteredCandidates.size() > 1) {
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
  4064
                JCDiagnostic err = diags.create(dkind,
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4065
                        null,
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4066
                        truncatedDiag ?
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4067
                            EnumSet.of(DiagnosticFlag.COMPRESSED) :
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4068
                            EnumSet.noneOf(DiagnosticFlag.class),
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
  4069
                        log.currentSource(),
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
  4070
                        pos,
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
  4071
                        "cant.apply.symbols",
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  4072
                        name == names.init ? KindName.CONSTRUCTOR : kind.absentKind(),
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  4073
                        name == names.init ? site.tsym.name : name,
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  4074
                        methodArguments(argtypes));
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4075
                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
  4076
            } else if (filteredCandidates.size() == 1) {
18913
a29ea2022929 8020286: Wrong diagnostic after compaction
mcimadamore
parents: 18911
diff changeset
  4077
                Map.Entry<Symbol, JCDiagnostic> _e =
a29ea2022929 8020286: Wrong diagnostic after compaction
mcimadamore
parents: 18911
diff changeset
  4078
                                filteredCandidates.entrySet().iterator().next();
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22161
diff changeset
  4079
                final Pair<Symbol, JCDiagnostic> p = new Pair<>(_e.getKey(), _e.getValue());
18913
a29ea2022929 8020286: Wrong diagnostic after compaction
mcimadamore
parents: 18911
diff changeset
  4080
                JCDiagnostic d = new InapplicableSymbolError(resolveContext) {
a29ea2022929 8020286: Wrong diagnostic after compaction
mcimadamore
parents: 18911
diff changeset
  4081
                    @Override
a29ea2022929 8020286: Wrong diagnostic after compaction
mcimadamore
parents: 18911
diff changeset
  4082
                    protected Pair<Symbol, JCDiagnostic> errCandidate() {
a29ea2022929 8020286: Wrong diagnostic after compaction
mcimadamore
parents: 18911
diff changeset
  4083
                        return p;
a29ea2022929 8020286: Wrong diagnostic after compaction
mcimadamore
parents: 18911
diff changeset
  4084
                    }
a29ea2022929 8020286: Wrong diagnostic after compaction
mcimadamore
parents: 18911
diff changeset
  4085
                }.getDiagnostic(dkind, pos,
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4086
                    location, site, name, argtypes, typeargtypes);
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4087
                if (truncatedDiag) {
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4088
                    d.setFlag(DiagnosticFlag.COMPRESSED);
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4089
                }
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4090
                return d;
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
  4091
            } else {
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
  4092
                return new SymbolNotFoundError(ABSENT_MTH).getDiagnostic(dkind, pos,
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  4093
                    location, site, name, argtypes, typeargtypes);
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
  4094
            }
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
  4095
        }
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
  4096
        //where
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4097
            private Map<Symbol, JCDiagnostic> mapCandidates() {
47298
2e947e1bd907 8187978: javac can show overload error messages that include non-valid candidates
vromero
parents: 47297
diff changeset
  4098
                MostSpecificMap candidates = new MostSpecificMap();
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4099
                for (Candidate c : resolveContext.candidates) {
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4100
                    if (c.isApplicable()) continue;
47298
2e947e1bd907 8187978: javac can show overload error messages that include non-valid candidates
vromero
parents: 47297
diff changeset
  4101
                    candidates.put(c);
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4102
                }
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4103
                return candidates;
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4104
            }
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4105
47298
2e947e1bd907 8187978: javac can show overload error messages that include non-valid candidates
vromero
parents: 47297
diff changeset
  4106
            @SuppressWarnings("serial")
2e947e1bd907 8187978: javac can show overload error messages that include non-valid candidates
vromero
parents: 47297
diff changeset
  4107
            private class MostSpecificMap extends LinkedHashMap<Symbol, JCDiagnostic> {
2e947e1bd907 8187978: javac can show overload error messages that include non-valid candidates
vromero
parents: 47297
diff changeset
  4108
                private void put(Candidate c) {
2e947e1bd907 8187978: javac can show overload error messages that include non-valid candidates
vromero
parents: 47297
diff changeset
  4109
                    ListBuffer<Symbol> overridden = new ListBuffer<>();
2e947e1bd907 8187978: javac can show overload error messages that include non-valid candidates
vromero
parents: 47297
diff changeset
  4110
                    for (Symbol s : keySet()) {
2e947e1bd907 8187978: javac can show overload error messages that include non-valid candidates
vromero
parents: 47297
diff changeset
  4111
                        if (s == c.sym) {
2e947e1bd907 8187978: javac can show overload error messages that include non-valid candidates
vromero
parents: 47297
diff changeset
  4112
                            continue;
2e947e1bd907 8187978: javac can show overload error messages that include non-valid candidates
vromero
parents: 47297
diff changeset
  4113
                        }
2e947e1bd907 8187978: javac can show overload error messages that include non-valid candidates
vromero
parents: 47297
diff changeset
  4114
                        if (c.sym.overrides(s, (TypeSymbol)s.owner, types, false)) {
2e947e1bd907 8187978: javac can show overload error messages that include non-valid candidates
vromero
parents: 47297
diff changeset
  4115
                            overridden.add(s);
2e947e1bd907 8187978: javac can show overload error messages that include non-valid candidates
vromero
parents: 47297
diff changeset
  4116
                        } else if (s.overrides(c.sym, (TypeSymbol)c.sym.owner, types, false)) {
2e947e1bd907 8187978: javac can show overload error messages that include non-valid candidates
vromero
parents: 47297
diff changeset
  4117
                            return;
2e947e1bd907 8187978: javac can show overload error messages that include non-valid candidates
vromero
parents: 47297
diff changeset
  4118
                        }
2e947e1bd907 8187978: javac can show overload error messages that include non-valid candidates
vromero
parents: 47297
diff changeset
  4119
                    }
2e947e1bd907 8187978: javac can show overload error messages that include non-valid candidates
vromero
parents: 47297
diff changeset
  4120
                    for (Symbol s : overridden) {
2e947e1bd907 8187978: javac can show overload error messages that include non-valid candidates
vromero
parents: 47297
diff changeset
  4121
                        remove(s);
2e947e1bd907 8187978: javac can show overload error messages that include non-valid candidates
vromero
parents: 47297
diff changeset
  4122
                    }
2e947e1bd907 8187978: javac can show overload error messages that include non-valid candidates
vromero
parents: 47297
diff changeset
  4123
                    put(c.sym, c.details);
2e947e1bd907 8187978: javac can show overload error messages that include non-valid candidates
vromero
parents: 47297
diff changeset
  4124
                }
2e947e1bd907 8187978: javac can show overload error messages that include non-valid candidates
vromero
parents: 47297
diff changeset
  4125
            }
2e947e1bd907 8187978: javac can show overload error messages that include non-valid candidates
vromero
parents: 47297
diff changeset
  4126
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4127
            Map<Symbol, JCDiagnostic> filterCandidates(Map<Symbol, JCDiagnostic> candidatesMap) {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22161
diff changeset
  4128
                Map<Symbol, JCDiagnostic> candidates = new LinkedHashMap<>();
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4129
                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
  4130
                    JCDiagnostic d = _entry.getValue();
19129
b1409ae675f8 8020843: javac crashes on accessibility check with method reference with typevar receiver
mcimadamore
parents: 18914
diff changeset
  4131
                    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
  4132
                        candidates.put(_entry.getKey(), d);
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
                }
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4135
                return candidates;
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4136
            }
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
            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
  4139
                List<JCDiagnostic> details = List.nil();
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4140
                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
  4141
                    Symbol sym = _entry.getKey();
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44504
diff changeset
  4142
                    JCDiagnostic detailDiag =
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44504
diff changeset
  4143
                            diags.fragment(Fragments.InapplicableMethod(Kinds.kindName(sym),
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44504
diff changeset
  4144
                                                                        sym.location(site, types),
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44504
diff changeset
  4145
                                                                        sym.asMemberOf(site, types),
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44504
diff changeset
  4146
                                                                        _entry.getValue()));
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4147
                    details = details.prepend(detailDiag);
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4148
                }
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4149
                //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
  4150
                //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
  4151
                //conform to source order
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4152
                return details;
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4153
            }
50898
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50675
diff changeset
  4154
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50675
diff changeset
  4155
        @Override
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50675
diff changeset
  4156
        protected Symbol bestCandidate() {
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50675
diff changeset
  4157
            Map<Symbol, JCDiagnostic> candidatesMap = mapCandidates();
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50675
diff changeset
  4158
            Map<Symbol, JCDiagnostic> filteredCandidates = filterCandidates(candidatesMap);
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50675
diff changeset
  4159
            if (filteredCandidates.size() == 1) {
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50675
diff changeset
  4160
                return filteredCandidates.keySet().iterator().next();
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50675
diff changeset
  4161
            }
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50675
diff changeset
  4162
            return null;
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50675
diff changeset
  4163
        }
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4164
    }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4165
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4166
    /**
28331
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4167
     * 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
  4168
     * given an actual arguments/type argument list using diamond inference.
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4169
     */
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4170
    class DiamondError extends InapplicableSymbolError {
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
        Symbol sym;
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
        public DiamondError(Symbol sym, MethodResolutionContext context) {
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4175
            super(sym.kind, "diamondError", context);
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4176
            this.sym = sym;
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4177
        }
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
        JCDiagnostic getDetails() {
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4180
            return (sym.kind == WRONG_MTH) ?
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4181
                    ((InapplicableSymbolError)sym.baseSymbol()).errCandidate().snd :
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4182
                    null;
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4183
        }
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4184
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4185
        @Override
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4186
        JCDiagnostic getDiagnostic(DiagnosticType dkind, DiagnosticPosition pos,
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4187
                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
  4188
            JCDiagnostic details = getDetails();
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4189
            if (details != null && compactMethodDiags) {
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4190
                JCDiagnostic simpleDiag =
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4191
                        MethodResolutionDiagHelper.rewrite(diags, pos, log.currentSource(), dkind, details);
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4192
                if (simpleDiag != null) {
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4193
                    return simpleDiag;
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4194
                }
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4195
            }
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4196
            String key = details == null ?
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4197
                "cant.apply.diamond" :
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4198
                "cant.apply.diamond.1";
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4199
            return diags.create(dkind, log.currentSource(), pos, key,
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44504
diff changeset
  4200
                    Fragments.Diamond(site.tsym), details);
28331
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4201
        }
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4202
    }
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
    /**
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4205
     * An InvalidSymbolError error class indicating that a symbol is not
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4206
     * accessible from a given site
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4207
     */
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4208
    class AccessError extends InvalidSymbolError {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4209
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4210
        private Env<AttrContext> env;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4211
        private Type site;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4212
06bc494ca11e Initial load
duke
parents:
diff changeset
  4213
        AccessError(Env<AttrContext> env, Type site, Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4214
            super(HIDDEN, sym, "access error");
06bc494ca11e Initial load
duke
parents:
diff changeset
  4215
            this.env = env;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4216
            this.site = site;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4217
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4218
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4219
        @Override
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4220
        public boolean exists() {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4221
            return false;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4222
        }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4223
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4224
        @Override
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4225
        JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4226
                DiagnosticPosition pos,
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  4227
                Symbol location,
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4228
                Type site,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4229
                Name name,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4230
                List<Type> argtypes,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4231
                List<Type> typeargtypes) {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4232
            if (sym.name == names.init && sym.owner != site.tsym) {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4233
                return new SymbolNotFoundError(ABSENT_MTH).getDiagnostic(dkind,
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  4234
                        pos, location, site, name, argtypes, typeargtypes);
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4235
            }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4236
            else if ((sym.flags() & PUBLIC) != 0
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4237
                || (env != null && this.site != null
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4238
                    && !isAccessible(env, this.site))) {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36278
diff changeset
  4239
                if (sym.owner.kind == PCK) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36278
diff changeset
  4240
                    return diags.create(dkind, log.currentSource(),
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36278
diff changeset
  4241
                            pos, "not.def.access.package.cant.access",
43138
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4242
                        sym, sym.location(), inaccessiblePackageReason(env, sym.packge()));
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4243
                } else if (   sym.packge() != syms.rootPackage
43866
3195ea126044 8175007: Incorrect error messages for inaccessible classes in visible packages
jlahoda
parents: 43865
diff changeset
  4244
                           && !symbolPackageVisible(env, sym)) {
43138
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4245
                    return diags.create(dkind, log.currentSource(),
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4246
                            pos, "not.def.access.class.intf.cant.access.reason",
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4247
                            sym, sym.location(), sym.location().packge(),
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4248
                            inaccessiblePackageReason(env, sym.packge()));
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36278
diff changeset
  4249
                } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36278
diff changeset
  4250
                    return diags.create(dkind, log.currentSource(),
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36278
diff changeset
  4251
                            pos, "not.def.access.class.intf.cant.access",
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36278
diff changeset
  4252
                        sym, sym.location());
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36278
diff changeset
  4253
                }
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4254
            }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4255
            else if ((sym.flags() & (PRIVATE | PROTECTED)) != 0) {
6151
dd513881e71d 6957438: improve code for generating warning messages containing option names
jjg
parents: 5857
diff changeset
  4256
                return diags.create(dkind, log.currentSource(),
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4257
                        pos, "report.access", sym,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4258
                        asFlagSet(sym.flags() & (PRIVATE | PROTECTED)),
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4259
                        sym.location());
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4260
            }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4261
            else {
6151
dd513881e71d 6957438: improve code for generating warning messages containing option names
jjg
parents: 5857
diff changeset
  4262
                return diags.create(dkind, log.currentSource(),
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4263
                        pos, "not.def.public.cant.access", sym, sym.location());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4264
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4265
        }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36278
diff changeset
  4266
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36278
diff changeset
  4267
        private String toString(Type type) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36278
diff changeset
  4268
            StringBuilder sb = new StringBuilder();
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36278
diff changeset
  4269
            sb.append(type);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36278
diff changeset
  4270
            if (type != null) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36278
diff changeset
  4271
                sb.append("[tsym:").append(type.tsym);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36278
diff changeset
  4272
                if (type.tsym != null)
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36278
diff changeset
  4273
                    sb.append("packge:").append(type.tsym.packge());
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36278
diff changeset
  4274
                sb.append("]");
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36278
diff changeset
  4275
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36278
diff changeset
  4276
            return sb.toString();
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36278
diff changeset
  4277
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4278
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4279
43138
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4280
    class InvisibleSymbolError extends InvalidSymbolError {
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4281
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4282
        private final Env<AttrContext> env;
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4283
        private final boolean suppressError;
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4284
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4285
        InvisibleSymbolError(Env<AttrContext> env, boolean suppressError, Symbol sym) {
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4286
            super(HIDDEN, sym, "invisible class error");
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4287
            this.env = env;
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4288
            this.suppressError = suppressError;
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4289
            this.name = sym.name;
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4290
        }
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4291
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4292
        @Override
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4293
        JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4294
                DiagnosticPosition pos,
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4295
                Symbol location,
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4296
                Type site,
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4297
                Name name,
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4298
                List<Type> argtypes,
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4299
                List<Type> typeargtypes) {
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4300
            if (suppressError)
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4301
                return null;
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4302
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4303
            if (sym.kind == PCK) {
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4304
                JCDiagnostic details = inaccessiblePackageReason(env, sym.packge());
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4305
                return diags.create(dkind, log.currentSource(),
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4306
                        pos, "package.not.visible", sym, details);
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4307
            }
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4308
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4309
            JCDiagnostic details = inaccessiblePackageReason(env, sym.packge());
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4310
43272
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  4311
            if (pos.getTree() != null) {
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  4312
                Symbol o = sym;
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  4313
                JCTree tree = pos.getTree();
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  4314
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  4315
                while (o.kind != PCK && tree.hasTag(SELECT)) {
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  4316
                    o = o.owner;
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  4317
                    tree = ((JCFieldAccess) tree).selected;
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  4318
                }
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  4319
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  4320
                if (o.kind == PCK) {
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  4321
                    pos = tree.pos();
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  4322
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  4323
                    return diags.create(dkind, log.currentSource(),
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  4324
                            pos, "package.not.visible", o, details);
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43138
diff changeset
  4325
                }
43138
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4326
            }
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4327
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4328
            return diags.create(dkind, log.currentSource(),
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4329
                    pos, "not.def.access.package.cant.access", sym, sym.packge(), details);
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4330
        }
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4331
    }
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4332
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4333
    JCDiagnostic inaccessiblePackageReason(Env<AttrContext> env, PackageSymbol sym) {
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4334
        //no dependency:
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4335
        if (!env.toplevel.modle.readModules.contains(sym.modle)) {
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4336
            //does not read:
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4337
            if (sym.modle != syms.unnamedModule) {
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4338
                if (env.toplevel.modle != syms.unnamedModule) {
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4339
                    return diags.fragment(Fragments.NotDefAccessDoesNotRead(env.toplevel.modle,
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4340
                                                                            sym,
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4341
                                                                            sym.modle));
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4342
                } else {
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4343
                    return diags.fragment(Fragments.NotDefAccessDoesNotReadFromUnnamed(sym,
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4344
                                                                                       sym.modle));
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4345
                }
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4346
            } else {
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4347
                return diags.fragment(Fragments.NotDefAccessDoesNotReadUnnamed(sym,
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4348
                                                                               env.toplevel.modle));
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4349
            }
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4350
        } else {
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4351
            if (sym.packge().modle.exports.stream().anyMatch(e -> e.packge == sym)) {
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4352
                //not exported to this module:
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4353
                if (env.toplevel.modle != syms.unnamedModule) {
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4354
                    return diags.fragment(Fragments.NotDefAccessNotExportedToModule(sym,
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4355
                                                                                    sym.modle,
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4356
                                                                                    env.toplevel.modle));
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4357
                } else {
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4358
                    return diags.fragment(Fragments.NotDefAccessNotExportedToModuleFromUnnamed(sym,
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4359
                                                                                               sym.modle));
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4360
                }
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4361
            } else {
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4362
                //not exported:
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4363
                if (env.toplevel.modle != syms.unnamedModule) {
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4364
                    return diags.fragment(Fragments.NotDefAccessNotExported(sym,
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4365
                                                                            sym.modle));
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4366
                } else {
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4367
                    return diags.fragment(Fragments.NotDefAccessNotExportedFromUnnamed(sym,
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4368
                                                                                       sym.modle));
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4369
                }
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4370
            }
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4371
        }
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4372
    }
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 43131
diff changeset
  4373
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4374
    /**
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4375
     * InvalidSymbolError error class indicating that an instance member
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4376
     * has erroneously been accessed from a static context.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4377
     */
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4378
    class StaticError extends InvalidSymbolError {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4379
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4380
        StaticError(Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4381
            super(STATICERR, sym, "static error");
06bc494ca11e Initial load
duke
parents:
diff changeset
  4382
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4383
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4384
        @Override
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4385
        JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4386
                DiagnosticPosition pos,
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  4387
                Symbol location,
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4388
                Type site,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4389
                Name name,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4390
                List<Type> argtypes,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4391
                List<Type> typeargtypes) {
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14062
diff changeset
  4392
            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
  4393
                ? types.erasure(sym.type).tsym
38e24969c7e9 6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents: 735
diff changeset
  4394
                : sym);
6151
dd513881e71d 6957438: improve code for generating warning messages containing option names
jjg
parents: 5857
diff changeset
  4395
            return diags.create(dkind, log.currentSource(), pos,
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4396
                    "non-static.cant.be.ref", kindName(sym), errSym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4397
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4398
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4399
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4400
    /**
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4401
     * InvalidSymbolError error class indicating that a pair of symbols
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4402
     * (either methods, constructors or operands) are ambiguous
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4403
     * given an actual arguments/type argument list.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4404
     */
15038
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4405
    class AmbiguityError extends ResolveError {
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4406
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4407
        /** The other maximally specific symbol */
15038
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4408
        List<Symbol> ambiguousSyms = List.nil();
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4409
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4410
        @Override
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4411
        public boolean exists() {
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4412
            return true;
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4413
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4414
06bc494ca11e Initial load
duke
parents:
diff changeset
  4415
        AmbiguityError(Symbol sym1, Symbol sym2) {
15038
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4416
            super(AMBIGUOUS, "ambiguity error");
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4417
            ambiguousSyms = flatten(sym2).appendList(flatten(sym1));
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4418
        }
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4419
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4420
        private List<Symbol> flatten(Symbol sym) {
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4421
            if (sym.kind == AMBIGUOUS) {
22161
d79fb23b5dca 8029569: internal javac cast exception when resolving varargs ambiguity
vromero
parents: 21718
diff changeset
  4422
                return ((AmbiguityError)sym.baseSymbol()).ambiguousSyms;
15038
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4423
            } else {
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4424
                return List.of(sym);
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4425
            }
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4426
        }
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4427
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4428
        AmbiguityError addAmbiguousSymbol(Symbol s) {
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4429
            ambiguousSyms = ambiguousSyms.prepend(s);
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4430
            return this;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4431
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4432
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4433
        @Override
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4434
        JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4435
                DiagnosticPosition pos,
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  4436
                Symbol location,
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4437
                Type site,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4438
                Name name,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4439
                List<Type> argtypes,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  4440
                List<Type> typeargtypes) {
15038
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4441
            List<Symbol> diagSyms = ambiguousSyms.reverse();
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4442
            Symbol s1 = diagSyms.head;
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4443
            Symbol s2 = diagSyms.tail.head;
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4444
            Name sname = s1.name;
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4445
            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
  4446
            return diags.create(dkind, log.currentSource(),
28456
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  4447
                    pos, "ref.ambiguous", sname,
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  4448
                    kindName(s1),
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  4449
                    s1,
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  4450
                    s1.location(site, types),
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  4451
                    kindName(s2),
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  4452
                    s2,
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  4453
                    s2.location(site, types));
15038
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4454
        }
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4455
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4456
        /**
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4457
         * If multiple applicable methods are found during overload and none of them
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4458
         * is more specific than the others, attempt to merge their signatures.
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4459
         */
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4460
        Symbol mergeAbstracts(Type site) {
18668
1f99c1b43ab2 7034798: Ambiguity error for abstract method call is too eager
mcimadamore
parents: 18646
diff changeset
  4461
            List<Symbol> ambiguousInOrder = ambiguousSyms.reverse();
41526
265017792980 8167000: Refine handling of multiple maximally specific abstract methods
mcimadamore
parents: 39599
diff changeset
  4462
            return types.mergeAbstracts(ambiguousInOrder, site, true).orElse(this);
15038
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4463
        }
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4464
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4465
        @Override
3ad27d268874 8005179: Cleanup Resolve.AmbiguityError
mcimadamore
parents: 15037
diff changeset
  4466
        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
  4467
            Symbol firstAmbiguity = ambiguousSyms.last();
f564dcca5525 8005964: Regression: difference in error recovery after ambiguity causes JCK test failure
mcimadamore
parents: 15360
diff changeset
  4468
            return firstAmbiguity.kind == TYP ?
f564dcca5525 8005964: Regression: difference in error recovery after ambiguity causes JCK test failure
mcimadamore
parents: 15360
diff changeset
  4469
                    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
  4470
                    firstAmbiguity;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4471
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4472
    }
1533
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  4473
16312
7fb570abcb54 8007461: Regression: bad overload resolution when inner class and outer class have method with same name
mcimadamore
parents: 16301
diff changeset
  4474
    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
  4475
7fb570abcb54 8007461: Regression: bad overload resolution when inner class and outer class have method with same name
mcimadamore
parents: 16301
diff changeset
  4476
        ResolveError delegatedError;
7fb570abcb54 8007461: Regression: bad overload resolution when inner class and outer class have method with same name
mcimadamore
parents: 16301
diff changeset
  4477
7fb570abcb54 8007461: Regression: bad overload resolution when inner class and outer class have method with same name
mcimadamore
parents: 16301
diff changeset
  4478
        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
  4479
            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
  4480
            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
  4481
        }
7fb570abcb54 8007461: Regression: bad overload resolution when inner class and outer class have method with same name
mcimadamore
parents: 16301
diff changeset
  4482
7fb570abcb54 8007461: Regression: bad overload resolution when inner class and outer class have method with same name
mcimadamore
parents: 16301
diff changeset
  4483
        @Override
16975
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  4484
        public Symbol baseSymbol() {
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  4485
            return delegatedError.baseSymbol();
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  4486
        }
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  4487
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  4488
        @Override
16312
7fb570abcb54 8007461: Regression: bad overload resolution when inner class and outer class have method with same name
mcimadamore
parents: 16301
diff changeset
  4489
        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
  4490
            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
  4491
        }
7fb570abcb54 8007461: Regression: bad overload resolution when inner class and outer class have method with same name
mcimadamore
parents: 16301
diff changeset
  4492
7fb570abcb54 8007461: Regression: bad overload resolution when inner class and outer class have method with same name
mcimadamore
parents: 16301
diff changeset
  4493
        @Override
7fb570abcb54 8007461: Regression: bad overload resolution when inner class and outer class have method with same name
mcimadamore
parents: 16301
diff changeset
  4494
        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
  4495
            return true;
7fb570abcb54 8007461: Regression: bad overload resolution when inner class and outer class have method with same name
mcimadamore
parents: 16301
diff changeset
  4496
        }
7fb570abcb54 8007461: Regression: bad overload resolution when inner class and outer class have method with same name
mcimadamore
parents: 16301
diff changeset
  4497
7fb570abcb54 8007461: Regression: bad overload resolution when inner class and outer class have method with same name
mcimadamore
parents: 16301
diff changeset
  4498
        @Override
7fb570abcb54 8007461: Regression: bad overload resolution when inner class and outer class have method with same name
mcimadamore
parents: 16301
diff changeset
  4499
        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
  4500
            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
  4501
        }
7fb570abcb54 8007461: Regression: bad overload resolution when inner class and outer class have method with same name
mcimadamore
parents: 16301
diff changeset
  4502
    }
7fb570abcb54 8007461: Regression: bad overload resolution when inner class and outer class have method with same name
mcimadamore
parents: 16301
diff changeset
  4503
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4504
    /**
28456
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  4505
     * BadMethodReferenceError error class indicating that a method reference symbol has been found,
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  4506
     * but with the wrong staticness.
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  4507
     */
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  4508
    class BadMethodReferenceError extends StaticError {
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  4509
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  4510
        boolean unboundLookup;
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  4511
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  4512
        public BadMethodReferenceError(Symbol sym, boolean unboundLookup) {
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  4513
            super(sym);
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  4514
            this.unboundLookup = unboundLookup;
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  4515
        }
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  4516
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  4517
        @Override
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  4518
        JCDiagnostic getDiagnostic(DiagnosticType dkind, DiagnosticPosition pos, Symbol location, Type site, Name name, List<Type> argtypes, List<Type> typeargtypes) {
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  4519
            final String key;
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  4520
            if (!unboundLookup) {
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  4521
                key = "bad.static.method.in.bound.lookup";
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  4522
            } else if (sym.isStatic()) {
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  4523
                key = "bad.static.method.in.unbound.lookup";
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  4524
            } else {
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  4525
                key = "bad.instance.method.in.unbound.lookup";
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  4526
            }
29774
9d438163db79 8075184: javac is mistakenly considering an missing enclosing instance error as an overload error
vromero
parents: 29554
diff changeset
  4527
            return sym.kind.isResolutionError() ?
28456
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  4528
                    ((ResolveError)sym).getDiagnostic(dkind, pos, location, site, name, argtypes, typeargtypes) :
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  4529
                    diags.create(dkind, log.currentSource(), pos, key, Kinds.kindName(sym), sym);
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  4530
        }
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  4531
    }
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  4532
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  4533
    /**
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  4534
     * BadConstructorReferenceError error class indicating that a constructor reference symbol has been found,
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  4535
     * but pointing to a class for which an enclosing instance is not available.
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  4536
     */
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  4537
    class BadConstructorReferenceError extends InvalidSymbolError {
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  4538
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  4539
        public BadConstructorReferenceError(Symbol sym) {
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  4540
            super(MISSING_ENCL, sym, "BadConstructorReferenceError");
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  4541
        }
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  4542
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  4543
        @Override
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  4544
        JCDiagnostic getDiagnostic(DiagnosticType dkind, DiagnosticPosition pos, Symbol location, Type site, Name name, List<Type> argtypes, List<Type> typeargtypes) {
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  4545
           return diags.create(dkind, log.currentSource(), pos,
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  4546
                "cant.access.inner.cls.constr", site.tsym.name, argtypes, site.getEnclosingType());
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  4547
        }
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  4548
    }
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  4549
50374
2d0647b9ac18 8198378: javac crash when checking imports and a broken class is present
jlahoda
parents: 50287
diff changeset
  4550
    class BadClassFileError extends InvalidSymbolError {
2d0647b9ac18 8198378: javac crash when checking imports and a broken class is present
jlahoda
parents: 50287
diff changeset
  4551
2d0647b9ac18 8198378: javac crash when checking imports and a broken class is present
jlahoda
parents: 50287
diff changeset
  4552
        private final CompletionFailure ex;
2d0647b9ac18 8198378: javac crash when checking imports and a broken class is present
jlahoda
parents: 50287
diff changeset
  4553
2d0647b9ac18 8198378: javac crash when checking imports and a broken class is present
jlahoda
parents: 50287
diff changeset
  4554
        public BadClassFileError(CompletionFailure ex) {
2d0647b9ac18 8198378: javac crash when checking imports and a broken class is present
jlahoda
parents: 50287
diff changeset
  4555
            super(HIDDEN, ex.sym, "BadClassFileError");
2d0647b9ac18 8198378: javac crash when checking imports and a broken class is present
jlahoda
parents: 50287
diff changeset
  4556
            this.name = sym.name;
2d0647b9ac18 8198378: javac crash when checking imports and a broken class is present
jlahoda
parents: 50287
diff changeset
  4557
            this.ex = ex;
2d0647b9ac18 8198378: javac crash when checking imports and a broken class is present
jlahoda
parents: 50287
diff changeset
  4558
        }
2d0647b9ac18 8198378: javac crash when checking imports and a broken class is present
jlahoda
parents: 50287
diff changeset
  4559
2d0647b9ac18 8198378: javac crash when checking imports and a broken class is present
jlahoda
parents: 50287
diff changeset
  4560
        @Override
2d0647b9ac18 8198378: javac crash when checking imports and a broken class is present
jlahoda
parents: 50287
diff changeset
  4561
        JCDiagnostic getDiagnostic(DiagnosticType dkind, DiagnosticPosition pos, Symbol location, Type site, Name name, List<Type> argtypes, List<Type> typeargtypes) {
2d0647b9ac18 8198378: javac crash when checking imports and a broken class is present
jlahoda
parents: 50287
diff changeset
  4562
            JCDiagnostic d = diags.create(dkind, log.currentSource(), pos,
2d0647b9ac18 8198378: javac crash when checking imports and a broken class is present
jlahoda
parents: 50287
diff changeset
  4563
                "cant.access", ex.sym, ex.getDetailValue());
2d0647b9ac18 8198378: javac crash when checking imports and a broken class is present
jlahoda
parents: 50287
diff changeset
  4564
2d0647b9ac18 8198378: javac crash when checking imports and a broken class is present
jlahoda
parents: 50287
diff changeset
  4565
            d.setFlag(DiagnosticFlag.NON_DEFERRABLE);
2d0647b9ac18 8198378: javac crash when checking imports and a broken class is present
jlahoda
parents: 50287
diff changeset
  4566
            return d;
2d0647b9ac18 8198378: javac crash when checking imports and a broken class is present
jlahoda
parents: 50287
diff changeset
  4567
        }
2d0647b9ac18 8198378: javac crash when checking imports and a broken class is present
jlahoda
parents: 50287
diff changeset
  4568
2d0647b9ac18 8198378: javac crash when checking imports and a broken class is present
jlahoda
parents: 50287
diff changeset
  4569
    }
2d0647b9ac18 8198378: javac crash when checking imports and a broken class is present
jlahoda
parents: 50287
diff changeset
  4570
28456
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  4571
    /**
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4572
     * Helper class for method resolution diagnostic simplification.
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4573
     * 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
  4574
     * 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
  4575
     * 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
  4576
     * 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
  4577
     * 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
  4578
     * 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
  4579
     * 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
  4580
     * the diagnostic to a simpler one.
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4581
     */
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4582
    static class MethodResolutionDiagHelper {
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4583
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4584
        /**
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4585
         * 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
  4586
         * into a simpler one
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4587
         */
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4588
        interface DiagnosticRewriter {
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4589
            JCDiagnostic rewriteDiagnostic(JCDiagnostic.Factory diags,
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4590
                    DiagnosticPosition preferedPos, DiagnosticSource preferredSource,
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4591
                    DiagnosticType preferredKind, JCDiagnostic d);
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4592
        }
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4593
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4594
        /**
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4595
         * 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
  4596
         * 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
  4597
         * for matching diagnostic arguments.
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4598
         */
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4599
        static class Template {
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4600
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4601
            /** regex used to match diag key */
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4602
            String regex;
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4603
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4604
            /** templates used to match diagnostic args */
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4605
            Template[] subTemplates;
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4606
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4607
            Template(String key, Template... subTemplates) {
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4608
                this.regex = key;
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4609
                this.subTemplates = subTemplates;
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4610
            }
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4611
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4612
            /**
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4613
             * 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
  4614
             * 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
  4615
             */
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4616
            boolean matches(Object o) {
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4617
                JCDiagnostic d = (JCDiagnostic)o;
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4618
                Object[] args = d.getArgs();
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4619
                if (!d.getCode().matches(regex) ||
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4620
                        subTemplates.length != d.getArgs().length) {
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4621
                    return false;
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4622
                }
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4623
                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
  4624
                    if (!subTemplates[i].matches(args[i])) {
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4625
                        return false;
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4626
                    }
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4627
                }
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4628
                return true;
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4629
            }
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4630
        }
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4631
28331
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4632
        /**
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4633
         * Common rewriter for all argument mismatch simplifications.
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4634
         */
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4635
        static class ArgMismatchRewriter implements DiagnosticRewriter {
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4636
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4637
            /** the index of the subdiagnostic to be used as primary. */
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4638
            int causeIndex;
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4639
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4640
            public ArgMismatchRewriter(int causeIndex) {
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4641
                this.causeIndex = causeIndex;
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4642
            }
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4643
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4644
            @Override
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4645
            public JCDiagnostic rewriteDiagnostic(JCDiagnostic.Factory diags,
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4646
                    DiagnosticPosition preferedPos, DiagnosticSource preferredSource,
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4647
                    DiagnosticType preferredKind, JCDiagnostic d) {
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4648
                JCDiagnostic cause = (JCDiagnostic)d.getArgs()[causeIndex];
34854
d90aa0a83be4 8145466: javac: No line numbers in compilation error
sadayapalam
parents: 34566
diff changeset
  4649
                DiagnosticPosition pos = d.getDiagnosticPosition();
d90aa0a83be4 8145466: javac: No line numbers in compilation error
sadayapalam
parents: 34566
diff changeset
  4650
                if (pos == null) {
d90aa0a83be4 8145466: javac: No line numbers in compilation error
sadayapalam
parents: 34566
diff changeset
  4651
                    pos = preferedPos;
d90aa0a83be4 8145466: javac: No line numbers in compilation error
sadayapalam
parents: 34566
diff changeset
  4652
                }
d90aa0a83be4 8145466: javac: No line numbers in compilation error
sadayapalam
parents: 34566
diff changeset
  4653
                return diags.create(preferredKind, preferredSource, pos,
28331
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4654
                        "prob.found.req", cause);
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4655
            }
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4656
        }
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4657
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4658
        /** 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
  4659
        static final Template skip = new Template("") {
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4660
            @Override
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4661
            boolean matches(Object d) {
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4662
                return true;
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4663
            }
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4664
        };
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4665
28331
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4666
        /** template for matching inference-free arguments mismatch failures */
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4667
        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
  4668
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4669
        /** template for matching inference related arguments mismatch failures */
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4670
        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
  4671
            @Override
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4672
            boolean matches(Object o) {
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4673
                if (!super.matches(o)) {
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4674
                    return false;
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4675
                }
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4676
                JCDiagnostic d = (JCDiagnostic)o;
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4677
                @SuppressWarnings("unchecked")
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4678
                List<Type> tvars = (List<Type>)d.getArgs()[0];
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4679
                return !containsAny(d, tvars);
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4680
            }
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4681
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4682
            BiPredicate<Object, List<Type>> containsPredicate = (o, ts) -> {
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4683
                if (o instanceof Type) {
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4684
                    return ((Type)o).containsAny(ts);
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4685
                } else if (o instanceof JCDiagnostic) {
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4686
                    return containsAny((JCDiagnostic)o, ts);
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4687
                } else {
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4688
                    return false;
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4689
                }
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4690
            };
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4691
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4692
            boolean containsAny(JCDiagnostic d, List<Type> ts) {
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4693
                return Stream.of(d.getArgs())
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4694
                        .anyMatch(o -> containsPredicate.test(o, ts));
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4695
            }
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4696
        };
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4697
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4698
        /** rewriter map used for method resolution simplification */
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22161
diff changeset
  4699
        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
  4700
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4701
        static {
28331
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4702
            rewriters.put(argMismatchTemplate, new ArgMismatchRewriter(0));
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4703
            rewriters.put(inferArgMismatchTemplate, new ArgMismatchRewriter(1));
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4704
        }
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4705
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4706
        /**
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4707
         * 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
  4708
         * and rewrite it accordingly.
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4709
         */
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4710
        static JCDiagnostic rewrite(JCDiagnostic.Factory diags, DiagnosticPosition pos, DiagnosticSource source,
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4711
                                    DiagnosticType dkind, JCDiagnostic d) {
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4712
            for (Map.Entry<Template, DiagnosticRewriter> _entry : rewriters.entrySet()) {
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4713
                if (_entry.getKey().matches(d)) {
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4714
                    JCDiagnostic simpleDiag =
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4715
                            _entry.getValue().rewriteDiagnostic(diags, pos, source, dkind, d);
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4716
                    simpleDiag.setFlag(DiagnosticFlag.COMPRESSED);
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4717
                    return simpleDiag;
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4718
                }
28331
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4719
            }
43ed6b2e0e3b 8067883: Javac misses some opportunities for diagnostic simplification
mcimadamore
parents: 27849
diff changeset
  4720
            return null;
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4721
        }
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4722
    }
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16975
diff changeset
  4723
1533
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  4724
    enum MethodResolutionPhase {
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  4725
        BASIC(false, false),
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  4726
        BOX(true, false),
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  4727
        VARARITY(true, true) {
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  4728
            @Override
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  4729
            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
  4730
                //Check invariants (see {@code LookupHelper.shouldStop})
29774
9d438163db79 8075184: javac is mistakenly considering an missing enclosing instance error as an overload error
vromero
parents: 29554
diff changeset
  4731
                Assert.check(bestSoFar.kind.isResolutionError() && bestSoFar.kind != AMBIGUOUS);
9d438163db79 8075184: javac is mistakenly considering an missing enclosing instance error as an overload error
vromero
parents: 29554
diff changeset
  4732
                if (!sym.kind.isResolutionError()) {
26533
8fba085fbdbf 8055514: Wrong, confusing error when non-static varargs referenced in static context
mcimadamore
parents: 26266
diff changeset
  4733
                    //varargs resolution successful
8fba085fbdbf 8055514: Wrong, confusing error when non-static varargs referenced in static context
mcimadamore
parents: 26266
diff changeset
  4734
                    return sym;
8fba085fbdbf 8055514: Wrong, confusing error when non-static varargs referenced in static context
mcimadamore
parents: 26266
diff changeset
  4735
                } else {
8fba085fbdbf 8055514: Wrong, confusing error when non-static varargs referenced in static context
mcimadamore
parents: 26266
diff changeset
  4736
                    //pick best error
8fba085fbdbf 8055514: Wrong, confusing error when non-static varargs referenced in static context
mcimadamore
parents: 26266
diff changeset
  4737
                    switch (bestSoFar.kind) {
8fba085fbdbf 8055514: Wrong, confusing error when non-static varargs referenced in static context
mcimadamore
parents: 26266
diff changeset
  4738
                        case WRONG_MTH:
8fba085fbdbf 8055514: Wrong, confusing error when non-static varargs referenced in static context
mcimadamore
parents: 26266
diff changeset
  4739
                        case WRONG_MTHS:
8fba085fbdbf 8055514: Wrong, confusing error when non-static varargs referenced in static context
mcimadamore
parents: 26266
diff changeset
  4740
                            //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
  4741
                            //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
  4742
                            //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
  4743
                            //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
  4744
                            switch (sym.kind) {
8fba085fbdbf 8055514: Wrong, confusing error when non-static varargs referenced in static context
mcimadamore
parents: 26266
diff changeset
  4745
                                case WRONG_MTH:
8fba085fbdbf 8055514: Wrong, confusing error when non-static varargs referenced in static context
mcimadamore
parents: 26266
diff changeset
  4746
                                    //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
  4747
                                    //result instead
8fba085fbdbf 8055514: Wrong, confusing error when non-static varargs referenced in static context
mcimadamore
parents: 26266
diff changeset
  4748
                                    return bestSoFar.kind == WRONG_MTHS ?
8fba085fbdbf 8055514: Wrong, confusing error when non-static varargs referenced in static context
mcimadamore
parents: 26266
diff changeset
  4749
                                            bestSoFar : sym;
8fba085fbdbf 8055514: Wrong, confusing error when non-static varargs referenced in static context
mcimadamore
parents: 26266
diff changeset
  4750
                                case ABSENT_MTH:
8fba085fbdbf 8055514: Wrong, confusing error when non-static varargs referenced in static context
mcimadamore
parents: 26266
diff changeset
  4751
                                    //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
  4752
                                    //match any method
8fba085fbdbf 8055514: Wrong, confusing error when non-static varargs referenced in static context
mcimadamore
parents: 26266
diff changeset
  4753
                                    return bestSoFar;
8fba085fbdbf 8055514: Wrong, confusing error when non-static varargs referenced in static context
mcimadamore
parents: 26266
diff changeset
  4754
                                case WRONG_MTHS:
8fba085fbdbf 8055514: Wrong, confusing error when non-static varargs referenced in static context
mcimadamore
parents: 26266
diff changeset
  4755
                                default:
8fba085fbdbf 8055514: Wrong, confusing error when non-static varargs referenced in static context
mcimadamore
parents: 26266
diff changeset
  4756
                                    //safe to override
8fba085fbdbf 8055514: Wrong, confusing error when non-static varargs referenced in static context
mcimadamore
parents: 26266
diff changeset
  4757
                                    return sym;
8fba085fbdbf 8055514: Wrong, confusing error when non-static varargs referenced in static context
mcimadamore
parents: 26266
diff changeset
  4758
                            }
8fba085fbdbf 8055514: Wrong, confusing error when non-static varargs referenced in static context
mcimadamore
parents: 26266
diff changeset
  4759
                        default:
8fba085fbdbf 8055514: Wrong, confusing error when non-static varargs referenced in static context
mcimadamore
parents: 26266
diff changeset
  4760
                            //otherwise, return first error
8fba085fbdbf 8055514: Wrong, confusing error when non-static varargs referenced in static context
mcimadamore
parents: 26266
diff changeset
  4761
                            return bestSoFar;
8fba085fbdbf 8055514: Wrong, confusing error when non-static varargs referenced in static context
mcimadamore
parents: 26266
diff changeset
  4762
                    }
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  4763
                }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  4764
            }
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  4765
        };
1533
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  4766
14801
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents: 14724
diff changeset
  4767
        final boolean isBoxingRequired;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents: 14724
diff changeset
  4768
        final boolean isVarargsRequired;
1533
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  4769
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  4770
        MethodResolutionPhase(boolean isBoxingRequired, boolean isVarargsRequired) {
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  4771
           this.isBoxingRequired = isBoxingRequired;
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  4772
           this.isVarargsRequired = isVarargsRequired;
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  4773
        }
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  4774
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  4775
        public boolean isBoxingRequired() {
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  4776
            return isBoxingRequired;
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  4777
        }
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  4778
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  4779
        public boolean isVarargsRequired() {
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  4780
            return isVarargsRequired;
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  4781
        }
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  4782
14444
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  4783
        public Symbol mergeResults(Symbol prev, Symbol sym) {
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  4784
            return sym;
2069262b27c5 8000931: Cleanup Resolve.java
mcimadamore
parents: 14443
diff changeset
  4785
        }
1533
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  4786
    }
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  4787
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  4788
    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
  4789
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4790
    /**
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4791
     * 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
  4792
     * overload resolution, such as list of method that are not applicable
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4793
     * (used to generate more precise diagnostics) and so on. Resolution contexts
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4794
     * 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
  4795
     * work within the resolution context it created.
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4796
     */
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4797
    class MethodResolutionContext {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4798
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4799
        private List<Candidate> candidates = List.nil();
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4800
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  4801
        MethodResolutionPhase step = null;
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4802
16805
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  4803
        MethodCheck methodCheck = resolveMethodCheck;
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16323
diff changeset
  4804
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4805
        private boolean internalResolution = false;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  4806
        private DeferredAttr.AttrMode attrMode = DeferredAttr.AttrMode.SPECULATIVE;
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
  4807
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4808
        void addInapplicableCandidate(Symbol sym, JCDiagnostic details) {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4809
            Candidate c = new Candidate(currentResolutionContext.step, sym, details, null);
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  4810
            candidates = candidates.append(c);
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4811
        }
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4812
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4813
        void addApplicableCandidate(Symbol sym, Type mtype) {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4814
            Candidate c = new Candidate(currentResolutionContext.step, sym, null, mtype);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4815
            candidates = candidates.append(c);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4816
        }
10816
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
  4817
15706
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
  4818
        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
  4819
            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
  4820
                ? deferredAttr.emptyDeferredAttrContext
00a059740d87 8035891: javac, rename method asFree() in InferenceContext to asUndetVar() which reflects better it's purpose
vromero
parents: 23105
diff changeset
  4821
                : pendingResult.checkContext.deferredAttrContext();
00a059740d87 8035891: javac, rename method asFree() in InferenceContext to asUndetVar() which reflects better it's purpose
vromero
parents: 23105
diff changeset
  4822
            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
  4823
                    inferenceContext, parent, warn);
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
  4824
        }
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14957
diff changeset
  4825
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4826
        /**
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4827
         * This class represents an overload resolution candidate. There are two
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4828
         * kinds of candidates: applicable methods and inapplicable methods;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4829
         * applicable methods have a pointer to the instantiated method type,
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4830
         * while inapplicable candidates contain further details about the
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4831
         * reason why the method has been considered inapplicable.
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4832
         */
16301
b6fd735ea78e 6563143: javac should issue a warning for overriding equals without hashCode
vromero
parents: 16294
diff changeset
  4833
        @SuppressWarnings("overrides")
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4834
        class Candidate {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4835
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4836
            final MethodResolutionPhase step;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4837
            final Symbol sym;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4838
            final JCDiagnostic details;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4839
            final Type mtype;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4840
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4841
            private Candidate(MethodResolutionPhase step, Symbol sym, JCDiagnostic details, Type mtype) {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4842
                this.step = step;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4843
                this.sym = sym;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4844
                this.details = details;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4845
                this.mtype = mtype;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4846
            }
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4847
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4848
            boolean isApplicable() {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4849
                return mtype != null;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4850
            }
1533
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  4851
        }
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  4852
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  4853
        DeferredAttr.AttrMode attrMode() {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  4854
            return attrMode;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  4855
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  4856
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  4857
        boolean internal() {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  4858
            return internalResolution;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14056
diff changeset
  4859
        }
1533
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  4860
    }
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4861
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  4862
    MethodResolutionContext currentResolutionContext = null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4863
}