langtools/src/share/classes/com/sun/tools/javac/comp/Resolve.java
author jjg
Thu, 10 Jun 2010 16:08:01 -0700
changeset 5847 1908176fd6e3
parent 5846 6df0e6bcb388
child 5857 84d4886d48aa
permissions -rw-r--r--
6944312: Potential rebranding issues in openjdk/langtools repository sources Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5489
diff changeset
     2
 * Copyright (c) 1999, 2008, 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
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import com.sun.tools.javac.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import com.sun.tools.javac.code.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import com.sun.tools.javac.jvm.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import com.sun.tools.javac.tree.*;
1534
e923a41e84cc 6758789: Some method resolution diagnostic should be improved
mcimadamore
parents: 1533
diff changeset
    33
import com.sun.tools.javac.api.Formattable.LocalizedString;
1533
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
    34
import static com.sun.tools.javac.comp.Resolve.MethodResolutionPhase.*;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
import com.sun.tools.javac.code.Type.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
import com.sun.tools.javac.code.Symbol.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
import com.sun.tools.javac.tree.JCTree.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
import static com.sun.tools.javac.code.Flags.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
import static com.sun.tools.javac.code.Kinds.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
import static com.sun.tools.javac.code.TypeTags.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
import javax.lang.model.element.ElementVisitor;
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
1533
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
    45
import java.util.Map;
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
    46
import java.util.HashMap;
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
    47
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
/** Helper class for name resolution, used mostly by the attribution phase.
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5846
diff changeset
    50
 *  <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
    51
 *  If you write code that depends on this, you do so at your own risk.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
 *  This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
 *  deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
public class Resolve {
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
    protected static final Context.Key<Resolve> resolveKey =
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
        new Context.Key<Resolve>();
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 1257
diff changeset
    59
    Names names;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
    Log log;
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
    Symtab syms;
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
    Check chk;
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
    Infer infer;
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
    ClassReader reader;
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
    TreeInfo treeinfo;
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
    Types types;
1040
c0f5acfd9d15 6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents: 939
diff changeset
    67
    JCDiagnostic.Factory diags;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
    public final boolean boxingEnabled; // = source.allowBoxing();
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
    public final boolean varargsEnabled; // = source.allowVarargs();
2723
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
    70
    public final boolean allowInvokedynamic; // = options.get("invokedynamic");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
    private final boolean debugResolve;
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
    public static Resolve instance(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
        Resolve instance = context.get(resolveKey);
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
        if (instance == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
            instance = new Resolve(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
        return instance;
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
    protected Resolve(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
        context.put(resolveKey, this);
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
        syms = Symtab.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
        varNotFound = new
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
    85
            SymbolNotFoundError(ABSENT_VAR);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
        wrongMethod = new
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
    87
            InapplicableSymbolError(syms.errSymbol);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
        wrongMethods = new
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
    89
            InapplicableSymbolsError(syms.errSymbol);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
        methodNotFound = new
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
    91
            SymbolNotFoundError(ABSENT_MTH);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
        typeNotFound = new
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
    93
            SymbolNotFoundError(ABSENT_TYP);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 1257
diff changeset
    95
        names = Names.instance(context);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
        log = Log.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
        chk = Check.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
        infer = Infer.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
        reader = ClassReader.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
        treeinfo = TreeInfo.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
        types = Types.instance(context);
1040
c0f5acfd9d15 6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents: 939
diff changeset
   102
        diags = JCDiagnostic.Factory.instance(context);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
        Source source = Source.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
        boxingEnabled = source.allowBoxing();
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
        varargsEnabled = source.allowVarargs();
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
        Options options = Options.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
        debugResolve = options.get("debugresolve") != null;
2723
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   108
        allowInvokedynamic = options.get("invokedynamic") != null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
    /** error symbols, which are returned when resolution fails
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
     */
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
   113
    final SymbolNotFoundError varNotFound;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
   114
    final InapplicableSymbolError wrongMethod;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
   115
    final InapplicableSymbolsError wrongMethods;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
   116
    final SymbolNotFoundError methodNotFound;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
   117
    final SymbolNotFoundError typeNotFound;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
