langtools/src/share/classes/com/sun/tools/javac/comp/Resolve.java
author ksrini
Sat, 29 Sep 2012 09:00:58 -0700
changeset 14056 0ea78d6e0b7b
parent 14052 8b839ae9074b
child 14057 b4b0377b8dba
permissions -rw-r--r--
7198582: (java) Minor refactor of JavacParser Reviewed-by: jjg, ksrini Contributed-by: jan.lahoda@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
11707
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
     2
 * Copyright (c) 1999, 2012, 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.*;
14052
8b839ae9074b 8000233: Fix issues in recent push
mcimadamore
parents: 14051
diff changeset
    30
import com.sun.tools.javac.code.Symbol.*;
10816
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
    31
import com.sun.tools.javac.code.Type.*;
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
    32
import com.sun.tools.javac.comp.Attr.ResultInfo;
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
    33
import com.sun.tools.javac.comp.Check.CheckContext;
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 14045
diff changeset
    34
import com.sun.tools.javac.comp.Infer.InferenceContext;
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 14045
diff changeset
    35
import com.sun.tools.javac.comp.Infer.InferenceContext.FreeTypeListener;
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
    36
import com.sun.tools.javac.comp.Resolve.MethodResolutionContext.Candidate;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
import com.sun.tools.javac.jvm.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
import com.sun.tools.javac.tree.*;
10816
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
    39
import com.sun.tools.javac.tree.JCTree.*;
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
    40
import com.sun.tools.javac.util.*;
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
    41
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
    42
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
    43
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticType;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
14045
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
    45
import java.util.ArrayList;
10816
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
    46
import java.util.Arrays;
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
    47
import java.util.Collection;
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
    48
import java.util.EnumMap;
10816
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
    49
import java.util.EnumSet;
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
    50
import java.util.HashSet;
14052
8b839ae9074b 8000233: Fix issues in recent push
mcimadamore
parents: 14051
diff changeset
    51
import java.util.Iterator;
10816
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
    52
import java.util.Map;
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
    53
import java.util.Set;
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
    54
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
    55
import javax.lang.model.element.ElementVisitor;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
import static com.sun.tools.javac.code.Flags.*;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10816
diff changeset
    58
import static com.sun.tools.javac.code.Flags.BLOCK;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
import static com.sun.tools.javac.code.Kinds.*;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10816
diff changeset
    60
import static com.sun.tools.javac.code.Kinds.ERRONEOUS;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
import static com.sun.tools.javac.code.TypeTags.*;
10816
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
    62
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
    63
import static com.sun.tools.javac.tree.JCTree.Tag.*;
1533
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
    64
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
/** Helper class for name resolution, used mostly by the attribution phase.
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5846
diff changeset
    67
 *  <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
    68
 *  If you write code that depends on this, you do so at your own risk.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
 *  This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
 *  deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
public class Resolve {
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
    protected static final Context.Key<Resolve> resolveKey =
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
        new Context.Key<Resolve>();
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 1257
diff changeset
    76
    Names names;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
    Log log;
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
    Symtab syms;
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
    79
    Attr attr;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
    Check chk;
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
    Infer infer;
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
    ClassReader reader;
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
    TreeInfo treeinfo;
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
    Types types;
1040
c0f5acfd9d15 6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents: 939
diff changeset
    85
    JCDiagnostic.Factory diags;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
    public final boolean boxingEnabled; // = source.allowBoxing();
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
    public final boolean varargsEnabled; // = source.allowVarargs();
6592
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6356
diff changeset
    88
    public final boolean allowMethodHandles;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
    private final boolean debugResolve;
10816
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
    90
    final EnumSet<VerboseResolutionMode> verboseResolutionMode;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
6592
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6356
diff changeset
    92
    Scope polymorphicSignatureScope;
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6356
diff changeset
    93
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
    94
    protected Resolve(Context context) {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
    95
        context.put(resolveKey, this);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
    96
        syms = Symtab.instance(context);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
    97
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
    98
        varNotFound = new
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
    99
            SymbolNotFoundError(ABSENT_VAR);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   100
        wrongMethod = new
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   101
            InapplicableSymbolError();
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   102
        wrongMethods = new
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   103
            InapplicableSymbolsError();
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   104
        methodNotFound = new
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   105
            SymbolNotFoundError(ABSENT_MTH);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   106
        typeNotFound = new
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   107
            SymbolNotFoundError(ABSENT_TYP);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   108
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   109
        names = Names.instance(context);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   110
        log = Log.instance(context);
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   111
        attr = Attr.instance(context);
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   112
        chk = Check.instance(context);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   113
        infer = Infer.instance(context);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   114
        reader = ClassReader.instance(context);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   115
        treeinfo = TreeInfo.instance(context);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   116
        types = Types.instance(context);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   117
        diags = JCDiagnostic.Factory.instance(context);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   118
        Source source = Source.instance(context);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   119
        boxingEnabled = source.allowBoxing();
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   120
        varargsEnabled = source.allowVarargs();
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   121
        Options options = Options.instance(context);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   122
        debugResolve = options.isSet("debugresolve");
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   123
        verboseResolutionMode = VerboseResolutionMode.getVerboseResolutionMode(options);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   124
        Target target = Target.instance(context);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   125
        allowMethodHandles = target.hasMethodHandles();
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   126
        polymorphicSignatureScope = new Scope(syms.noSymbol);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   127
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   128
        inapplicableMethodException = new InapplicableMethodException(diags);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   129
    }
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   130
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   131
    /** error symbols, which are returned when resolution fails
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   132
     */
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   133
    private final SymbolNotFoundError varNotFound;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   134
    private final InapplicableSymbolError wrongMethod;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   135
    private final InapplicableSymbolsError wrongMethods;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   136
    private final SymbolNotFoundError methodNotFound;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   137
    private final SymbolNotFoundError typeNotFound;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   138
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   139
    public static Resolve instance(Context context) {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   140
        Resolve instance = context.get(resolveKey);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   141
        if (instance == null)
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   142
            instance = new Resolve(context);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   143
        return instance;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   144
    }
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   145
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   146
    // <editor-fold defaultstate="collapsed" desc="Verbose resolution diagnostics support">
