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