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