/* ************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
 * Identifier resolution
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
 *************************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
    /** An environment is "static" if its static level is greater than
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
     *  the one of its outer environment
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
    static boolean isStatic(Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
        return env.info.staticLevel > env.outer.info.staticLevel;
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
    /** An environment is an "initializer" if it is a constructor or
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
     *  an instance initializer.
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
    static boolean isInitializer(Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
        Symbol owner = env.info.scope.owner;
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
        return owner.isConstructor() ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
            owner.owner.kind == TYP &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
            (owner.kind == VAR ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
             owner.kind == MTH && (owner.flags() & BLOCK) != 0) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
            (owner.flags() & STATIC) == 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
    /** Is class accessible in given evironment?
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
     *  @param env    The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
     *  @param c      The class whose accessibility is checked.
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
    public boolean isAccessible(Env<AttrContext> env, TypeSymbol c) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
        switch ((short)(c.flags() & AccessFlags)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
        case PRIVATE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
            return
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
                env.enclClass.sym.outermostClass() ==
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
                c.owner.outermostClass();
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
        case 0:
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
            return
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
                env.toplevel.packge == c.owner // fast special case
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
                ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
                env.toplevel.packge == c.packge()
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
                ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
                // Hack: this case is added since synthesized default constructors
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
                // of anonymous classes should be allowed to access
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
                // classes which would be inaccessible otherwise.
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
                env.enclMethod != null &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
                (env.enclMethod.mods.flags & ANONCONSTR) != 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
        default: // error recovery
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
        case PUBLIC:
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
            return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
        case PROTECTED:
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
            return
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
                env.toplevel.packge == c.owner // fast special case
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
                ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
                env.toplevel.packge == c.packge()
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
                ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
                isInnerSubClass(env.enclClass.sym, c.owner);
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
    //where
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
        /** Is given class a subclass of given base class, or an inner class
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
         *  of a subclass?
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
         *  Return null if no such class exists.
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
         *  @param c     The class which is the subclass or is contained in it.
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
         *  @param base  The base class
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
        private boolean isInnerSubClass(ClassSymbol c, Symbol base) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
            while (c != null && !c.isSubClass(base, types)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
                c = c.owner.enclClass();
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
            return c != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
    boolean isAccessible(Env<AttrContext> env, Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
        return (t.tag == ARRAY)
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
            ? isAccessible(env, types.elemtype(t))
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
            : isAccessible(env, t.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
    /** Is symbol accessible as a member of given type in given evironment?
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
     *  @param env    The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
     *  @param site   The type of which the tested symbol is regarded
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
     *                as a member.
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
     *  @param sym    The symbol.
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
    public boolean isAccessible(Env<AttrContext> env, Type site, Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
        if (sym.name == names.init && sym.owner != site.tsym) return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
        ClassSymbol sub;
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
        switch ((short)(sym.flags() & AccessFlags)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
        case PRIVATE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
            return
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
                (env.enclClass.sym == sym.owner // fast special case
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
                 ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
                 env.enclClass.sym.outermostClass() ==
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
                 sym.owner.outermostClass())
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
                &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
                sym.isInheritedIn(site.tsym, types);
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
        case 0:
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
            return
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
                (env.toplevel.packge == sym.owner.owner // fast special case
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
                 ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
                 env.toplevel.packge == sym.packge())
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
                &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
                isAccessible(env, site)
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
                &&
2511
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   221
                sym.isInheritedIn(site.tsym, types)
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   222
                &&
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   223
                notOverriddenIn(site, sym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
        case PROTECTED:
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
            return
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
                (env.toplevel.packge == sym.owner.owner // fast special case
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
                 ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
                 env.toplevel.packge == sym.packge()
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
                 ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
                 isProtectedAccessible(sym, env.enclClass.sym, site)
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
                 ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
                 // OK to select instance method or field from 'super' or type name
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
                 // (but type names should be disallowed elsewhere!)
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
                 env.info.selectSuper && (sym.flags() & STATIC) == 0 && sym.kind != TYP)
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
                &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
                isAccessible(env, site)
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
                &&
2511
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   238
                notOverriddenIn(site, sym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
        default: // this case includes erroneous combinations as well
2511
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   240
            return isAccessible(env, site) && notOverriddenIn(site, sym);
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   241
        }
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   242
    }
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   243
    //where
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   244
    /* `sym' is accessible only if not overridden by
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   245
     * another symbol which is a member of `site'
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   246
     * (because, if it is overridden, `sym' is not strictly
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   247
     * speaking a member of `site'.)
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   248
     */
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   249
    private boolean notOverriddenIn(Type site, Symbol sym) {
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   250
        if (sym.kind != MTH || sym.isConstructor() || sym.isStatic())
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   251
            return true;
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   252
        else {
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   253
            Symbol s2 = ((MethodSymbol)sym).implementation(site.tsym, types, true);
3372
544ec1ab333c 6846972: cannot access member of raw type when erasure change overriding into overloading
mcimadamore
parents: 3143
diff changeset
   254
            return (s2 == null || s2 == sym ||
544ec1ab333c 6846972: cannot access member of raw type when erasure change overriding into overloading
mcimadamore
parents: 3143
diff changeset
   255
                    !types.isSubSignature(types.memberType(site, s2), types.memberType(site, sym)));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
    //where
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
        /** Is given protected symbol accessible if it is selected from given site
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
         *  and the selection takes place in given class?
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
         *  @param sym     The symbol with protected access
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
         *  @param c       The class where the access takes place
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
         *  @site          The type of the qualifier
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
        private
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
        boolean isProtectedAccessible(Symbol sym, ClassSymbol c, Type site) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
            while (c != null &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
                   !(c.isSubClass(sym.owner, types) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
                     (c.flags() & INTERFACE) == 0 &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
                     // In JLS 2e 6.6.2.1, the subclass restriction applies
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
                     // only to instance fields and methods -- types are excluded
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
                     // regardless of whether they are declared 'static' or not.
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
                     ((sym.flags() & STATIC) != 0 || sym.kind == TYP || site.tsym.isSubClass(c, types))))
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
                c = c.owner.enclClass();
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
            return c != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
    /** Try to instantiate the type of a method so that it fits
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
     *  given type arguments and argument types. If succesful, return
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
     *  the method's instantiated type, else return null.
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
     *  The instantiation will take into account an additional leading
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
     *  formal parameter if the method is an instance method seen as a member
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
     *  of un underdetermined site In this case, we treat site as an additional
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
     *  parameter and the parameters of the class containing the method as
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
     *  additional type variables that get instantiated.
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
     *  @param env         The current environment
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
     *  @param site        The type of which the method is a member.
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
     *  @param m           The method symbol.
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
     *  @param argtypes    The invocation's given value arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
     *  @param typeargtypes    The invocation's given type arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
     *  @param allowBoxing Allow boxing conversions of arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
     *  @param useVarargs Box trailing arguments into an array for varargs.
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
    Type rawInstantiate(Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
                        Type site,
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
                        Symbol m,
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
                        List<Type> argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
                        List<Type> typeargtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
                        boolean allowBoxing,
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
                        boolean useVarargs,
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
                        Warner warn)
3140
15a274b13051 6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents: 2723
diff changeset
   303
        throws Infer.InferenceException {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
        if (useVarargs && (m.flags() & VARARGS) == 0) return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
        Type mt = types.memberType(site, m);
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
        // tvars is the list of formal type variables for which type arguments
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
        // need to inferred.
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
        List<Type> tvars = env.info.tvars;
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
        if (typeargtypes == null) typeargtypes = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
        if (mt.tag != FORALL && typeargtypes.nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
            // This is not a polymorphic method, but typeargs are supplied
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
            // which is fine, see JLS3 15.12.2.1
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
        } else if (mt.tag == FORALL && typeargtypes.nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
            ForAll pmt = (ForAll) mt;
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
            if (typeargtypes.length() != pmt.tvars.length())
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
                return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
            // Check type arguments are within bounds
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
            List<Type> formals = pmt.tvars;
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
            List<Type> actuals = typeargtypes;
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
            while (formals.nonEmpty() && actuals.nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
                List<Type> bounds = types.subst(types.getBounds((TypeVar)formals.head),
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
                                                pmt.tvars, typeargtypes);
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
                for (; bounds.nonEmpty(); bounds = bounds.tail)
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
                    if (!types.isSubtypeUnchecked(actuals.head, bounds.head, warn))
06bc494ca11e Initial load
duke
parents:
diff changeset
   326
                        return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
                formals = formals.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
                actuals = actuals.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
            mt = types.subst(pmt.qtype, pmt.tvars, typeargtypes);
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
        } else if (mt.tag == FORALL) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   332
            ForAll pmt = (ForAll) mt;
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
            List<Type> tvars1 = types.newInstances(pmt.tvars);
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
            tvars = tvars.appendList(tvars1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
            mt = types.subst(pmt.qtype, pmt.tvars, tvars1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
06bc494ca11e Initial load
duke
parents:
diff changeset
   338
        // find out whether we need to go the slow route via infer
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
        boolean instNeeded = tvars.tail != null/*inlined: tvars.nonEmpty()*/;
06bc494ca11e Initial load
duke
parents:
diff changeset
   340
        for (List<Type> l = argtypes;
06bc494ca11e Initial load
duke
parents:
diff changeset
   341
             l.tail != null/*inlined: l.nonEmpty()*/ && !instNeeded;
06bc494ca11e Initial load
duke
parents:
diff changeset
   342
             l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
            if (l.head.tag == FORALL) instNeeded = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
        if (instNeeded)
06bc494ca11e Initial load
duke
parents:
diff changeset
   347
            return
5489
e7af65bf7577 6730476: invalid "unchecked generic array" warning
mcimadamore
parents: 5321
diff changeset
   348
            infer.instantiateMethod(env,
e7af65bf7577 6730476: invalid "unchecked generic array" warning
mcimadamore
parents: 5321
diff changeset
   349
                                    tvars,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   350
                                    (MethodType)mt,
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents: 5520
diff changeset
   351
                                    m,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   352
                                    argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
   353
                                    allowBoxing,
06bc494ca11e Initial load
duke
parents:
diff changeset
   354
                                    useVarargs,
06bc494ca11e Initial load
duke
parents:
diff changeset
   355
                                    warn);
06bc494ca11e Initial load
duke
parents:
diff changeset
   356
        return
06bc494ca11e Initial load
duke
parents:
diff changeset
   357
            argumentsAcceptable(argtypes, mt.getParameterTypes(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   358
                                allowBoxing, useVarargs, warn)
06bc494ca11e Initial load
duke
parents:
diff changeset
   359
            ? mt
06bc494ca11e Initial load
duke
parents:
diff changeset
   360
            : null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   361
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   362
06bc494ca11e Initial load
duke
parents:
diff changeset
   363
    /** Same but returns null instead throwing a NoInstanceException
06bc494ca11e Initial load
duke
parents:
diff changeset
   364
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   365
    Type instantiate(Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
   366
                     Type site,
06bc494ca11e Initial load
duke
parents:
diff changeset
   367
                     Symbol m,
06bc494ca11e Initial load
duke
parents:
diff changeset
   368
                     List<Type> argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
   369
                     List<Type> typeargtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
   370
                     boolean allowBoxing,
06bc494ca11e Initial load
duke
parents:
diff changeset
   371
                     boolean useVarargs,
06bc494ca11e Initial load
duke
parents:
diff changeset
   372
                     Warner warn) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   373
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   374
            return rawInstantiate(env, site, m, argtypes, typeargtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
   375
                                  allowBoxing, useVarargs, warn);
3140
15a274b13051 6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents: 2723
diff changeset
   376
        } catch (Infer.InferenceException ex) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   377
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   378
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   379
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   380
06bc494ca11e Initial load
duke
parents:
diff changeset
   381
    /** Check if a parameter list accepts a list of args.
06bc494ca11e Initial load
duke
parents:
diff changeset
   382
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   383
    boolean argumentsAcceptable(List<Type> argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
   384
                                List<Type> formals,
06bc494ca11e Initial load
duke
parents:
diff changeset
   385
                                boolean allowBoxing,
06bc494ca11e Initial load
duke
parents:
diff changeset
   386
                                boolean useVarargs,
06bc494ca11e Initial load
duke
parents:
diff changeset
   387
                                Warner warn) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   388
        Type varargsFormal = useVarargs ? formals.last() : null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   389
        while (argtypes.nonEmpty() && formals.head != varargsFormal) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   390
            boolean works = allowBoxing
06bc494ca11e Initial load
duke
parents:
diff changeset
   391
                ? types.isConvertible(argtypes.head, formals.head, warn)
06bc494ca11e Initial load
duke
parents:
diff changeset
   392
                : types.isSubtypeUnchecked(argtypes.head, formals.head, warn);
06bc494ca11e Initial load
duke
parents:
diff changeset
   393
            if (!works) return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   394
            argtypes = argtypes.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   395
            formals = formals.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   396
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   397
        if (formals.head != varargsFormal) return false; // not enough args
06bc494ca11e Initial load
duke
parents:
diff changeset
   398
        if (!useVarargs)
06bc494ca11e Initial load
duke
parents:
diff changeset
   399
            return argtypes.isEmpty();
06bc494ca11e Initial load
duke
parents:
diff changeset
   400
        Type elt = types.elemtype(varargsFormal);
06bc494ca11e Initial load
duke
parents:
diff changeset
   401
        while (argtypes.nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   402
            if (!types.isConvertible(argtypes.head, elt, warn))
06bc494ca11e Initial load
duke
parents:
diff changeset
   403
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   404
            argtypes = argtypes.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   405
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   406
        return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   407
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   408
06bc494ca11e Initial load
duke
parents:
diff changeset
   409
/* ***************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
   410
 *  Symbol lookup
06bc494ca11e Initial load
duke
parents:
diff changeset
   411
 *  the following naming conventions for arguments are used
06bc494ca11e Initial load
duke
parents:
diff changeset
   412
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
   413
 *       env      is the environment where the symbol was mentioned
06bc494ca11e Initial load
duke
parents:
diff changeset
   414
 *       site     is the type of which the symbol is a member
06bc494ca11e Initial load
duke
parents:
diff changeset
   415
 *       name     is the symbol's name
06bc494ca11e Initial load
duke
parents:
diff changeset
   416
 *                if no arguments are given
06bc494ca11e Initial load
duke
parents:
diff changeset
   417
 *       argtypes are the value arguments, if we search for a method
06bc494ca11e Initial load
duke
parents:
diff changeset
   418
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
   419
 *  If no symbol was found, a ResolveError detailing the problem is returned.
06bc494ca11e Initial load
duke
parents:
diff changeset
   420
 ****************************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
   421
06bc494ca11e Initial load
duke
parents:
diff changeset
   422
    /** Find field. Synthetic fields are always skipped.
06bc494ca11e Initial load
duke
parents:
diff changeset
   423
     *  @param env     The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
   424
     *  @param site    The original type from where the selection takes place.
06bc494ca11e Initial load
duke
parents:
diff changeset
   425
     *  @param name    The name of the field.
06bc494ca11e Initial load
duke
parents:
diff changeset
   426
     *  @param c       The class to search for the field. This is always
06bc494ca11e Initial load
duke
parents:
diff changeset
   427
     *                 a superclass or implemented interface of site's class.
06bc494ca11e Initial load
duke
parents:
diff changeset
   428
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   429
    Symbol findField(Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
   430
                     Type site,
06bc494ca11e Initial load
duke
parents:
diff changeset
   431
                     Name name,
06bc494ca11e Initial load
duke
parents:
diff changeset
   432
                     TypeSymbol c) {
326
d51f30ce6796 6531090: Cannot access methods/fields of a captured type belonging to an intersection type
mcimadamore
parents: 10
diff changeset
   433
        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
   434
            c = c.type.getUpperBound().tsym;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   435
        Symbol bestSoFar = varNotFound;
06bc494ca11e Initial load
duke
parents:
diff changeset
   436
        Symbol sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   437
        Scope.Entry e = c.members().lookup(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   438
        while (e.scope != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   439
            if (e.sym.kind == VAR && (e.sym.flags_field & SYNTHETIC) == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   440
                return isAccessible(env, site, e.sym)
06bc494ca11e Initial load
duke
parents:
diff changeset
   441
                    ? e.sym : new AccessError(env, site, e.sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   442
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   443
            e = e.next();
06bc494ca11e Initial load
duke
parents:
diff changeset
   444
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   445
        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
   446
        if (st != null && (st.tag == CLASS || st.tag == TYPEVAR)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   447
            sym = findField(env, site, name, st.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   448
            if (sym.kind < bestSoFar.kind) bestSoFar = sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   449
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   450
        for (List<Type> l = types.interfaces(c.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   451
             bestSoFar.kind != AMBIGUOUS && l.nonEmpty();
06bc494ca11e Initial load
duke
parents:
diff changeset
   452
             l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   453
            sym = findField(env, site, name, l.head.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   454
            if (bestSoFar.kind < AMBIGUOUS && sym.kind < AMBIGUOUS &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   455
                sym.owner != bestSoFar.owner)
06bc494ca11e Initial load
duke
parents:
diff changeset
   456
                bestSoFar = new AmbiguityError(bestSoFar, sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   457
            else if (sym.kind < bestSoFar.kind)
06bc494ca11e Initial load
duke
parents:
diff changeset
   458
                bestSoFar = sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   459
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   460
        return bestSoFar;
06bc494ca11e Initial load
duke
parents:
diff changeset
   461
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   462
06bc494ca11e Initial load
duke
parents:
diff changeset
   463
    /** Resolve a field identifier, throw a fatal error if not found.
06bc494ca11e Initial load
duke
parents:
diff changeset
   464
     *  @param pos       The position to use for error reporting.
06bc494ca11e Initial load
duke
parents:
diff changeset
   465
     *  @param env       The environment current at the method invocation.
06bc494ca11e Initial load
duke
parents:
diff changeset
   466
     *  @param site      The type of the qualifying expression, in which
06bc494ca11e Initial load
duke
parents:
diff changeset
   467
     *                   identifier is searched.
06bc494ca11e Initial load
duke
parents:
diff changeset
   468
     *  @param name      The identifier's name.
06bc494ca11e Initial load
duke
parents:
diff changeset
   469
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   470
    public VarSymbol resolveInternalField(DiagnosticPosition pos, Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
   471
                                          Type site, Name name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   472
        Symbol sym = findField(env, site, name, site.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   473
        if (sym.kind == VAR) return (VarSymbol)sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   474
        else throw new FatalError(
1040
c0f5acfd9d15 6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents: 939
diff changeset
   475
                 diags.fragment("fatal.err.cant.locate.field",
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   476
                                name));
06bc494ca11e Initial load
duke
parents:
diff changeset
   477
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   478
06bc494ca11e Initial load
duke
parents:
diff changeset
   479
    /** Find unqualified variable or field with given name.
06bc494ca11e Initial load
duke
parents:
diff changeset
   480
     *  Synthetic fields always skipped.
06bc494ca11e Initial load
duke
parents:
diff changeset
   481
     *  @param env     The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
   482
     *  @param name    The name of the variable or field.
06bc494ca11e Initial load
duke
parents:
diff changeset
   483
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   484
    Symbol findVar(Env<AttrContext> env, Name name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   485
        Symbol bestSoFar = varNotFound;
06bc494ca11e Initial load
duke
parents:
diff changeset
   486
        Symbol sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   487
        Env<AttrContext> env1 = env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   488
        boolean staticOnly = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   489
        while (env1.outer != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   490
            if (isStatic(env1)) staticOnly = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   491
            Scope.Entry e = env1.info.scope.lookup(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   492
            while (e.scope != null &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   493
                   (e.sym.kind != VAR ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   494
                    (e.sym.flags_field & SYNTHETIC) != 0))
06bc494ca11e Initial load
duke
parents:
diff changeset
   495
                e = e.next();
06bc494ca11e Initial load
duke
parents:
diff changeset
   496
            sym = (e.scope != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   497
                ? e.sym
06bc494ca11e Initial load
duke
parents:
diff changeset
   498
                : findField(
06bc494ca11e Initial load
duke
parents:
diff changeset
   499
                    env1, env1.enclClass.sym.type, name, env1.enclClass.sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   500
            if (sym.exists()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   501
                if (staticOnly &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   502
                    sym.kind == VAR &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   503
                    sym.owner.kind == TYP &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   504
                    (sym.flags() & STATIC) == 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   505
                    return new StaticError(sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   506
                else
06bc494ca11e Initial load
duke
parents:
diff changeset
   507
                    return sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   508
            } else if (sym.kind < bestSoFar.kind) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   509
                bestSoFar = sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   510
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   511
06bc494ca11e Initial load
duke
parents:
diff changeset
   512
            if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   513
            env1 = env1.outer;
06bc494ca11e Initial load
duke
parents:
diff changeset
   514
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   515
06bc494ca11e Initial load
duke
parents:
diff changeset
   516
        sym = findField(env, syms.predefClass.type, name, syms.predefClass);
06bc494ca11e Initial load
duke
parents:
diff changeset
   517
        if (sym.exists())
06bc494ca11e Initial load
duke
parents:
diff changeset
   518
            return sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   519
        if (bestSoFar.exists())
06bc494ca11e Initial load
duke
parents:
diff changeset
   520
            return bestSoFar;
06bc494ca11e Initial load
duke
parents:
diff changeset
   521
06bc494ca11e Initial load
duke
parents:
diff changeset
   522
        Scope.Entry e = env.toplevel.namedImportScope.lookup(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   523
        for (; e.scope != null; e = e.next()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   524
            sym = e.sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   525
            Type origin = e.getOrigin().owner.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
   526
            if (sym.kind == VAR) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   527
                if (e.sym.owner.type != origin)
06bc494ca11e Initial load
duke
parents:
diff changeset
   528
                    sym = sym.clone(e.getOrigin().owner);
06bc494ca11e Initial load
duke
parents:
diff changeset
   529
                return isAccessible(env, origin, sym)
06bc494ca11e Initial load
duke
parents:
diff changeset
   530
                    ? sym : new AccessError(env, origin, sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   531
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   532
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   533
06bc494ca11e Initial load
duke
parents:
diff changeset
   534
        Symbol origin = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   535
        e = env.toplevel.starImportScope.lookup(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   536
        for (; e.scope != null; e = e.next()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   537
            sym = e.sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   538
            if (sym.kind != VAR)
06bc494ca11e Initial load
duke
parents:
diff changeset
   539
                continue;
06bc494ca11e Initial load
duke
parents:
diff changeset
   540
            // invariant: sym.kind == VAR
06bc494ca11e Initial load
duke
parents:
diff changeset
   541
            if (bestSoFar.kind < AMBIGUOUS && sym.owner != bestSoFar.owner)
06bc494ca11e Initial load
duke
parents:
diff changeset
   542
                return new AmbiguityError(bestSoFar, sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   543
            else if (bestSoFar.kind >= VAR) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   544
                origin = e.getOrigin().owner;
06bc494ca11e Initial load
duke
parents:
diff changeset
   545
                bestSoFar = isAccessible(env, origin.type, sym)
06bc494ca11e Initial load
duke
parents:
diff changeset
   546
                    ? sym : new AccessError(env, origin.type, sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   547
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   548
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   549
        if (bestSoFar.kind == VAR && bestSoFar.owner.type != origin.type)
06bc494ca11e Initial load
duke
parents:
diff changeset
   550
            return bestSoFar.clone(origin);
06bc494ca11e Initial load
duke
parents:
diff changeset
   551
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
   552
            return bestSoFar;
06bc494ca11e Initial load
duke
parents:
diff changeset
   553
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   554
06bc494ca11e Initial load
duke
parents:
diff changeset
   555
    Warner noteWarner = new Warner();
06bc494ca11e Initial load
duke
parents:
diff changeset
   556
06bc494ca11e Initial load
duke
parents:
diff changeset
   557
    /** Select the best method for a call site among two choices.
06bc494ca11e Initial load
duke
parents:
diff changeset
   558
     *  @param env              The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
   559
     *  @param site             The original type from where the
06bc494ca11e Initial load
duke
parents:
diff changeset
   560
     *                          selection takes place.
06bc494ca11e Initial load
duke
parents:
diff changeset
   561
     *  @param argtypes         The invocation's value arguments,
06bc494ca11e Initial load
duke
parents:
diff changeset
   562
     *  @param typeargtypes     The invocation's type arguments,
06bc494ca11e Initial load
duke
parents:
diff changeset
   563
     *  @param sym              Proposed new best match.
06bc494ca11e Initial load
duke
parents:
diff changeset
   564
     *  @param bestSoFar        Previously found best match.
06bc494ca11e Initial load
duke
parents:
diff changeset
   565
     *  @param allowBoxing Allow boxing conversions of arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
   566
     *  @param useVarargs Box trailing arguments into an array for varargs.
06bc494ca11e Initial load
duke
parents:
diff changeset
   567
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   568
    Symbol selectBest(Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
   569
                      Type site,
06bc494ca11e Initial load
duke
parents:
diff changeset
   570
                      List<Type> argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
   571
                      List<Type> typeargtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
   572
                      Symbol sym,
06bc494ca11e Initial load
duke
parents:
diff changeset
   573
                      Symbol bestSoFar,
06bc494ca11e Initial load
duke
parents:
diff changeset
   574
                      boolean allowBoxing,
06bc494ca11e Initial load
duke
parents:
diff changeset
   575
                      boolean useVarargs,
06bc494ca11e Initial load
duke
parents:
diff changeset
   576
                      boolean operator) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   577
        if (sym.kind == ERR) return bestSoFar;
1649
9ec015f3661e 6776289: Regression: javac7 doesnt resolve method calls properly
mcimadamore
parents: 1534
diff changeset
   578
        if (!sym.isInheritedIn(site.tsym, types)) return bestSoFar;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   579
        assert sym.kind < AMBIGUOUS;
06bc494ca11e Initial load
duke
parents:
diff changeset
   580
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   581
            if (rawInstantiate(env, site, sym, argtypes, typeargtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
   582
                               allowBoxing, useVarargs, Warner.noWarnings) == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   583
                // inapplicable
06bc494ca11e Initial load
duke
parents:
diff changeset
   584
                switch (bestSoFar.kind) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   585
                case ABSENT_MTH: return wrongMethod.setWrongSym(sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   586
                case WRONG_MTH: return wrongMethods;
06bc494ca11e Initial load
duke
parents:
diff changeset
   587
                default: return bestSoFar;
06bc494ca11e Initial load
duke
parents:
diff changeset
   588
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   589
            }
3140
15a274b13051 6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents: 2723
diff changeset
   590
        } catch (Infer.InferenceException ex) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   591
            switch (bestSoFar.kind) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   592
            case ABSENT_MTH:
06bc494ca11e Initial load
duke
parents:
diff changeset
   593
                return wrongMethod.setWrongSym(sym, ex.getDiagnostic());
06bc494ca11e Initial load
duke
parents:
diff changeset
   594
            case WRONG_MTH:
06bc494ca11e Initial load
duke
parents:
diff changeset
   595
                return wrongMethods;
06bc494ca11e Initial load
duke
parents:
diff changeset
   596
            default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   597
                return bestSoFar;
06bc494ca11e Initial load
duke
parents:
diff changeset
   598
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   599
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   600
        if (!isAccessible(env, site, sym)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   601
            return (bestSoFar.kind == ABSENT_MTH)
06bc494ca11e Initial load
duke
parents:
diff changeset
   602
                ? new AccessError(env, site, sym)
06bc494ca11e Initial load
duke
parents:
diff changeset
   603
                : bestSoFar;
06bc494ca11e Initial load
duke
parents:
diff changeset
   604
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   605
        return (bestSoFar.kind > AMBIGUOUS)
06bc494ca11e Initial load
duke
parents:
diff changeset
   606
            ? sym
06bc494ca11e Initial load
duke
parents:
diff changeset
   607
            : mostSpecific(sym, bestSoFar, env, site,
06bc494ca11e Initial load
duke
parents:
diff changeset
   608
                           allowBoxing && operator, useVarargs);
06bc494ca11e Initial load
duke
parents:
diff changeset
   609
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   610
06bc494ca11e Initial load
duke
parents:
diff changeset
   611
    /* Return the most specific of the two methods for a call,
06bc494ca11e Initial load
duke
parents:
diff changeset
   612
     *  given that both are accessible and applicable.
06bc494ca11e Initial load
duke
parents:
diff changeset
   613
     *  @param m1               A new candidate for most specific.
06bc494ca11e Initial load
duke
parents:
diff changeset
   614
     *  @param m2               The previous most specific candidate.
06bc494ca11e Initial load
duke
parents:
diff changeset
   615
     *  @param env              The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
   616
     *  @param site             The original type from where the selection
06bc494ca11e Initial load
duke
parents:
diff changeset
   617
     *                          takes place.
06bc494ca11e Initial load
duke
parents:
diff changeset
   618
     *  @param allowBoxing Allow boxing conversions of arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
   619
     *  @param useVarargs Box trailing arguments into an array for varargs.
06bc494ca11e Initial load
duke
parents:
diff changeset
   620
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   621
    Symbol mostSpecific(Symbol m1,
06bc494ca11e Initial load
duke
parents:
diff changeset
   622
                        Symbol m2,
06bc494ca11e Initial load
duke
parents:
diff changeset
   623
                        Env<AttrContext> env,
2511
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   624
                        final Type site,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   625
                        boolean allowBoxing,
06bc494ca11e Initial load
duke
parents:
diff changeset
   626
                        boolean useVarargs) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   627
        switch (m2.kind) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   628
        case MTH:
06bc494ca11e Initial load
duke
parents:
diff changeset
   629
            if (m1 == m2) return m1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   630
            Type mt1 = types.memberType(site, m1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   631
            noteWarner.unchecked = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   632
            boolean m1SignatureMoreSpecific =
06bc494ca11e Initial load
duke
parents:
diff changeset
   633
                (instantiate(env, site, m2, types.lowerBoundArgtypes(mt1), null,
06bc494ca11e Initial load
duke
parents:
diff changeset
   634
                             allowBoxing, false, noteWarner) != null ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   635
                 useVarargs && instantiate(env, site, m2, types.lowerBoundArgtypes(mt1), null,
06bc494ca11e Initial load
duke
parents:
diff changeset
   636
                                           allowBoxing, true, noteWarner) != null) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   637
                !noteWarner.unchecked;
06bc494ca11e Initial load
duke
parents:
diff changeset
   638
            Type mt2 = types.memberType(site, m2);
06bc494ca11e Initial load
duke
parents:
diff changeset
   639
            noteWarner.unchecked = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   640
            boolean m2SignatureMoreSpecific =
06bc494ca11e Initial load
duke
parents:
diff changeset
   641
                (instantiate(env, site, m1, types.lowerBoundArgtypes(mt2), null,
06bc494ca11e Initial load
duke
parents:
diff changeset
   642
                             allowBoxing, false, noteWarner) != null ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   643
                 useVarargs && instantiate(env, site, m1, types.lowerBoundArgtypes(mt2), null,
06bc494ca11e Initial load
duke
parents:
diff changeset
   644
                                           allowBoxing, true, noteWarner) != null) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   645
                !noteWarner.unchecked;
06bc494ca11e Initial load
duke
parents:
diff changeset
   646
            if (m1SignatureMoreSpecific && m2SignatureMoreSpecific) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   647
                if (!types.overrideEquivalent(mt1, mt2))
06bc494ca11e Initial load
duke
parents:
diff changeset
   648
                    return new AmbiguityError(m1, m2);
06bc494ca11e Initial load
duke
parents:
diff changeset
   649
                // same signature; select (a) the non-bridge method, or
06bc494ca11e Initial load
duke
parents:
diff changeset
   650
                // (b) the one that overrides the other, or (c) the concrete
06bc494ca11e Initial load
duke
parents:
diff changeset
   651
                // one, or (d) merge both abstract signatures
06bc494ca11e Initial load
duke
parents:
diff changeset
   652
                if ((m1.flags() & BRIDGE) != (m2.flags() & BRIDGE)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   653
                    return ((m1.flags() & BRIDGE) != 0) ? m2 : m1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   654
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   655
                // if one overrides or hides the other, use it
06bc494ca11e Initial load
duke
parents:
diff changeset
   656
                TypeSymbol m1Owner = (TypeSymbol)m1.owner;
06bc494ca11e Initial load
duke
parents:
diff changeset
   657
                TypeSymbol m2Owner = (TypeSymbol)m2.owner;
06bc494ca11e Initial load
duke
parents:
diff changeset
   658
                if (types.asSuper(m1Owner.type, m2Owner) != null &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   659
                    ((m1.owner.flags_field & INTERFACE) == 0 ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   660
                     (m2.owner.flags_field & INTERFACE) != 0) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   661
                    m1.overrides(m2, m1Owner, types, false))
06bc494ca11e Initial load
duke
parents:
diff changeset
   662
                    return m1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   663
                if (types.asSuper(m2Owner.type, m1Owner) != null &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   664
                    ((m2.owner.flags_field & INTERFACE) == 0 ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   665
                     (m1.owner.flags_field & INTERFACE) != 0) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   666
                    m2.overrides(m1, m2Owner, types, false))
06bc494ca11e Initial load
duke
parents:
diff changeset
   667
                    return m2;
06bc494ca11e Initial load
duke
parents:
diff changeset
   668
                boolean m1Abstract = (m1.flags() & ABSTRACT) != 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   669
                boolean m2Abstract = (m2.flags() & ABSTRACT) != 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   670
                if (m1Abstract && !m2Abstract) return m2;
06bc494ca11e Initial load
duke
parents:
diff changeset
   671
                if (m2Abstract && !m1Abstract) return m1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   672
                // both abstract or both concrete
06bc494ca11e Initial load
duke
parents:
diff changeset
   673
                if (!m1Abstract && !m2Abstract)
06bc494ca11e Initial load
duke
parents:
diff changeset
   674
                    return new AmbiguityError(m1, m2);
1529
a076d4cd3048 6487370: javac incorrectly gives ambiguity warning with override-equivalent abstract inherited methods
mcimadamore
parents: 1528
diff changeset
   675
                // 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
   676
                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
   677
                                       m2.erasure(types).getParameterTypes()))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   678
                    return new AmbiguityError(m1, m2);
06bc494ca11e Initial load
duke
parents:
diff changeset
   679
                // both abstract, neither overridden; merge throws clause and result type
2511
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   680
                Symbol mostSpecific;
1257
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 1040
diff changeset
   681
                Type result2 = mt2.getReturnType();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   682
                if (mt2.tag == FORALL)
06bc494ca11e Initial load
duke
parents:
diff changeset
   683
                    result2 = types.subst(result2, ((ForAll)mt2).tvars, ((ForAll)mt1).tvars);
06bc494ca11e Initial load
duke
parents:
diff changeset
   684
                if (types.isSubtype(mt1.getReturnType(), result2)) {
2511
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   685
                    mostSpecific = m1;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   686
                } else if (types.isSubtype(result2, mt1.getReturnType())) {
2511
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   687
                    mostSpecific = m2;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   688
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   689
                    // Theoretically, this can't happen, but it is possible
06bc494ca11e Initial load
duke
parents:
diff changeset
   690
                    // due to error recovery or mixing incompatible class files
06bc494ca11e Initial load
duke
parents:
diff changeset
   691
                    return new AmbiguityError(m1, m2);
06bc494ca11e Initial load
duke
parents:
diff changeset
   692
                }
2511
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   693
                MethodSymbol result = new MethodSymbol(
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   694
                        mostSpecific.flags(),
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   695
                        mostSpecific.name,
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   696
                        null,
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   697
                        mostSpecific.owner) {
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   698
                    @Override
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   699
                    public MethodSymbol implementation(TypeSymbol origin, Types types, boolean checkResult) {
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   700
                        if (origin == site.tsym)
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   701
                            return this;
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   702
                        else
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   703
                            return super.implementation(origin, types, checkResult);
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   704
                    }
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   705
                };
1ccb94d04005 6400189: raw types and inference
mcimadamore
parents: 1649
diff changeset
   706
                result.type = (Type)mostSpecific.type.clone();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   707
                result.type.setThrown(chk.intersect(mt1.getThrownTypes(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   708
                                                    mt2.getThrownTypes()));
06bc494ca11e Initial load
duke
parents:
diff changeset
   709
                return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
   710
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   711
            if (m1SignatureMoreSpecific) return m1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   712
            if (m2SignatureMoreSpecific) return m2;
06bc494ca11e Initial load
duke
parents:
diff changeset
   713
            return new AmbiguityError(m1, m2);
06bc494ca11e Initial load
duke
parents:
diff changeset
   714
        case AMBIGUOUS:
06bc494ca11e Initial load
duke
parents:
diff changeset
   715
            AmbiguityError e = (AmbiguityError)m2;
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
   716
            Symbol err1 = mostSpecific(m1, e.sym, env, site, allowBoxing, useVarargs);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   717
            Symbol err2 = mostSpecific(m1, e.sym2, env, site, allowBoxing, useVarargs);
06bc494ca11e Initial load
duke
parents:
diff changeset
   718
            if (err1 == err2) return err1;
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
   719
            if (err1 == e.sym && err2 == e.sym2) return m2;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   720
            if (err1 instanceof AmbiguityError &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   721
                err2 instanceof AmbiguityError &&
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
   722
                ((AmbiguityError)err1).sym == ((AmbiguityError)err2).sym)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   723
                return new AmbiguityError(m1, m2);
06bc494ca11e Initial load
duke
parents:
diff changeset
   724
            else
06bc494ca11e Initial load
duke
parents:
diff changeset
   725
                return new AmbiguityError(err1, err2);
06bc494ca11e Initial load
duke
parents:
diff changeset
   726
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   727
            throw new AssertionError();
06bc494ca11e Initial load
duke
parents:
diff changeset
   728
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   729
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   730
06bc494ca11e Initial load
duke
parents:
diff changeset
   731
    /** Find best qualified method matching given name, type and value
06bc494ca11e Initial load
duke
parents:
diff changeset
   732
     *  arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
   733
     *  @param env       The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
   734
     *  @param site      The original type from where the selection
06bc494ca11e Initial load
duke
parents:
diff changeset
   735
     *                   takes place.
06bc494ca11e Initial load
duke
parents:
diff changeset
   736
     *  @param name      The method's name.
06bc494ca11e Initial load
duke
parents:
diff changeset
   737
     *  @param argtypes  The method's value arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
   738
     *  @param typeargtypes The method's type arguments
06bc494ca11e Initial load
duke
parents:
diff changeset
   739
     *  @param allowBoxing Allow boxing conversions of arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
   740
     *  @param useVarargs Box trailing arguments into an array for varargs.
06bc494ca11e Initial load
duke
parents:
diff changeset
   741
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   742
    Symbol findMethod(Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
   743
                      Type site,
06bc494ca11e Initial load
duke
parents:
diff changeset
   744
                      Name name,
06bc494ca11e Initial load
duke
parents:
diff changeset
   745
                      List<Type> argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
   746
                      List<Type> typeargtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
   747
                      boolean allowBoxing,
06bc494ca11e Initial load
duke
parents:
diff changeset
   748
                      boolean useVarargs,
06bc494ca11e Initial load
duke
parents:
diff changeset
   749
                      boolean operator) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   750
        return findMethod(env,
06bc494ca11e Initial load
duke
parents:
diff changeset
   751
                          site,
06bc494ca11e Initial load
duke
parents:
diff changeset
   752
                          name,
06bc494ca11e Initial load
duke
parents:
diff changeset
   753
                          argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
   754
                          typeargtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
   755
                          site.tsym.type,
06bc494ca11e Initial load
duke
parents:
diff changeset
   756
                          true,
06bc494ca11e Initial load
duke
parents:
diff changeset
   757
                          methodNotFound,
06bc494ca11e Initial load
duke
parents:
diff changeset
   758
                          allowBoxing,
06bc494ca11e Initial load
duke
parents:
diff changeset
   759
                          useVarargs,
06bc494ca11e Initial load
duke
parents:
diff changeset
   760
                          operator);
06bc494ca11e Initial load
duke
parents:
diff changeset
   761
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   762
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
   763
    private Symbol findMethod(Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
   764
                              Type site,
06bc494ca11e Initial load
duke
parents:
diff changeset
   765
                              Name name,
06bc494ca11e Initial load
duke
parents:
diff changeset
   766
                              List<Type> argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
   767
                              List<Type> typeargtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
   768
                              Type intype,
06bc494ca11e Initial load
duke
parents:
diff changeset
   769
                              boolean abstractok,
06bc494ca11e Initial load
duke
parents:
diff changeset
   770
                              Symbol bestSoFar,
06bc494ca11e Initial load
duke
parents:
diff changeset
   771
                              boolean allowBoxing,
06bc494ca11e Initial load
duke
parents:
diff changeset
   772
                              boolean useVarargs,
06bc494ca11e Initial load
duke
parents:
diff changeset
   773
                              boolean operator) {
326
d51f30ce6796 6531090: Cannot access methods/fields of a captured type belonging to an intersection type
mcimadamore
parents: 10
diff changeset
   774
        for (Type ct = intype; ct.tag == CLASS || ct.tag == TYPEVAR; ct = types.supertype(ct)) {
d51f30ce6796 6531090: Cannot access methods/fields of a captured type belonging to an intersection type
mcimadamore
parents: 10
diff changeset
   775
            while (ct.tag == TYPEVAR)
d51f30ce6796 6531090: Cannot access methods/fields of a captured type belonging to an intersection type
mcimadamore
parents: 10
diff changeset
   776
                ct = ct.getUpperBound();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   777
            ClassSymbol c = (ClassSymbol)ct.tsym;
1470
6ff8524783fa 6724345: incorrect method resolution for enum classes entered as source files
mcimadamore
parents: 1260
diff changeset
   778
            if ((c.flags() & (ABSTRACT | INTERFACE | ENUM)) == 0)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   779
                abstractok = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   780
            for (Scope.Entry e = c.members().lookup(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   781
                 e.scope != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   782
                 e = e.next()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   783
                //- System.out.println(" e " + e.sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   784
                if (e.sym.kind == MTH &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   785
                    (e.sym.flags_field & SYNTHETIC) == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   786
                    bestSoFar = selectBest(env, site, argtypes, typeargtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
   787
                                           e.sym, bestSoFar,
06bc494ca11e Initial load
duke
parents:
diff changeset
   788
                                           allowBoxing,
06bc494ca11e Initial load
duke
parents:
diff changeset
   789
                                           useVarargs,
06bc494ca11e Initial load
duke
parents:
diff changeset
   790
                                           operator);
06bc494ca11e Initial load
duke
parents:
diff changeset
   791
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   792
            }
5321
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
   793
            if (name == names.init)
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
   794
                break;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   795
            //- System.out.println(" - " + bestSoFar);
06bc494ca11e Initial load
duke
parents:
diff changeset
   796
            if (abstractok) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   797
                Symbol concrete = methodNotFound;
06bc494ca11e Initial load
duke
parents:
diff changeset
   798
                if ((bestSoFar.flags() & ABSTRACT) == 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   799
                    concrete = bestSoFar;
06bc494ca11e Initial load
duke
parents:
diff changeset
   800
                for (List<Type> l = types.interfaces(c.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   801
                     l.nonEmpty();
06bc494ca11e Initial load
duke
parents:
diff changeset
   802
                     l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   803
                    bestSoFar = findMethod(env, site, name, argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
   804
                                           typeargtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
   805
                                           l.head, abstractok, bestSoFar,
06bc494ca11e Initial load
duke
parents:
diff changeset
   806
                                           allowBoxing, useVarargs, operator);
06bc494ca11e Initial load
duke
parents:
diff changeset
   807
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   808
                if (concrete != bestSoFar &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   809
                    concrete.kind < ERR  && bestSoFar.kind < ERR &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   810
                    types.isSubSignature(concrete.type, bestSoFar.type))
06bc494ca11e Initial load
duke
parents:
diff changeset
   811
                    bestSoFar = concrete;
06bc494ca11e Initial load
duke
parents:
diff changeset
   812
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   813
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   814
        return bestSoFar;
06bc494ca11e Initial load
duke
parents:
diff changeset
   815
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   816
06bc494ca11e Initial load
duke
parents:
diff changeset
   817
    /** Find unqualified method matching given name, type and value arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
   818
     *  @param env       The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
   819
     *  @param name      The method's name.
06bc494ca11e Initial load
duke
parents:
diff changeset
   820
     *  @param argtypes  The method's value arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
   821
     *  @param typeargtypes  The method's type arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
   822
     *  @param allowBoxing Allow boxing conversions of arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
   823
     *  @param useVarargs Box trailing arguments into an array for varargs.
06bc494ca11e Initial load
duke
parents:
diff changeset
   824
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   825
    Symbol findFun(Env<AttrContext> env, Name name,
06bc494ca11e Initial load
duke
parents:
diff changeset
   826
                   List<Type> argtypes, List<Type> typeargtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
   827
                   boolean allowBoxing, boolean useVarargs) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   828
        Symbol bestSoFar = methodNotFound;
06bc494ca11e Initial load
duke
parents:
diff changeset
   829
        Symbol sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   830
        Env<AttrContext> env1 = env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   831
        boolean staticOnly = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   832
        while (env1.outer != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   833
            if (isStatic(env1)) staticOnly = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   834
            sym = findMethod(
06bc494ca11e Initial load
duke
parents:
diff changeset
   835
                env1, env1.enclClass.sym.type, name, argtypes, typeargtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
   836
                allowBoxing, useVarargs, false);
06bc494ca11e Initial load
duke
parents:
diff changeset
   837
            if (sym.exists()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   838
                if (staticOnly &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   839
                    sym.kind == MTH &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   840
                    sym.owner.kind == TYP &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   841
                    (sym.flags() & STATIC) == 0) return new StaticError(sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   842
                else return sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   843
            } else if (sym.kind < bestSoFar.kind) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   844
                bestSoFar = sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   845
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   846
            if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   847
            env1 = env1.outer;
06bc494ca11e Initial load
duke
parents:
diff changeset
   848
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   849
06bc494ca11e Initial load
duke
parents:
diff changeset
   850
        sym = findMethod(env, syms.predefClass.type, name, argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
   851
                         typeargtypes, allowBoxing, useVarargs, false);
06bc494ca11e Initial load
duke
parents:
diff changeset
   852
        if (sym.exists())
06bc494ca11e Initial load
duke
parents:
diff changeset
   853
            return sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   854
06bc494ca11e Initial load
duke
parents:
diff changeset
   855
        Scope.Entry e = env.toplevel.namedImportScope.lookup(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   856
        for (; e.scope != null; e = e.next()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   857
            sym = e.sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   858
            Type origin = e.getOrigin().owner.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
   859
            if (sym.kind == MTH) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   860
                if (e.sym.owner.type != origin)
06bc494ca11e Initial load
duke
parents:
diff changeset
   861
                    sym = sym.clone(e.getOrigin().owner);
06bc494ca11e Initial load
duke
parents:
diff changeset
   862
                if (!isAccessible(env, origin, sym))
06bc494ca11e Initial load
duke
parents:
diff changeset
   863
                    sym = new AccessError(env, origin, sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   864
                bestSoFar = selectBest(env, origin,
06bc494ca11e Initial load
duke
parents:
diff changeset
   865
                                       argtypes, typeargtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
   866
                                       sym, bestSoFar,
06bc494ca11e Initial load
duke
parents:
diff changeset
   867
                                       allowBoxing, useVarargs, false);
06bc494ca11e Initial load
duke
parents:
diff changeset
   868
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   869
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   870
        if (bestSoFar.exists())
06bc494ca11e Initial load
duke
parents:
diff changeset
   871
            return bestSoFar;
06bc494ca11e Initial load
duke
parents:
diff changeset
   872
06bc494ca11e Initial load
duke
parents:
diff changeset
   873
        e = env.toplevel.starImportScope.lookup(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   874
        for (; e.scope != null; e = e.next()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   875
            sym = e.sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   876
            Type origin = e.getOrigin().owner.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
   877
            if (sym.kind == MTH) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   878
                if (e.sym.owner.type != origin)
06bc494ca11e Initial load
duke
parents:
diff changeset
   879
                    sym = sym.clone(e.getOrigin().owner);
06bc494ca11e Initial load
duke
parents:
diff changeset
   880
                if (!isAccessible(env, origin, sym))
06bc494ca11e Initial load
duke
parents:
diff changeset
   881
                    sym = new AccessError(env, origin, sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   882
                bestSoFar = selectBest(env, origin,
06bc494ca11e Initial load
duke
parents:
diff changeset
   883
                                       argtypes, typeargtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
   884
                                       sym, bestSoFar,
06bc494ca11e Initial load
duke
parents:
diff changeset
   885
                                       allowBoxing, useVarargs, false);
06bc494ca11e Initial load
duke
parents:
diff changeset
   886
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   887
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   888
        return bestSoFar;
06bc494ca11e Initial load
duke
parents:
diff changeset
   889
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   890
2723
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   891
    /** Find or create an implicit method of exactly the given type (after erasure).
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   892
     *  Searches in a side table, not the main scope of the site.
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   893
     *  This emulates the lookup process required by JSR 292 in JVM.
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   894
     *  @param env       The current environment.
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   895
     *  @param site      The original type from where the selection
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   896
     *                   takes place.
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   897
     *  @param name      The method's name.
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   898
     *  @param argtypes  The method's value arguments.
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   899
     *  @param typeargtypes The method's type arguments
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   900
     */
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   901
    Symbol findImplicitMethod(Env<AttrContext> env,
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   902
                              Type site,
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   903
                              Name name,
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   904
                              List<Type> argtypes,
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   905
                              List<Type> typeargtypes) {
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   906
        assert allowInvokedynamic;
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   907
        assert site == syms.invokeDynamicType || (site == syms.methodHandleType && name == names.invoke);
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   908
        ClassSymbol c = (ClassSymbol) site.tsym;
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   909
        Scope implicit = c.members().next;
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   910
        if (implicit == null) {
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   911
            c.members().next = implicit = new Scope(c);
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   912
        }
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   913
        Type restype;
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   914
        if (typeargtypes.isEmpty()) {
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   915
            restype = syms.objectType;
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   916
        } else {
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   917
            restype = typeargtypes.head;
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   918
            if (!typeargtypes.tail.isEmpty())
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   919
                return methodNotFound;
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   920
        }
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   921
        List<Type> paramtypes = Type.map(argtypes, implicitArgType);
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   922
        MethodType mtype = new MethodType(paramtypes,
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   923
                                          restype,
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   924
                                          List.<Type>nil(),
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   925
                                          syms.methodClass);
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   926
        int flags = PUBLIC | ABSTRACT;
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   927
        if (site == syms.invokeDynamicType)  flags |= STATIC;
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   928
        Symbol m = null;
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   929
        for (Scope.Entry e = implicit.lookup(name);
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   930
             e.scope != null;
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   931
             e = e.next()) {
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   932
            Symbol sym = e.sym;
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   933
            assert sym.kind == MTH;
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   934
            if (types.isSameType(mtype, sym.type)
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   935
                && (sym.flags() & STATIC) == (flags & STATIC)) {
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   936
                m = sym;
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   937
                break;
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   938
            }
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   939
        }
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   940
        if (m == null) {
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   941
            // create the desired method
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   942
            m = new MethodSymbol(flags, name, mtype, c);
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   943
            implicit.enter(m);
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   944
        }
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   945
        assert argumentsAcceptable(argtypes, types.memberType(site, m).getParameterTypes(),
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   946
                                   false, false, Warner.noWarnings);
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   947
        assert null != instantiate(env, site, m, argtypes, typeargtypes, false, false, Warner.noWarnings);
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   948
        return m;
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   949
    }
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   950
    //where
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   951
        Mapping implicitArgType = new Mapping ("implicitArgType") {
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   952
                public Type apply(Type t) { return implicitArgType(t); }
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   953
            };
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   954
        Type implicitArgType(Type argType) {
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   955
            argType = types.erasure(argType);
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   956
            if (argType.tag == BOT)
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   957
                // nulls type as the marker type Null (which has no instances)
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   958
                // TO DO: figure out how to access java.lang.Null safely, else throw nice error
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   959
                //argType = types.boxedClass(syms.botType).type;
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   960
                argType = types.boxedClass(syms.voidType).type;  // REMOVE
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   961
            return argType;
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   962
        }
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
   963
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   964
    /** Load toplevel or member class with given fully qualified name and
06bc494ca11e Initial load
duke
parents:
diff changeset
   965
     *  verify that it is accessible.
06bc494ca11e Initial load
duke
parents:
diff changeset
   966
     *  @param env       The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
   967
     *  @param name      The fully qualified name of the class to be loaded.
06bc494ca11e Initial load
duke
parents:
diff changeset
   968
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   969
    Symbol loadClass(Env<AttrContext> env, Name name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   970
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   971
            ClassSymbol c = reader.loadClass(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   972
            return isAccessible(env, c) ? c : new AccessError(c);
06bc494ca11e Initial load
duke
parents:
diff changeset
   973
        } catch (ClassReader.BadClassFile err) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   974
            throw err;
06bc494ca11e Initial load
duke
parents:
diff changeset
   975
        } catch (CompletionFailure ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   976
            return typeNotFound;
06bc494ca11e Initial load
duke
parents:
diff changeset
   977
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   978
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   979
06bc494ca11e Initial load
duke
parents:
diff changeset
   980
    /** Find qualified member type.
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 takes
06bc494ca11e Initial load
duke
parents:
diff changeset
   983
     *                   place.
06bc494ca11e Initial load
duke
parents:
diff changeset
   984
     *  @param name      The type's name.
06bc494ca11e Initial load
duke
parents:
diff changeset
   985
     *  @param c         The class to search for the member type. This is
06bc494ca11e Initial load
duke
parents:
diff changeset
   986
     *                   always a superclass or implemented interface of
06bc494ca11e Initial load
duke
parents:
diff changeset
   987
     *                   site's class.
06bc494ca11e Initial load
duke
parents:
diff changeset
   988
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   989
    Symbol findMemberType(Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
   990
                          Type site,
06bc494ca11e Initial load
duke
parents:
diff changeset
   991
                          Name name,
06bc494ca11e Initial load
duke
parents:
diff changeset
   992
                          TypeSymbol c) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   993
        Symbol bestSoFar = typeNotFound;
06bc494ca11e Initial load
duke
parents:
diff changeset
   994
        Symbol sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   995
        Scope.Entry e = c.members().lookup(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   996
        while (e.scope != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   997
            if (e.sym.kind == TYP) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   998
                return isAccessible(env, site, e.sym)
06bc494ca11e Initial load
duke
parents:
diff changeset
   999
                    ? e.sym
06bc494ca11e Initial load
duke
parents:
diff changeset
  1000
                    : new AccessError(env, site, e.sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1001
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1002
            e = e.next();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1003
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1004
        Type st = types.supertype(c.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1005
        if (st != null && st.tag == CLASS) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1006
            sym = findMemberType(env, site, name, st.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1007
            if (sym.kind < bestSoFar.kind) bestSoFar = sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1008
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1009
        for (List<Type> l = types.interfaces(c.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1010
             bestSoFar.kind != AMBIGUOUS && l.nonEmpty();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1011
             l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1012
            sym = findMemberType(env, site, name, l.head.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1013
            if (bestSoFar.kind < AMBIGUOUS && sym.kind < AMBIGUOUS &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1014
                sym.owner != bestSoFar.owner)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1015
                bestSoFar = new AmbiguityError(bestSoFar, sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1016
            else if (sym.kind < bestSoFar.kind)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1017
                bestSoFar = sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1018
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1019
        return bestSoFar;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1020
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1021
06bc494ca11e Initial load
duke
parents:
diff changeset
  1022
    /** Find a global type in given scope and load corresponding class.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1023
     *  @param env       The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1024
     *  @param scope     The scope in which to look for the type.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1025
     *  @param name      The type's name.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1026
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1027
    Symbol findGlobalType(Env<AttrContext> env, Scope scope, Name name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1028
        Symbol bestSoFar = typeNotFound;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1029
        for (Scope.Entry e = scope.lookup(name); e.scope != null; e = e.next()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1030
            Symbol sym = loadClass(env, e.sym.flatName());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1031
            if (bestSoFar.kind == TYP && sym.kind == TYP &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1032
                bestSoFar != sym)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1033
                return new AmbiguityError(bestSoFar, sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1034
            else if (sym.kind < bestSoFar.kind)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1035
                bestSoFar = sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1036
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1037
        return bestSoFar;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1038
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1039
06bc494ca11e Initial load
duke
parents:
diff changeset
  1040
    /** Find an unqualified type symbol.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1041
     *  @param env       The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1042
     *  @param name      The type's name.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1043
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1044
    Symbol findType(Env<AttrContext> env, Name name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1045
        Symbol bestSoFar = typeNotFound;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1046
        Symbol sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1047
        boolean staticOnly = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1048
        for (Env<AttrContext> env1 = env; env1.outer != null; env1 = env1.outer) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1049
            if (isStatic(env1)) staticOnly = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1050
            for (Scope.Entry e = env1.info.scope.lookup(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1051
                 e.scope != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1052
                 e = e.next()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1053
                if (e.sym.kind == TYP) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1054
                    if (staticOnly &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1055
                        e.sym.type.tag == TYPEVAR &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1056
                        e.sym.owner.kind == TYP) return new StaticError(e.sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1057
                    return e.sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1058
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1059
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1060
06bc494ca11e Initial load
duke
parents:
diff changeset
  1061
            sym = findMemberType(env1, env1.enclClass.sym.type, name,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1062
                                 env1.enclClass.sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1063
            if (staticOnly && sym.kind == TYP &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1064
                sym.type.tag == CLASS &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1065
                sym.type.getEnclosingType().tag == CLASS &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1066
                env1.enclClass.sym.type.isParameterized() &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1067
                sym.type.getEnclosingType().isParameterized())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1068
                return new StaticError(sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1069
            else if (sym.exists()) return sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1070
            else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1071
06bc494ca11e Initial load
duke
parents:
diff changeset
  1072
            JCClassDecl encl = env1.baseClause ? (JCClassDecl)env1.tree : env1.enclClass;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1073
            if ((encl.sym.flags() & STATIC) != 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1074
                staticOnly = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1075
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1076
06bc494ca11e Initial load
duke
parents:
diff changeset
  1077
        if (env.tree.getTag() != JCTree.IMPORT) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1078
            sym = findGlobalType(env, env.toplevel.namedImportScope, name);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1079
            if (sym.exists()) return sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1080
            else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1081
06bc494ca11e Initial load
duke
parents:
diff changeset
  1082
            sym = findGlobalType(env, env.toplevel.packge.members(), name);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1083
            if (sym.exists()) return sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1084
            else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1085
06bc494ca11e Initial load
duke
parents:
diff changeset
  1086
            sym = findGlobalType(env, env.toplevel.starImportScope, name);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1087
            if (sym.exists()) return sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1088
            else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1089
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1090
06bc494ca11e Initial load
duke
parents:
diff changeset
  1091
        return bestSoFar;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1092
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1093
06bc494ca11e Initial load
duke
parents:
diff changeset
  1094
    /** Find an unqualified identifier which matches a specified kind set.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1095
     *  @param env       The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1096
     *  @param name      The indentifier's name.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1097
     *  @param kind      Indicates the possible symbol kinds
06bc494ca11e Initial load
duke
parents:
diff changeset
  1098
     *                   (a subset of VAL, TYP, PCK).
06bc494ca11e Initial load
duke
parents:
diff changeset
  1099
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1100
    Symbol findIdent(Env<AttrContext> env, Name name, int kind) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1101
        Symbol bestSoFar = typeNotFound;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1102
        Symbol sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1103
06bc494ca11e Initial load
duke
parents:
diff changeset
  1104
        if ((kind & VAR) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1105
            sym = findVar(env, name);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1106
            if (sym.exists()) return sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1107
            else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1108
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1109
06bc494ca11e Initial load
duke
parents:
diff changeset
  1110
        if ((kind & TYP) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1111
            sym = findType(env, name);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1112
            if (sym.exists()) return sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1113
            else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1114
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1115
06bc494ca11e Initial load
duke
parents:
diff changeset
  1116
        if ((kind & PCK) != 0) return reader.enterPackage(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1117
        else return bestSoFar;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1118
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1119
06bc494ca11e Initial load
duke
parents:
diff changeset
  1120
    /** Find an identifier in a package which matches a specified kind set.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1121
     *  @param env       The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1122
     *  @param name      The identifier's name.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1123
     *  @param kind      Indicates the possible symbol kinds
06bc494ca11e Initial load
duke
parents:
diff changeset
  1124
     *                   (a nonempty subset of TYP, PCK).
06bc494ca11e Initial load
duke
parents:
diff changeset
  1125
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1126
    Symbol findIdentInPackage(Env<AttrContext> env, TypeSymbol pck,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1127
                              Name name, int kind) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1128
        Name fullname = TypeSymbol.formFullName(name, pck);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1129
        Symbol bestSoFar = typeNotFound;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1130
        PackageSymbol pack = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1131
        if ((kind & PCK) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1132
            pack = reader.enterPackage(fullname);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1133
            if (pack.exists()) return pack;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1134
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1135
        if ((kind & TYP) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1136
            Symbol sym = loadClass(env, fullname);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1137
            if (sym.exists()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1138
                // don't allow programs to use flatnames
06bc494ca11e Initial load
duke
parents:
diff changeset
  1139
                if (name == sym.name) return sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1140
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1141
            else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1142
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1143
        return (pack != null) ? pack : bestSoFar;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1144
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1145
06bc494ca11e Initial load
duke
parents:
diff changeset
  1146
    /** Find an identifier among the members of a given type `site'.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1147
     *  @param env       The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1148
     *  @param site      The type containing the symbol to be found.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1149
     *  @param name      The identifier's name.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1150
     *  @param kind      Indicates the possible symbol kinds
06bc494ca11e Initial load
duke
parents:
diff changeset
  1151
     *                   (a subset of VAL, TYP).
06bc494ca11e Initial load
duke
parents:
diff changeset
  1152
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1153
    Symbol findIdentInType(Env<AttrContext> env, Type site,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1154
                           Name name, int kind) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1155
        Symbol bestSoFar = typeNotFound;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1156
        Symbol sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1157
        if ((kind & VAR) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1158
            sym = findField(env, site, name, site.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1159
            if (sym.exists()) return sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1160
            else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1161
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1162
06bc494ca11e Initial load
duke
parents:
diff changeset
  1163
        if ((kind & TYP) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1164
            sym = findMemberType(env, site, name, site.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1165
            if (sym.exists()) return sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1166
            else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1167
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1168
        return bestSoFar;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1169
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1170
06bc494ca11e Initial load
duke
parents:
diff changeset
  1171
/* ***************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
  1172
 *  Access checking
06bc494ca11e Initial load
duke
parents:
diff changeset
  1173
 *  The following methods convert ResolveErrors to ErrorSymbols, issuing
06bc494ca11e Initial load
duke
parents:
diff changeset
  1174
 *  an error message in the process
06bc494ca11e Initial load
duke
parents:
diff changeset
  1175
 ****************************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
  1176
06bc494ca11e Initial load
duke
parents:
diff changeset
  1177
    /** If `sym' is a bad symbol: report error and return errSymbol
06bc494ca11e Initial load
duke
parents:
diff changeset
  1178
     *  else pass through unchanged,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1179
     *  additional arguments duplicate what has been used in trying to find the
06bc494ca11e Initial load
duke
parents:
diff changeset
  1180
     *  symbol (--> flyweight pattern). This improves performance since we
06bc494ca11e Initial load
duke
parents:
diff changeset
  1181
     *  expect misses to happen frequently.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1182
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  1183
     *  @param sym       The symbol that was found, or a ResolveError.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1184
     *  @param pos       The position to use for error reporting.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1185
     *  @param site      The original type from where the selection took place.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1186
     *  @param name      The symbol's name.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1187
     *  @param argtypes  The invocation's value arguments,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1188
     *                   if we looked for a method.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1189
     *  @param typeargtypes  The invocation's type arguments,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1190
     *                   if we looked for a method.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1191
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1192
    Symbol access(Symbol sym,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1193
                  DiagnosticPosition pos,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1194
                  Type site,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1195
                  Name name,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1196
                  boolean qualified,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1197
                  List<Type> argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1198
                  List<Type> typeargtypes) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1199
        if (sym.kind >= AMBIGUOUS) {
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1200
            ResolveError errSym = (ResolveError)sym;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1201
            if (!site.isErroneous() &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1202
                !Type.isErroneous(argtypes) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1203
                (typeargtypes==null || !Type.isErroneous(typeargtypes)))
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1204
                logResolveError(errSym, pos, site, name, argtypes, typeargtypes);
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1205
            sym = errSym.access(name, qualified ? site.tsym : syms.noSymbol);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1206
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1207
        return sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1208
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1209
06bc494ca11e Initial load
duke
parents:
diff changeset
  1210
    /** Same as above, but without type arguments and arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1211
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1212
    Symbol access(Symbol sym,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1213
                  DiagnosticPosition pos,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1214
                  Type site,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1215
                  Name name,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1216
                  boolean qualified) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1217
        if (sym.kind >= AMBIGUOUS)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1218
            return access(sym, pos, site, name, qualified, List.<Type>nil(), null);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1219
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
  1220
            return sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1221
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1222
06bc494ca11e Initial load
duke
parents:
diff changeset
  1223
    /** Check that sym is not an abstract method.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1224
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1225
    void checkNonAbstract(DiagnosticPosition pos, Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1226
        if ((sym.flags() & ABSTRACT) != 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1227
            log.error(pos, "abstract.cant.be.accessed.directly",
06bc494ca11e Initial load
duke
parents:
diff changeset
  1228
                      kindName(sym), sym, sym.location());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1229
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1230
06bc494ca11e Initial load
duke
parents:
diff changeset
  1231
/* ***************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
  1232
 *  Debugging
06bc494ca11e Initial load
duke
parents:
diff changeset
  1233
 ****************************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
  1234
06bc494ca11e Initial load
duke
parents:
diff changeset
  1235
    /** print all scopes starting with scope s and proceeding outwards.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1236
     *  used for debugging.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1237
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1238
    public void printscopes(Scope s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1239
        while (s != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1240
            if (s.owner != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1241
                System.err.print(s.owner + ": ");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1242
            for (Scope.Entry e = s.elems; e != null; e = e.sibling) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1243
                if ((e.sym.flags() & ABSTRACT) != 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1244
                    System.err.print("abstract ");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1245
                System.err.print(e.sym + " ");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1246
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1247
            System.err.println();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1248
            s = s.next;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1249
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1250
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1251
06bc494ca11e Initial load
duke
parents:
diff changeset
  1252
    void printscopes(Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1253
        while (env.outer != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1254
            System.err.println("------------------------------");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1255
            printscopes(env.info.scope);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1256
            env = env.outer;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1257
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1258
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1259
06bc494ca11e Initial load
duke
parents:
diff changeset
  1260
    public void printscopes(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1261
        while (t.tag == CLASS) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1262
            printscopes(t.tsym.members());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1263
            t = types.supertype(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1264
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1265
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1266
06bc494ca11e Initial load
duke
parents:
diff changeset
  1267
/* ***************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
  1268
 *  Name resolution
06bc494ca11e Initial load
duke
parents:
diff changeset
  1269
 *  Naming conventions are as for symbol lookup
06bc494ca11e Initial load
duke
parents:
diff changeset
  1270
 *  Unlike the find... methods these methods will report access errors
06bc494ca11e Initial load
duke
parents:
diff changeset
  1271
 ****************************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
  1272
06bc494ca11e Initial load
duke
parents:
diff changeset
  1273
    /** Resolve an unqualified (non-method) identifier.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1274
     *  @param pos       The position to use for error reporting.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1275
     *  @param env       The environment current at the identifier use.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1276
     *  @param name      The identifier's name.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1277
     *  @param kind      The set of admissible symbol kinds for the identifier.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1278
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1279
    Symbol resolveIdent(DiagnosticPosition pos, Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1280
                        Name name, int kind) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1281
        return access(
06bc494ca11e Initial load
duke
parents:
diff changeset
  1282
            findIdent(env, name, kind),
06bc494ca11e Initial load
duke
parents:
diff changeset
  1283
            pos, env.enclClass.sym.type, name, false);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1284
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1285
06bc494ca11e Initial load
duke
parents:
diff changeset
  1286
    /** Resolve an unqualified method identifier.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1287
     *  @param pos       The position to use for error reporting.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1288
     *  @param env       The environment current at the method invocation.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1289
     *  @param name      The identifier's name.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1290
     *  @param argtypes  The types of the invocation's value arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1291
     *  @param typeargtypes  The types of the invocation's type arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1292
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1293
    Symbol resolveMethod(DiagnosticPosition pos,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1294
                         Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1295
                         Name name,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1296
                         List<Type> argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1297
                         List<Type> typeargtypes) {
1533
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  1298
        Symbol sym = methodNotFound;
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  1299
        List<MethodResolutionPhase> steps = methodResolutionSteps;
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  1300
        while (steps.nonEmpty() &&
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  1301
               steps.head.isApplicable(boxingEnabled, varargsEnabled) &&
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  1302
               sym.kind >= ERRONEOUS) {
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  1303
            sym = findFun(env, name, argtypes, typeargtypes,
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  1304
                    steps.head.isBoxingRequired,
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  1305
                    env.info.varArgs = steps.head.isVarargsRequired);
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  1306
            methodResolutionCache.put(steps.head, sym);
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  1307
            steps = steps.tail;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1308
        }
1533
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  1309
        if (sym.kind >= AMBIGUOUS) {//if nothing is found return the 'first' error
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  1310
            MethodResolutionPhase errPhase =
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  1311
                    firstErroneousResolutionPhase();
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  1312
            sym = access(methodResolutionCache.get(errPhase),
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  1313
                    pos, env.enclClass.sym.type, name, false, argtypes, typeargtypes);
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  1314
            env.info.varArgs = errPhase.isVarargsRequired;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1315
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1316
        return sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1317
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1318
06bc494ca11e Initial load
duke
parents:
diff changeset
  1319
    /** Resolve a qualified method identifier
06bc494ca11e Initial load
duke
parents:
diff changeset
  1320
     *  @param pos       The position to use for error reporting.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1321
     *  @param env       The environment current at the method invocation.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1322
     *  @param site      The type of the qualifying expression, in which
06bc494ca11e Initial load
duke
parents:
diff changeset
  1323
     *                   identifier is searched.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1324
     *  @param name      The identifier's name.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1325
     *  @param argtypes  The types of the invocation's value arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1326
     *  @param typeargtypes  The types of the invocation's type arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1327
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1328
    Symbol resolveQualifiedMethod(DiagnosticPosition pos, Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1329
                                  Type site, Name name, List<Type> argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1330
                                  List<Type> typeargtypes) {
1533
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  1331
        Symbol sym = methodNotFound;
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  1332
        List<MethodResolutionPhase> steps = methodResolutionSteps;
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  1333
        while (steps.nonEmpty() &&
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  1334
               steps.head.isApplicable(boxingEnabled, varargsEnabled) &&
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  1335
               sym.kind >= ERRONEOUS) {
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  1336
            sym = findMethod(env, site, name, argtypes, typeargtypes,
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  1337
                    steps.head.isBoxingRequired(),
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  1338
                    env.info.varArgs = steps.head.isVarargsRequired(), false);
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  1339
            methodResolutionCache.put(steps.head, sym);
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  1340
            steps = steps.tail;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1341
        }
2723
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
  1342
        if (sym.kind >= AMBIGUOUS &&
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
  1343
            allowInvokedynamic &&
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
  1344
            (site == syms.invokeDynamicType ||
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
  1345
             site == syms.methodHandleType && name == names.invoke)) {
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
  1346
            // lookup failed; supply an exactly-typed implicit method
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
  1347
            sym = findImplicitMethod(env, site, name, argtypes, typeargtypes);
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
  1348
            env.info.varArgs = false;
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2511
diff changeset
  1349
        }
1533
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  1350
        if (sym.kind >= AMBIGUOUS) {//if nothing is found return the 'first' error
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  1351
            MethodResolutionPhase errPhase =
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  1352
                    firstErroneousResolutionPhase();
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  1353
            sym = access(methodResolutionCache.get(errPhase),
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  1354
                    pos, site, name, true, argtypes, typeargtypes);
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  1355
            env.info.varArgs = errPhase.isVarargsRequired;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1356
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1357
        return sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1358
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1359
06bc494ca11e Initial load
duke
parents:
diff changeset
  1360
    /** Resolve a qualified method identifier, throw a fatal error if not
06bc494ca11e Initial load
duke
parents:
diff changeset
  1361
     *  found.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1362
     *  @param pos       The position to use for error reporting.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1363
     *  @param env       The environment current at the method invocation.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1364
     *  @param site      The type of the qualifying expression, in which
06bc494ca11e Initial load
duke
parents:
diff changeset
  1365
     *                   identifier is searched.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1366
     *  @param name      The identifier's name.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1367
     *  @param argtypes  The types of the invocation's value arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1368
     *  @param typeargtypes  The types of the invocation's type arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1369
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1370
    public MethodSymbol resolveInternalMethod(DiagnosticPosition pos, Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1371
                                        Type site, Name name,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1372
                                        List<Type> argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1373
                                        List<Type> typeargtypes) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1374
        Symbol sym = resolveQualifiedMethod(
06bc494ca11e Initial load
duke
parents:
diff changeset
  1375
            pos, env, site, name, argtypes, typeargtypes);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1376
        if (sym.kind == MTH) return (MethodSymbol)sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1377
        else throw new FatalError(
1040
c0f5acfd9d15 6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents: 939
diff changeset
  1378
                 diags.fragment("fatal.err.cant.locate.meth",
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1379
                                name));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1380
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1381
06bc494ca11e Initial load
duke
parents:
diff changeset
  1382
    /** Resolve constructor.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1383
     *  @param pos       The position to use for error reporting.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1384
     *  @param env       The environment current at the constructor invocation.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1385
     *  @param site      The type of class for which a constructor is searched.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1386
     *  @param argtypes  The types of the constructor invocation's value
06bc494ca11e Initial load
duke
parents:
diff changeset
  1387
     *                   arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1388
     *  @param typeargtypes  The types of the constructor invocation's type
06bc494ca11e Initial load
duke
parents:
diff changeset
  1389
     *                   arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1390
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1391
    Symbol resolveConstructor(DiagnosticPosition pos,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1392
                              Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1393
                              Type site,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1394
                              List<Type> argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1395
                              List<Type> typeargtypes) {
1533
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  1396
        Symbol sym = methodNotFound;
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  1397
        List<MethodResolutionPhase> steps = methodResolutionSteps;
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  1398
        while (steps.nonEmpty() &&
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  1399
               steps.head.isApplicable(boxingEnabled, varargsEnabled) &&
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  1400
               sym.kind >= ERRONEOUS) {
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  1401
            sym = resolveConstructor(pos, env, site, argtypes, typeargtypes,
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  1402
                    steps.head.isBoxingRequired(),
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  1403
                    env.info.varArgs = steps.head.isVarargsRequired());
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  1404
            methodResolutionCache.put(steps.head, sym);
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  1405
            steps = steps.tail;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1406
        }
1533
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  1407
        if (sym.kind >= AMBIGUOUS) {//if nothing is found return the 'first' error
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  1408
            MethodResolutionPhase errPhase = firstErroneousResolutionPhase();
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  1409
            sym = access(methodResolutionCache.get(errPhase),
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  1410
                    pos, site, names.init, true, argtypes, typeargtypes);
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  1411
            env.info.varArgs = errPhase.isVarargsRequired();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1412
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1413
        return sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1414
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1415
5321
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  1416
    /** Resolve constructor using diamond inference.
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  1417
     *  @param pos       The position to use for error reporting.
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  1418
     *  @param env       The environment current at the constructor invocation.
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  1419
     *  @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
  1420
     *                   The scope of this class has been touched in attribution.
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  1421
     *  @param argtypes  The types of the constructor invocation's value
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  1422
     *                   arguments.
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  1423
     *  @param typeargtypes  The types of the constructor invocation's type
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  1424
     *                   arguments.
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  1425
     */
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  1426
    Symbol resolveDiamond(DiagnosticPosition pos,
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  1427
                              Env<AttrContext> env,
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  1428
                              Type site,
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  1429
                              List<Type> argtypes,
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  1430
                              List<Type> typeargtypes, boolean reportErrors) {
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  1431
        Symbol sym = methodNotFound;
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  1432
        JCDiagnostic explanation = null;
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  1433
        List<MethodResolutionPhase> steps = methodResolutionSteps;
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  1434
        while (steps.nonEmpty() &&
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  1435
               steps.head.isApplicable(boxingEnabled, varargsEnabled) &&
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  1436
               sym.kind >= ERRONEOUS) {
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  1437
            sym = resolveConstructor(pos, env, site, argtypes, typeargtypes,
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  1438
                    steps.head.isBoxingRequired(),
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  1439
                    env.info.varArgs = steps.head.isVarargsRequired());
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  1440
            methodResolutionCache.put(steps.head, sym);
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  1441
            if (sym.kind == WRONG_MTH &&
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  1442
                    ((InapplicableSymbolError)sym).explanation != null) {
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  1443
                //if the symbol is an inapplicable method symbol, then the
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  1444
                //explanation contains the reason for which inference failed
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  1445
                explanation = ((InapplicableSymbolError)sym).explanation;
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  1446
            }
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  1447
            steps = steps.tail;
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  1448
        }
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  1449
        if (sym.kind >= AMBIGUOUS && reportErrors) {
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  1450
            String key = explanation == null ?
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  1451
                "cant.apply.diamond" :
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  1452
                "cant.apply.diamond.1";
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  1453
            log.error(pos, key, diags.fragment("diamond", site.tsym), explanation);
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  1454
        }
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  1455
        return sym;
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  1456
    }
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 3372
diff changeset
  1457
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1458
    /** Resolve constructor.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1459
     *  @param pos       The position to use for error reporting.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1460
     *  @param env       The environment current at the constructor invocation.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1461
     *  @param site      The type of class for which a constructor is searched.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1462
     *  @param argtypes  The types of the constructor invocation's value
06bc494ca11e Initial load
duke
parents:
diff changeset
  1463
     *                   arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1464
     *  @param typeargtypes  The types of the constructor invocation's type
06bc494ca11e Initial load
duke
parents:
diff changeset
  1465
     *                   arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1466
     *  @param allowBoxing Allow boxing and varargs conversions.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1467
     *  @param useVarargs Box trailing arguments into an array for varargs.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1468
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1469
    Symbol resolveConstructor(DiagnosticPosition pos, Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1470
                              Type site, List<Type> argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1471
                              List<Type> typeargtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1472
                              boolean allowBoxing,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1473
                              boolean useVarargs) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1474
        Symbol sym = findMethod(env, site,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1475
                                names.init, argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1476
                                typeargtypes, allowBoxing,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1477
                                useVarargs, false);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1478
        if ((sym.flags() & DEPRECATED) != 0 &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1479
            (env.info.scope.owner.flags() & DEPRECATED) == 0 &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1480
            env.info.scope.owner.outermostClass() != sym.outermostClass())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1481
            chk.warnDeprecated(pos, sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1482
        return sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1483
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1484
06bc494ca11e Initial load
duke
parents:
diff changeset
  1485
    /** Resolve a constructor, throw a fatal error if not found.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1486
     *  @param pos       The position to use for error reporting.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1487
     *  @param env       The environment current at the method invocation.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1488
     *  @param site      The type to be constructed.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1489
     *  @param argtypes  The types of the invocation's value arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1490
     *  @param typeargtypes  The types of the invocation's type arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1491
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1492
    public MethodSymbol resolveInternalConstructor(DiagnosticPosition pos, Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1493
                                        Type site,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1494
                                        List<Type> argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1495
                                        List<Type> typeargtypes) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1496
        Symbol sym = resolveConstructor(
06bc494ca11e Initial load
duke
parents:
diff changeset
  1497
            pos, env, site, argtypes, typeargtypes);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1498
        if (sym.kind == MTH) return (MethodSymbol)sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1499
        else throw new FatalError(
1040
c0f5acfd9d15 6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents: 939
diff changeset
  1500
                 diags.fragment("fatal.err.cant.locate.ctor", site));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1501
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1502
06bc494ca11e Initial load
duke
parents:
diff changeset
  1503
    /** Resolve operator.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1504
     *  @param pos       The position to use for error reporting.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1505
     *  @param optag     The tag of the operation tree.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1506
     *  @param env       The environment current at the operation.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1507
     *  @param argtypes  The types of the operands.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1508
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1509
    Symbol resolveOperator(DiagnosticPosition pos, int optag,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1510
                           Env<AttrContext> env, List<Type> argtypes) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1511
        Name name = treeinfo.operatorName(optag);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1512
        Symbol sym = findMethod(env, syms.predefClass.type, name, argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1513
                                null, false, false, true);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1514
        if (boxingEnabled && sym.kind >= WRONG_MTHS)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1515
            sym = findMethod(env, syms.predefClass.type, name, argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1516
                             null, true, false, true);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1517
        return access(sym, pos, env.enclClass.sym.type, name,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1518
                      false, argtypes, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1519
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1520
06bc494ca11e Initial load
duke
parents:
diff changeset
  1521
    /** Resolve operator.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1522
     *  @param pos       The position to use for error reporting.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1523
     *  @param optag     The tag of the operation tree.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1524
     *  @param env       The environment current at the operation.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1525
     *  @param arg       The type of the operand.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1526
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1527
    Symbol resolveUnaryOperator(DiagnosticPosition pos, int optag, Env<AttrContext> env, Type arg) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1528
        return resolveOperator(pos, optag, env, List.of(arg));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1529
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1530
06bc494ca11e Initial load
duke
parents:
diff changeset
  1531
    /** Resolve binary operator.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1532
     *  @param pos       The position to use for error reporting.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1533
     *  @param optag     The tag of the operation tree.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1534
     *  @param env       The environment current at the operation.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1535
     *  @param left      The types of the left operand.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1536
     *  @param right     The types of the right operand.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1537
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1538
    Symbol resolveBinaryOperator(DiagnosticPosition pos,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1539
                                 int optag,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1540
                                 Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1541
                                 Type left,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1542
                                 Type right) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1543
        return resolveOperator(pos, optag, env, List.of(left, right));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1544
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1545
06bc494ca11e Initial load
duke
parents:
diff changeset
  1546
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1547
     * Resolve `c.name' where name == this or name == super.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1548
     * @param pos           The position to use for error reporting.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1549
     * @param env           The environment current at the expression.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1550
     * @param c             The qualifier.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1551
     * @param name          The identifier's name.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1552
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1553
    Symbol resolveSelf(DiagnosticPosition pos,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1554
                       Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1555
                       TypeSymbol c,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1556
                       Name name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1557
        Env<AttrContext> env1 = env;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1558
        boolean staticOnly = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1559
        while (env1.outer != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1560
            if (isStatic(env1)) staticOnly = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1561
            if (env1.enclClass.sym == c) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1562
                Symbol sym = env1.info.scope.lookup(name).sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1563
                if (sym != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1564
                    if (staticOnly) sym = new StaticError(sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1565
                    return access(sym, pos, env.enclClass.sym.type,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1566
                                  name, true);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1567
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1568
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1569
            if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1570
            env1 = env1.outer;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1571
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1572
        log.error(pos, "not.encl.class", c);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1573
        return syms.errSymbol;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1574
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1575
06bc494ca11e Initial load
duke
parents:
diff changeset
  1576
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1577
     * Resolve `c.this' for an enclosing class c that contains the
06bc494ca11e Initial load
duke
parents:
diff changeset
  1578
     * named member.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1579
     * @param pos           The position to use for error reporting.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1580
     * @param env           The environment current at the expression.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1581
     * @param member        The member that must be contained in the result.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1582
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1583
    Symbol resolveSelfContaining(DiagnosticPosition pos,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1584
                                 Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1585
                                 Symbol member) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1586
        Name name = names._this;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1587
        Env<AttrContext> env1 = env;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1588
        boolean staticOnly = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1589
        while (env1.outer != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1590
            if (isStatic(env1)) staticOnly = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1591
            if (env1.enclClass.sym.isSubClass(member.owner, types) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1592
                isAccessible(env, env1.enclClass.sym.type, member)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1593
                Symbol sym = env1.info.scope.lookup(name).sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1594
                if (sym != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1595
                    if (staticOnly) sym = new StaticError(sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1596
                    return access(sym, pos, env.enclClass.sym.type,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1597
                                  name, true);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1598
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1599
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1600
            if ((env1.enclClass.sym.flags() & STATIC) != 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1601
                staticOnly = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1602
            env1 = env1.outer;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1603
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1604
        log.error(pos, "encl.class.required", member);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1605
        return syms.errSymbol;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1606
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1607
06bc494ca11e Initial load
duke
parents:
diff changeset
  1608
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1609
     * Resolve an appropriate implicit this instance for t's container.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1610
     * JLS2 8.8.5.1 and 15.9.2
06bc494ca11e Initial load
duke
parents:
diff changeset
  1611
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1612
    Type resolveImplicitThis(DiagnosticPosition pos, Env<AttrContext> env, Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1613
        Type thisType = (((t.tsym.owner.kind & (MTH|VAR)) != 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1614
                         ? resolveSelf(pos, env, t.getEnclosingType().tsym, names._this)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1615
                         : resolveSelfContaining(pos, env, t.tsym)).type;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1616
        if (env.info.isSelfCall && thisType.tsym == env.enclClass.sym)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1617
            log.error(pos, "cant.ref.before.ctor.called", "this");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1618
        return thisType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1619
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1620
06bc494ca11e Initial load
duke
parents:
diff changeset
  1621
/* ***************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
  1622
 *  ResolveError classes, indicating error situations when accessing symbols
06bc494ca11e Initial load
duke
parents:
diff changeset
  1623
 ****************************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
  1624
06bc494ca11e Initial load
duke
parents:
diff changeset
  1625
    public void logAccessError(Env<AttrContext> env, JCTree tree, Type type) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1626
        AccessError error = new AccessError(env, type.getEnclosingType(), type.tsym);
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1627
        logResolveError(error, tree.pos(), type.getEnclosingType(), null, null, null);
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1628
    }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1629
    //where
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1630
    private void logResolveError(ResolveError error,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1631
            DiagnosticPosition pos,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1632
            Type site,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1633
            Name name,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1634
            List<Type> argtypes,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1635
            List<Type> typeargtypes) {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1636
        JCDiagnostic d = error.getDiagnostic(JCDiagnostic.DiagnosticType.ERROR,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1637
                pos, site, name, argtypes, typeargtypes);
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1638
        if (d != null)
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1639
            log.report(d);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1640
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1641
1534
e923a41e84cc 6758789: Some method resolution diagnostic should be improved
mcimadamore
parents: 1533
diff changeset
  1642
    private final LocalizedString noArgs = new LocalizedString("compiler.misc.no.args");
e923a41e84cc 6758789: Some method resolution diagnostic should be improved
mcimadamore
parents: 1533
diff changeset
  1643
e923a41e84cc 6758789: Some method resolution diagnostic should be improved
mcimadamore
parents: 1533
diff changeset
  1644
    public Object methodArguments(List<Type> argtypes) {
e923a41e84cc 6758789: Some method resolution diagnostic should be improved
mcimadamore
parents: 1533
diff changeset
  1645
        return argtypes.isEmpty() ? noArgs : argtypes;
e923a41e84cc 6758789: Some method resolution diagnostic should be improved
mcimadamore
parents: 1533
diff changeset
  1646
    }
e923a41e84cc 6758789: Some method resolution diagnostic should be improved
mcimadamore
parents: 1533
diff changeset
  1647
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1648
    /**
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1649
     * Root class for resolution errors. Subclass of ResolveError
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1650
     * represent a different kinds of resolution error - as such they must
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1651
     * specify how they map into concrete compiler diagnostics.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1652
     */
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1653
    private abstract class ResolveError extends Symbol {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1654
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1655
        /** The name of the kind of error, for debugging only. */
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1656
        final String debugName;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1657
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1658
        ResolveError(int kind, String debugName) {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1659
            super(kind, 0, null, null, null);
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1660
            this.debugName = debugName;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1661
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1662
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1663
        @Override
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1664
        public <R, P> R accept(ElementVisitor<R, P> v, P p) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1665
            throw new AssertionError();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1666
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1667
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1668
        @Override
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1669
        public String toString() {
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1670
            return debugName;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1671
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1672
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1673
        @Override
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1674
        public boolean exists() {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1675
            return false;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1676
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1677
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1678
        /**
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1679
         * Create an external representation for this erroneous symbol to be
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1680
         * used during attribution - by default this returns the symbol of a
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1681
         * brand new error type which stores the original type found
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1682
         * during resolution.
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1683
         *
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1684
         * @param name     the name used during resolution
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1685
         * @param location the location from which the symbol is accessed
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1686
         */
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1687
        protected Symbol access(Name name, TypeSymbol location) {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1688
            return types.createErrorType(name, location, syms.errSymbol.type).tsym;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1689
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1690
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1691
        /**
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1692
         * Create a diagnostic representing this resolution error.
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1693
         *
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1694
         * @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
  1695
         * @param pos       The position to be used for error reporting.
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1696
         * @param site      The original type from where the selection took place.
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1697
         * @param name      The name of the symbol to be resolved.
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1698
         * @param argtypes  The invocation's value arguments,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1699
         *                  if we looked for a method.
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1700
         * @param typeargtypes  The invocation's type arguments,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1701
         *                      if we looked for a method.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1702
         */
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1703
        abstract JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1704
                DiagnosticPosition pos,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1705
                Type site,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1706
                Name name,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1707
                List<Type> argtypes,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1708
                List<Type> typeargtypes);
939
38e24969c7e9 6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents: 735
diff changeset
  1709
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1710
        /**
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1711
         * A name designates an operator if it consists
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1712
         * of a non-empty sequence of operator symbols +-~!/*%&|^<>=
939
38e24969c7e9 6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents: 735
diff changeset
  1713
         */
38e24969c7e9 6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents: 735
diff changeset
  1714
        boolean isOperator(Name name) {
38e24969c7e9 6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents: 735
diff changeset
  1715
            int i = 0;
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 1257
diff changeset
  1716
            while (i < name.getByteLength() &&
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 1257
diff changeset
  1717
                   "+-~!*/%&|^<>=".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
  1718
            return i > 0 && i == name.getByteLength();
939
38e24969c7e9 6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents: 735
diff changeset
  1719
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1720
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1721
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1722
    /**
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1723
     * 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
  1724
     * an invalid symbol being found during resolution.
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1725
     */
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1726
    abstract class InvalidSymbolError extends ResolveError {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1727
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1728
        /** The invalid symbol found during resolution */
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1729
        Symbol sym;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1730
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1731
        InvalidSymbolError(int kind, Symbol sym, String debugName) {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1732
            super(kind, debugName);
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1733
            this.sym = sym;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1734
        }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1735
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1736
        @Override
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1737
        public boolean exists() {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1738
            return true;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1739
        }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1740
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1741
        @Override
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1742
        public String toString() {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1743
             return super.toString() + " wrongSym=" + sym;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1744
        }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1745
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1746
        @Override
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1747
        public Symbol access(Name name, TypeSymbol location) {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1748
            if (sym.kind >= AMBIGUOUS)
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1749
                return ((ResolveError)sym).access(name, location);
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1750
            else if ((sym.kind & ERRONEOUS) == 0 && (sym.kind & TYP) != 0)
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1751
                return types.createErrorType(name, location, sym.type).tsym;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1752
            else
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1753
                return sym;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1754
        }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1755
    }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1756
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1757
    /**
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1758
     * InvalidSymbolError error class indicating that a symbol matching a
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1759
     * given name does not exists in a given site.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1760
     */
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1761
    class SymbolNotFoundError extends ResolveError {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1762
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1763
        SymbolNotFoundError(int kind) {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1764
            super(kind, "symbol not found error");
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1765
        }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1766
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1767
        @Override
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1768
        JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1769
                DiagnosticPosition pos,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1770
                Type site,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1771
                Name name,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1772
                List<Type> argtypes,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1773
                List<Type> typeargtypes) {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1774
            argtypes = argtypes == null ? List.<Type>nil() : argtypes;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1775
            typeargtypes = typeargtypes == null ? List.<Type>nil() : typeargtypes;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1776
            if (name == names.error)
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1777
                return null;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1778
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1779
            if (isOperator(name)) {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1780
                return diags.create(dkind, false, log.currentSource(), pos,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1781
                        "operator.cant.be.applied", name, argtypes);
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1782
            }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1783
            boolean hasLocation = false;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1784
            if (!site.tsym.name.isEmpty()) {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1785
                if (site.tsym.kind == PCK && !site.tsym.exists()) {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1786
                    return diags.create(dkind, false, log.currentSource(), pos,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1787
                        "doesnt.exist", site.tsym);
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1788
                }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1789
                hasLocation = true;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1790
            }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1791
            boolean isConstructor = kind == ABSENT_MTH &&
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1792
                    name == names.table.names.init;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1793
            KindName kindname = isConstructor ? KindName.CONSTRUCTOR : absentKind(kind);
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1794
            Name idname = isConstructor ? site.tsym.name : name;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1795
            String errKey = getErrorKey(kindname, typeargtypes.nonEmpty(), hasLocation);
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1796
            if (hasLocation) {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1797
                return diags.create(dkind, false, log.currentSource(), pos,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1798
                        errKey, kindname, idname, //symbol kindname, name
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1799
                        typeargtypes, argtypes, //type parameters and arguments (if any)
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1800
                        typeKindName(site), site); //location kindname, type
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1801
            }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1802
            else {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1803
                return diags.create(dkind, false, log.currentSource(), pos,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1804
                        errKey, kindname, idname, //symbol kindname, name
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1805
                        typeargtypes, argtypes); //type parameters and arguments (if any)
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1806
            }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1807
        }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1808
        //where
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1809
        private String getErrorKey(KindName kindname, boolean hasTypeArgs, boolean hasLocation) {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1810
            String key = "cant.resolve";
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1811
            String suffix = hasLocation ? ".location" : "";
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1812
            switch (kindname) {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1813
                case METHOD:
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1814
                case CONSTRUCTOR: {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1815
                    suffix += ".args";
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1816
                    suffix += hasTypeArgs ? ".params" : "";
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1817
                }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1818
            }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1819
            return key + suffix;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1820
        }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1821
    }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1822
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1823
    /**
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1824
     * InvalidSymbolError error class indicating that a given symbol
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1825
     * (either a method, a constructor or an operand) is not applicable
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1826
     * given an actual arguments/type argument list.
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1827
     */
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1828
    class InapplicableSymbolError extends InvalidSymbolError {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1829
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1830
        /** An auxiliary explanation set in case of instantiation errors. */
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1831
        JCDiagnostic explanation;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1832
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1833
        InapplicableSymbolError(Symbol sym) {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1834
            super(WRONG_MTH, sym, "inapplicable symbol error");
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1835
        }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1836
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1837
        /** Update sym and explanation and return this.
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1838
         */
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1839
        InapplicableSymbolError setWrongSym(Symbol sym, JCDiagnostic explanation) {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1840
            this.sym = sym;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1841
            this.explanation = explanation;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1842
            return this;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1843
        }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1844
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1845
        /** Update sym and return this.
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1846
         */
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1847
        InapplicableSymbolError setWrongSym(Symbol sym) {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1848
            this.sym = sym;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1849
            this.explanation = null;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1850
            return this;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1851
        }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1852
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1853
        @Override
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1854
        public String toString() {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1855
            return super.toString() + " explanation=" + explanation;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1856
        }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1857
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1858
        @Override
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1859
        JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1860
                DiagnosticPosition pos,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1861
                Type site,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1862
                Name name,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1863
                List<Type> argtypes,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1864
                List<Type> typeargtypes) {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1865
            if (name == names.error)
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1866
                return null;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1867
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1868
            if (isOperator(name)) {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1869
                return diags.create(dkind, false, log.currentSource(),
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1870
                        pos, "operator.cant.be.applied", name, argtypes);
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1871
            }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1872
            else {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1873
                Symbol ws = sym.asMemberOf(site, types);
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1874
                return diags.create(dkind, false, log.currentSource(), pos,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1875
                          "cant.apply.symbol" + (explanation != null ? ".1" : ""),
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1876
                          kindName(ws),
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1877
                          ws.name == names.init ? ws.owner.name : ws.name,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1878
                          methodArguments(ws.type.getParameterTypes()),
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1879
                          methodArguments(argtypes),
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1880
                          kindName(ws.owner),
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1881
                          ws.owner.type,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1882
                          explanation);
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1883
            }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1884
        }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1885
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1886
        @Override
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1887
        public Symbol access(Name name, TypeSymbol location) {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1888
            return types.createErrorType(name, location, syms.errSymbol.type).tsym;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1889
        }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1890
    }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1891
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1892
    /**
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1893
     * ResolveError error class indicating that a set of symbols
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1894
     * (either methods, constructors or operands) is not applicable
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1895
     * given an actual arguments/type argument list.
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1896
     */
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1897
    class InapplicableSymbolsError extends ResolveError {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1898
        InapplicableSymbolsError(Symbol sym) {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1899
            super(WRONG_MTHS, "inapplicable symbols");
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1900
        }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1901
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1902
        @Override
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1903
        JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1904
                DiagnosticPosition pos,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1905
                Type site,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1906
                Name name,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1907
                List<Type> argtypes,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1908
                List<Type> typeargtypes) {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1909
            return new SymbolNotFoundError(ABSENT_MTH).getDiagnostic(dkind, pos,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1910
                    site, name, argtypes, typeargtypes);
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1911
        }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1912
    }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1913
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1914
    /**
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1915
     * An InvalidSymbolError error class indicating that a symbol is not
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1916
     * accessible from a given site
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1917
     */
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1918
    class AccessError extends InvalidSymbolError {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1919
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1920
        private Env<AttrContext> env;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1921
        private Type site;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1922
06bc494ca11e Initial load
duke
parents:
diff changeset
  1923
        AccessError(Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1924
            this(null, null, sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1925
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1926
06bc494ca11e Initial load
duke
parents:
diff changeset
  1927
        AccessError(Env<AttrContext> env, Type site, Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1928
            super(HIDDEN, sym, "access error");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1929
            this.env = env;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1930
            this.site = site;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1931
            if (debugResolve)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1932
                log.error("proc.messager", sym + " @ " + site + " is inaccessible.");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1933
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1934
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1935
        @Override
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1936
        public boolean exists() {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1937
            return false;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1938
        }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1939
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1940
        @Override
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1941
        JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1942
                DiagnosticPosition pos,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1943
                Type site,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1944
                Name name,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1945
                List<Type> argtypes,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1946
                List<Type> typeargtypes) {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1947
            if (sym.owner.type.tag == ERROR)
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1948
                return null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1949
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1950
            if (sym.name == names.init && sym.owner != site.tsym) {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1951
                return new SymbolNotFoundError(ABSENT_MTH).getDiagnostic(dkind,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1952
                        pos, site, name, argtypes, typeargtypes);
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1953
            }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1954
            else if ((sym.flags() & PUBLIC) != 0
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1955
                || (env != null && this.site != null
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1956
                    && !isAccessible(env, this.site))) {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1957
                return diags.create(dkind, false, log.currentSource(),
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1958
                        pos, "not.def.access.class.intf.cant.access",
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1959
                    sym, sym.location());
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1960
            }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1961
            else if ((sym.flags() & (PRIVATE | PROTECTED)) != 0) {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1962
                return diags.create(dkind, false, log.currentSource(),
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1963
                        pos, "report.access", sym,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1964
                        asFlagSet(sym.flags() & (PRIVATE | PROTECTED)),
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1965
                        sym.location());
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1966
            }
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1967
            else {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1968
                return diags.create(dkind, false, log.currentSource(),
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1969
                        pos, "not.def.public.cant.access", sym, sym.location());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1970
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1971
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1972
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1973
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1974
    /**
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1975
     * InvalidSymbolError error class indicating that an instance member
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1976
     * has erroneously been accessed from a static context.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1977
     */
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1978
    class StaticError extends InvalidSymbolError {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1979
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1980
        StaticError(Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1981
            super(STATICERR, sym, "static error");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1982
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1983
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1984
        @Override
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1985
        JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1986
                DiagnosticPosition pos,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1987
                Type site,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1988
                Name name,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1989
                List<Type> argtypes,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1990
                List<Type> typeargtypes) {
939
38e24969c7e9 6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents: 735
diff changeset
  1991
            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
  1992
                ? types.erasure(sym.type).tsym
38e24969c7e9 6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents: 735
diff changeset
  1993
                : sym);
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1994
            return diags.create(dkind, false, log.currentSource(), pos,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1995
                    "non-static.cant.be.ref", kindName(sym), errSym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1996
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1997
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1998
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  1999
    /**
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2000
     * InvalidSymbolError error class indicating that a pair of symbols
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2001
     * (either methods, constructors or operands) are ambiguous
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2002
     * given an actual arguments/type argument list.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2003
     */
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2004
    class AmbiguityError extends InvalidSymbolError {
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2005
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2006
        /** The other maximally specific symbol */
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2007
        Symbol sym2;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2008
06bc494ca11e Initial load
duke
parents:
diff changeset
  2009
        AmbiguityError(Symbol sym1, Symbol sym2) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2010
            super(AMBIGUOUS, sym1, "ambiguity error");
06bc494ca11e Initial load
duke
parents:
diff changeset
  2011
            this.sym2 = sym2;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2012
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2013
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2014
        @Override
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2015
        JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2016
                DiagnosticPosition pos,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2017
                Type site,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2018
                Name name,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2019
                List<Type> argtypes,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2020
                List<Type> typeargtypes) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2021
            AmbiguityError pair = this;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2022
            while (true) {
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2023
                if (pair.sym.kind == AMBIGUOUS)
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2024
                    pair = (AmbiguityError)pair.sym;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2025
                else if (pair.sym2.kind == AMBIGUOUS)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2026
                    pair = (AmbiguityError)pair.sym2;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2027
                else break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2028
            }
3143
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2029
            Name sname = pair.sym.name;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2030
            if (sname == names.init) sname = pair.sym.owner.name;
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2031
            return diags.create(dkind, false, log.currentSource(),
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2032
                      pos, "ref.ambiguous", sname,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2033
                      kindName(pair.sym),
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2034
                      pair.sym,
0413d5b5b7fd 6822637: ResolveError hierarchy needs to be refactored
mcimadamore
parents: 3140
diff changeset
  2035
                      pair.sym.location(site, types),
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2036
                      kindName(pair.sym2),
06bc494ca11e Initial load
duke
parents:
diff changeset
  2037
                      pair.sym2,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2038
                      pair.sym2.location(site, types));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2039
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2040
    }
1533
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2041
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2042
    enum MethodResolutionPhase {
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2043
        BASIC(false, false),
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2044
        BOX(true, false),
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2045
        VARARITY(true, true);
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2046
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2047
        boolean isBoxingRequired;
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2048
        boolean isVarargsRequired;
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2049
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2050
        MethodResolutionPhase(boolean isBoxingRequired, boolean isVarargsRequired) {
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2051
           this.isBoxingRequired = isBoxingRequired;
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2052
           this.isVarargsRequired = isVarargsRequired;
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2053
        }
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2054
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2055
        public boolean isBoxingRequired() {
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2056
            return isBoxingRequired;
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2057
        }
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2058
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2059
        public boolean isVarargsRequired() {
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2060
            return isVarargsRequired;
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2061
        }
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2062
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2063
        public boolean isApplicable(boolean boxingEnabled, boolean varargsEnabled) {
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2064
            return (varargsEnabled || !isVarargsRequired) &&
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2065
                   (boxingEnabled || !isBoxingRequired);
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2066
        }
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2067
    }
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2068
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2069
    private Map<MethodResolutionPhase, Symbol> methodResolutionCache =
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2070
        new HashMap<MethodResolutionPhase, Symbol>(MethodResolutionPhase.values().length);
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2071
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2072
    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
  2073
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2074
    private MethodResolutionPhase firstErroneousResolutionPhase() {
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2075
        MethodResolutionPhase bestSoFar = BASIC;
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2076
        Symbol sym = methodNotFound;
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2077
        List<MethodResolutionPhase> steps = methodResolutionSteps;
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2078
        while (steps.nonEmpty() &&
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2079
               steps.head.isApplicable(boxingEnabled, varargsEnabled) &&
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2080
               sym.kind >= WRONG_MTHS) {
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2081
            sym = methodResolutionCache.get(steps.head);
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2082
            bestSoFar = steps.head;
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2083
            steps = steps.tail;
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2084
        }
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2085
        return bestSoFar;
6a9a2f681d24 6746184: javac fails to compile call to public varargs method
mcimadamore
parents: 1529
diff changeset
  2086
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2087
}