10816
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   147
    enum VerboseResolutionMode {
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   148
        SUCCESS("success"),
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   149
        FAILURE("failure"),
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   150
        APPLICABLE("applicable"),
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   151
        INAPPLICABLE("inapplicable"),
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   152
        DEFERRED_INST("deferred-inference"),
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   153
        PREDEF("predef"),
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   154
        OBJECT_INIT("object-init"),
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   155
        INTERNAL("internal");
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   156
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   157
        String opt;
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   158
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   159
        private VerboseResolutionMode(String opt) {
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   160
            this.opt = opt;
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   161
        }
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   162
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   163
        static EnumSet<VerboseResolutionMode> getVerboseResolutionMode(Options opts) {
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   164
            String s = opts.get("verboseResolution");
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   165
            EnumSet<VerboseResolutionMode> res = EnumSet.noneOf(VerboseResolutionMode.class);
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   166
            if (s == null) return res;
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   167
            if (s.contains("all")) {
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   168
                res = EnumSet.allOf(VerboseResolutionMode.class);
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   169
            }
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   170
            Collection<String> args = Arrays.asList(s.split(","));
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   171
            for (VerboseResolutionMode mode : values()) {
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   172
                if (args.contains(mode.opt)) {
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   173
                    res.add(mode);
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   174
                } else if (args.contains("-" + mode.opt)) {
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   175
                    res.remove(mode);
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   176
                }
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   177
            }
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   178
            return res;
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   179
        }
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   180
    }
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
   181
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   182
    void reportVerboseResolutionDiagnostic(DiagnosticPosition dpos, Name name, Type site,
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   183
            List<Type> argtypes, List<Type> typeargtypes, Symbol bestSoFar) {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   184
        boolean success = bestSoFar.kind < ERRONEOUS;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   185
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   186
        if (success && !verboseResolutionMode.contains(VerboseResolutionMode.SUCCESS)) {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   187
            return;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   188
        } else if (!success && !verboseResolutionMode.contains(VerboseResolutionMode.FAILURE)) {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   189
            return;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   190
        }
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   191
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   192
        if (bestSoFar.name == names.init &&
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   193
                bestSoFar.owner == syms.objectType.tsym &&
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   194
                !verboseResolutionMode.contains(VerboseResolutionMode.OBJECT_INIT)) {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   195
            return; //skip diags for Object constructor resolution
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   196
        } else if (site == syms.predefClass.type &&
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   197
                !verboseResolutionMode.contains(VerboseResolutionMode.PREDEF)) {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   198
            return; //skip spurious diags for predef symbols (i.e. operators)
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   199
        } else if (currentResolutionContext.internalResolution &&
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   200
                !verboseResolutionMode.contains(VerboseResolutionMode.INTERNAL)) {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   201
            return;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   202
        }
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   203
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   204
        int pos = 0;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   205
        int mostSpecificPos = -1;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   206
        ListBuffer<JCDiagnostic> subDiags = ListBuffer.lb();
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   207
        for (Candidate c : currentResolutionContext.candidates) {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   208
            if (currentResolutionContext.step != c.step ||
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   209
                    (c.isApplicable() && !verboseResolutionMode.contains(VerboseResolutionMode.APPLICABLE)) ||
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   210
                    (!c.isApplicable() && !verboseResolutionMode.contains(VerboseResolutionMode.INAPPLICABLE))) {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   211
                continue;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   212
            } else {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   213
                subDiags.append(c.isApplicable() ?
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   214
                        getVerboseApplicableCandidateDiag(pos, c.sym, c.mtype) :
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   215
                        getVerboseInapplicableCandidateDiag(pos, c.sym, c.details));
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   216
                if (c.sym == bestSoFar)
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   217
                    mostSpecificPos = pos;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   218
                pos++;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   219
            }
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   220
        }
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   221
        String key = success ? "verbose.resolve.multi" : "verbose.resolve.multi.1";
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   222
        JCDiagnostic main = diags.note(log.currentSource(), dpos, key, name,
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   223
                site.tsym, mostSpecificPos, currentResolutionContext.step,
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   224
                methodArguments(argtypes), methodArguments(typeargtypes));
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   225
        JCDiagnostic d = new JCDiagnostic.MultilineDiagnostic(main, subDiags.toList());
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   226
        log.report(d);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   229
    JCDiagnostic getVerboseApplicableCandidateDiag(int pos, Symbol sym, Type inst) {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   230
        JCDiagnostic subDiag = null;
12915
28cf1e0dafdc 7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents: 12335
diff changeset
   231
        if (sym.type.tag == FORALL) {
28cf1e0dafdc 7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents: 12335
diff changeset
   232
            subDiag = diags.fragment("partial.inst.sig", inst);
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   233
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   235
        String key = subDiag == null ?
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   236
                "applicable.method.found" :
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   237
                "applicable.method.found.1";
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
   238
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   239
        return diags.fragment(key, pos, sym, subDiag);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   242
    JCDiagnostic getVerboseInapplicableCandidateDiag(int pos, Symbol sym, JCDiagnostic subDiag) {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   243
        return diags.fragment("not.applicable.method.found", pos, sym, subDiag);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   244
    }
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   245
    // </editor-fold>
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
/* ************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
 * Identifier resolution
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
 *************************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
    /** An environment is "static" if its static level is greater than
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
     *  the one of its outer environment
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
     */
14056
0ea78d6e0b7b 7198582: (java) Minor refactor of JavacParser
ksrini
parents: 14052
diff changeset
   254
    protected static boolean isStatic(Env<AttrContext> env) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
        return env.info.staticLevel > env.outer.info.staticLevel;
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
    /** An environment is an "initializer" if it is a constructor or
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
     *  an instance initializer.
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
    static boolean isInitializer(Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
        Symbol owner = env.info.scope.owner;
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
        return owner.isConstructor() ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
            owner.owner.kind == TYP &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
            (owner.kind == VAR ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
             owner.kind == MTH && (owner.flags() & BLOCK) != 0) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
            (owner.flags() & STATIC) == 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
    /** Is class accessible in given evironment?
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
     *  @param env    The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
     *  @param c      The class whose accessibility is checked.
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
    public boolean isAccessible(Env<AttrContext> env, TypeSymbol c) {
7209
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   275
        return isAccessible(env, c, false);
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   276
    }
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   277
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   278
    public boolean isAccessible(Env<AttrContext> env, TypeSymbol c, boolean checkInner) {
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   279
        boolean isAccessible = false;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
        switch ((short)(c.flags() & AccessFlags)) {
7209
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   281
            case PRIVATE:
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   282
                isAccessible =
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   283
                    env.enclClass.sym.outermostClass() ==
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   284
                    c.owner.outermostClass();
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   285
                break;
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   286
            case 0:
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   287
                isAccessible =
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   288
                    env.toplevel.packge == c.owner // fast special case
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   289
                    ||
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   290
                    env.toplevel.packge == c.packge()
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   291
                    ||
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   292
                    // Hack: this case is added since synthesized default constructors
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   293
                    // of anonymous classes should be allowed to access
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   294
                    // classes which would be inaccessible otherwise.
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   295
                    env.enclMethod != null &&
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   296
                    (env.enclMethod.mods.flags & ANONCONSTR) != 0;
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   297
                break;
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   298
            default: // error recovery
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   299
            case PUBLIC:
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   300
                isAccessible = true;
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   301
                break;
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   302
            case PROTECTED:
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   303
                isAccessible =
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   304
                    env.toplevel.packge == c.owner // fast special case
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
                    env.toplevel.packge == c.packge()
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   307
                    ||
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   308
                    isInnerSubClass(env.enclClass.sym, c.owner);
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   309
                break;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
        }
7209
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   311
        return (checkInner == false || c.type.getEnclosingType() == Type.noType) ?
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   312
            isAccessible :
7637
467bc8fc514e 6990134: minor (but red) findbugs warnings
jjg
parents: 7635
diff changeset
   313
            isAccessible && isAccessible(env, c.type.getEnclosingType(), checkInner);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
    //where
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
        /** Is given class a subclass of given base class, or an inner class
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
         *  of a subclass?
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
         *  Return null if no such class exists.
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
         *  @param c     The class which is the subclass or is contained in it.
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
         *  @param base  The base class
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
        private boolean isInnerSubClass(ClassSymbol c, Symbol base) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
            while (c != null && !c.isSubClass(base, types)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
                c = c.owner.enclClass();
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   326
            return c != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
    boolean isAccessible(Env<AttrContext> env, Type t) {
7209
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   330
        return isAccessible(env, t, false);
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   331
    }
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   332
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   333
    boolean isAccessible(Env<AttrContext> env, Type t, boolean checkInner) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
        return (t.tag == ARRAY)
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
            ? isAccessible(env, types.elemtype(t))
7209
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   336
            : isAccessible(env, t.tsym, checkInner);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   338
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
    /** Is symbol accessible as a member of given type in given evironment?
06bc494ca11e Initial load
duke
parents:
diff changeset
   340
     *  @param env    The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
   341
     *  @param site   The type of which the tested symbol is regarded
06bc494ca11e Initial load
duke
parents:
diff changeset
   342
     *                as a member.
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
     *  @param sym    The symbol.
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
    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
   346
        return isAccessible(env, site, sym, false);
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   347
    }
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   348
    public boolean isAccessible(Env<AttrContext> env, Type site, Symbol sym, boolean checkInner) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   349
        if (sym.name == names.init && sym.owner != site.tsym) return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   350
        switch ((short)(sym.flags() & AccessFlags)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   351
        case PRIVATE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   352
            return
06bc494ca11e Initial load
duke
parents:
diff changeset
   353
                (env.enclClass.sym == sym.owner // fast special case
06bc494ca11e Initial load
duke
parents:
diff changeset
   354
                 ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   355
                 env.enclClass.sym.outermostClass() ==
06bc494ca11e Initial load
duke
parents:
diff changeset
   356
                 sym.owner.outermostClass())
06bc494ca11e Initial load
duke
parents:
diff changeset
   357
                &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   358
                sym.isInheritedIn(site.tsym, types);
06bc494ca11e Initial load
duke
parents:
diff changeset
   359
        case 0:
06bc494ca11e Initial load
duke
parents:
diff changeset
   360
            return
06bc494ca11e Initial load
duke
parents:
diff changeset
   361
                (env.toplevel.packge == sym.owner.owner // fast special case
06bc494ca11e Initial load
duke
parents:
diff changeset
   362
                 ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   363
                 env.toplevel.packge == sym.packge())
06bc494ca11e Initial load
duke
parents:
diff changeset
   364
                &&
7209
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   365
                isAccessible(env, site, checkInner)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   366
                &&
2511
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   367
                sym.isInheritedIn(site.tsym, types)
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   368
                &&
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   369
                notOverriddenIn(site, sym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   370
        case PROTECTED:
06bc494ca11e Initial load
duke
parents:
diff changeset
   371
            return
06bc494ca11e Initial load
duke
parents:
diff changeset
   372
                (env.toplevel.packge == sym.owner.owner // fast special case
06bc494ca11e Initial load
duke
parents:
diff changeset
   373
                 ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   374
                 env.toplevel.packge == sym.packge()
06bc494ca11e Initial load
duke
parents:
diff changeset
   375
                 ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   376
                 isProtectedAccessible(sym, env.enclClass.sym, site)
06bc494ca11e Initial load
duke
parents:
diff changeset
   377
                 ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   378
                 // OK to select instance method or field from 'super' or type name
06bc494ca11e Initial load
duke
parents:
diff changeset
   379
                 // (but type names should be disallowed elsewhere!)
06bc494ca11e Initial load
duke
parents:
diff changeset
   380
                 env.info.selectSuper && (sym.flags() & STATIC) == 0 && sym.kind != TYP)
06bc494ca11e Initial load
duke
parents:
diff changeset
   381
                &&
7209
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   382
                isAccessible(env, site, checkInner)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   383
                &&
2511
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   384
                notOverriddenIn(site, sym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   385
        default: // this case includes erroneous combinations as well
7209
889e80aa1d2e 6598108: com.sun.source.util.Trees.isAccessible incorrect
mcimadamore
parents: 6721
diff changeset
   386
            return isAccessible(env, site, checkInner) && notOverriddenIn(site, sym);
2511
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   387
        }
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   388
    }
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   389
    //where
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   390
    /* `sym' is accessible only if not overridden by
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   391
     * another symbol which is a member of `site'
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   392
     * (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
   393
     * 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
   394
     * cannot be overridden (e.g. MH.invokeExact(Object[])).
2511
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   395
     */
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   396
    private boolean notOverriddenIn(Type site, Symbol sym) {
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   397
        if (sym.kind != MTH || sym.isConstructor() || sym.isStatic())
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   398
            return true;
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   399
        else {
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   400
            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
   401
            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
   402
                    !types.isSubSignature(types.memberType(site, s2), types.memberType(site, sym)));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   403
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   404
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   405
    //where
06bc494ca11e Initial load
duke
parents:
diff changeset
   406
        /** Is given protected symbol accessible if it is selected from given site
06bc494ca11e Initial load
duke
parents:
diff changeset
   407
         *  and the selection takes place in given class?
06bc494ca11e Initial load
duke
parents:
diff changeset
   408
         *  @param sym     The symbol with protected access
06bc494ca11e Initial load
duke
parents:
diff changeset
   409
         *  @param c       The class where the access takes place
06bc494ca11e Initial load
duke
parents:
diff changeset
   410
         *  @site          The type of the qualifier
06bc494ca11e Initial load
duke
parents:
diff changeset
   411
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   412
        private
06bc494ca11e Initial load
duke
parents:
diff changeset
   413
        boolean isProtectedAccessible(Symbol sym, ClassSymbol c, Type site) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   414
            while (c != null &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   415
                   !(c.isSubClass(sym.owner, types) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   416
                     (c.flags() & INTERFACE) == 0 &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   417
                     // In JLS 2e 6.6.2.1, the subclass restriction applies
06bc494ca11e Initial load
duke
parents:
diff changeset
   418
                     // only to instance fields and methods -- types are excluded
06bc494ca11e Initial load
duke
parents:
diff changeset
   419
                     // regardless of whether they are declared 'static' or not.
06bc494ca11e Initial load
duke
parents:
diff changeset
   420
                     ((sym.flags() & STATIC) != 0 || sym.kind == TYP || site.tsym.isSubClass(c, types))))
06bc494ca11e Initial load
duke
parents:
diff changeset
   421
                c = c.owner.enclClass();
06bc494ca11e Initial load
duke
parents:
diff changeset
   422
            return c != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   423
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   424
06bc494ca11e Initial load
duke
parents:
diff changeset
   425
    /** Try to instantiate the type of a method so that it fits
06bc494ca11e Initial load
duke
parents:
diff changeset
   426
     *  given type arguments and argument types. If succesful, return
06bc494ca11e Initial load
duke
parents:
diff changeset
   427
     *  the method's instantiated type, else return null.
06bc494ca11e Initial load
duke
parents:
diff changeset
   428
     *  The instantiation will take into account an additional leading
06bc494ca11e Initial load
duke
parents:
diff changeset
   429
     *  formal parameter if the method is an instance method seen as a member
06bc494ca11e Initial load
duke
parents:
diff changeset
   430
     *  of un underdetermined site In this case, we treat site as an additional
06bc494ca11e Initial load
duke
parents:
diff changeset
   431
     *  parameter and the parameters of the class containing the method as
06bc494ca11e Initial load
duke
parents:
diff changeset
   432
     *  additional type variables that get instantiated.
06bc494ca11e Initial load
duke
parents:
diff changeset
   433
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   434
     *  @param env         The current environment
06bc494ca11e Initial load
duke
parents:
diff changeset
   435
     *  @param site        The type of which the method is a member.
06bc494ca11e Initial load
duke
parents:
diff changeset
   436
     *  @param m           The method symbol.
06bc494ca11e Initial load
duke
parents:
diff changeset
   437
     *  @param argtypes    The invocation's given value arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
   438
     *  @param typeargtypes    The invocation's given type arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
   439
     *  @param allowBoxing Allow boxing conversions of arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
   440
     *  @param useVarargs Box trailing arguments into an array for varargs.
06bc494ca11e Initial load
duke
parents:
diff changeset
   441
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   442
    Type rawInstantiate(Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
   443
                        Type site,
06bc494ca11e Initial load
duke
parents:
diff changeset
   444
                        Symbol m,
12915
28cf1e0dafdc 7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents: 12335
diff changeset
   445
                        ResultInfo resultInfo,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   446
                        List<Type> argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
   447
                        List<Type> typeargtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
   448
                        boolean allowBoxing,
06bc494ca11e Initial load
duke
parents:
diff changeset
   449
                        boolean useVarargs,
06bc494ca11e Initial load
duke
parents:
diff changeset
   450
                        Warner warn)
3140
15a274b13051 6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents: 2723
diff changeset
   451
        throws Infer.InferenceException {
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
   452
        if (useVarargs && (m.flags() & VARARGS) == 0)
8229
39266c1b1b0e 6313164: javac generates code that fails byte code verification for the varargs feature
mcimadamore
parents: 8228
diff changeset
   453
            throw inapplicableMethodException.setMessage();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   454
        Type mt = types.memberType(site, m);
06bc494ca11e Initial load
duke
parents:
diff changeset
   455
06bc494ca11e Initial load
duke
parents:
diff changeset
   456
        // tvars is the list of formal type variables for which type arguments
06bc494ca11e Initial load
duke
parents:
diff changeset
   457
        // need to inferred.
12915
28cf1e0dafdc 7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents: 12335
diff changeset
   458
        List<Type> tvars = List.nil();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   459
        if (typeargtypes == null) typeargtypes = List.nil();
8036
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 8032
diff changeset
   460
        if (mt.tag != FORALL && typeargtypes.nonEmpty()) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   461
            // 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
   462
            // which is fine, see JLS 15.12.2.1
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   463
        } else if (mt.tag == FORALL && typeargtypes.nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   464
            ForAll pmt = (ForAll) mt;
06bc494ca11e Initial load
duke
parents:
diff changeset
   465
            if (typeargtypes.length() != pmt.tvars.length())
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
   466
                throw inapplicableMethodException.setMessage("arg.length.mismatch"); // not enough args
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   467
            // Check type arguments are within bounds
06bc494ca11e Initial load
duke
parents:
diff changeset
   468
            List<Type> formals = pmt.tvars;
06bc494ca11e Initial load
duke
parents:
diff changeset
   469
            List<Type> actuals = typeargtypes;
06bc494ca11e Initial load
duke
parents:
diff changeset
   470
            while (formals.nonEmpty() && actuals.nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   471
                List<Type> bounds = types.subst(types.getBounds((TypeVar)formals.head),
06bc494ca11e Initial load
duke
parents:
diff changeset
   472
                                                pmt.tvars, typeargtypes);
06bc494ca11e Initial load
duke
parents:
diff changeset
   473
                for (; bounds.nonEmpty(); bounds = bounds.tail)
06bc494ca11e Initial load
duke
parents:
diff changeset
   474
                    if (!types.isSubtypeUnchecked(actuals.head, bounds.head, warn))
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
   475
                        throw inapplicableMethodException.setMessage("explicit.param.do.not.conform.to.bounds",actuals.head, bounds);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   476
                formals = formals.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   477
                actuals = actuals.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   478
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   479
            mt = types.subst(pmt.qtype, pmt.tvars, typeargtypes);
06bc494ca11e Initial load
duke
parents:
diff changeset
   480
        } else if (mt.tag == FORALL) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   481
            ForAll pmt = (ForAll) mt;
06bc494ca11e Initial load
duke
parents:
diff changeset
   482
            List<Type> tvars1 = types.newInstances(pmt.tvars);
06bc494ca11e Initial load
duke
parents:
diff changeset
   483
            tvars = tvars.appendList(tvars1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   484
            mt = types.subst(pmt.qtype, pmt.tvars, tvars1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   485
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   486
06bc494ca11e Initial load
duke
parents:
diff changeset
   487
        // 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
   488
        boolean instNeeded = tvars.tail != null; /*inlined: tvars.nonEmpty()*/
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   489
        for (List<Type> l = argtypes;
06bc494ca11e Initial load
duke
parents:
diff changeset
   490
             l.tail != null/*inlined: l.nonEmpty()*/ && !instNeeded;
06bc494ca11e Initial load
duke
parents:
diff changeset
   491
             l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   492
            if (l.head.tag == FORALL) instNeeded = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   493
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   494
06bc494ca11e Initial load
duke
parents:
diff changeset
   495
        if (instNeeded)
12335
4725d88691dd 7133185: Update 292 overload resolution logic to match JLS
mcimadamore
parents: 12334
diff changeset
   496
            return infer.instantiateMethod(env,
5489
e7af65bf7577 6730476: invalid "unchecked generic array" warning
mcimadamore
parents: 5321
diff changeset
   497
                                    tvars,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   498
                                    (MethodType)mt,
12915
28cf1e0dafdc 7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents: 12335
diff changeset
   499
                                    resultInfo,
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents: 5520
diff changeset
   500
                                    m,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   501
                                    argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
   502
                                    allowBoxing,
06bc494ca11e Initial load
duke
parents:
diff changeset
   503
                                    useVarargs,
06bc494ca11e Initial load
duke
parents:
diff changeset
   504
                                    warn);
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
   505
8229
39266c1b1b0e 6313164: javac generates code that fails byte code verification for the varargs feature
mcimadamore
parents: 8228
diff changeset
   506
        checkRawArgumentsAcceptable(env, argtypes, mt.getParameterTypes(),
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
   507
                                allowBoxing, useVarargs, warn);
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
   508
        return mt;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   509
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   510
06bc494ca11e Initial load
duke
parents:
diff changeset
   511
    /** Same but returns null instead throwing a NoInstanceException
06bc494ca11e Initial load
duke
parents:
diff changeset
   512
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   513
    Type instantiate(Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
   514
                     Type site,
06bc494ca11e Initial load
duke
parents:
diff changeset
   515
                     Symbol m,
12915
28cf1e0dafdc 7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents: 12335
diff changeset
   516
                     ResultInfo resultInfo,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   517
                     List<Type> argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
   518
                     List<Type> typeargtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
   519
                     boolean allowBoxing,
06bc494ca11e Initial load
duke
parents:
diff changeset
   520
                     boolean useVarargs,
06bc494ca11e Initial load
duke
parents:
diff changeset
   521
                     Warner warn) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   522
        try {
12915
28cf1e0dafdc 7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents: 12335
diff changeset
   523
            return rawInstantiate(env, site, m, resultInfo, argtypes, typeargtypes,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   524
                                  allowBoxing, useVarargs, warn);
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
   525
        } catch (InapplicableMethodException ex) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   526
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   527
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   528
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   529
06bc494ca11e Initial load
duke
parents:
diff changeset
   530
    /** Check if a parameter list accepts a list of args.
06bc494ca11e Initial load
duke
parents:
diff changeset
   531
     */
8229
39266c1b1b0e 6313164: javac generates code that fails byte code verification for the varargs feature
mcimadamore
parents: 8228
diff changeset
   532
    boolean argumentsAcceptable(Env<AttrContext> env,
39266c1b1b0e 6313164: javac generates code that fails byte code verification for the varargs feature
mcimadamore
parents: 8228
diff changeset
   533
                                List<Type> argtypes,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   534
                                List<Type> formals,
06bc494ca11e Initial load
duke
parents:
diff changeset
   535
                                boolean allowBoxing,
06bc494ca11e Initial load
duke
parents:
diff changeset
   536
                                boolean useVarargs,
06bc494ca11e Initial load
duke
parents:
diff changeset
   537
                                Warner warn) {
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
   538
        try {
8229
39266c1b1b0e 6313164: javac generates code that fails byte code verification for the varargs feature
mcimadamore
parents: 8228
diff changeset
   539
            checkRawArgumentsAcceptable(env, argtypes, formals, allowBoxing, useVarargs, warn);
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
   540
            return true;
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
   541
        } catch (InapplicableMethodException ex) {
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
   542
            return false;
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
   543
        }
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
   544
    }
11707
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   545
    /**
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   546
     * A check handler is used by the main method applicability routine in order
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   547
     * to handle specific method applicability failures. It is assumed that a class
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   548
     * implementing this interface should throw exceptions that are a subtype of
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   549
     * InapplicableMethodException (see below). Such exception will terminate the
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   550
     * method applicability check and propagate important info outwards (for the
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   551
     * purpose of generating better diagnostics).
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   552
     */
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   553
    interface MethodCheckHandler {
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   554
        /* The number of actuals and formals differ */
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   555
        InapplicableMethodException arityMismatch();
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   556
        /* An actual argument type does not conform to the corresponding formal type */
13438
83729994273a 7175911: Simplify error reporting API in Check.CheckContext interface
mcimadamore
parents: 12915
diff changeset
   557
        InapplicableMethodException argumentMismatch(boolean varargs, JCDiagnostic details);
11707
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   558
        /* The element type of a varargs is not accessible in the current context */
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   559
        InapplicableMethodException inaccessibleVarargs(Symbol location, Type expected);
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   560
    }
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   561
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   562
    /**
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   563
     * Basic method check handler used within Resolve - all methods end up
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   564
     * throwing InapplicableMethodException; a diagnostic fragment that describes
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   565
     * the cause as to why the method is not applicable is set on the exception
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   566
     * before it is thrown.
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   567
     */
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   568
    MethodCheckHandler resolveHandler = new MethodCheckHandler() {
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   569
            public InapplicableMethodException arityMismatch() {
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   570
                return inapplicableMethodException.setMessage("arg.length.mismatch");
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   571
            }
13438
83729994273a 7175911: Simplify error reporting API in Check.CheckContext interface
mcimadamore
parents: 12915
diff changeset
   572
            public InapplicableMethodException argumentMismatch(boolean varargs, JCDiagnostic details) {
11707
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   573
                String key = varargs ?
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   574
                        "varargs.argument.mismatch" :
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   575
                        "no.conforming.assignment.exists";
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   576
                return inapplicableMethodException.setMessage(key,
13438
83729994273a 7175911: Simplify error reporting API in Check.CheckContext interface
mcimadamore
parents: 12915
diff changeset
   577
                        details);
11707
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   578
            }
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   579
            public InapplicableMethodException inaccessibleVarargs(Symbol location, Type expected) {
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   580
                return inapplicableMethodException.setMessage("inaccessible.varargs.type",
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   581
                        expected, Kinds.kindName(location), location);
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   582
            }
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   583
    };
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   584
8229
39266c1b1b0e 6313164: javac generates code that fails byte code verification for the varargs feature
mcimadamore
parents: 8228
diff changeset
   585
    void checkRawArgumentsAcceptable(Env<AttrContext> env,
39266c1b1b0e 6313164: javac generates code that fails byte code verification for the varargs feature
mcimadamore
parents: 8228
diff changeset
   586
                                List<Type> argtypes,
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
   587
                                List<Type> formals,
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
   588
                                boolean allowBoxing,
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
   589
                                boolean useVarargs,
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
   590
                                Warner warn) {
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 14045
diff changeset
   591
        checkRawArgumentsAcceptable(env, infer.emptyContext, argtypes, formals,
11707
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   592
                allowBoxing, useVarargs, warn, resolveHandler);
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   593
    }
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   594
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   595
    /**
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   596
     * Main method applicability routine. Given a list of actual types A,
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   597
     * a list of formal types F, determines whether the types in A are
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   598
     * compatible (by method invocation conversion) with the types in F.
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   599
     *
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   600
     * Since this routine is shared between overload resolution and method
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   601
     * type-inference, it is crucial that actual types are converted to the
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   602
     * corresponding 'undet' form (i.e. where inference variables are replaced
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   603
     * with undetvars) so that constraints can be propagated and collected.
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   604
     *
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   605
     * Moreover, if one or more types in A is a poly type, this routine calls
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   606
     * Infer.instantiateArg in order to complete the poly type (this might involve
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   607
     * deferred attribution).
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   608
     *
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   609
     * A method check handler (see above) is used in order to report errors.
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   610
     */
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 14045
diff changeset
   611
    void checkRawArgumentsAcceptable(final Env<AttrContext> env,
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 14045
diff changeset
   612
                                final Infer.InferenceContext inferenceContext,
11707
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   613
                                List<Type> argtypes,
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   614
                                List<Type> formals,
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   615
                                boolean allowBoxing,
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   616
                                boolean useVarargs,
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   617
                                Warner warn,
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   618
                                MethodCheckHandler handler) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   619
        Type varargsFormal = useVarargs ? formals.last() : null;
11707
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   620
        ListBuffer<Type> checkedArgs = ListBuffer.lb();
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   621
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
   622
        if (varargsFormal == null &&
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
   623
                argtypes.size() != formals.size()) {
11707
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   624
            throw handler.arityMismatch(); // not enough args
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
   625
        }
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
   626
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   627
        while (argtypes.nonEmpty() && formals.head != varargsFormal) {
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 14045
diff changeset
   628
            ResultInfo resultInfo = methodCheckResult(formals.head, allowBoxing, false, inferenceContext, handler, warn);
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   629
            checkedArgs.append(resultInfo.check(env.tree.pos(), argtypes.head));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   630
            argtypes = argtypes.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   631
            formals = formals.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   632
        }
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
   633
11707
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   634
        if (formals.head != varargsFormal) {
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   635
            throw handler.arityMismatch(); // not enough args
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   636
        }
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
   637
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
   638
        if (useVarargs) {
11707
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   639
            //note: if applicability check is triggered by most specific test,
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 10950
diff changeset
   640
            //the last argument of a varargs is _not_ an array type (see JLS 15.12.2.5)
9720
bc06a797f393 7042566: Regression: new ambiguity between varargs method
mcimadamore
parents: 9303
diff changeset
   641
            Type elt = types.elemtype(varargsFormal);
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
   642
            while (argtypes.nonEmpty()) {
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 14045
diff changeset
   643
                ResultInfo resultInfo = methodCheckResult(elt, allowBoxing, true, inferenceContext, handler, warn);
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   644
                checkedArgs.append(resultInfo.check(env.tree.pos(), argtypes.head));
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
   645
                argtypes = argtypes.tail;
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
   646
            }
8229
39266c1b1b0e 6313164: javac generates code that fails byte code verification for the varargs feature
mcimadamore
parents: 8228
diff changeset
   647
            //check varargs element type accessibility
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 14045
diff changeset
   648
            varargsAccessible(env, elt, handler, inferenceContext);
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 14045
diff changeset
   649
        }
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 14045
diff changeset
   650
    }
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 14045
diff changeset
   651
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 14045
diff changeset
   652
    void varargsAccessible(final Env<AttrContext> env, final Type t, final Resolve.MethodCheckHandler handler, final InferenceContext inferenceContext) {
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 14045
diff changeset
   653
        if (inferenceContext.free(t)) {
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 14045
diff changeset
   654
            inferenceContext.addFreeTypeListener(List.of(t), new FreeTypeListener() {
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 14045
diff changeset
   655
                @Override
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 14045
diff changeset
   656
                public void typesInferred(InferenceContext inferenceContext) {
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 14045
diff changeset
   657
                    varargsAccessible(env, inferenceContext.asInstType(t, types), handler, inferenceContext);
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 14045
diff changeset
   658
                }
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 14045
diff changeset
   659
            });
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 14045
diff changeset
   660
        } else {
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 14045
diff changeset
   661
            if (!isAccessible(env, t)) {
8229
39266c1b1b0e 6313164: javac generates code that fails byte code verification for the varargs feature
mcimadamore
parents: 8228
diff changeset
   662
                Symbol location = env.enclClass.sym;
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 14045
diff changeset
   663
                throw handler.inaccessibleVarargs(location, t);
8229
39266c1b1b0e 6313164: javac generates code that fails byte code verification for the varargs feature
mcimadamore
parents: 8228
diff changeset
   664
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   665
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   666
    }
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   667
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   668
    /**
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   669
     * 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
   670
     * context might contain inference variables.
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   671
     */
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   672
    abstract class MethodCheckContext implements CheckContext {
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   673
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   674
        MethodCheckHandler handler;
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   675
        boolean useVarargs;
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 14045
diff changeset
   676
        Infer.InferenceContext inferenceContext;
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   677
        Warner rsWarner;
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
   678
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 14045
diff changeset
   679
        public MethodCheckContext(MethodCheckHandler handler, boolean useVarargs, Infer.InferenceContext inferenceContext, Warner rsWarner) {
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   680
            this.handler = handler;
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   681
            this.useVarargs = useVarargs;
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 14045
diff changeset
   682
            this.inferenceContext = inferenceContext;
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   683
            this.rsWarner = rsWarner;
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   684
        }
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   685
13438
83729994273a 7175911: Simplify error reporting API in Check.CheckContext interface
mcimadamore
parents: 12915
diff changeset
   686
        public void report(DiagnosticPosition pos, JCDiagnostic details) {
83729994273a 7175911: Simplify error reporting API in Check.CheckContext interface
mcimadamore
parents: 12915
diff changeset
   687
            throw handler.argumentMismatch(useVarargs, details);
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   688
        }
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   689
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   690
        public Warner checkWarner(DiagnosticPosition pos, Type found, Type req) {
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   691
            return rsWarner;
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   692
        }
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 14045
diff changeset
   693
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 14045
diff changeset
   694
        public InferenceContext inferenceContext() {
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 14045
diff changeset
   695
            return inferenceContext;
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 14045
diff changeset
   696
        }
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   697
    }
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
   698
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   699
    /**
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   700
     * Subclass of method check context class that implements strict method conversion.
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   701
     * Strict method conversion checks compatibility between types using subtyping tests.
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   702
     */
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   703
    class StrictMethodContext extends MethodCheckContext {
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   704
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 14045
diff changeset
   705
        public StrictMethodContext(MethodCheckHandler handler, boolean useVarargs, Infer.InferenceContext inferenceContext, Warner rsWarner) {
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 14045
diff changeset
   706
            super(handler, useVarargs, inferenceContext, rsWarner);
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   707
        }
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   708
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   709
        public boolean compatible(Type found, Type req, Warner warn) {
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 14045
diff changeset
   710
            return types.isSubtypeUnchecked(found, inferenceContext.asFree(req, types), warn);
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   711
        }
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   712
    }
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   713
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   714
    /**
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   715
     * Subclass of method check context class that implements loose method conversion.
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   716
     * Loose method conversion checks compatibility between types using method conversion tests.
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   717
     */
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   718
    class LooseMethodContext extends MethodCheckContext {
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   719
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 14045
diff changeset
   720
        public LooseMethodContext(MethodCheckHandler handler, boolean useVarargs, Infer.InferenceContext inferenceContext, Warner rsWarner) {
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 14045
diff changeset
   721
            super(handler, useVarargs, inferenceContext, rsWarner);
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   722
        }
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   723
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   724
        public boolean compatible(Type found, Type req, Warner warn) {
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 14045
diff changeset
   725
            return types.isConvertible(found, inferenceContext.asFree(req, types), warn);
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   726
        }
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   727
    }
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   728
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   729
    /**
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   730
     * Create a method check context to be used during method applicability check
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   731
     */
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   732
    ResultInfo methodCheckResult(Type to, boolean allowBoxing, boolean useVarargs,
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 14045
diff changeset
   733
            Infer.InferenceContext inferenceContext, MethodCheckHandler methodHandler, Warner rsWarner) {
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   734
        MethodCheckContext checkContext = allowBoxing ?
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 14045
diff changeset
   735
                new LooseMethodContext(methodHandler, useVarargs, inferenceContext, rsWarner) :
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 14045
diff changeset
   736
                new StrictMethodContext(methodHandler, useVarargs, inferenceContext, rsWarner);
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   737
        return attr.new ResultInfo(VAL, to, checkContext) {
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   738
            @Override
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   739
            protected Type check(DiagnosticPosition pos, Type found) {
14048
308d1cf8fe46 7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents: 14047
diff changeset
   740
                return super.check(pos, chk.checkNonVoid(pos, types.capture(types.upperBound(found.baseType()))));
8229
39266c1b1b0e 6313164: javac generates code that fails byte code verification for the varargs feature
mcimadamore
parents: 8228
diff changeset
   741
            }
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   742
        };
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   743
    }
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   744
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   745
    public static class InapplicableMethodException extends RuntimeException {
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   746
        private static final long serialVersionUID = 0;
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   747
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   748
        JCDiagnostic diagnostic;
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   749
        JCDiagnostic.Factory diags;
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
   750
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   751
        InapplicableMethodException(JCDiagnostic.Factory diags) {
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   752
            this.diagnostic = null;
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   753
            this.diags = diags;
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   754
        }
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   755
        InapplicableMethodException setMessage() {
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 14045
diff changeset
   756
            return setMessage((JCDiagnostic)null);
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   757
        }
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   758
        InapplicableMethodException setMessage(String key) {
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 14045
diff changeset
   759
            return setMessage(key != null ? diags.fragment(key) : null);
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
   760
        }
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   761
        InapplicableMethodException setMessage(String key, Object... args) {
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 14045
diff changeset
   762
            return setMessage(key != null ? diags.fragment(key, args) : null);
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   763
        }
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   764
        InapplicableMethodException setMessage(JCDiagnostic diag) {
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   765
            this.diagnostic = diag;
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   766
            return this;
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   767
        }
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   768
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   769
        public JCDiagnostic getDiagnostic() {
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   770
            return diagnostic;
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   771
        }
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   772
    }
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12082
diff changeset
   773
    private final InapplicableMethodException inapplicableMethodException;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   774
06bc494ca11e Initial load
duke
parents:
diff changeset
   775
/* ***************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
   776
 *  Symbol lookup
06bc494ca11e Initial load
duke
parents:
diff changeset
   777
 *  the following naming conventions for arguments are used
06bc494ca11e Initial load
duke
parents:
diff changeset
   778
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
   779
 *       env      is the environment where the symbol was mentioned
06bc494ca11e Initial load
duke
parents:
diff changeset
   780
 *       site     is the type of which the symbol is a member
06bc494ca11e Initial load
duke
parents:
diff changeset
   781
 *       name     is the symbol's name
06bc494ca11e Initial load
duke
parents:
diff changeset
   782
 *                if no arguments are given
06bc494ca11e Initial load
duke
parents:
diff changeset
   783
 *       argtypes are the value arguments, if we search for a method
06bc494ca11e Initial load
duke
parents:
diff changeset
   784
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
   785
 *  If no symbol was found, a ResolveError detailing the problem is returned.
06bc494ca11e Initial load
duke
parents:
diff changeset
   786
 ****************************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
   787
06bc494ca11e Initial load
duke
parents:
diff changeset
   788
    /** Find field. Synthetic fields are always skipped.
06bc494ca11e Initial load
duke
parents:
diff changeset
   789
     *  @param env     The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
   790
     *  @param site    The original type from where the selection takes place.
06bc494ca11e Initial load
duke
parents:
diff changeset
   791
     *  @param name    The name of the field.
06bc494ca11e Initial load
duke
parents:
diff changeset
   792
     *  @param c       The class to search for the field. This is always
06bc494ca11e Initial load
duke
parents:
diff changeset
   793
     *                 a superclass or implemented interface of site's class.
06bc494ca11e Initial load
duke
parents:
diff changeset
   794
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   795
    Symbol findField(Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
   796
                     Type site,
06bc494ca11e Initial load
duke
parents:
diff changeset
   797
                     Name name,
06bc494ca11e Initial load
duke
parents:
diff changeset
   798
                     TypeSymbol c) {
326
d51f30ce6796 6531090: Cannot access methods/fields of a captured type belonging to an intersection type
mcimadamore
parents: 10
diff changeset
   799
        while (c.type.tag == TYPEVAR)
d51f30ce6796 6531090: Cannot access methods/fields of a captured type belonging to an intersection type
mcimadamore
parents: 10
diff changeset
   800
            c = c.type.getUpperBound().tsym;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   801
        Symbol bestSoFar = varNotFound;
06bc494ca11e Initial load
duke
parents:
diff changeset
   802
        Symbol sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   803
        Scope.Entry e = c.members().lookup(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   804
        while (e.scope != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   805
            if (e.sym.kind == VAR && (e.sym.flags_field & SYNTHETIC) == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   806
                return isAccessible(env, site, e.sym)
06bc494ca11e Initial load
duke
parents:
diff changeset
   807
                    ? e.sym : new AccessError(env, site, e.sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   808
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   809
            e = e.next();
06bc494ca11e Initial load
duke
parents:
diff changeset
   810
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   811
        Type st = types.supertype(c.type);
326
d51f30ce6796 6531090: Cannot access methods/fields of a captured type belonging to an intersection type
mcimadamore
parents: 10
diff changeset
   812
        if (st != null && (st.tag == CLASS || st.tag == TYPEVAR)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   813
            sym = findField(env, site, name, st.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   814
            if (sym.kind < bestSoFar.kind) bestSoFar = sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   815
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   816
        for (List<Type> l = types.interfaces(c.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   817
             bestSoFar.kind != AMBIGUOUS && l.nonEmpty();
06bc494ca11e Initial load
duke
parents:
diff changeset
   818
             l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   819
            sym = findField(env, site, name, l.head.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   820
            if (bestSoFar.kind < AMBIGUOUS && sym.kind < AMBIGUOUS &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   821
                sym.owner != bestSoFar.owner)
06bc494ca11e Initial load
duke
parents:
diff changeset
   822
                bestSoFar = new AmbiguityError(bestSoFar, sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   823
            else if (sym.kind < bestSoFar.kind)
06bc494ca11e Initial load
duke
parents:
diff changeset
   824
                bestSoFar = sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   825
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   826
        return bestSoFar;
06bc494ca11e Initial load
duke
parents:
diff changeset
   827
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   828
06bc494ca11e Initial load
duke
parents:
diff changeset
   829
    /** Resolve a field identifier, throw a fatal error if not found.
06bc494ca11e Initial load
duke
parents:
diff changeset
   830
     *  @param pos       The position to use for error reporting.
06bc494ca11e Initial load
duke
parents:
diff changeset
   831
     *  @param env       The environment current at the method invocation.
06bc494ca11e Initial load
duke
parents:
diff changeset
   832
     *  @param site      The type of the qualifying expression, in which
06bc494ca11e Initial load
duke
parents:
diff changeset
   833
     *                   identifier is searched.
06bc494ca11e Initial load
duke
parents:
diff changeset
   834
     *  @param name      The identifier's name.
06bc494ca11e Initial load
duke
parents:
diff changeset
   835
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   836
    public VarSymbol resolveInternalField(DiagnosticPosition pos, Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
   837
                                          Type site, Name name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   838
        Symbol sym = findField(env, site, name, site.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   839
        if (sym.kind == VAR) return (VarSymbol)sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   840
        else throw new FatalError(
1040
c0f5acfd9d15 6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents: 939
diff changeset
   841
                 diags.fragment("fatal.err.cant.locate.field",
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   842
                                name));
06bc494ca11e Initial load
duke
parents:
diff changeset
   843
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   844
06bc494ca11e Initial load
duke
parents:
diff changeset
   845
    /** Find unqualified variable or field with given name.
06bc494ca11e Initial load
duke
parents:
diff changeset
   846
     *  Synthetic fields always skipped.
06bc494ca11e Initial load
duke
parents:
diff changeset
   847
     *  @param env     The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
   848
     *  @param name    The name of the variable or field.
06bc494ca11e Initial load
duke
parents:
diff changeset
   849
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   850
    Symbol findVar(Env<AttrContext> env, Name name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   851
        Symbol bestSoFar = varNotFound;
06bc494ca11e Initial load
duke
parents:
diff changeset
   852
        Symbol sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   853
        Env<AttrContext> env1 = env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   854
        boolean staticOnly = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   855
        while (env1.outer != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   856
            if (isStatic(env1)) staticOnly = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   857
            Scope.Entry e = env1.info.scope.lookup(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   858
            while (e.scope != null &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   859
                   (e.sym.kind != VAR ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   860
                    (e.sym.flags_field & SYNTHETIC) != 0))
06bc494ca11e Initial load
duke
parents:
diff changeset
   861
                e = e.next();
06bc494ca11e Initial load
duke
parents:
diff changeset
   862
            sym = (e.scope != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   863
                ? e.sym
06bc494ca11e Initial load
duke
parents:
diff changeset
   864
                : findField(
06bc494ca11e Initial load
duke
parents:
diff changeset
   865
                    env1, env1.enclClass.sym.type, name, env1.enclClass.sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   866
            if (sym.exists()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   867
                if (staticOnly &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   868
                    sym.kind == VAR &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   869
                    sym.owner.kind == TYP &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   870
                    (sym.flags() & STATIC) == 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   871
                    return new StaticError(sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   872
                else
06bc494ca11e Initial load
duke
parents:
diff changeset
   873
                    return sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   874
            } else if (sym.kind < bestSoFar.kind) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   875
                bestSoFar = sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   876
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   877
06bc494ca11e Initial load
duke
parents:
diff changeset
   878
            if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   879
            env1 = env1.outer;
06bc494ca11e Initial load
duke
parents:
diff changeset
   880
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   881
06bc494ca11e Initial load
duke
parents:
diff changeset
   882
        sym = findField(env, syms.predefClass.type, name, syms.predefClass);
06bc494ca11e Initial load
duke
parents:
diff changeset
   883
        if (sym.exists())
06bc494ca11e Initial load
duke
parents:
diff changeset
   884
            return sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   885
        if (bestSoFar.exists())
06bc494ca11e Initial load
duke
parents:
diff changeset
   886
            return bestSoFar;
06bc494ca11e Initial load
duke
parents:
diff changeset
   887
06bc494ca11e Initial load
duke
parents:
diff changeset
   888
        Scope.Entry e = env.toplevel.namedImportScope.lookup(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   889
        for (; e.scope != null; e = e.next()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   890
            sym = e.sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   891
            Type origin = e.getOrigin().owner.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
   892
            if (sym.kind == VAR) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   893
                if (e.sym.owner.type != origin)
06bc494ca11e Initial load
duke
parents:
diff changeset
   894
                    sym = sym.clone(e.getOrigin().owner);
06bc494ca11e Initial load
duke
parents:
diff changeset
   895
                return isAccessible(env, origin, sym)
06bc494ca11e Initial load
duke
parents:
diff changeset
   896
                    ? sym : new AccessError(env, origin, sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   897
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   898
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   899
06bc494ca11e Initial load
duke
parents:
diff changeset
   900
        Symbol origin = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   901
        e = env.toplevel.starImportScope.lookup(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   902
        for (; e.scope != null; e = e.next()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   903
            sym = e.sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   904
            if (sym.kind != VAR)
06bc494ca11e Initial load
duke
parents:
diff changeset
   905
                continue;
06bc494ca11e Initial load
duke
parents:
diff changeset
   906
            // invariant: sym.kind == VAR
06bc494ca11e Initial load
duke
parents:
diff changeset
   907
            if (bestSoFar.kind < AMBIGUOUS && sym.owner != bestSoFar.owner)
06bc494ca11e Initial load
duke
parents:
diff changeset
   908
                return new AmbiguityError(bestSoFar, sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   909
            else if (bestSoFar.kind >= VAR) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   910
                origin = e.getOrigin().owner;
06bc494ca11e Initial load
duke
parents:
diff changeset
   911
                bestSoFar = isAccessible(env, origin.type, sym)
06bc494ca11e Initial load
duke
parents:
diff changeset
   912
                    ? sym : new AccessError(env, origin.type, sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   913
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   914
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   915
        if (bestSoFar.kind == VAR && bestSoFar.owner.type != origin.type)
06bc494ca11e Initial load
duke
parents:
diff changeset
   916
            return bestSoFar.clone(origin);
06bc494ca11e Initial load
duke
parents:
diff changeset
   917
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
   918
            return bestSoFar;
06bc494ca11e Initial load
duke
parents:
diff changeset
   919
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   920
06bc494ca11e Initial load
duke
parents:
diff changeset
   921
    Warner noteWarner = new Warner();
06bc494ca11e Initial load
duke
parents:
diff changeset
   922
06bc494ca11e Initial load
duke
parents:
diff changeset
   923
    /** Select the best method for a call site among two choices.
06bc494ca11e Initial load
duke
parents:
diff changeset
   924
     *  @param env              The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
   925
     *  @param site             The original type from where the
06bc494ca11e Initial load
duke
parents:
diff changeset
   926
     *                          selection takes place.
06bc494ca11e Initial load
duke
parents:
diff changeset
   927
     *  @param argtypes         The invocation's value arguments,
06bc494ca11e Initial load
duke
parents:
diff changeset
   928
     *  @param typeargtypes     The invocation's type arguments,
06bc494ca11e Initial load
duke
parents:
diff changeset
   929
     *  @param sym              Proposed new best match.
06bc494ca11e Initial load
duke
parents:
diff changeset
   930
     *  @param bestSoFar        Previously found best match.
06bc494ca11e Initial load
duke
parents:
diff changeset
   931
     *  @param allowBoxing Allow boxing conversions of arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
   932
     *  @param useVarargs Box trailing arguments into an array for varargs.
06bc494ca11e Initial load
duke
parents:
diff changeset
   933
     */
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
   934
    @SuppressWarnings("fallthrough")
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   935
    Symbol selectBest(Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
   936
                      Type site,
06bc494ca11e Initial load
duke
parents:
diff changeset
   937
                      List<Type> argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
   938
                      List<Type> typeargtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
   939
                      Symbol sym,
06bc494ca11e Initial load
duke
parents:
diff changeset
   940
                      Symbol bestSoFar,
06bc494ca11e Initial load
duke
parents:
diff changeset
   941
                      boolean allowBoxing,
06bc494ca11e Initial load
duke
parents:
diff changeset
   942
                      boolean useVarargs,
06bc494ca11e Initial load
duke
parents:
diff changeset
   943
                      boolean operator) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   944
        if (sym.kind == ERR) return bestSoFar;
1649
9ec015f3661e 6776289: Regression: javac7 doesnt resolve method calls properly
mcimadamore
parents: 1534
diff changeset
   945
        if (!sym.isInheritedIn(site.tsym, types)) return bestSoFar;
8032
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents: 7681
diff changeset
   946
        Assert.check(sym.kind < AMBIGUOUS);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   947
        try {
12915
28cf1e0dafdc 7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents: 12335
diff changeset
   948
            Type mt = rawInstantiate(env, site, sym, null, argtypes, typeargtypes,
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
   949
                               allowBoxing, useVarargs, Warner.noWarnings);
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   950
            if (!operator)
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   951
                currentResolutionContext.addApplicableCandidate(sym, mt);
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
   952
        } catch (InapplicableMethodException ex) {
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   953
            if (!operator)
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   954
                currentResolutionContext.addInapplicableCandidate(sym, ex.getDiagnostic());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   955
            switch (bestSoFar.kind) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   956
            case ABSENT_MTH:
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   957
                return wrongMethod;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   958
            case WRONG_MTH:
10812
7341374e12e8 7102515: javac running very very long and not returning
mcimadamore
parents: 10197
diff changeset
   959
                if (operator) return bestSoFar;
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
   960
            case WRONG_MTHS:
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   961
                return wrongMethods;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   962
            default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   963
                return bestSoFar;
06bc494ca11e Initial load
duke
parents:
diff changeset
   964
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   965
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   966
        if (!isAccessible(env, site, sym)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   967
            return (bestSoFar.kind == ABSENT_MTH)
06bc494ca11e Initial load
duke
parents:
diff changeset
   968
                ? new AccessError(env, site, sym)
06bc494ca11e Initial load
duke
parents:
diff changeset
   969
                : bestSoFar;
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
   970
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   971
        return (bestSoFar.kind > AMBIGUOUS)
06bc494ca11e Initial load
duke
parents:
diff changeset
   972
            ? sym
06bc494ca11e Initial load
duke
parents:
diff changeset
   973
            : mostSpecific(sym, bestSoFar, env, site,
06bc494ca11e Initial load
duke
parents:
diff changeset
   974
                           allowBoxing && operator, useVarargs);
06bc494ca11e Initial load
duke
parents:
diff changeset
   975
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   976
06bc494ca11e Initial load
duke
parents:
diff changeset
   977
    /* Return the most specific of the two methods for a call,
06bc494ca11e Initial load
duke
parents:
diff changeset
   978
     *  given that both are accessible and applicable.
06bc494ca11e Initial load
duke
parents:
diff changeset
   979
     *  @param m1               A new candidate for most specific.
06bc494ca11e Initial load
duke
parents:
diff changeset
   980
     *  @param m2               The previous most specific candidate.
06bc494ca11e Initial load
duke
parents:
diff changeset
   981
     *  @param env              The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
   982
     *  @param site             The original type from where the selection
06bc494ca11e Initial load
duke
parents:
diff changeset
   983
     *                          takes place.
06bc494ca11e Initial load
duke
parents:
diff changeset
   984
     *  @param allowBoxing Allow boxing conversions of arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
   985
     *  @param useVarargs Box trailing arguments into an array for varargs.
06bc494ca11e Initial load
duke
parents:
diff changeset
   986
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   987
    Symbol mostSpecific(Symbol m1,
06bc494ca11e Initial load
duke
parents:
diff changeset
   988
                        Symbol m2,
06bc494ca11e Initial load
duke
parents:
diff changeset
   989
                        Env<AttrContext> env,
2511
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   990
                        final Type site,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   991
                        boolean allowBoxing,
06bc494ca11e Initial load
duke
parents:
diff changeset
   992
                        boolean useVarargs) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   993
        switch (m2.kind) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   994
        case MTH:
06bc494ca11e Initial load
duke
parents:
diff changeset
   995
            if (m1 == m2) return m1;
7623
66d98f7ba8c7 5088429: varargs overloading problem
mcimadamore
parents: 7209
diff changeset
   996
            boolean m1SignatureMoreSpecific = signatureMoreSpecific(env, site, m1, m2, allowBoxing, useVarargs);
66d98f7ba8c7 5088429: varargs overloading problem
mcimadamore
parents: 7209
diff changeset
   997
            boolean m2SignatureMoreSpecific = signatureMoreSpecific(env, site, m2, m1, allowBoxing, useVarargs);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   998
            if (m1SignatureMoreSpecific && m2SignatureMoreSpecific) {
7623
66d98f7ba8c7 5088429: varargs overloading problem
mcimadamore
parents: 7209
diff changeset
   999
                Type mt1 = types.memberType(site, m1);
66d98f7ba8c7 5088429: varargs overloading problem
mcimadamore
parents: 7209
diff changeset
  1000
                Type mt2 = types.memberType(site, m2);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1001
                if (!types.overrideEquivalent(mt1, mt2))
8228
4e6ee38974b2 6910550: javac 1.5.0_17 fails with incorrect error message
mcimadamore
parents: 8045
diff changeset
  1002
                    return ambiguityError(m1, m2);
4e6ee38974b2 6910550: javac 1.5.0_17 fails with incorrect error message
mcimadamore
parents: 8045
diff changeset
  1003
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1004
                // same signature; select (a) the non-bridge method, or
06bc494ca11e Initial load
duke
parents:
diff changeset
  1005
                // (b) the one that overrides the other, or (c) the concrete
06bc494ca11e Initial load
duke
parents:
diff changeset
  1006
                // 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
  1007
                if ((m1.flags() & BRIDGE) != (m2.flags() & BRIDGE))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1008
                    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
  1009
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1010
                // if one overrides or hides the other, use it
06bc494ca11e Initial load
duke
parents:
diff changeset
  1011
                TypeSymbol m1Owner = (TypeSymbol)m1.owner;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1012
                TypeSymbol m2Owner = (TypeSymbol)m2.owner;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1013
                if (types.asSuper(m1Owner.type, m2Owner) != null &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1014
                    ((m1.owner.flags_field & INTERFACE) == 0 ||
06bc494ca11e Initial load
duke
parents:
diff changeset
  1015
                     (m2.owner.flags_field & INTERFACE) != 0) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1016
                    m1.overrides(m2, m1Owner, types, false))
06bc494ca11e Initial load
duke
parents:
diff changeset
  1017
                    return m1;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1018
                if (types.asSuper(m2Owner.type, m1Owner) != null &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1019
                    ((m2.owner.flags_field & INTERFACE) == 0 ||
06bc494ca11e Initial load
duke
parents:
diff changeset
  1020
                     (m1.owner.flags_field & INTERFACE) != 0) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1021
                    m2.overrides(m1, m2Owner, types, false))
06bc494ca11e Initial load
duke
parents:
diff changeset
  1022
                    return m2;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1023
                boolean m1Abstract = (m1.flags() & ABSTRACT) != 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1024
                boolean m2Abstract = (m2.flags() & ABSTRACT) != 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1025
                if (m1Abstract && !m2Abstract) return m2;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1026
                if (m2Abstract && !m1Abstract) return m1;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1027
                // both abstract or both concrete
06bc494ca11e Initial load
duke
parents:
diff changeset
  1028
                if (!m1Abstract && !m2Abstract)
8228
4e6ee38974b2 6910550: javac 1.5.0_17 fails with incorrect error message
mcimadamore
parents: 8045
diff changeset
  1029
                    return ambiguityError(m1, m2);
1529
a076d4cd3048 6487370: javac incorrectly gives ambiguity warning with override-equivalent abstract inherited methods
mcimadamore
parents: 1528
diff changeset
  1030
                // check that both signatures have the same erasure
a076d4cd3048 6487370: javac incorrectly gives ambiguity warning with override-equivalent abstract inherited methods
mcimadamore
parents: 1528
diff changeset
  1031
                if (!types.isSameTypes(m1.erasure(types).getParameterTypes(),
a076d4cd3048 6487370: javac incorrectly gives ambiguity warning with override-equivalent abstract inherited methods
mcimadamore
parents: 1528
diff changeset
  1032
                                       m2.erasure(types).getParameterTypes()))
8228
4e6ee38974b2 6910550: javac 1.5.0_17 fails with incorrect error message
mcimadamore
parents: 8045
diff changeset
  1033
                    return ambiguityError(m1, m2);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1034
                // both abstract, neither overridden; merge throws clause and result type
10197
28afe0fb34c8 7062745: Regression: difference in overload resolution when two methods are maximally specific
mcimadamore
parents: 9720
diff changeset
  1035
                Type mst = mostSpecificReturnType(mt1, mt2);
28afe0fb34c8 7062745: Regression: difference in overload resolution when two methods are maximally specific
mcimadamore
parents: 9720
diff changeset
  1036
                if (mst == null) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1037
                    // Theoretically, this can't happen, but it is possible
06bc494ca11e Initial load
duke
parents:
diff changeset
  1038
                    // due to error recovery or mixing incompatible class files
8228
4e6ee38974b2 6910550: javac 1.5.0_17 fails with incorrect error message
mcimadamore
parents: 8045
diff changeset
  1039
                    return ambiguityError(m1, m2);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1040
                }
10197
28afe0fb34c8 7062745: Regression: difference in overload resolution when two methods are maximally specific
mcimadamore
parents: 9720
diff changeset
  1041
                Symbol mostSpecific = mst == mt1 ? m1 : m2;
8430
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8239
diff changeset
  1042
                List<Type> allThrown = chk.intersect(mt1.getThrownTypes(), mt2.getThrownTypes());
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8239
diff changeset
  1043
                Type newSig = types.createMethodTypeWithThrown(mostSpecific.type, allThrown);
2511
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
  1044
                MethodSymbol result = new MethodSymbol(
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
  1045
                        mostSpecific.flags(),
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
  1046
                        mostSpecific.name,
8430
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8239
diff changeset
  1047
                        newSig,
2511
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
  1048
                        mostSpecific.owner) {
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
  1049
                    @Override
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
  1050
                    public MethodSymbol implementation(TypeSymbol origin, Types types, boolean checkResult) {
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
  1051
                        if (origin == site.tsym)
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
  1052
                            return this;
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
  1053
                        else
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
  1054
                            return super.implementation(origin, types, checkResult);
14045
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1055
                        }
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1056
                    };
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1057
                return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1058
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1059
            if (m1SignatureMoreSpecific) return m1;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1060
            if (m2SignatureMoreSpecific) return m2;
8228
4e6ee38974b2 6910550: javac 1.5.0_17 fails with incorrect error message
mcimadamore
parents: 8045
diff changeset
  1061
            return ambiguityError(m1, m2);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1062
        case AMBIGUOUS:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1063
            AmbiguityError e = (AmbiguityError)m2;
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1064
            Symbol err1 = mostSpecific(m1, e.sym, env, site, allowBoxing, useVarargs);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1065
            Symbol err2 = mostSpecific(m1, e.sym2, env, site, allowBoxing, useVarargs);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1066
            if (err1 == err2) return err1;
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1067
            if (err1 == e.sym && err2 == e.sym2) return m2;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1068
            if (err1 instanceof AmbiguityError &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1069
                err2 instanceof AmbiguityError &&
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1070
                ((AmbiguityError)err1).sym == ((AmbiguityError)err2).sym)
8228
4e6ee38974b2 6910550: javac 1.5.0_17 fails with incorrect error message
mcimadamore
parents: 8045
diff changeset
  1071
                return ambiguityError(m1, m2);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1072
            else
8228
4e6ee38974b2 6910550: javac 1.5.0_17 fails with incorrect error message
mcimadamore
parents: 8045
diff changeset
  1073
                return ambiguityError(err1, err2);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1074
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1075
            throw new AssertionError();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1076
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1077
    }
7623
66d98f7ba8c7 5088429: varargs overloading problem
mcimadamore
parents: 7209
diff changeset
  1078
    //where
66d98f7ba8c7 5088429: varargs overloading problem
mcimadamore
parents: 7209
diff changeset
  1079
    private boolean signatureMoreSpecific(Env<AttrContext> env, Type site, Symbol m1, Symbol m2, boolean allowBoxing, boolean useVarargs) {
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 7637
diff changeset
  1080
        noteWarner.clear();
7623
66d98f7ba8c7 5088429: varargs overloading problem
mcimadamore
parents: 7209
diff changeset
  1081
        Type mtype1 = types.memberType(site, adjustVarargs(m1, m2, useVarargs));
12915
28cf1e0dafdc 7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents: 12335
diff changeset
  1082
        Type mtype2 = instantiate(env, site, adjustVarargs(m2, m1, useVarargs), null,
9720
bc06a797f393 7042566: Regression: new ambiguity between varargs method
mcimadamore
parents: 9303
diff changeset
  1083
                types.lowerBoundArgtypes(mtype1), null,
bc06a797f393 7042566: Regression: new ambiguity between varargs method
mcimadamore
parents: 9303
diff changeset
  1084
                allowBoxing, false, noteWarner);
bc06a797f393 7042566: Regression: new ambiguity between varargs method
mcimadamore
parents: 9303
diff changeset
  1085
        return mtype2 != null &&
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 7637
diff changeset
  1086
                !noteWarner.hasLint(Lint.LintCategory.UNCHECKED);
7623
66d98f7ba8c7 5088429: varargs overloading problem
mcimadamore
parents: 7209
diff changeset
  1087
    }
66d98f7ba8c7 5088429: varargs overloading problem
mcimadamore
parents: 7209
diff changeset
  1088
    //where
66d98f7ba8c7 5088429: varargs overloading problem
mcimadamore
parents: 7209
diff changeset
  1089
    private Symbol adjustVarargs(Symbol to, Symbol from, boolean useVarargs) {
66d98f7ba8c7 5088429: varargs overloading problem
mcimadamore
parents: 7209
diff changeset
  1090
        List<Type> fromArgs = from.type.getParameterTypes();
66d98f7ba8c7 5088429: varargs overloading problem
mcimadamore
parents: 7209
diff changeset
  1091
        List<Type> toArgs = to.type.getParameterTypes();
66d98f7ba8c7 5088429: varargs overloading problem
mcimadamore
parents: 7209
diff changeset
  1092
        if (useVarargs &&
66d98f7ba8c7 5088429: varargs overloading problem
mcimadamore
parents: 7209
diff changeset
  1093
                (from.flags() & VARARGS) != 0 &&
66d98f7ba8c7 5088429: varargs overloading problem
mcimadamore
parents: 7209
diff changeset
  1094
                (to.flags() & VARARGS) != 0) {
66d98f7ba8c7 5088429: varargs overloading problem
mcimadamore
parents: 7209
diff changeset
  1095
            Type varargsTypeFrom = fromArgs.last();
66d98f7ba8c7 5088429: varargs overloading problem
mcimadamore
parents: 7209
diff changeset
  1096
            Type varargsTypeTo = toArgs.last();
7635
e0a3aeefe4e7 6199075: Unambiguous varargs method calls flagged as ambiguous
mcimadamore
parents: 7623
diff changeset
  1097
            ListBuffer<Type> args = ListBuffer.lb();
e0a3aeefe4e7 6199075: Unambiguous varargs method calls flagged as ambiguous
mcimadamore
parents: 7623
diff changeset
  1098
            if (toArgs.length() < fromArgs.length()) {
e0a3aeefe4e7 6199075: Unambiguous varargs method calls flagged as ambiguous
mcimadamore
parents: 7623
diff changeset
  1099
                //if we are checking a varargs method 'from' against another varargs
e0a3aeefe4e7 6199075: Unambiguous varargs method calls flagged as ambiguous
mcimadamore
parents: 7623
diff changeset
  1100
                //method 'to' (where arity of 'to' < arity of 'from') then expand signature
e0a3aeefe4e7 6199075: Unambiguous varargs method calls flagged as ambiguous
mcimadamore
parents: 7623
diff changeset
  1101
                //of 'to' to 'fit' arity of 'from' (this means adding fake formals to 'to'
e0a3aeefe4e7 6199075: Unambiguous varargs method calls flagged as ambiguous
mcimadamore
parents: 7623
diff changeset
  1102
                //until 'to' signature has the same arity as 'from')
e0a3aeefe4e7 6199075: Unambiguous varargs method calls flagged as ambiguous
mcimadamore
parents: 7623
diff changeset
  1103
                while (fromArgs.head != varargsTypeFrom) {
e0a3aeefe4e7 6199075: Unambiguous varargs method calls flagged as ambiguous
mcimadamore
parents: 7623
diff changeset
  1104
                    args.append(toArgs.head == varargsTypeTo ? types.elemtype(varargsTypeTo) : toArgs.head);
e0a3aeefe4e7 6199075: Unambiguous varargs method calls flagged as ambiguous
mcimadamore
parents: 7623
diff changeset
  1105
                    fromArgs = fromArgs.tail;
e0a3aeefe4e7 6199075: Unambiguous varargs method calls flagged as ambiguous
mcimadamore
parents: 7623
diff changeset
  1106
                    toArgs = toArgs.head == varargsTypeTo ?
e0a3aeefe4e7 6199075: Unambiguous varargs method calls flagged as ambiguous
mcimadamore
parents: 7623
diff changeset
  1107
                        toArgs :
e0a3aeefe4e7 6199075: Unambiguous varargs method calls flagged as ambiguous
mcimadamore
parents: 7623
diff changeset
  1108
                        toArgs.tail;
e0a3aeefe4e7 6199075: Unambiguous varargs method calls flagged as ambiguous
mcimadamore
parents: 7623
diff changeset
  1109
                }
e0a3aeefe4e7 6199075: Unambiguous varargs method calls flagged as ambiguous
mcimadamore
parents: 7623
diff changeset
  1110
            } else {
e0a3aeefe4e7 6199075: Unambiguous varargs method calls flagged as ambiguous
mcimadamore
parents: 7623
diff changeset
  1111
                //formal argument list is same as original list where last
e0a3aeefe4e7 6199075: Unambiguous varargs method calls flagged as ambiguous
mcimadamore
parents: 7623
diff changeset
  1112
                //argument (array type) is removed
e0a3aeefe4e7 6199075: Unambiguous varargs method calls flagged as ambiguous
mcimadamore
parents: 7623
diff changeset
  1113
                args.appendList(toArgs.reverse().tail.reverse());
7623
66d98f7ba8c7 5088429: varargs overloading problem
mcimadamore
parents: 7209
diff changeset
  1114
            }
7635
e0a3aeefe4e7 6199075: Unambiguous varargs method calls flagged as ambiguous
mcimadamore
parents: 7623
diff changeset
  1115
            //append varargs element type as last synthetic formal
e0a3aeefe4e7 6199075: Unambiguous varargs method calls flagged as ambiguous
mcimadamore
parents: 7623
diff changeset
  1116
            args.append(types.elemtype(varargsTypeTo));
8430
be3e5581ea25 6990136: Cleanup use of Type.clone()
dlsmith
parents: 8239
diff changeset
  1117
            Type mtype = types.createMethodTypeWithParameters(to.type, args.toList());
9720
bc06a797f393 7042566: Regression: new ambiguity between varargs method
mcimadamore
parents: 9303
diff changeset
  1118
            return new MethodSymbol(to.flags_field & ~VARARGS, to.name, mtype, to.owner);
7623
66d98f7ba8c7 5088429: varargs overloading problem
mcimadamore
parents: 7209
diff changeset
  1119
        } else {
66d98f7ba8c7 5088429: varargs overloading problem
mcimadamore
parents: 7209
diff changeset
  1120
            return to;
66d98f7ba8c7 5088429: varargs overloading problem
mcimadamore
parents: 7209
diff changeset
  1121
        }
66d98f7ba8c7 5088429: varargs overloading problem
mcimadamore
parents: 7209
diff changeset
  1122
    }
8228
4e6ee38974b2 6910550: javac 1.5.0_17 fails with incorrect error message
mcimadamore
parents: 8045
diff changeset
  1123
    //where
10197
28afe0fb34c8 7062745: Regression: difference in overload resolution when two methods are maximally specific
mcimadamore
parents: 9720
diff changeset
  1124
    Type mostSpecificReturnType(Type mt1, Type mt2) {
28afe0fb34c8 7062745: Regression: difference in overload resolution when two methods are maximally specific
mcimadamore
parents: 9720
diff changeset
  1125
        Type rt1 = mt1.getReturnType();
28afe0fb34c8 7062745: Regression: difference in overload resolution when two methods are maximally specific
mcimadamore
parents: 9720
diff changeset
  1126
        Type rt2 = mt2.getReturnType();
28afe0fb34c8 7062745: Regression: difference in overload resolution when two methods are maximally specific
mcimadamore
parents: 9720
diff changeset
  1127
28afe0fb34c8 7062745: Regression: difference in overload resolution when two methods are maximally specific
mcimadamore
parents: 9720
diff changeset
  1128
        if (mt1.tag == FORALL && mt2.tag == FORALL) {
28afe0fb34c8 7062745: Regression: difference in overload resolution when two methods are maximally specific
mcimadamore
parents: 9720
diff changeset
  1129
            //if both are generic methods, adjust return type ahead of subtyping check
28afe0fb34c8 7062745: Regression: difference in overload resolution when two methods are maximally specific
mcimadamore
parents: 9720
diff changeset
  1130
            rt1 = types.subst(rt1, mt1.getTypeArguments(), mt2.getTypeArguments());
28afe0fb34c8 7062745: Regression: difference in overload resolution when two methods are maximally specific
mcimadamore
parents: 9720
diff changeset
  1131
        }
28afe0fb34c8 7062745: Regression: difference in overload resolution when two methods are maximally specific
mcimadamore
parents: 9720
diff changeset
  1132
        //first use subtyping, then return type substitutability
28afe0fb34c8 7062745: Regression: difference in overload resolution when two methods are maximally specific
mcimadamore
parents: 9720
diff changeset
  1133
        if (types.isSubtype(rt1, rt2)) {
28afe0fb34c8 7062745: Regression: difference in overload resolution when two methods are maximally specific
mcimadamore
parents: 9720
diff changeset
  1134
            return mt1;
28afe0fb34c8 7062745: Regression: difference in overload resolution when two methods are maximally specific
mcimadamore
parents: 9720
diff changeset
  1135
        } else if (types.isSubtype(rt2, rt1)) {
28afe0fb34c8 7062745: Regression: difference in overload resolution when two methods are maximally specific
mcimadamore
parents: 9720
diff changeset
  1136
            return mt2;
28afe0fb34c8 7062745: Regression: difference in overload resolution when two methods are maximally specific
mcimadamore
parents: 9720
diff changeset
  1137
        } else if (types.returnTypeSubstitutable(mt1, mt2)) {
28afe0fb34c8 7062745: Regression: difference in overload resolution when two methods are maximally specific
mcimadamore
parents: 9720
diff changeset
  1138
            return mt1;
28afe0fb34c8 7062745: Regression: difference in overload resolution when two methods are maximally specific
mcimadamore
parents: 9720
diff changeset
  1139
        } else if (types.returnTypeSubstitutable(mt2, mt1)) {
28afe0fb34c8 7062745: Regression: difference in overload resolution when two methods are maximally specific
mcimadamore
parents: 9720
diff changeset
  1140
            return mt2;
28afe0fb34c8 7062745: Regression: difference in overload resolution when two methods are maximally specific
mcimadamore
parents: 9720
diff changeset
  1141
        } else {
28afe0fb34c8 7062745: Regression: difference in overload resolution when two methods are maximally specific
mcimadamore
parents: 9720
diff changeset
  1142
            return null;
28afe0fb34c8 7062745: Regression: difference in overload resolution when two methods are maximally specific
mcimadamore
parents: 9720
diff changeset
  1143
        }
28afe0fb34c8 7062745: Regression: difference in overload resolution when two methods are maximally specific
mcimadamore
parents: 9720
diff changeset
  1144
    }
28afe0fb34c8 7062745: Regression: difference in overload resolution when two methods are maximally specific
mcimadamore
parents: 9720
diff changeset
  1145
    //where
8228
4e6ee38974b2 6910550: javac 1.5.0_17 fails with incorrect error message
mcimadamore
parents: 8045
diff changeset
  1146
    Symbol ambiguityError(Symbol m1, Symbol m2) {
4e6ee38974b2 6910550: javac 1.5.0_17 fails with incorrect error message
mcimadamore
parents: 8045
diff changeset
  1147
        if (((m1.flags() | m2.flags()) & CLASH) != 0) {
4e6ee38974b2 6910550: javac 1.5.0_17 fails with incorrect error message
mcimadamore
parents: 8045
diff changeset
  1148
            return (m1.flags() & CLASH) == 0 ? m1 : m2;
4e6ee38974b2 6910550: javac 1.5.0_17 fails with incorrect error message
mcimadamore
parents: 8045
diff changeset
  1149
        } else {
4e6ee38974b2 6910550: javac 1.5.0_17 fails with incorrect error message
mcimadamore
parents: 8045
diff changeset
  1150
            return new AmbiguityError(m1, m2);
4e6ee38974b2 6910550: javac 1.5.0_17 fails with incorrect error message
mcimadamore
parents: 8045
diff changeset
  1151
        }
4e6ee38974b2 6910550: javac 1.5.0_17 fails with incorrect error message
mcimadamore
parents: 8045
diff changeset
  1152
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1153
06bc494ca11e Initial load
duke
parents:
diff changeset
  1154
    /** Find best qualified method matching given name, type and value
06bc494ca11e Initial load
duke
parents:
diff changeset
  1155
     *  arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1156
     *  @param env       The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1157
     *  @param site      The original type from where the selection
06bc494ca11e Initial load
duke
parents:
diff changeset
  1158
     *                   takes place.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1159
     *  @param name      The method's name.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1160
     *  @param argtypes  The method's value arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1161
     *  @param typeargtypes The method's type arguments
06bc494ca11e Initial load
duke
parents:
diff changeset
  1162
     *  @param allowBoxing Allow boxing conversions of arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1163
     *  @param useVarargs Box trailing arguments into an array for varargs.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1164
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1165
    Symbol findMethod(Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1166
                      Type site,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1167
                      Name name,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1168
                      List<Type> argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1169
                      List<Type> typeargtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1170
                      boolean allowBoxing,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1171
                      boolean useVarargs,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1172
                      boolean operator) {
5736
ee0850472ca1 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 5321
diff changeset
  1173
        Symbol bestSoFar = methodNotFound;
10816
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
  1174
        bestSoFar = findMethod(env,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1175
                          site,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1176
                          name,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1177
                          argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1178
                          typeargtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1179
                          site.tsym.type,
5736
ee0850472ca1 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 5321
diff changeset
  1180
                          bestSoFar,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1181
                          allowBoxing,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1182
                          useVarargs,
14045
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1183
                          operator);
10816
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
  1184
        reportVerboseResolutionDiagnostic(env.tree.pos(), name, site, argtypes, typeargtypes, bestSoFar);
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
  1185
        return bestSoFar;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1186
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1187
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
  1188
    private Symbol findMethod(Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1189
                              Type site,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1190
                              Name name,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1191
                              List<Type> argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1192
                              List<Type> typeargtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1193
                              Type intype,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1194
                              Symbol bestSoFar,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1195
                              boolean allowBoxing,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1196
                              boolean useVarargs,
14045
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1197
                              boolean operator) {
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1198
        boolean abstractOk = true;
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1199
        List<Type> itypes = List.nil();
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1200
        for (TypeSymbol s : superclasses(intype)) {
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1201
            bestSoFar = lookupMethod(env, site, name, argtypes, typeargtypes,
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1202
                    s.members(), bestSoFar, allowBoxing, useVarargs, operator, true);
14052
8b839ae9074b 8000233: Fix issues in recent push
mcimadamore
parents: 14051
diff changeset
  1203
            //We should not look for abstract methods if receiver is a concrete class
8b839ae9074b 8000233: Fix issues in recent push
mcimadamore
parents: 14051
diff changeset
  1204
            //(as concrete classes are expected to implement all abstracts coming
8b839ae9074b 8000233: Fix issues in recent push
mcimadamore
parents: 14051
diff changeset
  1205
            //from superinterfaces)
8b839ae9074b 8000233: Fix issues in recent push
mcimadamore
parents: 14051
diff changeset
  1206
            abstractOk &= (s.flags() & (ABSTRACT | INTERFACE | ENUM)) != 0;
14045
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1207
            if (abstractOk) {
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1208
                for (Type itype : types.interfaces(s.type)) {
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1209
                    itypes = types.union(types.closure(itype), itypes);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1210
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1211
            }
14045
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1212
            if (name == names.init) break;
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1213
        }
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1214
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1215
        Symbol concrete = bestSoFar.kind < ERR &&
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1216
                (bestSoFar.flags() & ABSTRACT) == 0 ?
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1217
                bestSoFar : methodNotFound;
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1218
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1219
        if (name != names.init) {
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1220
            //keep searching for abstract methods
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1221
            for (Type itype : itypes) {
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1222
                if (!itype.isInterface()) continue; //skip j.l.Object (included by Types.closure())
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1223
                bestSoFar = lookupMethod(env, site, name, argtypes, typeargtypes,
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1224
                    itype.tsym.members(), bestSoFar, allowBoxing, useVarargs, operator, true);
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1225
                    if (concrete != bestSoFar &&
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1226
                            concrete.kind < ERR  && bestSoFar.kind < ERR &&
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1227
                            types.isSubSignature(concrete.type, bestSoFar.type)) {
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1228
                        //this is an hack - as javac does not do full membership checks
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1229
                        //most specific ends up comparing abstract methods that might have
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1230
                        //been implemented by some concrete method in a subclass and,
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1231
                        //because of raw override, it is possible for an abstract method
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1232
                        //to be more specific than the concrete method - so we need
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1233
                        //to explicitly call that out (see CR 6178365)
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1234
                        bestSoFar = concrete;
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1235
                    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1236
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1237
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1238
        return bestSoFar;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1239
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1240
14045
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1241
    /**
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1242
     * Return an Iterable object to scan the superclasses of a given type.
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1243
     * 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
  1244
     * access more supertypes than strictly needed (as this could trigger completion
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1245
     * errors if some of the not-needed supertypes are missing/ill-formed).
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1246
     */
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1247
    Iterable<TypeSymbol> superclasses(final Type intype) {
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1248
        return new Iterable<TypeSymbol>() {
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1249
            public Iterator<TypeSymbol> iterator() {
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1250
                return new Iterator<TypeSymbol>() {
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1251
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1252
                    List<TypeSymbol> seen = List.nil();
14052
8b839ae9074b 8000233: Fix issues in recent push
mcimadamore
parents: 14051
diff changeset
  1253
                    TypeSymbol currentSym = symbolFor(intype);
8b839ae9074b 8000233: Fix issues in recent push
mcimadamore
parents: 14051
diff changeset
  1254
                    TypeSymbol prevSym = null;
14045
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1255
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1256
                    public boolean hasNext() {
14052
8b839ae9074b 8000233: Fix issues in recent push
mcimadamore
parents: 14051
diff changeset
  1257
                        if (currentSym == syms.noSymbol) {
8b839ae9074b 8000233: Fix issues in recent push
mcimadamore
parents: 14051
diff changeset
  1258
                            currentSym = symbolFor(types.supertype(prevSym.type));
8b839ae9074b 8000233: Fix issues in recent push
mcimadamore
parents: 14051
diff changeset
  1259
                        }
14045
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1260
                        return currentSym != null;
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1261
                    }
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1262
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1263
                    public TypeSymbol next() {
14052
8b839ae9074b 8000233: Fix issues in recent push
mcimadamore
parents: 14051
diff changeset
  1264
                        prevSym = currentSym;
8b839ae9074b 8000233: Fix issues in recent push
mcimadamore
parents: 14051
diff changeset
  1265
                        currentSym = syms.noSymbol;
8b839ae9074b 8000233: Fix issues in recent push
mcimadamore
parents: 14051
diff changeset
  1266
                        Assert.check(prevSym != null || prevSym != syms.noSymbol);
14045
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1267
                        return prevSym;
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1268
                    }
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1269
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1270
                    public void remove() {
14052
8b839ae9074b 8000233: Fix issues in recent push
mcimadamore
parents: 14051
diff changeset
  1271
                        throw new UnsupportedOperationException();
14045
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1272
                    }
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1273
14052
8b839ae9074b 8000233: Fix issues in recent push
mcimadamore
parents: 14051
diff changeset
  1274
                    TypeSymbol symbolFor(Type t) {
8b839ae9074b 8000233: Fix issues in recent push
mcimadamore
parents: 14051
diff changeset
  1275
                        if (t.tag != CLASS &&
8b839ae9074b 8000233: Fix issues in recent push
mcimadamore
parents: 14051
diff changeset
  1276
                                t.tag != TYPEVAR) {
14045
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1277
                            return null;
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1278
                        }
14052
8b839ae9074b 8000233: Fix issues in recent push
mcimadamore
parents: 14051
diff changeset
  1279
                        while (t.tag == TYPEVAR)
8b839ae9074b 8000233: Fix issues in recent push
mcimadamore
parents: 14051
diff changeset
  1280
                            t = t.getUpperBound();
8b839ae9074b 8000233: Fix issues in recent push
mcimadamore
parents: 14051
diff changeset
  1281
                        if (seen.contains(t.tsym)) {
14045
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1282
                            //degenerate case in which we have a circular
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1283
                            //class hierarchy - because of ill-formed classfiles
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1284
                            return null;
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1285
                        }
14052
8b839ae9074b 8000233: Fix issues in recent push
mcimadamore
parents: 14051
diff changeset
  1286
                        seen = seen.prepend(t.tsym);
8b839ae9074b 8000233: Fix issues in recent push
mcimadamore
parents: 14051
diff changeset
  1287
                        return t.tsym;
14045
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1288
                    }
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1289
                };
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1290
            }
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1291
        };
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1292
    }
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1293
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1294
    /**
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1295
     * Lookup a method with given name and argument types in a given scope
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1296
     */
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1297
    Symbol lookupMethod(Env<AttrContext> env,
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1298
            Type site,
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1299
            Name name,
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1300
            List<Type> argtypes,
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1301
            List<Type> typeargtypes,
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1302
            Scope sc,
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1303
            Symbol bestSoFar,
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1304
            boolean allowBoxing,
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1305
            boolean useVarargs,
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1306
            boolean operator,
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1307
            boolean abstractok) {
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1308
        for (Symbol s : sc.getElementsByName(name, lookupFilter)) {
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1309
            bestSoFar = selectBest(env, site, argtypes, typeargtypes, s,
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1310
                    bestSoFar, allowBoxing, useVarargs, operator);
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1311
        }
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1312
        return bestSoFar;
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1313
    }
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1314
    //where
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1315
        Filter<Symbol> lookupFilter = new Filter<Symbol>() {
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1316
            public boolean accepts(Symbol s) {
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1317
                return s.kind == MTH &&
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1318
                        (s.flags() & SYNTHETIC) == 0;
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1319
            }
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1320
        };
c66c0a81c4ab 7193913: Cleanup Resolve.findMethod
mcimadamore
parents: 13844
diff changeset
  1321
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1322
    /** Find unqualified method matching given name, type and value arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1323
     *  @param env       The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1324
     *  @param name      The method's name.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1325
     *  @param argtypes  The method's value arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1326
     *  @param typeargtypes  The method's type arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1327
     *  @param allowBoxing Allow boxing conversions of arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1328
     *  @param useVarargs Box trailing arguments into an array for varargs.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1329
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1330
    Symbol findFun(Env<AttrContext> env, Name name,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1331
                   List<Type> argtypes, List<Type> typeargtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1332
                   boolean allowBoxing, boolean useVarargs) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1333
        Symbol bestSoFar = methodNotFound;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1334
        Symbol sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1335
        Env<AttrContext> env1 = env;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1336
        boolean staticOnly = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1337
        while (env1.outer != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1338
            if (isStatic(env1)) staticOnly = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1339
            sym = findMethod(
06bc494ca11e Initial load
duke
parents:
diff changeset
  1340
                env1, env1.enclClass.sym.type, name, argtypes, typeargtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1341
                allowBoxing, useVarargs, false);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1342
            if (sym.exists()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1343
                if (staticOnly &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1344
                    sym.kind == MTH &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1345
                    sym.owner.kind == TYP &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1346
                    (sym.flags() & STATIC) == 0) return new StaticError(sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1347
                else return sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1348
            } else if (sym.kind < bestSoFar.kind) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1349
                bestSoFar = sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1350
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1351
            if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1352
            env1 = env1.outer;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1353
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1354
06bc494ca11e Initial load
duke
parents:
diff changeset
  1355
        sym = findMethod(env, syms.predefClass.type, name, argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1356
                         typeargtypes, allowBoxing, useVarargs, false);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1357
        if (sym.exists())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1358
            return sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1359
06bc494ca11e Initial load
duke
parents:
diff changeset
  1360
        Scope.Entry e = env.toplevel.namedImportScope.lookup(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1361
        for (; e.scope != null; e = e.next()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1362
            sym = e.sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1363
            Type origin = e.getOrigin().owner.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1364
            if (sym.kind == MTH) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1365
                if (e.sym.owner.type != origin)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1366
                    sym = sym.clone(e.getOrigin().owner);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1367
                if (!isAccessible(env, origin, sym))
06bc494ca11e Initial load
duke
parents:
diff changeset
  1368
                    sym = new AccessError(env, origin, sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1369
                bestSoFar = selectBest(env, origin,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1370
                                       argtypes, typeargtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1371
                                       sym, bestSoFar,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1372
                                       allowBoxing, useVarargs, false);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1373
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1374
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1375
        if (bestSoFar.exists())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1376
            return bestSoFar;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1377
06bc494ca11e Initial load
duke
parents:
diff changeset
  1378
        e = env.toplevel.starImportScope.lookup(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1379
        for (; e.scope != null; e = e.next()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1380
            sym = e.sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1381
            Type origin = e.getOrigin().owner.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1382
            if (sym.kind == MTH) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1383
                if (e.sym.owner.type != origin)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1384
                    sym = sym.clone(e.getOrigin().owner);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1385
                if (!isAccessible(env, origin, sym))
06bc494ca11e Initial load
duke
parents:
diff changeset
  1386
                    sym = new AccessError(env, origin, sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1387
                bestSoFar = selectBest(env, origin,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1388
                                       argtypes, typeargtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1389
                                       sym, bestSoFar,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1390
                                       allowBoxing, useVarargs, false);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1391
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1392
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1393
        return bestSoFar;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1394
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1395
06bc494ca11e Initial load
duke
parents:
diff changeset
  1396
    /** Load toplevel or member class with given fully qualified name and
06bc494ca11e Initial load
duke
parents:
diff changeset
  1397
     *  verify that it is accessible.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1398
     *  @param env       The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1399
     *  @param name      The fully qualified name of the class to be loaded.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1400
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1401
    Symbol loadClass(Env<AttrContext> env, Name name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1402
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1403
            ClassSymbol c = reader.loadClass(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1404
            return isAccessible(env, c) ? c : new AccessError(c);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1405
        } catch (ClassReader.BadClassFile err) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1406
            throw err;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1407
        } catch (CompletionFailure ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1408
            return typeNotFound;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1409
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1410
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1411
06bc494ca11e Initial load
duke
parents:
diff changeset
  1412
    /** Find qualified member type.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1413
     *  @param env       The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1414
     *  @param site      The original type from where the selection takes
06bc494ca11e Initial load
duke
parents:
diff changeset
  1415
     *                   place.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1416
     *  @param name      The type's name.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1417
     *  @param c         The class to search for the member type. This is
06bc494ca11e Initial load
duke
parents:
diff changeset
  1418
     *                   always a superclass or implemented interface of
06bc494ca11e Initial load
duke
parents:
diff changeset
  1419
     *                   site's class.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1420
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1421
    Symbol findMemberType(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) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1425
        Symbol bestSoFar = typeNotFound;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1426
        Symbol sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1427
        Scope.Entry e = c.members().lookup(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1428
        while (e.scope != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1429
            if (e.sym.kind == TYP) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1430
                return isAccessible(env, site, e.sym)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1431
                    ? e.sym
06bc494ca11e Initial load
duke
parents:
diff changeset
  1432
                    : new AccessError(env, site, e.sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1433
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1434
            e = e.next();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1435
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1436
        Type st = types.supertype(c.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1437
        if (st != null && st.tag == CLASS) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1438
            sym = findMemberType(env, site, name, st.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1439
            if (sym.kind < bestSoFar.kind) bestSoFar = sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1440
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1441
        for (List<Type> l = types.interfaces(c.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1442
             bestSoFar.kind != AMBIGUOUS && l.nonEmpty();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1443
             l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1444
            sym = findMemberType(env, site, name, l.head.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1445
            if (bestSoFar.kind < AMBIGUOUS && sym.kind < AMBIGUOUS &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1446
                sym.owner != bestSoFar.owner)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1447
                bestSoFar = new AmbiguityError(bestSoFar, sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1448
            else if (sym.kind < bestSoFar.kind)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1449
                bestSoFar = sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1450
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1451
        return bestSoFar;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1452
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1453
06bc494ca11e Initial load
duke
parents:
diff changeset
  1454
    /** Find a global type in given scope and load corresponding class.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1455
     *  @param env       The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1456
     *  @param scope     The scope in which to look for the type.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1457
     *  @param name      The type's name.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1458
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1459
    Symbol findGlobalType(Env<AttrContext> env, Scope scope, Name name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1460
        Symbol bestSoFar = typeNotFound;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1461
        for (Scope.Entry e = scope.lookup(name); e.scope != null; e = e.next()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1462
            Symbol sym = loadClass(env, e.sym.flatName());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1463
            if (bestSoFar.kind == TYP && sym.kind == TYP &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1464
                bestSoFar != sym)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1465
                return new AmbiguityError(bestSoFar, sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1466
            else if (sym.kind < bestSoFar.kind)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1467
                bestSoFar = sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1468
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1469
        return bestSoFar;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1470
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1471
06bc494ca11e Initial load
duke
parents:
diff changeset
  1472
    /** Find an unqualified type symbol.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1473
     *  @param env       The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1474
     *  @param name      The type's name.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1475
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1476
    Symbol findType(Env<AttrContext> env, Name name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1477
        Symbol bestSoFar = typeNotFound;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1478
        Symbol sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1479
        boolean staticOnly = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1480
        for (Env<AttrContext> env1 = env; env1.outer != null; env1 = env1.outer) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1481
            if (isStatic(env1)) staticOnly = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1482
            for (Scope.Entry e = env1.info.scope.lookup(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1483
                 e.scope != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1484
                 e = e.next()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1485
                if (e.sym.kind == TYP) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1486
                    if (staticOnly &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1487
                        e.sym.type.tag == TYPEVAR &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1488
                        e.sym.owner.kind == TYP) return new StaticError(e.sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1489
                    return e.sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1490
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1491
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1492
06bc494ca11e Initial load
duke
parents:
diff changeset
  1493
            sym = findMemberType(env1, env1.enclClass.sym.type, name,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1494
                                 env1.enclClass.sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1495
            if (staticOnly && sym.kind == TYP &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1496
                sym.type.tag == CLASS &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1497
                sym.type.getEnclosingType().tag == CLASS &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1498
                env1.enclClass.sym.type.isParameterized() &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1499
                sym.type.getEnclosingType().isParameterized())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1500
                return new StaticError(sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1501
            else if (sym.exists()) return sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1502
            else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1503
06bc494ca11e Initial load
duke
parents:
diff changeset
  1504
            JCClassDecl encl = env1.baseClause ? (JCClassDecl)env1.tree : env1.enclClass;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1505
            if ((encl.sym.flags() & STATIC) != 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1506
                staticOnly = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1507
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1508
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10816
diff changeset
  1509
        if (!env.tree.hasTag(IMPORT)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1510
            sym = findGlobalType(env, env.toplevel.namedImportScope, name);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1511
            if (sym.exists()) return sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1512
            else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1513
06bc494ca11e Initial load
duke
parents:
diff changeset
  1514
            sym = findGlobalType(env, env.toplevel.packge.members(), name);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1515
            if (sym.exists()) return sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1516
            else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1517
06bc494ca11e Initial load
duke
parents:
diff changeset
  1518
            sym = findGlobalType(env, env.toplevel.starImportScope, name);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1519
            if (sym.exists()) return sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1520
            else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1521
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1522
06bc494ca11e Initial load
duke
parents:
diff changeset
  1523
        return bestSoFar;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1524
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1525
06bc494ca11e Initial load
duke
parents:
diff changeset
  1526
    /** Find an unqualified identifier which matches a specified kind set.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1527
     *  @param env       The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1528
     *  @param name      The indentifier's name.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1529
     *  @param kind      Indicates the possible symbol kinds
06bc494ca11e Initial load
duke
parents:
diff changeset
  1530
     *                   (a subset of VAL, TYP, PCK).
06bc494ca11e Initial load
duke
parents:
diff changeset
  1531
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1532
    Symbol findIdent(Env<AttrContext> env, Name name, int kind) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1533
        Symbol bestSoFar = typeNotFound;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1534
        Symbol sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1535
06bc494ca11e Initial load
duke
parents:
diff changeset
  1536
        if ((kind & VAR) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1537
            sym = findVar(env, name);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1538
            if (sym.exists()) return sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1539
            else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1540
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1541
06bc494ca11e Initial load
duke
parents:
diff changeset
  1542
        if ((kind & TYP) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1543
            sym = findType(env, name);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1544
            if (sym.exists()) return sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1545
            else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1546
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1547
06bc494ca11e Initial load
duke
parents:
diff changeset
  1548
        if ((kind & PCK) != 0) return reader.enterPackage(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1549
        else return bestSoFar;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1550
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1551
06bc494ca11e Initial load
duke
parents:
diff changeset
  1552
    /** Find an identifier in a package which matches a specified kind set.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1553
     *  @param env       The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1554
     *  @param name      The identifier's name.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1555
     *  @param kind      Indicates the possible symbol kinds
06bc494ca11e Initial load
duke
parents:
diff changeset
  1556
     *                   (a nonempty subset of TYP, PCK).
06bc494ca11e Initial load
duke
parents:
diff changeset
  1557
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1558
    Symbol findIdentInPackage(Env<AttrContext> env, TypeSymbol pck,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1559
                              Name name, int kind) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1560
        Name fullname = TypeSymbol.formFullName(name, pck);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1561
        Symbol bestSoFar = typeNotFound;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1562
        PackageSymbol pack = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1563
        if ((kind & PCK) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1564
            pack = reader.enterPackage(fullname);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1565
            if (pack.exists()) return pack;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1566
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1567
        if ((kind & TYP) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1568
            Symbol sym = loadClass(env, fullname);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1569
            if (sym.exists()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1570
                // don't allow programs to use flatnames
06bc494ca11e Initial load
duke
parents:
diff changeset
  1571
                if (name == sym.name) return sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1572
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1573
            else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1574
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1575
        return (pack != null) ? pack : bestSoFar;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1576
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1577
06bc494ca11e Initial load
duke
parents:
diff changeset
  1578
    /** Find an identifier among the members of a given type `site'.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1579
     *  @param env       The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1580
     *  @param site      The type containing the symbol to be found.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1581
     *  @param name      The identifier's name.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1582
     *  @param kind      Indicates the possible symbol kinds
06bc494ca11e Initial load
duke
parents:
diff changeset
  1583
     *                   (a subset of VAL, TYP).
06bc494ca11e Initial load
duke
parents:
diff changeset
  1584
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1585
    Symbol findIdentInType(Env<AttrContext> env, Type site,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1586
                           Name name, int kind) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1587
        Symbol bestSoFar = typeNotFound;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1588
        Symbol sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1589
        if ((kind & VAR) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1590
            sym = findField(env, site, name, site.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1591
            if (sym.exists()) return sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1592
            else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1593
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1594
06bc494ca11e Initial load
duke
parents:
diff changeset
  1595
        if ((kind & TYP) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1596
            sym = findMemberType(env, site, name, site.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1597
            if (sym.exists()) return sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1598
            else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1599
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1600
        return bestSoFar;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1601
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1602
06bc494ca11e Initial load
duke
parents:
diff changeset
  1603
/* ***************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
  1604
 *  Access checking
06bc494ca11e Initial load
duke
parents:
diff changeset
  1605
 *  The following methods convert ResolveErrors to ErrorSymbols, issuing
06bc494ca11e Initial load
duke
parents:
diff changeset
  1606
 *  an error message in the process
06bc494ca11e Initial load
duke
parents:
diff changeset
  1607
 ****************************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
  1608
06bc494ca11e Initial load
duke
parents:
diff changeset
  1609
    /** If `sym' is a bad symbol: report error and return errSymbol
06bc494ca11e Initial load
duke
parents:
diff changeset
  1610
     *  else pass through unchanged,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1611
     *  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
  1612
     *  symbol {@literal (--> flyweight pattern)}. This improves performance since we
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1613
     *  expect misses to happen frequently.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1614
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  1615
     *  @param sym       The symbol that was found, or a ResolveError.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1616
     *  @param pos       The position to use for error reporting.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1617
     *  @param site      The original type from where the selection took place.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1618
     *  @param name      The symbol's name.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1619
     *  @param argtypes  The invocation's value arguments,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1620
     *                   if we looked for a method.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1621
     *  @param typeargtypes  The invocation's type arguments,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1622
     *                   if we looked for a method.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1623
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1624
    Symbol access(Symbol sym,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1625
                  DiagnosticPosition pos,
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  1626
                  Symbol location,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1627
                  Type site,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1628
                  Name name,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1629
                  boolean qualified,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1630
                  List<Type> argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1631
                  List<Type> typeargtypes) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1632
        if (sym.kind >= AMBIGUOUS) {
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1633
            ResolveError errSym = (ResolveError)sym;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1634
            if (!site.isErroneous() &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1635
                !Type.isErroneous(argtypes) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1636
                (typeargtypes==null || !Type.isErroneous(typeargtypes)))
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  1637
                logResolveError(errSym, pos, location, site, name, argtypes, typeargtypes);
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1638
            sym = errSym.access(name, qualified ? site.tsym : syms.noSymbol);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1639
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1640
        return sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1641
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1642
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  1643
    /** Same as original access(), but without location.
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  1644
     */
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  1645
    Symbol access(Symbol sym,
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  1646
                  DiagnosticPosition pos,
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  1647
                  Type site,
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  1648
                  Name name,
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  1649
                  boolean qualified,
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  1650
                  List<Type> argtypes,
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  1651
                  List<Type> typeargtypes) {
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  1652
        return access(sym, pos, site.tsym, site, name, qualified, argtypes, typeargtypes);
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  1653
    }
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  1654
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  1655
    /** Same as original access(), but without type arguments and arguments.
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  1656
     */
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  1657
    Symbol access(Symbol sym,
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  1658
                  DiagnosticPosition pos,
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  1659
                  Symbol location,
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  1660
                  Type site,
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  1661
                  Name name,
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  1662
                  boolean qualified) {
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  1663
        if (sym.kind >= AMBIGUOUS)
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  1664
            return access(sym, pos, location, site, name, qualified, List.<Type>nil(), null);
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  1665
        else
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  1666
            return sym;
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  1667
    }
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  1668
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  1669
    /** Same as original access(), but without location, type arguments and arguments.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1670
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1671
    Symbol access(Symbol sym,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1672
                  DiagnosticPosition pos,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1673
                  Type site,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1674
                  Name name,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1675
                  boolean qualified) {
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  1676
        return access(sym, pos, site.tsym, site, name, qualified);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1677
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1678
06bc494ca11e Initial load
duke
parents:
diff changeset
  1679
    /** Check that sym is not an abstract method.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1680
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1681
    void checkNonAbstract(DiagnosticPosition pos, Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1682
        if ((sym.flags() & ABSTRACT) != 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1683
            log.error(pos, "abstract.cant.be.accessed.directly",
06bc494ca11e Initial load
duke
parents:
diff changeset
  1684
                      kindName(sym), sym, sym.location());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1685
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1686
06bc494ca11e Initial load
duke
parents:
diff changeset
  1687
/* ***************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
  1688
 *  Debugging
06bc494ca11e Initial load
duke
parents:
diff changeset
  1689
 ****************************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
  1690
06bc494ca11e Initial load
duke
parents:
diff changeset
  1691
    /** print all scopes starting with scope s and proceeding outwards.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1692
     *  used for debugging.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1693
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1694
    public void printscopes(Scope s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1695
        while (s != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1696
            if (s.owner != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1697
                System.err.print(s.owner + ": ");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1698
            for (Scope.Entry e = s.elems; e != null; e = e.sibling) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1699
                if ((e.sym.flags() & ABSTRACT) != 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1700
                    System.err.print("abstract ");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1701
                System.err.print(e.sym + " ");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1702
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1703
            System.err.println();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1704
            s = s.next;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1705
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1706
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1707
06bc494ca11e Initial load
duke
parents:
diff changeset
  1708
    void printscopes(Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1709
        while (env.outer != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1710
            System.err.println("------------------------------");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1711
            printscopes(env.info.scope);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1712
            env = env.outer;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1713
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1714
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1715
06bc494ca11e Initial load
duke
parents:
diff changeset
  1716
    public void printscopes(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1717
        while (t.tag == CLASS) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1718
            printscopes(t.tsym.members());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1719
            t = types.supertype(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1720
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1721
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1722
06bc494ca11e Initial load
duke
parents:
diff changeset
  1723
/* ***************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
  1724
 *  Name resolution
06bc494ca11e Initial load
duke
parents:
diff changeset
  1725
 *  Naming conventions are as for symbol lookup
06bc494ca11e Initial load
duke
parents:
diff changeset
  1726
 *  Unlike the find... methods these methods will report access errors
06bc494ca11e Initial load
duke
parents:
diff changeset
  1727
 ****************************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
  1728
06bc494ca11e Initial load
duke
parents:
diff changeset
  1729
    /** Resolve an unqualified (non-method) identifier.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1730
     *  @param pos       The position to use for error reporting.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1731
     *  @param env       The environment current at the identifier use.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1732
     *  @param name      The identifier's name.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1733
     *  @param kind      The set of admissible symbol kinds for the identifier.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1734
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1735
    Symbol resolveIdent(DiagnosticPosition pos, Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1736
                        Name name, int kind) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1737
        return access(
06bc494ca11e Initial load
duke
parents:
diff changeset
  1738
            findIdent(env, name, kind),
06bc494ca11e Initial load
duke
parents:
diff changeset
  1739
            pos, env.enclClass.sym.type, name, false);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1740
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1741
06bc494ca11e Initial load
duke
parents:
diff changeset
  1742
    /** Resolve an unqualified method identifier.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1743
     *  @param pos       The position to use for error reporting.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1744
     *  @param env       The environment current at the method invocation.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1745
     *  @param name      The identifier's name.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1746
     *  @param argtypes  The types of the invocation's value arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1747
     *  @param typeargtypes  The types of the invocation's type arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1748
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1749
    Symbol resolveMethod(DiagnosticPosition pos,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1750
                         Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1751
                         Name name,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1752
                         List<Type> argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1753
                         List<Type> typeargtypes) {
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1754
        MethodResolutionContext prevResolutionContext = currentResolutionContext;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1755
        try {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1756
            currentResolutionContext = new MethodResolutionContext();
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1757
            Symbol sym = methodNotFound;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1758
            List<MethodResolutionPhase> steps = methodResolutionSteps;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1759
            while (steps.nonEmpty() &&
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1760
                   steps.head.isApplicable(boxingEnabled, varargsEnabled) &&
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1761
                   sym.kind >= ERRONEOUS) {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1762
                currentResolutionContext.step = steps.head;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1763
                sym = findFun(env, name, argtypes, typeargtypes,
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1764
                        steps.head.isBoxingRequired,
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1765
                        env.info.varArgs = steps.head.isVarargsRequired);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1766
                currentResolutionContext.resolutionCache.put(steps.head, sym);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1767
                steps = steps.tail;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1768
            }
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1769
            if (sym.kind >= AMBIGUOUS) {//if nothing is found return the 'first' error
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1770
                MethodResolutionPhase errPhase =
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1771
                        currentResolutionContext.firstErroneousResolutionPhase();
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1772
                sym = access(currentResolutionContext.resolutionCache.get(errPhase),
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1773
                        pos, env.enclClass.sym.type, name, false, argtypes, typeargtypes);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1774
                env.info.varArgs = errPhase.isVarargsRequired;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1775
            }
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1776
            return sym;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1777
        }
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1778
        finally {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1779
            currentResolutionContext = prevResolutionContext;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1780
        }
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
  1781
    }
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
  1782
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1783
    /** Resolve a qualified method identifier
06bc494ca11e Initial load
duke
parents:
diff changeset
  1784
     *  @param pos       The position to use for error reporting.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1785
     *  @param env       The environment current at the method invocation.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1786
     *  @param site      The type of the qualifying expression, in which
06bc494ca11e Initial load
duke
parents:
diff changeset
  1787
     *                   identifier is searched.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1788
     *  @param name      The identifier's name.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1789
     *  @param argtypes  The types of the invocation's value arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1790
     *  @param typeargtypes  The types of the invocation's type arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1791
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1792
    Symbol resolveQualifiedMethod(DiagnosticPosition pos, Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1793
                                  Type site, Name name, List<Type> argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1794
                                  List<Type> typeargtypes) {
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  1795
        return resolveQualifiedMethod(pos, env, site.tsym, site, name, argtypes, typeargtypes);
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  1796
    }
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  1797
    Symbol resolveQualifiedMethod(DiagnosticPosition pos, Env<AttrContext> env,
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  1798
                                  Symbol location, Type site, Name name, List<Type> argtypes,
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  1799
                                  List<Type> typeargtypes) {
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1800
        return resolveQualifiedMethod(new MethodResolutionContext(), pos, env, location, site, name, argtypes, typeargtypes);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1801
    }
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1802
    private Symbol resolveQualifiedMethod(MethodResolutionContext resolveContext,
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1803
                                  DiagnosticPosition pos, Env<AttrContext> env,
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1804
                                  Symbol location, Type site, Name name, List<Type> argtypes,
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1805
                                  List<Type> typeargtypes) {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1806
        MethodResolutionContext prevResolutionContext = currentResolutionContext;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1807
        try {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1808
            currentResolutionContext = resolveContext;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1809
            Symbol sym = methodNotFound;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1810
            List<MethodResolutionPhase> steps = methodResolutionSteps;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1811
            while (steps.nonEmpty() &&
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1812
                   steps.head.isApplicable(boxingEnabled, varargsEnabled) &&
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1813
                   sym.kind >= ERRONEOUS) {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1814
                currentResolutionContext.step = steps.head;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1815
                sym = findMethod(env, site, name, argtypes, typeargtypes,
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1816
                        steps.head.isBoxingRequired(),
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1817
                        env.info.varArgs = steps.head.isVarargsRequired(), false);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1818
                currentResolutionContext.resolutionCache.put(steps.head, sym);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1819
                steps = steps.tail;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1820
            }
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1821
            if (sym.kind >= AMBIGUOUS) {
12335
4725d88691dd 7133185: Update 292 overload resolution logic to match JLS
mcimadamore
parents: 12334
diff changeset
  1822
                //if nothing is found return the 'first' error
4725d88691dd 7133185: Update 292 overload resolution logic to match JLS
mcimadamore
parents: 12334
diff changeset
  1823
                MethodResolutionPhase errPhase =
4725d88691dd 7133185: Update 292 overload resolution logic to match JLS
mcimadamore
parents: 12334
diff changeset
  1824
                        currentResolutionContext.firstErroneousResolutionPhase();
4725d88691dd 7133185: Update 292 overload resolution logic to match JLS
mcimadamore
parents: 12334
diff changeset
  1825
                sym = access(currentResolutionContext.resolutionCache.get(errPhase),
4725d88691dd 7133185: Update 292 overload resolution logic to match JLS
mcimadamore
parents: 12334
diff changeset
  1826
                        pos, location, site, name, true, argtypes, typeargtypes);
4725d88691dd 7133185: Update 292 overload resolution logic to match JLS
mcimadamore
parents: 12334
diff changeset
  1827
                env.info.varArgs = errPhase.isVarargsRequired;
4725d88691dd 7133185: Update 292 overload resolution logic to match JLS
mcimadamore
parents: 12334
diff changeset
  1828
            } else if (allowMethodHandles) {
4725d88691dd 7133185: Update 292 overload resolution logic to match JLS
mcimadamore
parents: 12334
diff changeset
  1829
                MethodSymbol msym = (MethodSymbol)sym;
4725d88691dd 7133185: Update 292 overload resolution logic to match JLS
mcimadamore
parents: 12334
diff changeset
  1830
                if (msym.isSignaturePolymorphic(types)) {
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1831
                    env.info.varArgs = false;
12335
4725d88691dd 7133185: Update 292 overload resolution logic to match JLS
mcimadamore
parents: 12334
diff changeset
  1832
                    return findPolymorphicSignatureInstance(env, sym, argtypes);
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1833
                }
6592
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6356
diff changeset
  1834
            }
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1835
            return sym;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1836
        }
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1837
        finally {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1838
            currentResolutionContext = prevResolutionContext;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1839
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1840
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1841
6592
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6356
diff changeset
  1842
    /** 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
  1843
     *  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
  1844
     *  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
  1845
     *  @param env       Attribution environment
12335
4725d88691dd 7133185: Update 292 overload resolution logic to match JLS
mcimadamore
parents: 12334
diff changeset
  1846
     *  @param spMethod  signature polymorphic method - i.e. MH.invokeExact
4725d88691dd 7133185: Update 292 overload resolution logic to match JLS
mcimadamore
parents: 12334
diff changeset
  1847
     *  @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
  1848
     */
12335
4725d88691dd 7133185: Update 292 overload resolution logic to match JLS
mcimadamore
parents: 12334
diff changeset
  1849
    Symbol findPolymorphicSignatureInstance(Env<AttrContext> env,
4725d88691dd 7133185: Update 292 overload resolution logic to match JLS
mcimadamore
parents: 12334
diff changeset
  1850
                                            Symbol spMethod,
8036
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 8032
diff changeset
  1851
                                            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
  1852
        Type mtype = infer.instantiatePolymorphicSignatureInstance(env,
12335
4725d88691dd 7133185: Update 292 overload resolution logic to match JLS
mcimadamore
parents: 12334
diff changeset
  1853
                (MethodSymbol)spMethod, argtypes);
4725d88691dd 7133185: Update 292 overload resolution logic to match JLS
mcimadamore
parents: 12334
diff changeset
  1854
        for (Symbol sym : polymorphicSignatureScope.getElementsByName(spMethod.name)) {
4725d88691dd 7133185: Update 292 overload resolution logic to match JLS
mcimadamore
parents: 12334
diff changeset
  1855
            if (types.isSameType(mtype, sym.type)) {
4725d88691dd 7133185: Update 292 overload resolution logic to match JLS
mcimadamore
parents: 12334
diff changeset
  1856
               return sym;
6592
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6356
diff changeset
  1857
            }
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6356
diff changeset
  1858
        }
12335
4725d88691dd 7133185: Update 292 overload resolution logic to match JLS
mcimadamore
parents: 12334
diff changeset
  1859
4725d88691dd 7133185: Update 292 overload resolution logic to match JLS
mcimadamore
parents: 12334
diff changeset
  1860
        // create the desired method
4725d88691dd 7133185: Update 292 overload resolution logic to match JLS
mcimadamore
parents: 12334
diff changeset
  1861
        long flags = ABSTRACT | HYPOTHETICAL | spMethod.flags() & Flags.AccessFlags;
4725d88691dd 7133185: Update 292 overload resolution logic to match JLS
mcimadamore
parents: 12334
diff changeset
  1862
        Symbol msym = new MethodSymbol(flags, spMethod.name, mtype, spMethod.owner);
4725d88691dd 7133185: Update 292 overload resolution logic to match JLS
mcimadamore
parents: 12334
diff changeset
  1863
        polymorphicSignatureScope.enter(msym);
4725d88691dd 7133185: Update 292 overload resolution logic to match JLS
mcimadamore
parents: 12334
diff changeset
  1864
        return msym;
6592
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6356
diff changeset
  1865
    }
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6356
diff changeset
  1866
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1867
    /** Resolve a qualified method identifier, throw a fatal error if not
06bc494ca11e Initial load
duke
parents:
diff changeset
  1868
     *  found.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1869
     *  @param pos       The position to use for error reporting.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1870
     *  @param env       The environment current at the method invocation.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1871
     *  @param site      The type of the qualifying expression, in which
06bc494ca11e Initial load
duke
parents:
diff changeset
  1872
     *                   identifier is searched.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1873
     *  @param name      The identifier's name.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1874
     *  @param argtypes  The types of the invocation's value arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1875
     *  @param typeargtypes  The types of the invocation's type arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1876
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1877
    public MethodSymbol resolveInternalMethod(DiagnosticPosition pos, Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1878
                                        Type site, Name name,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1879
                                        List<Type> argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1880
                                        List<Type> typeargtypes) {
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1881
        MethodResolutionContext resolveContext = new MethodResolutionContext();
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1882
        resolveContext.internalResolution = true;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1883
        Symbol sym = resolveQualifiedMethod(resolveContext, pos, env, site.tsym,
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1884
                site, name, argtypes, typeargtypes);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1885
        if (sym.kind == MTH) return (MethodSymbol)sym;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1886
        else throw new FatalError(
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1887
                 diags.fragment("fatal.err.cant.locate.meth",
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1888
                                name));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1889
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1890
06bc494ca11e Initial load
duke
parents:
diff changeset
  1891
    /** Resolve constructor.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1892
     *  @param pos       The position to use for error reporting.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1893
     *  @param env       The environment current at the constructor invocation.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1894
     *  @param site      The type of class for which a constructor is searched.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1895
     *  @param argtypes  The types of the constructor invocation's value
06bc494ca11e Initial load
duke
parents:
diff changeset
  1896
     *                   arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1897
     *  @param typeargtypes  The types of the constructor invocation's type
06bc494ca11e Initial load
duke
parents:
diff changeset
  1898
     *                   arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1899
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1900
    Symbol resolveConstructor(DiagnosticPosition pos,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1901
                              Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1902
                              Type site,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1903
                              List<Type> argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1904
                              List<Type> typeargtypes) {
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1905
        return resolveConstructor(new MethodResolutionContext(), pos, env, site, argtypes, typeargtypes);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1906
    }
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1907
    private Symbol resolveConstructor(MethodResolutionContext resolveContext,
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1908
                              DiagnosticPosition pos,
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1909
                              Env<AttrContext> env,
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1910
                              Type site,
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1911
                              List<Type> argtypes,
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1912
                              List<Type> typeargtypes) {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1913
        MethodResolutionContext prevResolutionContext = currentResolutionContext;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1914
        try {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1915
            currentResolutionContext = resolveContext;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1916
            Symbol sym = methodNotFound;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1917
            List<MethodResolutionPhase> steps = methodResolutionSteps;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1918
            while (steps.nonEmpty() &&
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1919
                   steps.head.isApplicable(boxingEnabled, varargsEnabled) &&
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1920
                   sym.kind >= ERRONEOUS) {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1921
                currentResolutionContext.step = steps.head;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1922
                sym = findConstructor(pos, env, site, argtypes, typeargtypes,
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1923
                        steps.head.isBoxingRequired(),
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1924
                        env.info.varArgs = steps.head.isVarargsRequired());
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1925
                currentResolutionContext.resolutionCache.put(steps.head, sym);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1926
                steps = steps.tail;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1927
            }
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1928
            if (sym.kind >= AMBIGUOUS) {//if nothing is found return the 'first' error
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1929
                MethodResolutionPhase errPhase = currentResolutionContext.firstErroneousResolutionPhase();
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1930
                sym = access(currentResolutionContext.resolutionCache.get(errPhase),
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1931
                        pos, site, names.init, true, argtypes, typeargtypes);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1932
                env.info.varArgs = errPhase.isVarargsRequired();
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1933
            }
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1934
            return sym;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1935
        }
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1936
        finally {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1937
            currentResolutionContext = prevResolutionContext;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1938
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1939
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1940
5321
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  1941
    /** Resolve constructor using diamond inference.
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  1942
     *  @param pos       The position to use for error reporting.
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  1943
     *  @param env       The environment current at the constructor invocation.
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  1944
     *  @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
  1945
     *                   The scope of this class has been touched in attribution.
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  1946
     *  @param argtypes  The types of the constructor invocation's value
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  1947
     *                   arguments.
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  1948
     *  @param typeargtypes  The types of the constructor invocation's type
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  1949
     *                   arguments.
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  1950
     */
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  1951
    Symbol resolveDiamond(DiagnosticPosition pos,
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  1952
                              Env<AttrContext> env,
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  1953
                              Type site,
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  1954
                              List<Type> argtypes,
6344
f190fb7fdd2d 6975275: diamond implementation needs some cleanup
mcimadamore
parents: 6151
diff changeset
  1955
                              List<Type> typeargtypes) {
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1956
        MethodResolutionContext prevResolutionContext = currentResolutionContext;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1957
        try {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1958
            currentResolutionContext = new MethodResolutionContext();
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1959
            Symbol sym = methodNotFound;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1960
            List<MethodResolutionPhase> steps = methodResolutionSteps;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1961
            while (steps.nonEmpty() &&
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1962
                   steps.head.isApplicable(boxingEnabled, varargsEnabled) &&
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1963
                   sym.kind >= ERRONEOUS) {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1964
                currentResolutionContext.step = steps.head;
12078
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  1965
                sym = findDiamond(env, site, argtypes, typeargtypes,
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1966
                        steps.head.isBoxingRequired(),
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1967
                        env.info.varArgs = steps.head.isVarargsRequired());
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1968
                currentResolutionContext.resolutionCache.put(steps.head, sym);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1969
                steps = steps.tail;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1970
            }
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1971
            if (sym.kind >= AMBIGUOUS) {
14051
9097cec96212 7188968: New instance creation expression using diamond is checked twice
mcimadamore
parents: 14048
diff changeset
  1972
                Symbol errSym =
9097cec96212 7188968: New instance creation expression using diamond is checked twice
mcimadamore
parents: 14048
diff changeset
  1973
                        currentResolutionContext.resolutionCache.get(currentResolutionContext.firstErroneousResolutionPhase());
9097cec96212 7188968: New instance creation expression using diamond is checked twice
mcimadamore
parents: 14048
diff changeset
  1974
                final JCDiagnostic details = errSym.kind == WRONG_MTH ?
9097cec96212 7188968: New instance creation expression using diamond is checked twice
mcimadamore
parents: 14048
diff changeset
  1975
                                ((InapplicableSymbolError)errSym).errCandidate().details :
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1976
                                null;
14051
9097cec96212 7188968: New instance creation expression using diamond is checked twice
mcimadamore
parents: 14048
diff changeset
  1977
                errSym = new InapplicableSymbolError(errSym.kind, "diamondError") {
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1978
                    @Override
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1979
                    JCDiagnostic getDiagnostic(DiagnosticType dkind, DiagnosticPosition pos,
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1980
                            Symbol location, Type site, Name name, List<Type> argtypes, List<Type> typeargtypes) {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1981
                        String key = details == null ?
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1982
                            "cant.apply.diamond" :
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1983
                            "cant.apply.diamond.1";
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1984
                        return diags.create(dkind, log.currentSource(), pos, key,
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1985
                                diags.fragment("diamond", site.tsym), details);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1986
                    }
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1987
                };
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1988
                MethodResolutionPhase errPhase = currentResolutionContext.firstErroneousResolutionPhase();
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1989
                sym = access(errSym, pos, site, names.init, true, argtypes, typeargtypes);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1990
                env.info.varArgs = errPhase.isVarargsRequired();
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1991
            }
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1992
            return sym;
5321
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  1993
        }
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1994
        finally {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  1995
            currentResolutionContext = prevResolutionContext;
5321
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  1996
        }
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  1997
    }
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  1998
12078
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  1999
    /** 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
  2000
     *  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
  2001
     *  {@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
  2002
     *  a method check is executed against the modified constructor type:
13844
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 13438
diff changeset
  2003
     *  {@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
  2004
     *  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
  2005
     *  the inferred type for the diamond operator.
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2006
     */
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2007
    private Symbol findDiamond(Env<AttrContext> env,
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2008
                              Type site,
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2009
                              List<Type> argtypes,
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2010
                              List<Type> typeargtypes,
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2011
                              boolean allowBoxing,
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2012
                              boolean useVarargs) {
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2013
        Symbol bestSoFar = methodNotFound;
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2014
        for (Scope.Entry e = site.tsym.members().lookup(names.init);
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2015
             e.scope != null;
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2016
             e = e.next()) {
14051
9097cec96212 7188968: New instance creation expression using diamond is checked twice
mcimadamore
parents: 14048
diff changeset
  2017
            final Symbol sym = e.sym;
12078
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2018
            //- System.out.println(" e " + e.sym);
14051
9097cec96212 7188968: New instance creation expression using diamond is checked twice
mcimadamore
parents: 14048
diff changeset
  2019
            if (sym.kind == MTH &&
9097cec96212 7188968: New instance creation expression using diamond is checked twice
mcimadamore
parents: 14048
diff changeset
  2020
                (sym.flags_field & SYNTHETIC) == 0) {
12078
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2021
                    List<Type> oldParams = e.sym.type.tag == FORALL ?
14051
9097cec96212 7188968: New instance creation expression using diamond is checked twice
mcimadamore
parents: 14048
diff changeset
  2022
                            ((ForAll)sym.type).tvars :
12078
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2023
                            List.<Type>nil();
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2024
                    Type constrType = new ForAll(site.tsym.type.getTypeArguments().appendList(oldParams),
14051
9097cec96212 7188968: New instance creation expression using diamond is checked twice
mcimadamore
parents: 14048
diff changeset
  2025
                            types.createMethodTypeWithReturn(sym.type.asMethodType(), site));
9097cec96212 7188968: New instance creation expression using diamond is checked twice
mcimadamore
parents: 14048
diff changeset
  2026
                    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
  2027
                        @Override
9097cec96212 7188968: New instance creation expression using diamond is checked twice
mcimadamore
parents: 14048
diff changeset
  2028
                        public Symbol baseSymbol() {
9097cec96212 7188968: New instance creation expression using diamond is checked twice
mcimadamore
parents: 14048
diff changeset
  2029
                            return sym;
9097cec96212 7188968: New instance creation expression using diamond is checked twice
mcimadamore
parents: 14048
diff changeset
  2030
                        }
9097cec96212 7188968: New instance creation expression using diamond is checked twice
mcimadamore
parents: 14048
diff changeset
  2031
                    };
12078
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2032
                    bestSoFar = selectBest(env, site, argtypes, typeargtypes,
14051
9097cec96212 7188968: New instance creation expression using diamond is checked twice
mcimadamore
parents: 14048
diff changeset
  2033
                            newConstr,
12078
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2034
                            bestSoFar,
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2035
                            allowBoxing,
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2036
                            useVarargs,
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2037
                            false);
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2038
            }
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2039
        }
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2040
        return bestSoFar;
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2041
    }
7470e05d486b 7148164: attribution code for diamond should not rely on mutable state
mcimadamore
parents: 12076
diff changeset
  2042
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2043
    /** Resolve constructor.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2044
     *  @param pos       The position to use for error reporting.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2045
     *  @param env       The environment current at the constructor invocation.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2046
     *  @param site      The type of class for which a constructor is searched.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2047
     *  @param argtypes  The types of the constructor invocation's value
06bc494ca11e Initial load
duke
parents:
diff changeset
  2048
     *                   arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2049
     *  @param typeargtypes  The types of the constructor invocation's type
06bc494ca11e Initial load
duke
parents:
diff changeset
  2050
     *                   arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2051
     *  @param allowBoxing Allow boxing and varargs conversions.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2052
     *  @param useVarargs Box trailing arguments into an array for varargs.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2053
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2054
    Symbol resolveConstructor(DiagnosticPosition pos, Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2055
                              Type site, List<Type> argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2056
                              List<Type> typeargtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2057
                              boolean allowBoxing,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2058
                              boolean useVarargs) {
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2059
        MethodResolutionContext prevResolutionContext = currentResolutionContext;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2060
        try {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2061
            currentResolutionContext = new MethodResolutionContext();
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2062
            return findConstructor(pos, env, site, argtypes, typeargtypes, allowBoxing, useVarargs);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2063
        }
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2064
        finally {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2065
            currentResolutionContext = prevResolutionContext;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2066
        }
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2067
    }
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2068
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2069
    Symbol findConstructor(DiagnosticPosition pos, Env<AttrContext> env,
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2070
                              Type site, List<Type> argtypes,
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2071
                              List<Type> typeargtypes,
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2072
                              boolean allowBoxing,
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2073
                              boolean useVarargs) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2074
        Symbol sym = findMethod(env, site,
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2075
                                    names.init, argtypes,
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2076
                                    typeargtypes, allowBoxing,
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2077
                                    useVarargs, false);
8236
0d8646b7c602 6594914: @SuppressWarnings("deprecation") does not not work for the type of a variable
mcimadamore
parents: 8229
diff changeset
  2078
        chk.checkDeprecated(pos, env.info.scope.owner, sym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2079
        return sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2080
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2081
06bc494ca11e Initial load
duke
parents:
diff changeset
  2082
    /** Resolve a constructor, throw a fatal error if not found.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2083
     *  @param pos       The position to use for error reporting.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2084
     *  @param env       The environment current at the method invocation.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2085
     *  @param site      The type to be constructed.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2086
     *  @param argtypes  The types of the invocation's value arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2087
     *  @param typeargtypes  The types of the invocation's type arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2088
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2089
    public MethodSymbol resolveInternalConstructor(DiagnosticPosition pos, Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2090
                                        Type site,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2091
                                        List<Type> argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2092
                                        List<Type> typeargtypes) {
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2093
        MethodResolutionContext resolveContext = new MethodResolutionContext();
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2094
        resolveContext.internalResolution = true;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2095
        Symbol sym = resolveConstructor(resolveContext, pos, env, site, argtypes, typeargtypes);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2096
        if (sym.kind == MTH) return (MethodSymbol)sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2097
        else throw new FatalError(
1040
c0f5acfd9d15 6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents: 939
diff changeset
  2098
                 diags.fragment("fatal.err.cant.locate.ctor", site));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2099
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2100
06bc494ca11e Initial load
duke
parents:
diff changeset
  2101
    /** Resolve operator.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2102
     *  @param pos       The position to use for error reporting.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2103
     *  @param optag     The tag of the operation tree.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2104
     *  @param env       The environment current at the operation.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2105
     *  @param argtypes  The types of the operands.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2106
     */
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10816
diff changeset
  2107
    Symbol resolveOperator(DiagnosticPosition pos, JCTree.Tag optag,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2108
                           Env<AttrContext> env, List<Type> argtypes) {
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2109
        MethodResolutionContext prevResolutionContext = currentResolutionContext;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2110
        try {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2111
            currentResolutionContext = new MethodResolutionContext();
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2112
            Name name = treeinfo.operatorName(optag);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2113
            Symbol sym = findMethod(env, syms.predefClass.type, name, argtypes,
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2114
                                    null, false, false, true);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2115
            if (boxingEnabled && sym.kind >= WRONG_MTHS)
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2116
                sym = findMethod(env, syms.predefClass.type, name, argtypes,
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2117
                                 null, true, false, true);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2118
            return access(sym, pos, env.enclClass.sym.type, name,
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2119
                          false, argtypes, null);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2120
        }
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2121
        finally {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2122
            currentResolutionContext = prevResolutionContext;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2123
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2124
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2125
06bc494ca11e Initial load
duke
parents:
diff changeset
  2126
    /** Resolve operator.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2127
     *  @param pos       The position to use for error reporting.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2128
     *  @param optag     The tag of the operation tree.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2129
     *  @param env       The environment current at the operation.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2130
     *  @param arg       The type of the operand.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2131
     */
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10816
diff changeset
  2132
    Symbol resolveUnaryOperator(DiagnosticPosition pos, JCTree.Tag optag, Env<AttrContext> env, Type arg) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2133
        return resolveOperator(pos, optag, env, List.of(arg));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2134
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2135
06bc494ca11e Initial load
duke
parents:
diff changeset
  2136
    /** Resolve binary operator.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2137
     *  @param pos       The position to use for error reporting.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2138
     *  @param optag     The tag of the operation tree.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2139
     *  @param env       The environment current at the operation.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2140
     *  @param left      The types of the left operand.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2141
     *  @param right     The types of the right operand.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2142
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2143
    Symbol resolveBinaryOperator(DiagnosticPosition pos,
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10816
diff changeset
  2144
                                 JCTree.Tag optag,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2145
                                 Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2146
                                 Type left,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2147
                                 Type right) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2148
        return resolveOperator(pos, optag, env, List.of(left, right));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2149
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2150
06bc494ca11e Initial load
duke
parents:
diff changeset
  2151
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  2152
     * Resolve `c.name' where name == this or name == super.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2153
     * @param pos           The position to use for error reporting.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2154
     * @param env           The environment current at the expression.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2155
     * @param c             The qualifier.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2156
     * @param name          The identifier's name.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2157
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2158
    Symbol resolveSelf(DiagnosticPosition pos,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2159
                       Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2160
                       TypeSymbol c,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2161
                       Name name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2162
        Env<AttrContext> env1 = env;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2163
        boolean staticOnly = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2164
        while (env1.outer != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2165
            if (isStatic(env1)) staticOnly = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2166
            if (env1.enclClass.sym == c) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2167
                Symbol sym = env1.info.scope.lookup(name).sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2168
                if (sym != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2169
                    if (staticOnly) sym = new StaticError(sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2170
                    return access(sym, pos, env.enclClass.sym.type,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2171
                                  name, true);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2172
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2173
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2174
            if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2175
            env1 = env1.outer;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2176
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2177
        log.error(pos, "not.encl.class", c);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2178
        return syms.errSymbol;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2179
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2180
06bc494ca11e Initial load
duke
parents:
diff changeset
  2181
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  2182
     * Resolve `c.this' for an enclosing class c that contains the
06bc494ca11e Initial load
duke
parents:
diff changeset
  2183
     * named member.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2184
     * @param pos           The position to use for error reporting.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2185
     * @param env           The environment current at the expression.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2186
     * @param member        The member that must be contained in the result.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2187
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2188
    Symbol resolveSelfContaining(DiagnosticPosition pos,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2189
                                 Env<AttrContext> env,
8622
4f032629a0fd 6541876: "Enclosing Instance" error new in 1.6
mcimadamore
parents: 8430
diff changeset
  2190
                                 Symbol member,
4f032629a0fd 6541876: "Enclosing Instance" error new in 1.6
mcimadamore
parents: 8430
diff changeset
  2191
                                 boolean isSuperCall) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2192
        Name name = names._this;
8622
4f032629a0fd 6541876: "Enclosing Instance" error new in 1.6
mcimadamore
parents: 8430
diff changeset
  2193
        Env<AttrContext> env1 = isSuperCall ? env.outer : env;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2194
        boolean staticOnly = false;
8622
4f032629a0fd 6541876: "Enclosing Instance" error new in 1.6
mcimadamore
parents: 8430
diff changeset
  2195
        if (env1 != null) {
4f032629a0fd 6541876: "Enclosing Instance" error new in 1.6
mcimadamore
parents: 8430
diff changeset
  2196
            while (env1 != null && env1.outer != null) {
4f032629a0fd 6541876: "Enclosing Instance" error new in 1.6
mcimadamore
parents: 8430
diff changeset
  2197
                if (isStatic(env1)) staticOnly = true;
4f032629a0fd 6541876: "Enclosing Instance" error new in 1.6
mcimadamore
parents: 8430
diff changeset
  2198
                if (env1.enclClass.sym.isSubClass(member.owner, types)) {
4f032629a0fd 6541876: "Enclosing Instance" error new in 1.6
mcimadamore
parents: 8430
diff changeset
  2199
                    Symbol sym = env1.info.scope.lookup(name).sym;
4f032629a0fd 6541876: "Enclosing Instance" error new in 1.6
mcimadamore
parents: 8430
diff changeset
  2200
                    if (sym != null) {
4f032629a0fd 6541876: "Enclosing Instance" error new in 1.6
mcimadamore
parents: 8430
diff changeset
  2201
                        if (staticOnly) sym = new StaticError(sym);
4f032629a0fd 6541876: "Enclosing Instance" error new in 1.6
mcimadamore
parents: 8430
diff changeset
  2202
                        return access(sym, pos, env.enclClass.sym.type,
4f032629a0fd 6541876: "Enclosing Instance" error new in 1.6
mcimadamore
parents: 8430
diff changeset
  2203
                                      name, true);
4f032629a0fd 6541876: "Enclosing Instance" error new in 1.6
mcimadamore
parents: 8430
diff changeset
  2204
                    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2205
                }
8622
4f032629a0fd 6541876: "Enclosing Instance" error new in 1.6
mcimadamore
parents: 8430
diff changeset
  2206
                if ((env1.enclClass.sym.flags() & STATIC) != 0)
4f032629a0fd 6541876: "Enclosing Instance" error new in 1.6
mcimadamore
parents: 8430
diff changeset
  2207
                    staticOnly = true;
4f032629a0fd 6541876: "Enclosing Instance" error new in 1.6
mcimadamore
parents: 8430
diff changeset
  2208
                env1 = env1.outer;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2209
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2210
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2211
        log.error(pos, "encl.class.required", member);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2212
        return syms.errSymbol;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2213
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2214
06bc494ca11e Initial load
duke
parents:
diff changeset
  2215
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  2216
     * 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
  2217
     * JLS 8.8.5.1 and 15.9.2
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2218
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2219
    Type resolveImplicitThis(DiagnosticPosition pos, Env<AttrContext> env, Type t) {
8622
4f032629a0fd 6541876: "Enclosing Instance" error new in 1.6
mcimadamore
parents: 8430
diff changeset
  2220
        return resolveImplicitThis(pos, env, t, false);
4f032629a0fd 6541876: "Enclosing Instance" error new in 1.6
mcimadamore
parents: 8430
diff changeset
  2221
    }
4f032629a0fd 6541876: "Enclosing Instance" error new in 1.6
mcimadamore
parents: 8430
diff changeset
  2222
4f032629a0fd 6541876: "Enclosing Instance" error new in 1.6
mcimadamore
parents: 8430
diff changeset
  2223
    Type resolveImplicitThis(DiagnosticPosition pos, Env<AttrContext> env, Type t, boolean isSuperCall) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2224
        Type thisType = (((t.tsym.owner.kind & (MTH|VAR)) != 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2225
                         ? resolveSelf(pos, env, t.getEnclosingType().tsym, names._this)
8622
4f032629a0fd 6541876: "Enclosing Instance" error new in 1.6
mcimadamore
parents: 8430
diff changeset
  2226
                         : resolveSelfContaining(pos, env, t.tsym, isSuperCall)).type;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2227
        if (env.info.isSelfCall && thisType.tsym == env.enclClass.sym)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2228
            log.error(pos, "cant.ref.before.ctor.called", "this");
06bc494ca11e Initial load
duke
parents:
diff changeset
  2229
        return thisType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2230
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2231
06bc494ca11e Initial load
duke
parents:
diff changeset
  2232
/* ***************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
  2233
 *  ResolveError classes, indicating error situations when accessing symbols
06bc494ca11e Initial load
duke
parents:
diff changeset
  2234
 ****************************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
  2235
12082
32bdf6ef6388 7151070: NullPointerException in Resolve.isAccessible
mcimadamore
parents: 12078
diff changeset
  2236
    //used by TransTypes when checking target type of synthetic cast
32bdf6ef6388 7151070: NullPointerException in Resolve.isAccessible
mcimadamore
parents: 12078
diff changeset
  2237
    public void logAccessErrorInternal(Env<AttrContext> env, JCTree tree, Type type) {
32bdf6ef6388 7151070: NullPointerException in Resolve.isAccessible
mcimadamore
parents: 12078
diff changeset
  2238
        AccessError error = new AccessError(env, env.enclClass.type, type.tsym);
32bdf6ef6388 7151070: NullPointerException in Resolve.isAccessible
mcimadamore
parents: 12078
diff changeset
  2239
        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
  2240
    }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2241
    //where
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2242
    private void logResolveError(ResolveError error,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2243
            DiagnosticPosition pos,
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2244
            Symbol location,
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2245
            Type site,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2246
            Name name,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2247
            List<Type> argtypes,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2248
            List<Type> typeargtypes) {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2249
        JCDiagnostic d = error.getDiagnostic(JCDiagnostic.DiagnosticType.ERROR,
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2250
                pos, location, site, name, argtypes, typeargtypes);
6356
af24929939ca 6976747: JCDiagnostic: replace "boolean mandatory" with new "Set<JCDiagnostic.Flag>"
jjg
parents: 6344
diff changeset
  2251
        if (d != null) {
af24929939ca 6976747: JCDiagnostic: replace "boolean mandatory" with new "Set<JCDiagnostic.Flag>"
jjg
parents: 6344
diff changeset
  2252
            d.setFlag(DiagnosticFlag.RESOLVE_ERROR);
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2253
            log.report(d);
6356
af24929939ca 6976747: JCDiagnostic: replace "boolean mandatory" with new "Set<JCDiagnostic.Flag>"
jjg
parents: 6344
diff changeset
  2254
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2255
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2256
1534
e923a41e84cc 6758789: Some method resolution diagnostic should be improved
mcimadamore
parents: 1533
diff changeset
  2257
    private final LocalizedString noArgs = new LocalizedString("compiler.misc.no.args");
e923a41e84cc 6758789: Some method resolution diagnostic should be improved
mcimadamore
parents: 1533
diff changeset
  2258
e923a41e84cc 6758789: Some method resolution diagnostic should be improved
mcimadamore
parents: 1533
diff changeset
  2259
    public Object methodArguments(List<Type> argtypes) {
10816
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
  2260
        return argtypes == null || argtypes.isEmpty() ? noArgs : argtypes;
1534
e923a41e84cc 6758789: Some method resolution diagnostic should be improved
mcimadamore
parents: 1533
diff changeset
  2261
    }
e923a41e84cc 6758789: Some method resolution diagnostic should be improved
mcimadamore
parents: 1533
diff changeset
  2262
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2263
    /**
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2264
     * Root class for resolution errors. Subclass of ResolveError
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2265
     * represent a different kinds of resolution error - as such they must
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2266
     * specify how they map into concrete compiler diagnostics.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2267
     */
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2268
    private abstract class ResolveError extends Symbol {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2269
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2270
        /** The name of the kind of error, for debugging only. */
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2271
        final String debugName;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2272
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2273
        ResolveError(int kind, String debugName) {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2274
            super(kind, 0, null, null, null);
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2275
            this.debugName = debugName;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2276
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2277
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2278
        @Override
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2279
        public <R, P> R accept(ElementVisitor<R, P> v, P p) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2280
            throw new AssertionError();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2281
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2282
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2283
        @Override
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2284
        public String toString() {
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2285
            return debugName;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2286
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2287
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2288
        @Override
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2289
        public boolean exists() {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2290
            return false;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2291
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2292
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2293
        /**
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2294
         * Create an external representation for this erroneous symbol to be
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2295
         * used during attribution - by default this returns the symbol of a
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2296
         * brand new error type which stores the original type found
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2297
         * during resolution.
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2298
         *
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2299
         * @param name     the name used during resolution
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2300
         * @param location the location from which the symbol is accessed
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2301
         */
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2302
        protected Symbol access(Name name, TypeSymbol location) {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2303
            return types.createErrorType(name, location, syms.errSymbol.type).tsym;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2304
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2305
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2306
        /**
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2307
         * Create a diagnostic representing this resolution error.
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2308
         *
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2309
         * @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
  2310
         * @param pos       The position to be used for error reporting.
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2311
         * @param site      The original type from where the selection took place.
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2312
         * @param name      The name of the symbol to be resolved.
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2313
         * @param argtypes  The invocation's value arguments,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2314
         *                  if we looked for a method.
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2315
         * @param typeargtypes  The invocation's type arguments,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2316
         *                      if we looked for a method.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2317
         */
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2318
        abstract JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2319
                DiagnosticPosition pos,
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2320
                Symbol location,
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2321
                Type site,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2322
                Name name,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2323
                List<Type> argtypes,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2324
                List<Type> typeargtypes);
939
38e24969c7e9 6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents: 735
diff changeset
  2325
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2326
        /**
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2327
         * A name designates an operator if it consists
13844
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 13438
diff changeset
  2328
         * of a non-empty sequence of operator symbols {@literal +-~!/*%&|^<>= }
939
38e24969c7e9 6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents: 735
diff changeset
  2329
         */
38e24969c7e9 6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents: 735
diff changeset
  2330
        boolean isOperator(Name name) {
38e24969c7e9 6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents: 735
diff changeset
  2331
            int i = 0;
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 1257
diff changeset
  2332
            while (i < name.getByteLength() &&
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 1257
diff changeset
  2333
                   "+-~!*/%&|^<>=".indexOf(name.getByteAt(i)) >= 0) i++;
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 1257
diff changeset
  2334
            return i > 0 && i == name.getByteLength();
939
38e24969c7e9 6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents: 735
diff changeset
  2335
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2336
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2337
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2338
    /**
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2339
     * 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
  2340
     * an invalid symbol being found during resolution.
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2341
     */
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2342
    abstract class InvalidSymbolError extends ResolveError {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2343
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2344
        /** The invalid symbol found during resolution */
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2345
        Symbol sym;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2346
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2347
        InvalidSymbolError(int kind, Symbol sym, String debugName) {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2348
            super(kind, debugName);
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2349
            this.sym = sym;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2350
        }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2351
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2352
        @Override
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2353
        public boolean exists() {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2354
            return true;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2355
        }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2356
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2357
        @Override
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2358
        public String toString() {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2359
             return super.toString() + " wrongSym=" + sym;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2360
        }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2361
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2362
        @Override
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2363
        public Symbol access(Name name, TypeSymbol location) {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2364
            if (sym.kind >= AMBIGUOUS)
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2365
                return ((ResolveError)sym).access(name, location);
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2366
            else if ((sym.kind & ERRONEOUS) == 0 && (sym.kind & TYP) != 0)
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2367
                return types.createErrorType(name, location, sym.type).tsym;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2368
            else
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2369
                return sym;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2370
        }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2371
    }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2372
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2373
    /**
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2374
     * InvalidSymbolError error class indicating that a symbol matching a
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2375
     * given name does not exists in a given site.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2376
     */
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2377
    class SymbolNotFoundError extends ResolveError {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2378
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2379
        SymbolNotFoundError(int kind) {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2380
            super(kind, "symbol not found error");
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2381
        }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2382
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2383
        @Override
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2384
        JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2385
                DiagnosticPosition pos,
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2386
                Symbol location,
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2387
                Type site,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2388
                Name name,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2389
                List<Type> argtypes,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2390
                List<Type> typeargtypes) {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2391
            argtypes = argtypes == null ? List.<Type>nil() : argtypes;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2392
            typeargtypes = typeargtypes == null ? List.<Type>nil() : typeargtypes;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2393
            if (name == names.error)
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2394
                return null;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2395
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2396
            if (isOperator(name)) {
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2397
                boolean isUnaryOp = argtypes.size() == 1;
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2398
                String key = argtypes.size() == 1 ?
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2399
                    "operator.cant.be.applied" :
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2400
                    "operator.cant.be.applied.1";
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2401
                Type first = argtypes.head;
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2402
                Type second = !isUnaryOp ? argtypes.tail.head : null;
6151
dd513881e71d 6957438: improve code for generating warning messages containing option names
jjg
parents: 5857
diff changeset
  2403
                return diags.create(dkind, log.currentSource(), pos,
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2404
                        key, name, first, second);
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2405
            }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2406
            boolean hasLocation = false;
8239
d2c934e951e2 7014715: javac returns different error code for certain failure(s)
mcimadamore
parents: 8237
diff changeset
  2407
            if (location == null) {
d2c934e951e2 7014715: javac returns different error code for certain failure(s)
mcimadamore
parents: 8237
diff changeset
  2408
                location = site.tsym;
d2c934e951e2 7014715: javac returns different error code for certain failure(s)
mcimadamore
parents: 8237
diff changeset
  2409
            }
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2410
            if (!location.name.isEmpty()) {
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2411
                if (location.kind == PCK && !site.tsym.exists()) {
6151
dd513881e71d 6957438: improve code for generating warning messages containing option names
jjg
parents: 5857
diff changeset
  2412
                    return diags.create(dkind, log.currentSource(), pos,
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2413
                        "doesnt.exist", location);
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2414
                }
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2415
                hasLocation = !location.name.equals(names._this) &&
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2416
                        !location.name.equals(names._super);
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2417
            }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2418
            boolean isConstructor = kind == ABSENT_MTH &&
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2419
                    name == names.table.names.init;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2420
            KindName kindname = isConstructor ? KindName.CONSTRUCTOR : absentKind(kind);
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2421
            Name idname = isConstructor ? site.tsym.name : name;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2422
            String errKey = getErrorKey(kindname, typeargtypes.nonEmpty(), hasLocation);
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2423
            if (hasLocation) {
6151
dd513881e71d 6957438: improve code for generating warning messages containing option names
jjg
parents: 5857
diff changeset
  2424
                return diags.create(dkind, log.currentSource(), pos,
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2425
                        errKey, kindname, idname, //symbol kindname, name
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2426
                        typeargtypes, argtypes, //type parameters and arguments (if any)
8239
d2c934e951e2 7014715: javac returns different error code for certain failure(s)
mcimadamore
parents: 8237
diff changeset
  2427
                        getLocationDiag(location, site)); //location kindname, type
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2428
            }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2429
            else {
6151
dd513881e71d 6957438: improve code for generating warning messages containing option names
jjg
parents: 5857
diff changeset
  2430
                return diags.create(dkind, log.currentSource(), pos,
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2431
                        errKey, kindname, idname, //symbol kindname, name
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2432
                        typeargtypes, argtypes); //type parameters and arguments (if any)
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2433
            }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2434
        }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2435
        //where
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2436
        private String getErrorKey(KindName kindname, boolean hasTypeArgs, boolean hasLocation) {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2437
            String key = "cant.resolve";
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2438
            String suffix = hasLocation ? ".location" : "";
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2439
            switch (kindname) {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2440
                case METHOD:
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2441
                case CONSTRUCTOR: {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2442
                    suffix += ".args";
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2443
                    suffix += hasTypeArgs ? ".params" : "";
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2444
                }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2445
            }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2446
            return key + suffix;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2447
        }
8239
d2c934e951e2 7014715: javac returns different error code for certain failure(s)
mcimadamore
parents: 8237
diff changeset
  2448
        private JCDiagnostic getLocationDiag(Symbol location, Type site) {
d2c934e951e2 7014715: javac returns different error code for certain failure(s)
mcimadamore
parents: 8237
diff changeset
  2449
            if (location.kind == VAR) {
d2c934e951e2 7014715: javac returns different error code for certain failure(s)
mcimadamore
parents: 8237
diff changeset
  2450
                return diags.fragment("location.1",
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2451
                    kindName(location),
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2452
                    location,
8239
d2c934e951e2 7014715: javac returns different error code for certain failure(s)
mcimadamore
parents: 8237
diff changeset
  2453
                    location.type);
d2c934e951e2 7014715: javac returns different error code for certain failure(s)
mcimadamore
parents: 8237
diff changeset
  2454
            } else {
d2c934e951e2 7014715: javac returns different error code for certain failure(s)
mcimadamore
parents: 8237
diff changeset
  2455
                return diags.fragment("location",
d2c934e951e2 7014715: javac returns different error code for certain failure(s)
mcimadamore
parents: 8237
diff changeset
  2456
                    typeKindName(site),
d2c934e951e2 7014715: javac returns different error code for certain failure(s)
mcimadamore
parents: 8237
diff changeset
  2457
                    site,
d2c934e951e2 7014715: javac returns different error code for certain failure(s)
mcimadamore
parents: 8237
diff changeset
  2458
                    null);
d2c934e951e2 7014715: javac returns different error code for certain failure(s)
mcimadamore
parents: 8237
diff changeset
  2459
            }
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2460
        }
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2461
    }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2462
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2463
    /**
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2464
     * InvalidSymbolError error class indicating that a given symbol
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2465
     * (either a method, a constructor or an operand) is not applicable
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2466
     * given an actual arguments/type argument list.
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2467
     */
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2468
    class InapplicableSymbolError extends ResolveError {
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2469
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2470
        InapplicableSymbolError() {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2471
            super(WRONG_MTH, "inapplicable symbol error");
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2472
        }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2473
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2474
        protected InapplicableSymbolError(int kind, String debugName) {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2475
            super(kind, debugName);
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2476
        }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2477
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2478
        @Override
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2479
        public String toString() {
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2480
            return super.toString();
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2481
        }
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2482
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2483
        @Override
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2484
        public boolean exists() {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2485
            return true;
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2486
        }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2487
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2488
        @Override
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2489
        JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2490
                DiagnosticPosition pos,
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2491
                Symbol location,
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2492
                Type site,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2493
                Name name,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2494
                List<Type> argtypes,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2495
                List<Type> typeargtypes) {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2496
            if (name == names.error)
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2497
                return null;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2498
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2499
            if (isOperator(name)) {
8237
d5ef8db7ad09 5017953: spurious cascaded diagnostics when name not found
mcimadamore
parents: 8236
diff changeset
  2500
                boolean isUnaryOp = argtypes.size() == 1;
d5ef8db7ad09 5017953: spurious cascaded diagnostics when name not found
mcimadamore
parents: 8236
diff changeset
  2501
                String key = argtypes.size() == 1 ?
d5ef8db7ad09 5017953: spurious cascaded diagnostics when name not found
mcimadamore
parents: 8236
diff changeset
  2502
                    "operator.cant.be.applied" :
d5ef8db7ad09 5017953: spurious cascaded diagnostics when name not found
mcimadamore
parents: 8236
diff changeset
  2503
                    "operator.cant.be.applied.1";
d5ef8db7ad09 5017953: spurious cascaded diagnostics when name not found
mcimadamore
parents: 8236
diff changeset
  2504
                Type first = argtypes.head;
d5ef8db7ad09 5017953: spurious cascaded diagnostics when name not found
mcimadamore
parents: 8236
diff changeset
  2505
                Type second = !isUnaryOp ? argtypes.tail.head : null;
d5ef8db7ad09 5017953: spurious cascaded diagnostics when name not found
mcimadamore
parents: 8236
diff changeset
  2506
                return diags.create(dkind, log.currentSource(), pos,
d5ef8db7ad09 5017953: spurious cascaded diagnostics when name not found
mcimadamore
parents: 8236
diff changeset
  2507
                        key, name, first, second);
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2508
            }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2509
            else {
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2510
                Candidate c = errCandidate();
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2511
                Symbol ws = c.sym.asMemberOf(site, types);
6151
dd513881e71d 6957438: improve code for generating warning messages containing option names
jjg
parents: 5857
diff changeset
  2512
                return diags.create(dkind, log.currentSource(), pos,
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2513
                          "cant.apply.symbol" + (c.details != null ? ".1" : ""),
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2514
                          kindName(ws),
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2515
                          ws.name == names.init ? ws.owner.name : ws.name,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2516
                          methodArguments(ws.type.getParameterTypes()),
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2517
                          methodArguments(argtypes),
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2518
                          kindName(ws.owner),
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2519
                          ws.owner.type,
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2520
                          c.details);
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2521
            }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2522
        }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2523
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2524
        @Override
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2525
        public Symbol access(Name name, TypeSymbol location) {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2526
            return types.createErrorType(name, location, syms.errSymbol.type).tsym;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2527
        }
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2528
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2529
        protected boolean shouldReport(Candidate c) {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2530
            return !c.isApplicable() &&
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2531
                    (((c.sym.flags() & VARARGS) != 0 && c.step == VARARITY) ||
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2532
                      (c.sym.flags() & VARARGS) == 0 && c.step == (boxingEnabled ? BOX : BASIC));
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2533
        }
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2534
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2535
        private Candidate errCandidate() {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2536
            for (Candidate c : currentResolutionContext.candidates) {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2537
                if (shouldReport(c)) {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2538
                    return c;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2539
                }
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2540
            }
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2541
            Assert.error();
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2542
            return null;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2543
        }
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2544
    }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2545
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2546
    /**
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2547
     * ResolveError error class indicating that a set of symbols
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2548
     * (either methods, constructors or operands) is not applicable
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2549
     * given an actual arguments/type argument list.
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2550
     */
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2551
    class InapplicableSymbolsError extends InapplicableSymbolError {
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
  2552
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2553
        InapplicableSymbolsError() {
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2554
            super(WRONG_MTHS, "inapplicable symbols");
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2555
        }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2556
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2557
        @Override
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2558
        JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2559
                DiagnosticPosition pos,
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2560
                Symbol location,
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2561
                Type site,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2562
                Name name,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2563
                List<Type> argtypes,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2564
                List<Type> typeargtypes) {
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2565
            if (currentResolutionContext.candidates.nonEmpty()) {
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
  2566
                JCDiagnostic err = diags.create(dkind,
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
  2567
                        log.currentSource(),
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
  2568
                        pos,
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
  2569
                        "cant.apply.symbols",
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
  2570
                        name == names.init ? KindName.CONSTRUCTOR : absentKind(kind),
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
  2571
                        getName(),
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
  2572
                        argtypes);
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
  2573
                return new JCDiagnostic.MultilineDiagnostic(err, candidateDetails(site));
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
  2574
            } else {
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
  2575
                return new SymbolNotFoundError(ABSENT_MTH).getDiagnostic(dkind, pos,
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2576
                    location, site, name, argtypes, typeargtypes);
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
  2577
            }
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
  2578
        }
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
  2579
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
  2580
        //where
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
  2581
        List<JCDiagnostic> candidateDetails(Type site) {
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
  2582
            List<JCDiagnostic> details = List.nil();
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2583
            for (Candidate c : currentResolutionContext.candidates) {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2584
                if (!shouldReport(c)) continue;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2585
                JCDiagnostic detailDiag = diags.fragment("inapplicable.method",
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2586
                        Kinds.kindName(c.sym),
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2587
                        c.sym.location(site, types),
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2588
                        c.sym.asMemberOf(site, types),
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2589
                        c.details);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2590
                details = details.prepend(detailDiag);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2591
            }
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
  2592
            return details.reverse();
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
  2593
        }
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
  2594
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
  2595
        private Name getName() {
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2596
            Symbol sym = currentResolutionContext.candidates.head.sym;
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
  2597
            return sym.name == names.init ?
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
  2598
                sym.owner.name :
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
  2599
                sym.name;
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
  2600
        }
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2601
    }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2602
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2603
    /**
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2604
     * An InvalidSymbolError error class indicating that a symbol is not
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2605
     * accessible from a given site
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2606
     */
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2607
    class AccessError extends InvalidSymbolError {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2608
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2609
        private Env<AttrContext> env;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2610
        private Type site;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2611
06bc494ca11e Initial load
duke
parents:
diff changeset
  2612
        AccessError(Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2613
            this(null, null, sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2614
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2615
06bc494ca11e Initial load
duke
parents:
diff changeset
  2616
        AccessError(Env<AttrContext> env, Type site, Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2617
            super(HIDDEN, sym, "access error");
06bc494ca11e Initial load
duke
parents:
diff changeset
  2618
            this.env = env;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2619
            this.site = site;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2620
            if (debugResolve)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2621
                log.error("proc.messager", sym + " @ " + site + " is inaccessible.");
06bc494ca11e Initial load
duke
parents:
diff changeset
  2622
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2623
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2624
        @Override
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2625
        public boolean exists() {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2626
            return false;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2627
        }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2628
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2629
        @Override
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2630
        JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2631
                DiagnosticPosition pos,
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2632
                Symbol location,
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2633
                Type site,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2634
                Name name,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2635
                List<Type> argtypes,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2636
                List<Type> typeargtypes) {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2637
            if (sym.owner.type.tag == ERROR)
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2638
                return null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2639
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2640
            if (sym.name == names.init && sym.owner != site.tsym) {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2641
                return new SymbolNotFoundError(ABSENT_MTH).getDiagnostic(dkind,
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2642
                        pos, location, site, name, argtypes, typeargtypes);
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2643
            }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2644
            else if ((sym.flags() & PUBLIC) != 0
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2645
                || (env != null && this.site != null
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2646
                    && !isAccessible(env, this.site))) {
6151
dd513881e71d 6957438: improve code for generating warning messages containing option names
jjg
parents: 5857
diff changeset
  2647
                return diags.create(dkind, log.currentSource(),
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2648
                        pos, "not.def.access.class.intf.cant.access",
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2649
                    sym, sym.location());
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2650
            }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2651
            else if ((sym.flags() & (PRIVATE | PROTECTED)) != 0) {
6151
dd513881e71d 6957438: improve code for generating warning messages containing option names
jjg
parents: 5857
diff changeset
  2652
                return diags.create(dkind, log.currentSource(),
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2653
                        pos, "report.access", sym,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2654
                        asFlagSet(sym.flags() & (PRIVATE | PROTECTED)),
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2655
                        sym.location());
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2656
            }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2657
            else {
6151
dd513881e71d 6957438: improve code for generating warning messages containing option names
jjg
parents: 5857
diff changeset
  2658
                return diags.create(dkind, log.currentSource(),
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2659
                        pos, "not.def.public.cant.access", sym, sym.location());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2660
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2661
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2662
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2663
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2664
    /**
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2665
     * InvalidSymbolError error class indicating that an instance member
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2666
     * has erroneously been accessed from a static context.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2667
     */
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2668
    class StaticError extends InvalidSymbolError {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2669
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2670
        StaticError(Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2671
            super(STATICERR, sym, "static error");
06bc494ca11e Initial load
duke
parents:
diff changeset
  2672
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2673
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2674
        @Override
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2675
        JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2676
                DiagnosticPosition pos,
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2677
                Symbol location,
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2678
                Type site,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2679
                Name name,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2680
                List<Type> argtypes,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2681
                List<Type> typeargtypes) {
939
38e24969c7e9 6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents: 735
diff changeset
  2682
            Symbol errSym = ((sym.kind == TYP && sym.type.tag == CLASS)
38e24969c7e9 6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents: 735
diff changeset
  2683
                ? types.erasure(sym.type).tsym
38e24969c7e9 6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents: 735
diff changeset
  2684
                : sym);
6151
dd513881e71d 6957438: improve code for generating warning messages containing option names
jjg
parents: 5857
diff changeset
  2685
            return diags.create(dkind, log.currentSource(), pos,
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2686
                    "non-static.cant.be.ref", kindName(sym), errSym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2687
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2688
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2689
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2690
    /**
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2691
     * InvalidSymbolError error class indicating that a pair of symbols
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2692
     * (either methods, constructors or operands) are ambiguous
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2693
     * given an actual arguments/type argument list.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2694
     */
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2695
    class AmbiguityError extends InvalidSymbolError {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2696
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2697
        /** The other maximally specific symbol */
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2698
        Symbol sym2;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2699
06bc494ca11e Initial load
duke
parents:
diff changeset
  2700
        AmbiguityError(Symbol sym1, Symbol sym2) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2701
            super(AMBIGUOUS, sym1, "ambiguity error");
06bc494ca11e Initial load
duke
parents:
diff changeset
  2702
            this.sym2 = sym2;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2703
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2704
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2705
        @Override
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2706
        JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2707
                DiagnosticPosition pos,
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  2708
                Symbol location,
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2709
                Type site,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2710
                Name name,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2711
                List<Type> argtypes,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2712
                List<Type> typeargtypes) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2713
            AmbiguityError pair = this;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2714
            while (true) {
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2715
                if (pair.sym.kind == AMBIGUOUS)
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2716
                    pair = (AmbiguityError)pair.sym;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2717
                else if (pair.sym2.kind == AMBIGUOUS)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2718
                    pair = (AmbiguityError)pair.sym2;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2719
                else break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2720
            }
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2721
            Name sname = pair.sym.name;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2722
            if (sname == names.init) sname = pair.sym.owner.name;
6151
dd513881e71d 6957438: improve code for generating warning messages containing option names
jjg
parents: 5857
diff changeset
  2723
            return diags.create(dkind, log.currentSource(),
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2724
                      pos, "ref.ambiguous", sname,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2725
                      kindName(pair.sym),
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2726
                      pair.sym,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2727
                      pair.sym.location(site, types),
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2728
                      kindName(pair.sym2),
06bc494ca11e Initial load
duke
parents:
diff changeset
  2729
                      pair.sym2,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2730
                      pair.sym2.location(site, types));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2731
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2732
    }
1533
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2733
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2734
    enum MethodResolutionPhase {
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2735
        BASIC(false, false),
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2736
        BOX(true, false),
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2737
        VARARITY(true, true);
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2738
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2739
        boolean isBoxingRequired;
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2740
        boolean isVarargsRequired;
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2741
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2742
        MethodResolutionPhase(boolean isBoxingRequired, boolean isVarargsRequired) {
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2743
           this.isBoxingRequired = isBoxingRequired;
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2744
           this.isVarargsRequired = isVarargsRequired;
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2745
        }
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2746
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2747
        public boolean isBoxingRequired() {
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2748
            return isBoxingRequired;
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2749
        }
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2750
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2751
        public boolean isVarargsRequired() {
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2752
            return isVarargsRequired;
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2753
        }
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2754
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2755
        public boolean isApplicable(boolean boxingEnabled, boolean varargsEnabled) {
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2756
            return (varargsEnabled || !isVarargsRequired) &&
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2757
                   (boxingEnabled || !isBoxingRequired);
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2758
        }
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2759
    }
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2760
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2761
    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
  2762
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2763
    /**
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2764
     * 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
  2765
     * overload resolution, such as list of method that are not applicable
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2766
     * (used to generate more precise diagnostics) and so on. Resolution contexts
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2767
     * 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
  2768
     * work within the resolution context it created.
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2769
     */
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2770
    class MethodResolutionContext {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2771
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2772
        private List<Candidate> candidates = List.nil();
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2773
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2774
        private Map<MethodResolutionPhase, Symbol> resolutionCache =
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2775
            new EnumMap<MethodResolutionPhase, Symbol>(MethodResolutionPhase.class);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2776
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2777
        private MethodResolutionPhase step = null;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2778
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2779
        private boolean internalResolution = false;
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
  2780
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2781
        private MethodResolutionPhase firstErroneousResolutionPhase() {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2782
            MethodResolutionPhase bestSoFar = BASIC;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2783
            Symbol sym = methodNotFound;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2784
            List<MethodResolutionPhase> steps = methodResolutionSteps;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2785
            while (steps.nonEmpty() &&
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2786
                   steps.head.isApplicable(boxingEnabled, varargsEnabled) &&
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2787
                   sym.kind >= WRONG_MTHS) {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2788
                sym = resolutionCache.get(steps.head);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2789
                bestSoFar = steps.head;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2790
                steps = steps.tail;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2791
            }
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2792
            return bestSoFar;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2793
        }
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2794
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2795
        void addInapplicableCandidate(Symbol sym, JCDiagnostic details) {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2796
            Candidate c = new Candidate(currentResolutionContext.step, sym, details, null);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2797
            if (!candidates.contains(c))
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2798
                candidates = candidates.append(c);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2799
        }
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2800
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2801
        void addApplicableCandidate(Symbol sym, Type mtype) {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2802
            Candidate c = new Candidate(currentResolutionContext.step, sym, null, mtype);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2803
            candidates = candidates.append(c);
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2804
        }
10816
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10812
diff changeset
  2805
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2806
        /**
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2807
         * This class represents an overload resolution candidate. There are two
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2808
         * kinds of candidates: applicable methods and inapplicable methods;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2809
         * applicable methods have a pointer to the instantiated method type,
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2810
         * while inapplicable candidates contain further details about the
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2811
         * reason why the method has been considered inapplicable.
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2812
         */
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2813
        class Candidate {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2814
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2815
            final MethodResolutionPhase step;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2816
            final Symbol sym;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2817
            final JCDiagnostic details;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2818
            final Type mtype;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2819
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2820
            private Candidate(MethodResolutionPhase step, Symbol sym, JCDiagnostic details, Type mtype) {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2821
                this.step = step;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2822
                this.sym = sym;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2823
                this.details = details;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2824
                this.mtype = mtype;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2825
            }
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2826
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2827
            @Override
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2828
            public boolean equals(Object o) {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2829
                if (o instanceof Candidate) {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2830
                    Symbol s1 = this.sym;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2831
                    Symbol s2 = ((Candidate)o).sym;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2832
                    if  ((s1 != s2 &&
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2833
                        (s1.overrides(s2, s1.owner.type.tsym, types, false) ||
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2834
                        (s2.overrides(s1, s2.owner.type.tsym, types, false)))) ||
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2835
                        ((s1.isConstructor() || s2.isConstructor()) && s1.owner != s2.owner))
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2836
                        return true;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2837
                }
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2838
                return false;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2839
            }
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2840
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2841
            boolean isApplicable() {
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2842
                return mtype != null;
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2843
            }
1533
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2844
        }
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2845
    }
12076
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2846
1b7560bd1760 7132880: Resolve should support nested resolution contexts
mcimadamore
parents: 11707
diff changeset
  2847
    MethodResolutionContext currentResolutionContext = null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2848
}