duke@1: /*
jjg@816: * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1: *
duke@1: * This code is free software; you can redistribute it and/or modify it
duke@1: * under the terms of the GNU General Public License version 2 only, as
ohair@554: * published by the Free Software Foundation. Oracle designates this
duke@1: * particular file as subject to the "Classpath" exception as provided
ohair@554: * by Oracle in the LICENSE file that accompanied this code.
duke@1: *
duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1: * version 2 for more details (a copy is included in the LICENSE file that
duke@1: * accompanied this code).
duke@1: *
duke@1: * You should have received a copy of the GNU General Public License version
duke@1: * 2 along with this work; if not, write to the Free Software Foundation,
duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1: *
ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554: * or visit www.oracle.com if you need additional information or have any
ohair@554: * questions.
duke@1: */
duke@1:
duke@1: package com.sun.tools.javac.comp;
duke@1:
mcimadamore@1114: import com.sun.tools.javac.api.Formattable.LocalizedString;
duke@1: import com.sun.tools.javac.code.*;
mcimadamore@1114: import com.sun.tools.javac.code.Type.*;
mcimadamore@1114: import com.sun.tools.javac.code.Symbol.*;
duke@1: import com.sun.tools.javac.jvm.*;
duke@1: import com.sun.tools.javac.tree.*;
mcimadamore@1114: import com.sun.tools.javac.tree.JCTree.*;
mcimadamore@1114: import com.sun.tools.javac.util.*;
mcimadamore@1114: import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
mcimadamore@1114: import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
mcimadamore@1114: import com.sun.tools.javac.util.JCDiagnostic.DiagnosticType;
duke@1:
mcimadamore@1114: import java.util.Arrays;
mcimadamore@1114: import java.util.Collection;
mcimadamore@1114: import java.util.EnumSet;
mcimadamore@1114: import java.util.HashMap;
mcimadamore@1114: import java.util.HashSet;
mcimadamore@1114: import java.util.LinkedHashMap;
mcimadamore@1114: import java.util.Map;
mcimadamore@1114: import java.util.Set;
mcimadamore@1114:
mcimadamore@1114: import javax.lang.model.element.ElementVisitor;
duke@1:
duke@1: import static com.sun.tools.javac.code.Flags.*;
jjg@1127: import static com.sun.tools.javac.code.Flags.BLOCK;
duke@1: import static com.sun.tools.javac.code.Kinds.*;
jjg@1127: import static com.sun.tools.javac.code.Kinds.ERRONEOUS;
duke@1: import static com.sun.tools.javac.code.TypeTags.*;
mcimadamore@1114: import static com.sun.tools.javac.comp.Resolve.MethodResolutionPhase.*;
jjg@1127: import static com.sun.tools.javac.tree.JCTree.Tag.*;
mcimadamore@160:
duke@1: /** Helper class for name resolution, used mostly by the attribution phase.
duke@1: *
jjg@581: *
This is NOT part of any supported API.
jjg@581: * If you write code that depends on this, you do so at your own risk.
duke@1: * This code and its internal interfaces are subject to change or
duke@1: * deletion without notice.
duke@1: */
duke@1: public class Resolve {
duke@1: protected static final Context.Key resolveKey =
duke@1: new Context.Key();
duke@1:
jjg@113: Names names;
duke@1: Log log;
duke@1: Symtab syms;
duke@1: Check chk;
duke@1: Infer infer;
duke@1: ClassReader reader;
duke@1: TreeInfo treeinfo;
duke@1: Types types;
mcimadamore@89: JCDiagnostic.Factory diags;
duke@1: public final boolean boxingEnabled; // = source.allowBoxing();
duke@1: public final boolean varargsEnabled; // = source.allowVarargs();
mcimadamore@674: public final boolean allowMethodHandles;
duke@1: private final boolean debugResolve;
mcimadamore@1114: final EnumSet verboseResolutionMode;
duke@1:
mcimadamore@674: Scope polymorphicSignatureScope;
mcimadamore@674:
mcimadamore@1114: enum VerboseResolutionMode {
mcimadamore@1114: SUCCESS("success"),
mcimadamore@1114: FAILURE("failure"),
mcimadamore@1114: APPLICABLE("applicable"),
mcimadamore@1114: INAPPLICABLE("inapplicable"),
mcimadamore@1114: DEFERRED_INST("deferred-inference"),
mcimadamore@1114: PREDEF("predef"),
mcimadamore@1114: OBJECT_INIT("object-init"),
mcimadamore@1114: INTERNAL("internal");
mcimadamore@1114:
mcimadamore@1114: String opt;
mcimadamore@1114:
mcimadamore@1114: private VerboseResolutionMode(String opt) {
mcimadamore@1114: this.opt = opt;
mcimadamore@1114: }
mcimadamore@1114:
mcimadamore@1114: static EnumSet getVerboseResolutionMode(Options opts) {
mcimadamore@1114: String s = opts.get("verboseResolution");
mcimadamore@1114: EnumSet res = EnumSet.noneOf(VerboseResolutionMode.class);
mcimadamore@1114: if (s == null) return res;
mcimadamore@1114: if (s.contains("all")) {
mcimadamore@1114: res = EnumSet.allOf(VerboseResolutionMode.class);
mcimadamore@1114: }
mcimadamore@1114: Collection args = Arrays.asList(s.split(","));
mcimadamore@1114: for (VerboseResolutionMode mode : values()) {
mcimadamore@1114: if (args.contains(mode.opt)) {
mcimadamore@1114: res.add(mode);
mcimadamore@1114: } else if (args.contains("-" + mode.opt)) {
mcimadamore@1114: res.remove(mode);
mcimadamore@1114: }
mcimadamore@1114: }
mcimadamore@1114: return res;
mcimadamore@1114: }
mcimadamore@1114: }
mcimadamore@1114:
duke@1: public static Resolve instance(Context context) {
duke@1: Resolve instance = context.get(resolveKey);
duke@1: if (instance == null)
duke@1: instance = new Resolve(context);
duke@1: return instance;
duke@1: }
duke@1:
duke@1: protected Resolve(Context context) {
duke@1: context.put(resolveKey, this);
duke@1: syms = Symtab.instance(context);
duke@1:
duke@1: varNotFound = new
mcimadamore@302: SymbolNotFoundError(ABSENT_VAR);
duke@1: wrongMethod = new
mcimadamore@302: InapplicableSymbolError(syms.errSymbol);
duke@1: wrongMethods = new
mcimadamore@302: InapplicableSymbolsError(syms.errSymbol);
duke@1: methodNotFound = new
mcimadamore@302: SymbolNotFoundError(ABSENT_MTH);
duke@1: typeNotFound = new
mcimadamore@302: SymbolNotFoundError(ABSENT_TYP);
duke@1:
jjg@113: names = Names.instance(context);
duke@1: log = Log.instance(context);
duke@1: chk = Check.instance(context);
duke@1: infer = Infer.instance(context);
duke@1: reader = ClassReader.instance(context);
duke@1: treeinfo = TreeInfo.instance(context);
duke@1: types = Types.instance(context);
mcimadamore@89: diags = JCDiagnostic.Factory.instance(context);
duke@1: Source source = Source.instance(context);
duke@1: boxingEnabled = source.allowBoxing();
duke@1: varargsEnabled = source.allowVarargs();
duke@1: Options options = Options.instance(context);
jjg@700: debugResolve = options.isSet("debugresolve");
mcimadamore@1114: verboseResolutionMode = VerboseResolutionMode.getVerboseResolutionMode(options);
mcimadamore@674: Target target = Target.instance(context);
mcimadamore@820: allowMethodHandles = target.hasMethodHandles();
mcimadamore@674: polymorphicSignatureScope = new Scope(syms.noSymbol);
mcimadamore@689:
mcimadamore@689: inapplicableMethodException = new InapplicableMethodException(diags);
duke@1: }
duke@1:
duke@1: /** error symbols, which are returned when resolution fails
duke@1: */
mcimadamore@302: final SymbolNotFoundError varNotFound;
mcimadamore@302: final InapplicableSymbolError wrongMethod;
mcimadamore@302: final InapplicableSymbolsError wrongMethods;
mcimadamore@302: final SymbolNotFoundError methodNotFound;
mcimadamore@302: final SymbolNotFoundError typeNotFound;
duke@1:
duke@1: /* ************************************************************************
duke@1: * Identifier resolution
duke@1: *************************************************************************/
duke@1:
duke@1: /** An environment is "static" if its static level is greater than
duke@1: * the one of its outer environment
duke@1: */
duke@1: static boolean isStatic(Env env) {
duke@1: return env.info.staticLevel > env.outer.info.staticLevel;
duke@1: }
duke@1:
duke@1: /** An environment is an "initializer" if it is a constructor or
duke@1: * an instance initializer.
duke@1: */
duke@1: static boolean isInitializer(Env env) {
duke@1: Symbol owner = env.info.scope.owner;
duke@1: return owner.isConstructor() ||
duke@1: owner.owner.kind == TYP &&
duke@1: (owner.kind == VAR ||
duke@1: owner.kind == MTH && (owner.flags() & BLOCK) != 0) &&
duke@1: (owner.flags() & STATIC) == 0;
duke@1: }
duke@1:
duke@1: /** Is class accessible in given evironment?
duke@1: * @param env The current environment.
duke@1: * @param c The class whose accessibility is checked.
duke@1: */
duke@1: public boolean isAccessible(Env env, TypeSymbol c) {
mcimadamore@741: return isAccessible(env, c, false);
mcimadamore@741: }
mcimadamore@741:
mcimadamore@741: public boolean isAccessible(Env env, TypeSymbol c, boolean checkInner) {
mcimadamore@741: boolean isAccessible = false;
duke@1: switch ((short)(c.flags() & AccessFlags)) {
mcimadamore@741: case PRIVATE:
mcimadamore@741: isAccessible =
mcimadamore@741: env.enclClass.sym.outermostClass() ==
mcimadamore@741: c.owner.outermostClass();
mcimadamore@741: break;
mcimadamore@741: case 0:
mcimadamore@741: isAccessible =
mcimadamore@741: env.toplevel.packge == c.owner // fast special case
mcimadamore@741: ||
mcimadamore@741: env.toplevel.packge == c.packge()
mcimadamore@741: ||
mcimadamore@741: // Hack: this case is added since synthesized default constructors
mcimadamore@741: // of anonymous classes should be allowed to access
mcimadamore@741: // classes which would be inaccessible otherwise.
mcimadamore@741: env.enclMethod != null &&
mcimadamore@741: (env.enclMethod.mods.flags & ANONCONSTR) != 0;
mcimadamore@741: break;
mcimadamore@741: default: // error recovery
mcimadamore@741: case PUBLIC:
mcimadamore@741: isAccessible = true;
mcimadamore@741: break;
mcimadamore@741: case PROTECTED:
mcimadamore@741: isAccessible =
mcimadamore@741: env.toplevel.packge == c.owner // fast special case
mcimadamore@741: ||
mcimadamore@741: env.toplevel.packge == c.packge()
mcimadamore@741: ||
mcimadamore@741: isInnerSubClass(env.enclClass.sym, c.owner);
mcimadamore@741: break;
duke@1: }
mcimadamore@741: return (checkInner == false || c.type.getEnclosingType() == Type.noType) ?
mcimadamore@741: isAccessible :
jjg@789: isAccessible && isAccessible(env, c.type.getEnclosingType(), checkInner);
duke@1: }
duke@1: //where
duke@1: /** Is given class a subclass of given base class, or an inner class
duke@1: * of a subclass?
duke@1: * Return null if no such class exists.
duke@1: * @param c The class which is the subclass or is contained in it.
duke@1: * @param base The base class
duke@1: */
duke@1: private boolean isInnerSubClass(ClassSymbol c, Symbol base) {
duke@1: while (c != null && !c.isSubClass(base, types)) {
duke@1: c = c.owner.enclClass();
duke@1: }
duke@1: return c != null;
duke@1: }
duke@1:
duke@1: boolean isAccessible(Env env, Type t) {
mcimadamore@741: return isAccessible(env, t, false);
mcimadamore@741: }
mcimadamore@741:
mcimadamore@741: boolean isAccessible(Env env, Type t, boolean checkInner) {
duke@1: return (t.tag == ARRAY)
duke@1: ? isAccessible(env, types.elemtype(t))
mcimadamore@741: : isAccessible(env, t.tsym, checkInner);
duke@1: }
duke@1:
duke@1: /** Is symbol accessible as a member of given type in given evironment?
duke@1: * @param env The current environment.
duke@1: * @param site The type of which the tested symbol is regarded
duke@1: * as a member.
duke@1: * @param sym The symbol.
duke@1: */
duke@1: public boolean isAccessible(Env env, Type site, Symbol sym) {
mcimadamore@741: return isAccessible(env, site, sym, false);
mcimadamore@741: }
mcimadamore@741: public boolean isAccessible(Env env, Type site, Symbol sym, boolean checkInner) {
duke@1: if (sym.name == names.init && sym.owner != site.tsym) return false;
duke@1: switch ((short)(sym.flags() & AccessFlags)) {
duke@1: case PRIVATE:
duke@1: return
duke@1: (env.enclClass.sym == sym.owner // fast special case
duke@1: ||
duke@1: env.enclClass.sym.outermostClass() ==
duke@1: sym.owner.outermostClass())
duke@1: &&
duke@1: sym.isInheritedIn(site.tsym, types);
duke@1: case 0:
duke@1: return
duke@1: (env.toplevel.packge == sym.owner.owner // fast special case
duke@1: ||
duke@1: env.toplevel.packge == sym.packge())
duke@1: &&
mcimadamore@741: isAccessible(env, site, checkInner)
duke@1: &&
mcimadamore@254: sym.isInheritedIn(site.tsym, types)
mcimadamore@254: &&
mcimadamore@254: notOverriddenIn(site, sym);
duke@1: case PROTECTED:
duke@1: return
duke@1: (env.toplevel.packge == sym.owner.owner // fast special case
duke@1: ||
duke@1: env.toplevel.packge == sym.packge()
duke@1: ||
duke@1: isProtectedAccessible(sym, env.enclClass.sym, site)
duke@1: ||
duke@1: // OK to select instance method or field from 'super' or type name
duke@1: // (but type names should be disallowed elsewhere!)
duke@1: env.info.selectSuper && (sym.flags() & STATIC) == 0 && sym.kind != TYP)
duke@1: &&
mcimadamore@741: isAccessible(env, site, checkInner)
duke@1: &&
mcimadamore@254: notOverriddenIn(site, sym);
duke@1: default: // this case includes erroneous combinations as well
mcimadamore@741: return isAccessible(env, site, checkInner) && notOverriddenIn(site, sym);
mcimadamore@254: }
mcimadamore@254: }
mcimadamore@254: //where
mcimadamore@254: /* `sym' is accessible only if not overridden by
mcimadamore@254: * another symbol which is a member of `site'
mcimadamore@254: * (because, if it is overridden, `sym' is not strictly
mcimadamore@674: * speaking a member of `site'). A polymorphic signature method
mcimadamore@674: * cannot be overridden (e.g. MH.invokeExact(Object[])).
mcimadamore@254: */
mcimadamore@254: private boolean notOverriddenIn(Type site, Symbol sym) {
mcimadamore@254: if (sym.kind != MTH || sym.isConstructor() || sym.isStatic())
mcimadamore@254: return true;
mcimadamore@254: else {
mcimadamore@254: Symbol s2 = ((MethodSymbol)sym).implementation(site.tsym, types, true);
mcimadamore@844: return (s2 == null || s2 == sym || sym.owner == s2.owner ||
mcimadamore@674: s2.isPolymorphicSignatureGeneric() ||
mcimadamore@325: !types.isSubSignature(types.memberType(site, s2), types.memberType(site, sym)));
duke@1: }
duke@1: }
duke@1: //where
duke@1: /** Is given protected symbol accessible if it is selected from given site
duke@1: * and the selection takes place in given class?
duke@1: * @param sym The symbol with protected access
duke@1: * @param c The class where the access takes place
duke@1: * @site The type of the qualifier
duke@1: */
duke@1: private
duke@1: boolean isProtectedAccessible(Symbol sym, ClassSymbol c, Type site) {
duke@1: while (c != null &&
duke@1: !(c.isSubClass(sym.owner, types) &&
duke@1: (c.flags() & INTERFACE) == 0 &&
duke@1: // In JLS 2e 6.6.2.1, the subclass restriction applies
duke@1: // only to instance fields and methods -- types are excluded
duke@1: // regardless of whether they are declared 'static' or not.
duke@1: ((sym.flags() & STATIC) != 0 || sym.kind == TYP || site.tsym.isSubClass(c, types))))
duke@1: c = c.owner.enclClass();
duke@1: return c != null;
duke@1: }
duke@1:
duke@1: /** Try to instantiate the type of a method so that it fits
duke@1: * given type arguments and argument types. If succesful, return
duke@1: * the method's instantiated type, else return null.
duke@1: * The instantiation will take into account an additional leading
duke@1: * formal parameter if the method is an instance method seen as a member
duke@1: * of un underdetermined site In this case, we treat site as an additional
duke@1: * parameter and the parameters of the class containing the method as
duke@1: * additional type variables that get instantiated.
duke@1: *
duke@1: * @param env The current environment
duke@1: * @param site The type of which the method is a member.
duke@1: * @param m The method symbol.
duke@1: * @param argtypes The invocation's given value arguments.
duke@1: * @param typeargtypes The invocation's given type arguments.
duke@1: * @param allowBoxing Allow boxing conversions of arguments.
duke@1: * @param useVarargs Box trailing arguments into an array for varargs.
duke@1: */
duke@1: Type rawInstantiate(Env env,
duke@1: Type site,
duke@1: Symbol m,
duke@1: List argtypes,
duke@1: List typeargtypes,
duke@1: boolean allowBoxing,
duke@1: boolean useVarargs,
duke@1: Warner warn)
mcimadamore@299: throws Infer.InferenceException {
mcimadamore@820: boolean polymorphicSignature = m.isPolymorphicSignatureGeneric() && allowMethodHandles;
mcimadamore@689: if (useVarargs && (m.flags() & VARARGS) == 0)
mcimadamore@845: throw inapplicableMethodException.setMessage();
duke@1: Type mt = types.memberType(site, m);
duke@1:
duke@1: // tvars is the list of formal type variables for which type arguments
duke@1: // need to inferred.
mcimadamore@950: List tvars = null;
mcimadamore@950: if (env.info.tvars != null) {
mcimadamore@950: tvars = types.newInstances(env.info.tvars);
mcimadamore@950: mt = types.subst(mt, env.info.tvars, tvars);
mcimadamore@950: }
duke@1: if (typeargtypes == null) typeargtypes = List.nil();
mcimadamore@820: if (mt.tag != FORALL && typeargtypes.nonEmpty()) {
duke@1: // This is not a polymorphic method, but typeargs are supplied
jjh@972: // which is fine, see JLS 15.12.2.1
duke@1: } else if (mt.tag == FORALL && typeargtypes.nonEmpty()) {
duke@1: ForAll pmt = (ForAll) mt;
duke@1: if (typeargtypes.length() != pmt.tvars.length())
mcimadamore@689: throw inapplicableMethodException.setMessage("arg.length.mismatch"); // not enough args
duke@1: // Check type arguments are within bounds
duke@1: List formals = pmt.tvars;
duke@1: List actuals = typeargtypes;
duke@1: while (formals.nonEmpty() && actuals.nonEmpty()) {
duke@1: List bounds = types.subst(types.getBounds((TypeVar)formals.head),
duke@1: pmt.tvars, typeargtypes);
duke@1: for (; bounds.nonEmpty(); bounds = bounds.tail)
duke@1: if (!types.isSubtypeUnchecked(actuals.head, bounds.head, warn))
mcimadamore@689: throw inapplicableMethodException.setMessage("explicit.param.do.not.conform.to.bounds",actuals.head, bounds);
duke@1: formals = formals.tail;
duke@1: actuals = actuals.tail;
duke@1: }
duke@1: mt = types.subst(pmt.qtype, pmt.tvars, typeargtypes);
duke@1: } else if (mt.tag == FORALL) {
duke@1: ForAll pmt = (ForAll) mt;
duke@1: List tvars1 = types.newInstances(pmt.tvars);
duke@1: tvars = tvars.appendList(tvars1);
duke@1: mt = types.subst(pmt.qtype, pmt.tvars, tvars1);
duke@1: }
duke@1:
duke@1: // find out whether we need to go the slow route via infer
mcimadamore@674: boolean instNeeded = tvars.tail != null || /*inlined: tvars.nonEmpty()*/
mcimadamore@674: polymorphicSignature;
duke@1: for (List l = argtypes;
duke@1: l.tail != null/*inlined: l.nonEmpty()*/ && !instNeeded;
duke@1: l = l.tail) {
duke@1: if (l.head.tag == FORALL) instNeeded = true;
duke@1: }
duke@1:
duke@1: if (instNeeded)
mcimadamore@674: return polymorphicSignature ?
mcimadamore@820: infer.instantiatePolymorphicSignatureInstance(env, site, m.name, (MethodSymbol)m, argtypes) :
mcimadamore@674: infer.instantiateMethod(env,
mcimadamore@547: tvars,
duke@1: (MethodType)mt,
mcimadamore@580: m,
duke@1: argtypes,
duke@1: allowBoxing,
duke@1: useVarargs,
duke@1: warn);
mcimadamore@689:
mcimadamore@845: checkRawArgumentsAcceptable(env, argtypes, mt.getParameterTypes(),
mcimadamore@689: allowBoxing, useVarargs, warn);
mcimadamore@689: return mt;
duke@1: }
duke@1:
duke@1: /** Same but returns null instead throwing a NoInstanceException
duke@1: */
duke@1: Type instantiate(Env env,
duke@1: Type site,
duke@1: Symbol m,
duke@1: List argtypes,
duke@1: List typeargtypes,
duke@1: boolean allowBoxing,
duke@1: boolean useVarargs,
duke@1: Warner warn) {
duke@1: try {
duke@1: return rawInstantiate(env, site, m, argtypes, typeargtypes,
duke@1: allowBoxing, useVarargs, warn);
mcimadamore@689: } catch (InapplicableMethodException ex) {
duke@1: return null;
duke@1: }
duke@1: }
duke@1:
duke@1: /** Check if a parameter list accepts a list of args.
duke@1: */
mcimadamore@845: boolean argumentsAcceptable(Env env,
mcimadamore@845: List argtypes,
duke@1: List formals,
duke@1: boolean allowBoxing,
duke@1: boolean useVarargs,
duke@1: Warner warn) {
mcimadamore@689: try {
mcimadamore@845: checkRawArgumentsAcceptable(env, argtypes, formals, allowBoxing, useVarargs, warn);
mcimadamore@689: return true;
mcimadamore@689: } catch (InapplicableMethodException ex) {
mcimadamore@689: return false;
mcimadamore@689: }
mcimadamore@689: }
mcimadamore@845: void checkRawArgumentsAcceptable(Env env,
mcimadamore@845: List argtypes,
mcimadamore@689: List formals,
mcimadamore@689: boolean allowBoxing,
mcimadamore@689: boolean useVarargs,
mcimadamore@689: Warner warn) {
duke@1: Type varargsFormal = useVarargs ? formals.last() : null;
mcimadamore@689: if (varargsFormal == null &&
mcimadamore@689: argtypes.size() != formals.size()) {
mcimadamore@689: throw inapplicableMethodException.setMessage("arg.length.mismatch"); // not enough args
mcimadamore@689: }
mcimadamore@689:
duke@1: while (argtypes.nonEmpty() && formals.head != varargsFormal) {
duke@1: boolean works = allowBoxing
duke@1: ? types.isConvertible(argtypes.head, formals.head, warn)
duke@1: : types.isSubtypeUnchecked(argtypes.head, formals.head, warn);
mcimadamore@689: if (!works)
mcimadamore@689: throw inapplicableMethodException.setMessage("no.conforming.assignment.exists",
mcimadamore@689: argtypes.head,
mcimadamore@689: formals.head);
duke@1: argtypes = argtypes.tail;
duke@1: formals = formals.tail;
duke@1: }
mcimadamore@689:
mcimadamore@689: if (formals.head != varargsFormal)
mcimadamore@689: throw inapplicableMethodException.setMessage("arg.length.mismatch"); // not enough args
mcimadamore@689:
mcimadamore@689: if (useVarargs) {
mcimadamore@1006: Type elt = types.elemtype(varargsFormal);
mcimadamore@689: while (argtypes.nonEmpty()) {
mcimadamore@689: if (!types.isConvertible(argtypes.head, elt, warn))
mcimadamore@689: throw inapplicableMethodException.setMessage("varargs.argument.mismatch",
mcimadamore@689: argtypes.head,
mcimadamore@689: elt);
mcimadamore@689: argtypes = argtypes.tail;
mcimadamore@689: }
mcimadamore@845: //check varargs element type accessibility
mcimadamore@845: if (!isAccessible(env, elt)) {
mcimadamore@845: Symbol location = env.enclClass.sym;
mcimadamore@845: throw inapplicableMethodException.setMessage("inaccessible.varargs.type",
mcimadamore@845: elt,
mcimadamore@845: Kinds.kindName(location),
mcimadamore@845: location);
mcimadamore@845: }
duke@1: }
mcimadamore@689: return;
duke@1: }
mcimadamore@689: // where
mcimadamore@689: public static class InapplicableMethodException extends RuntimeException {
mcimadamore@689: private static final long serialVersionUID = 0;
mcimadamore@689:
mcimadamore@689: JCDiagnostic diagnostic;
mcimadamore@689: JCDiagnostic.Factory diags;
mcimadamore@689:
mcimadamore@689: InapplicableMethodException(JCDiagnostic.Factory diags) {
mcimadamore@689: this.diagnostic = null;
mcimadamore@689: this.diags = diags;
mcimadamore@689: }
mcimadamore@845: InapplicableMethodException setMessage() {
mcimadamore@845: this.diagnostic = null;
mcimadamore@845: return this;
mcimadamore@845: }
mcimadamore@689: InapplicableMethodException setMessage(String key) {
mcimadamore@689: this.diagnostic = key != null ? diags.fragment(key) : null;
mcimadamore@689: return this;
mcimadamore@689: }
mcimadamore@689: InapplicableMethodException setMessage(String key, Object... args) {
mcimadamore@689: this.diagnostic = key != null ? diags.fragment(key, args) : null;
mcimadamore@689: return this;
mcimadamore@689: }
mcimadamore@845: InapplicableMethodException setMessage(JCDiagnostic diag) {
mcimadamore@845: this.diagnostic = diag;
mcimadamore@845: return this;
mcimadamore@845: }
mcimadamore@689:
mcimadamore@689: public JCDiagnostic getDiagnostic() {
mcimadamore@689: return diagnostic;
mcimadamore@689: }
mcimadamore@689: }
mcimadamore@689: private final InapplicableMethodException inapplicableMethodException;
duke@1:
duke@1: /* ***************************************************************************
duke@1: * Symbol lookup
duke@1: * the following naming conventions for arguments are used
duke@1: *
duke@1: * env is the environment where the symbol was mentioned
duke@1: * site is the type of which the symbol is a member
duke@1: * name is the symbol's name
duke@1: * if no arguments are given
duke@1: * argtypes are the value arguments, if we search for a method
duke@1: *
duke@1: * If no symbol was found, a ResolveError detailing the problem is returned.
duke@1: ****************************************************************************/
duke@1:
duke@1: /** Find field. Synthetic fields are always skipped.
duke@1: * @param env The current environment.
duke@1: * @param site The original type from where the selection takes place.
duke@1: * @param name The name of the field.
duke@1: * @param c The class to search for the field. This is always
duke@1: * a superclass or implemented interface of site's class.
duke@1: */
duke@1: Symbol findField(Env env,
duke@1: Type site,
duke@1: Name name,
duke@1: TypeSymbol c) {
mcimadamore@19: while (c.type.tag == TYPEVAR)
mcimadamore@19: c = c.type.getUpperBound().tsym;
duke@1: Symbol bestSoFar = varNotFound;
duke@1: Symbol sym;
duke@1: Scope.Entry e = c.members().lookup(name);
duke@1: while (e.scope != null) {
duke@1: if (e.sym.kind == VAR && (e.sym.flags_field & SYNTHETIC) == 0) {
duke@1: return isAccessible(env, site, e.sym)
duke@1: ? e.sym : new AccessError(env, site, e.sym);
duke@1: }
duke@1: e = e.next();
duke@1: }
duke@1: Type st = types.supertype(c.type);
mcimadamore@19: if (st != null && (st.tag == CLASS || st.tag == TYPEVAR)) {
duke@1: sym = findField(env, site, name, st.tsym);
duke@1: if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1: }
duke@1: for (List l = types.interfaces(c.type);
duke@1: bestSoFar.kind != AMBIGUOUS && l.nonEmpty();
duke@1: l = l.tail) {
duke@1: sym = findField(env, site, name, l.head.tsym);
duke@1: if (bestSoFar.kind < AMBIGUOUS && sym.kind < AMBIGUOUS &&
duke@1: sym.owner != bestSoFar.owner)
duke@1: bestSoFar = new AmbiguityError(bestSoFar, sym);
duke@1: else if (sym.kind < bestSoFar.kind)
duke@1: bestSoFar = sym;
duke@1: }
duke@1: return bestSoFar;
duke@1: }
duke@1:
duke@1: /** Resolve a field identifier, throw a fatal error if not found.
duke@1: * @param pos The position to use for error reporting.
duke@1: * @param env The environment current at the method invocation.
duke@1: * @param site The type of the qualifying expression, in which
duke@1: * identifier is searched.
duke@1: * @param name The identifier's name.
duke@1: */
duke@1: public VarSymbol resolveInternalField(DiagnosticPosition pos, Env env,
duke@1: Type site, Name name) {
duke@1: Symbol sym = findField(env, site, name, site.tsym);
duke@1: if (sym.kind == VAR) return (VarSymbol)sym;
duke@1: else throw new FatalError(
mcimadamore@89: diags.fragment("fatal.err.cant.locate.field",
duke@1: name));
duke@1: }
duke@1:
duke@1: /** Find unqualified variable or field with given name.
duke@1: * Synthetic fields always skipped.
duke@1: * @param env The current environment.
duke@1: * @param name The name of the variable or field.
duke@1: */
duke@1: Symbol findVar(Env env, Name name) {
duke@1: Symbol bestSoFar = varNotFound;
duke@1: Symbol sym;
duke@1: Env env1 = env;
duke@1: boolean staticOnly = false;
duke@1: while (env1.outer != null) {
duke@1: if (isStatic(env1)) staticOnly = true;
duke@1: Scope.Entry e = env1.info.scope.lookup(name);
duke@1: while (e.scope != null &&
duke@1: (e.sym.kind != VAR ||
duke@1: (e.sym.flags_field & SYNTHETIC) != 0))
duke@1: e = e.next();
duke@1: sym = (e.scope != null)
duke@1: ? e.sym
duke@1: : findField(
duke@1: env1, env1.enclClass.sym.type, name, env1.enclClass.sym);
duke@1: if (sym.exists()) {
duke@1: if (staticOnly &&
duke@1: sym.kind == VAR &&
duke@1: sym.owner.kind == TYP &&
duke@1: (sym.flags() & STATIC) == 0)
duke@1: return new StaticError(sym);
duke@1: else
duke@1: return sym;
duke@1: } else if (sym.kind < bestSoFar.kind) {
duke@1: bestSoFar = sym;
duke@1: }
duke@1:
duke@1: if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true;
duke@1: env1 = env1.outer;
duke@1: }
duke@1:
duke@1: sym = findField(env, syms.predefClass.type, name, syms.predefClass);
duke@1: if (sym.exists())
duke@1: return sym;
duke@1: if (bestSoFar.exists())
duke@1: return bestSoFar;
duke@1:
duke@1: Scope.Entry e = env.toplevel.namedImportScope.lookup(name);
duke@1: for (; e.scope != null; e = e.next()) {
duke@1: sym = e.sym;
duke@1: Type origin = e.getOrigin().owner.type;
duke@1: if (sym.kind == VAR) {
duke@1: if (e.sym.owner.type != origin)
duke@1: sym = sym.clone(e.getOrigin().owner);
duke@1: return isAccessible(env, origin, sym)
duke@1: ? sym : new AccessError(env, origin, sym);
duke@1: }
duke@1: }
duke@1:
duke@1: Symbol origin = null;
duke@1: e = env.toplevel.starImportScope.lookup(name);
duke@1: for (; e.scope != null; e = e.next()) {
duke@1: sym = e.sym;
duke@1: if (sym.kind != VAR)
duke@1: continue;
duke@1: // invariant: sym.kind == VAR
duke@1: if (bestSoFar.kind < AMBIGUOUS && sym.owner != bestSoFar.owner)
duke@1: return new AmbiguityError(bestSoFar, sym);
duke@1: else if (bestSoFar.kind >= VAR) {
duke@1: origin = e.getOrigin().owner;
duke@1: bestSoFar = isAccessible(env, origin.type, sym)
duke@1: ? sym : new AccessError(env, origin.type, sym);
duke@1: }
duke@1: }
duke@1: if (bestSoFar.kind == VAR && bestSoFar.owner.type != origin.type)
duke@1: return bestSoFar.clone(origin);
duke@1: else
duke@1: return bestSoFar;
duke@1: }
duke@1:
duke@1: Warner noteWarner = new Warner();
duke@1:
duke@1: /** Select the best method for a call site among two choices.
duke@1: * @param env The current environment.
duke@1: * @param site The original type from where the
duke@1: * selection takes place.
duke@1: * @param argtypes The invocation's value arguments,
duke@1: * @param typeargtypes The invocation's type arguments,
duke@1: * @param sym Proposed new best match.
duke@1: * @param bestSoFar Previously found best match.
duke@1: * @param allowBoxing Allow boxing conversions of arguments.
duke@1: * @param useVarargs Box trailing arguments into an array for varargs.
duke@1: */
mcimadamore@689: @SuppressWarnings("fallthrough")
duke@1: Symbol selectBest(Env env,
duke@1: Type site,
duke@1: List argtypes,
duke@1: List typeargtypes,
duke@1: Symbol sym,
duke@1: Symbol bestSoFar,
duke@1: boolean allowBoxing,
duke@1: boolean useVarargs,
duke@1: boolean operator) {
duke@1: if (sym.kind == ERR) return bestSoFar;
mcimadamore@171: if (!sym.isInheritedIn(site.tsym, types)) return bestSoFar;
jjg@816: Assert.check(sym.kind < AMBIGUOUS);
duke@1: try {
mcimadamore@1114: Type mt = rawInstantiate(env, site, sym, argtypes, typeargtypes,
mcimadamore@689: allowBoxing, useVarargs, Warner.noWarnings);
mcimadamore@1114: if (!operator) addVerboseApplicableCandidateDiag(sym ,mt);
mcimadamore@689: } catch (InapplicableMethodException ex) {
mcimadamore@1114: if (!operator) addVerboseInapplicableCandidateDiag(sym, ex.getDiagnostic());
duke@1: switch (bestSoFar.kind) {
duke@1: case ABSENT_MTH:
duke@1: return wrongMethod.setWrongSym(sym, ex.getDiagnostic());
duke@1: case WRONG_MTH:
mcimadamore@1110: if (operator) return bestSoFar;
mcimadamore@689: wrongMethods.addCandidate(currentStep, wrongMethod.sym, wrongMethod.explanation);
mcimadamore@689: case WRONG_MTHS:
mcimadamore@689: return wrongMethods.addCandidate(currentStep, sym, ex.getDiagnostic());
duke@1: default:
duke@1: return bestSoFar;
duke@1: }
duke@1: }
duke@1: if (!isAccessible(env, site, sym)) {
duke@1: return (bestSoFar.kind == ABSENT_MTH)
duke@1: ? new AccessError(env, site, sym)
duke@1: : bestSoFar;
mcimadamore@689: }
duke@1: return (bestSoFar.kind > AMBIGUOUS)
duke@1: ? sym
duke@1: : mostSpecific(sym, bestSoFar, env, site,
duke@1: allowBoxing && operator, useVarargs);
duke@1: }
mcimadamore@1114: //where
mcimadamore@1114: void addVerboseApplicableCandidateDiag(Symbol sym, Type inst) {
mcimadamore@1114: if (!verboseResolutionMode.contains(VerboseResolutionMode.APPLICABLE))
mcimadamore@1114: return;
mcimadamore@1114:
mcimadamore@1114: JCDiagnostic subDiag = null;
mcimadamore@1114: if (inst.getReturnType().tag == FORALL) {
mcimadamore@1114: Type diagType = types.createMethodTypeWithReturn(inst.asMethodType(),
mcimadamore@1114: ((ForAll)inst.getReturnType()).qtype);
mcimadamore@1114: subDiag = diags.fragment("partial.inst.sig", diagType);
mcimadamore@1114: } else if (sym.type.tag == FORALL) {
mcimadamore@1114: subDiag = diags.fragment("full.inst.sig", inst.asMethodType());
mcimadamore@1114: }
mcimadamore@1114:
mcimadamore@1114: String key = subDiag == null ?
mcimadamore@1114: "applicable.method.found" :
mcimadamore@1114: "applicable.method.found.1";
mcimadamore@1114:
mcimadamore@1114: verboseResolutionCandidateDiags.put(sym,
mcimadamore@1114: diags.fragment(key, verboseResolutionCandidateDiags.size(), sym, subDiag));
mcimadamore@1114: }
mcimadamore@1114:
mcimadamore@1114: void addVerboseInapplicableCandidateDiag(Symbol sym, JCDiagnostic subDiag) {
mcimadamore@1114: if (!verboseResolutionMode.contains(VerboseResolutionMode.INAPPLICABLE))
mcimadamore@1114: return;
mcimadamore@1114: verboseResolutionCandidateDiags.put(sym,
mcimadamore@1114: diags.fragment("not.applicable.method.found", verboseResolutionCandidateDiags.size(), sym, subDiag));
mcimadamore@1114: }
duke@1:
duke@1: /* Return the most specific of the two methods for a call,
duke@1: * given that both are accessible and applicable.
duke@1: * @param m1 A new candidate for most specific.
duke@1: * @param m2 The previous most specific candidate.
duke@1: * @param env The current environment.
duke@1: * @param site The original type from where the selection
duke@1: * takes place.
duke@1: * @param allowBoxing Allow boxing conversions of arguments.
duke@1: * @param useVarargs Box trailing arguments into an array for varargs.
duke@1: */
duke@1: Symbol mostSpecific(Symbol m1,
duke@1: Symbol m2,
duke@1: Env env,
mcimadamore@254: final Type site,
duke@1: boolean allowBoxing,
duke@1: boolean useVarargs) {
duke@1: switch (m2.kind) {
duke@1: case MTH:
duke@1: if (m1 == m2) return m1;
mcimadamore@775: boolean m1SignatureMoreSpecific = signatureMoreSpecific(env, site, m1, m2, allowBoxing, useVarargs);
mcimadamore@775: boolean m2SignatureMoreSpecific = signatureMoreSpecific(env, site, m2, m1, allowBoxing, useVarargs);
duke@1: if (m1SignatureMoreSpecific && m2SignatureMoreSpecific) {
mcimadamore@775: Type mt1 = types.memberType(site, m1);
mcimadamore@775: Type mt2 = types.memberType(site, m2);
duke@1: if (!types.overrideEquivalent(mt1, mt2))
mcimadamore@844: return ambiguityError(m1, m2);
mcimadamore@844:
duke@1: // same signature; select (a) the non-bridge method, or
duke@1: // (b) the one that overrides the other, or (c) the concrete
duke@1: // one, or (d) merge both abstract signatures
mcimadamore@844: if ((m1.flags() & BRIDGE) != (m2.flags() & BRIDGE))
duke@1: return ((m1.flags() & BRIDGE) != 0) ? m2 : m1;
mcimadamore@844:
duke@1: // if one overrides or hides the other, use it
duke@1: TypeSymbol m1Owner = (TypeSymbol)m1.owner;
duke@1: TypeSymbol m2Owner = (TypeSymbol)m2.owner;
duke@1: if (types.asSuper(m1Owner.type, m2Owner) != null &&
duke@1: ((m1.owner.flags_field & INTERFACE) == 0 ||
duke@1: (m2.owner.flags_field & INTERFACE) != 0) &&
duke@1: m1.overrides(m2, m1Owner, types, false))
duke@1: return m1;
duke@1: if (types.asSuper(m2Owner.type, m1Owner) != null &&
duke@1: ((m2.owner.flags_field & INTERFACE) == 0 ||
duke@1: (m1.owner.flags_field & INTERFACE) != 0) &&
duke@1: m2.overrides(m1, m2Owner, types, false))
duke@1: return m2;
duke@1: boolean m1Abstract = (m1.flags() & ABSTRACT) != 0;
duke@1: boolean m2Abstract = (m2.flags() & ABSTRACT) != 0;
duke@1: if (m1Abstract && !m2Abstract) return m2;
duke@1: if (m2Abstract && !m1Abstract) return m1;
duke@1: // both abstract or both concrete
duke@1: if (!m1Abstract && !m2Abstract)
mcimadamore@844: return ambiguityError(m1, m2);
mcimadamore@156: // check that both signatures have the same erasure
mcimadamore@156: if (!types.isSameTypes(m1.erasure(types).getParameterTypes(),
mcimadamore@156: m2.erasure(types).getParameterTypes()))
mcimadamore@844: return ambiguityError(m1, m2);
duke@1: // both abstract, neither overridden; merge throws clause and result type
mcimadamore@1059: Type mst = mostSpecificReturnType(mt1, mt2);
mcimadamore@1059: if (mst == null) {
duke@1: // Theoretically, this can't happen, but it is possible
duke@1: // due to error recovery or mixing incompatible class files
mcimadamore@844: return ambiguityError(m1, m2);
duke@1: }
mcimadamore@1059: Symbol mostSpecific = mst == mt1 ? m1 : m2;
dlsmith@880: List allThrown = chk.intersect(mt1.getThrownTypes(), mt2.getThrownTypes());
dlsmith@880: Type newSig = types.createMethodTypeWithThrown(mostSpecific.type, allThrown);
mcimadamore@254: MethodSymbol result = new MethodSymbol(
mcimadamore@254: mostSpecific.flags(),
mcimadamore@254: mostSpecific.name,
dlsmith@880: newSig,
mcimadamore@254: mostSpecific.owner) {
mcimadamore@254: @Override
mcimadamore@254: public MethodSymbol implementation(TypeSymbol origin, Types types, boolean checkResult) {
mcimadamore@254: if (origin == site.tsym)
mcimadamore@254: return this;
mcimadamore@254: else
mcimadamore@254: return super.implementation(origin, types, checkResult);
mcimadamore@254: }
mcimadamore@254: };
duke@1: return result;
duke@1: }
duke@1: if (m1SignatureMoreSpecific) return m1;
duke@1: if (m2SignatureMoreSpecific) return m2;
mcimadamore@844: return ambiguityError(m1, m2);
duke@1: case AMBIGUOUS:
duke@1: AmbiguityError e = (AmbiguityError)m2;
mcimadamore@302: Symbol err1 = mostSpecific(m1, e.sym, env, site, allowBoxing, useVarargs);
duke@1: Symbol err2 = mostSpecific(m1, e.sym2, env, site, allowBoxing, useVarargs);
duke@1: if (err1 == err2) return err1;
mcimadamore@302: if (err1 == e.sym && err2 == e.sym2) return m2;
duke@1: if (err1 instanceof AmbiguityError &&
duke@1: err2 instanceof AmbiguityError &&
mcimadamore@302: ((AmbiguityError)err1).sym == ((AmbiguityError)err2).sym)
mcimadamore@844: return ambiguityError(m1, m2);
duke@1: else
mcimadamore@844: return ambiguityError(err1, err2);
duke@1: default:
duke@1: throw new AssertionError();
duke@1: }
duke@1: }
mcimadamore@775: //where
mcimadamore@775: private boolean signatureMoreSpecific(Env env, Type site, Symbol m1, Symbol m2, boolean allowBoxing, boolean useVarargs) {
mcimadamore@795: noteWarner.clear();
mcimadamore@775: Type mtype1 = types.memberType(site, adjustVarargs(m1, m2, useVarargs));
mcimadamore@1006: Type mtype2 = instantiate(env, site, adjustVarargs(m2, m1, useVarargs),
mcimadamore@1006: types.lowerBoundArgtypes(mtype1), null,
mcimadamore@1006: allowBoxing, false, noteWarner);
mcimadamore@1006: return mtype2 != null &&
mcimadamore@795: !noteWarner.hasLint(Lint.LintCategory.UNCHECKED);
mcimadamore@775: }
mcimadamore@775: //where
mcimadamore@775: private Symbol adjustVarargs(Symbol to, Symbol from, boolean useVarargs) {
mcimadamore@775: List fromArgs = from.type.getParameterTypes();
mcimadamore@775: List toArgs = to.type.getParameterTypes();
mcimadamore@775: if (useVarargs &&
mcimadamore@775: (from.flags() & VARARGS) != 0 &&
mcimadamore@775: (to.flags() & VARARGS) != 0) {
mcimadamore@775: Type varargsTypeFrom = fromArgs.last();
mcimadamore@775: Type varargsTypeTo = toArgs.last();
mcimadamore@787: ListBuffer args = ListBuffer.lb();
mcimadamore@787: if (toArgs.length() < fromArgs.length()) {
mcimadamore@787: //if we are checking a varargs method 'from' against another varargs
mcimadamore@787: //method 'to' (where arity of 'to' < arity of 'from') then expand signature
mcimadamore@787: //of 'to' to 'fit' arity of 'from' (this means adding fake formals to 'to'
mcimadamore@787: //until 'to' signature has the same arity as 'from')
mcimadamore@787: while (fromArgs.head != varargsTypeFrom) {
mcimadamore@787: args.append(toArgs.head == varargsTypeTo ? types.elemtype(varargsTypeTo) : toArgs.head);
mcimadamore@787: fromArgs = fromArgs.tail;
mcimadamore@787: toArgs = toArgs.head == varargsTypeTo ?
mcimadamore@787: toArgs :
mcimadamore@787: toArgs.tail;
mcimadamore@787: }
mcimadamore@787: } else {
mcimadamore@787: //formal argument list is same as original list where last
mcimadamore@787: //argument (array type) is removed
mcimadamore@787: args.appendList(toArgs.reverse().tail.reverse());
mcimadamore@775: }
mcimadamore@787: //append varargs element type as last synthetic formal
mcimadamore@787: args.append(types.elemtype(varargsTypeTo));
dlsmith@880: Type mtype = types.createMethodTypeWithParameters(to.type, args.toList());
mcimadamore@1006: return new MethodSymbol(to.flags_field & ~VARARGS, to.name, mtype, to.owner);
mcimadamore@775: } else {
mcimadamore@775: return to;
mcimadamore@775: }
mcimadamore@775: }
mcimadamore@844: //where
mcimadamore@1059: Type mostSpecificReturnType(Type mt1, Type mt2) {
mcimadamore@1059: Type rt1 = mt1.getReturnType();
mcimadamore@1059: Type rt2 = mt2.getReturnType();
mcimadamore@1059:
mcimadamore@1059: if (mt1.tag == FORALL && mt2.tag == FORALL) {
mcimadamore@1059: //if both are generic methods, adjust return type ahead of subtyping check
mcimadamore@1059: rt1 = types.subst(rt1, mt1.getTypeArguments(), mt2.getTypeArguments());
mcimadamore@1059: }
mcimadamore@1059: //first use subtyping, then return type substitutability
mcimadamore@1059: if (types.isSubtype(rt1, rt2)) {
mcimadamore@1059: return mt1;
mcimadamore@1059: } else if (types.isSubtype(rt2, rt1)) {
mcimadamore@1059: return mt2;
mcimadamore@1059: } else if (types.returnTypeSubstitutable(mt1, mt2)) {
mcimadamore@1059: return mt1;
mcimadamore@1059: } else if (types.returnTypeSubstitutable(mt2, mt1)) {
mcimadamore@1059: return mt2;
mcimadamore@1059: } else {
mcimadamore@1059: return null;
mcimadamore@1059: }
mcimadamore@1059: }
mcimadamore@1059: //where
mcimadamore@844: Symbol ambiguityError(Symbol m1, Symbol m2) {
mcimadamore@844: if (((m1.flags() | m2.flags()) & CLASH) != 0) {
mcimadamore@844: return (m1.flags() & CLASH) == 0 ? m1 : m2;
mcimadamore@844: } else {
mcimadamore@844: return new AmbiguityError(m1, m2);
mcimadamore@844: }
mcimadamore@844: }
duke@1:
duke@1: /** Find best qualified method matching given name, type and value
duke@1: * arguments.
duke@1: * @param env The current environment.
duke@1: * @param site The original type from where the selection
duke@1: * takes place.
duke@1: * @param name The method's name.
duke@1: * @param argtypes The method's value arguments.
duke@1: * @param typeargtypes The method's type arguments
duke@1: * @param allowBoxing Allow boxing conversions of arguments.
duke@1: * @param useVarargs Box trailing arguments into an array for varargs.
duke@1: */
duke@1: Symbol findMethod(Env env,
duke@1: Type site,
duke@1: Name name,
duke@1: List argtypes,
duke@1: List typeargtypes,
duke@1: boolean allowBoxing,
duke@1: boolean useVarargs,
duke@1: boolean operator) {
mcimadamore@1114: verboseResolutionCandidateDiags.clear();
jrose@571: Symbol bestSoFar = methodNotFound;
mcimadamore@1114: bestSoFar = findMethod(env,
duke@1: site,
duke@1: name,
duke@1: argtypes,
duke@1: typeargtypes,
duke@1: site.tsym.type,
duke@1: true,
jrose@571: bestSoFar,
duke@1: allowBoxing,
duke@1: useVarargs,
mcimadamore@913: operator,
mcimadamore@913: new HashSet());
mcimadamore@1114: reportVerboseResolutionDiagnostic(env.tree.pos(), name, site, argtypes, typeargtypes, bestSoFar);
mcimadamore@1114: return bestSoFar;
duke@1: }
duke@1: // where
duke@1: private Symbol findMethod(Env env,
duke@1: Type site,
duke@1: Name name,
duke@1: List argtypes,
duke@1: List typeargtypes,
duke@1: Type intype,
duke@1: boolean abstractok,
duke@1: Symbol bestSoFar,
duke@1: boolean allowBoxing,
duke@1: boolean useVarargs,
mcimadamore@913: boolean operator,
mcimadamore@913: Set seen) {
mcimadamore@19: for (Type ct = intype; ct.tag == CLASS || ct.tag == TYPEVAR; ct = types.supertype(ct)) {
mcimadamore@19: while (ct.tag == TYPEVAR)
mcimadamore@19: ct = ct.getUpperBound();
duke@1: ClassSymbol c = (ClassSymbol)ct.tsym;
mcimadamore@913: if (!seen.add(c)) return bestSoFar;
mcimadamore@135: if ((c.flags() & (ABSTRACT | INTERFACE | ENUM)) == 0)
duke@1: abstractok = false;
duke@1: for (Scope.Entry e = c.members().lookup(name);
duke@1: e.scope != null;
duke@1: e = e.next()) {
duke@1: //- System.out.println(" e " + e.sym);
duke@1: if (e.sym.kind == MTH &&
duke@1: (e.sym.flags_field & SYNTHETIC) == 0) {
duke@1: bestSoFar = selectBest(env, site, argtypes, typeargtypes,
duke@1: e.sym, bestSoFar,
duke@1: allowBoxing,
duke@1: useVarargs,
duke@1: operator);
duke@1: }
duke@1: }
mcimadamore@537: if (name == names.init)
mcimadamore@537: break;
duke@1: //- System.out.println(" - " + bestSoFar);
duke@1: if (abstractok) {
duke@1: Symbol concrete = methodNotFound;
duke@1: if ((bestSoFar.flags() & ABSTRACT) == 0)
duke@1: concrete = bestSoFar;
duke@1: for (List l = types.interfaces(c.type);
duke@1: l.nonEmpty();
duke@1: l = l.tail) {
duke@1: bestSoFar = findMethod(env, site, name, argtypes,
duke@1: typeargtypes,
duke@1: l.head, abstractok, bestSoFar,
mcimadamore@913: allowBoxing, useVarargs, operator, seen);
duke@1: }
duke@1: if (concrete != bestSoFar &&
duke@1: concrete.kind < ERR && bestSoFar.kind < ERR &&
duke@1: types.isSubSignature(concrete.type, bestSoFar.type))
duke@1: bestSoFar = concrete;
duke@1: }
duke@1: }
duke@1: return bestSoFar;
duke@1: }
mcimadamore@1114: //where
mcimadamore@1114: void reportVerboseResolutionDiagnostic(DiagnosticPosition dpos, Name name, Type site, List argtypes, List typeargtypes, Symbol bestSoFar) {
mcimadamore@1114: boolean success = bestSoFar.kind < ERRONEOUS;
mcimadamore@1114:
mcimadamore@1114: if (success && !verboseResolutionMode.contains(VerboseResolutionMode.SUCCESS)) {
mcimadamore@1114: return;
mcimadamore@1114: } else if (!success && !verboseResolutionMode.contains(VerboseResolutionMode.FAILURE)) {
mcimadamore@1114: return;
mcimadamore@1114: }
mcimadamore@1114:
mcimadamore@1114: if (bestSoFar.name == names.init &&
mcimadamore@1114: bestSoFar.owner == syms.objectType.tsym &&
mcimadamore@1114: !verboseResolutionMode.contains(VerboseResolutionMode.OBJECT_INIT)) {
mcimadamore@1114: return; //skip diags for Object constructor resolution
mcimadamore@1114: } else if (site == syms.predefClass.type && !verboseResolutionMode.contains(VerboseResolutionMode.PREDEF)) {
mcimadamore@1114: return; //skip spurious diags for predef symbols (i.e. operators)
mcimadamore@1114: } else if (internalResolution && !verboseResolutionMode.contains(VerboseResolutionMode.INTERNAL)) {
mcimadamore@1114: return;
mcimadamore@1114: }
mcimadamore@1114:
mcimadamore@1114: int pos = 0;
mcimadamore@1114: for (Symbol s : verboseResolutionCandidateDiags.keySet()) {
mcimadamore@1114: if (s == bestSoFar) break;
mcimadamore@1114: pos++;
mcimadamore@1114: }
mcimadamore@1114: String key = success ? "verbose.resolve.multi" : "verbose.resolve.multi.1";
mcimadamore@1114: JCDiagnostic main = diags.note(log.currentSource(), dpos, key, name, site.tsym, pos, currentStep,
mcimadamore@1114: methodArguments(argtypes), methodArguments(typeargtypes));
mcimadamore@1114: JCDiagnostic d = new JCDiagnostic.MultilineDiagnostic(main, List.from(verboseResolutionCandidateDiags.values().toArray(new JCDiagnostic[verboseResolutionCandidateDiags.size()])));
mcimadamore@1114: log.report(d);
mcimadamore@1114: }
duke@1:
duke@1: /** Find unqualified method matching given name, type and value arguments.
duke@1: * @param env The current environment.
duke@1: * @param name The method's name.
duke@1: * @param argtypes The method's value arguments.
duke@1: * @param typeargtypes The method's type arguments.
duke@1: * @param allowBoxing Allow boxing conversions of arguments.
duke@1: * @param useVarargs Box trailing arguments into an array for varargs.
duke@1: */
duke@1: Symbol findFun(Env env, Name name,
duke@1: List argtypes, List typeargtypes,
duke@1: boolean allowBoxing, boolean useVarargs) {
duke@1: Symbol bestSoFar = methodNotFound;
duke@1: Symbol sym;
duke@1: Env env1 = env;
duke@1: boolean staticOnly = false;
duke@1: while (env1.outer != null) {
duke@1: if (isStatic(env1)) staticOnly = true;
duke@1: sym = findMethod(
duke@1: env1, env1.enclClass.sym.type, name, argtypes, typeargtypes,
duke@1: allowBoxing, useVarargs, false);
duke@1: if (sym.exists()) {
duke@1: if (staticOnly &&
duke@1: sym.kind == MTH &&
duke@1: sym.owner.kind == TYP &&
duke@1: (sym.flags() & STATIC) == 0) return new StaticError(sym);
duke@1: else return sym;
duke@1: } else if (sym.kind < bestSoFar.kind) {
duke@1: bestSoFar = sym;
duke@1: }
duke@1: if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true;
duke@1: env1 = env1.outer;
duke@1: }
duke@1:
duke@1: sym = findMethod(env, syms.predefClass.type, name, argtypes,
duke@1: typeargtypes, allowBoxing, useVarargs, false);
duke@1: if (sym.exists())
duke@1: return sym;
duke@1:
duke@1: Scope.Entry e = env.toplevel.namedImportScope.lookup(name);
duke@1: for (; e.scope != null; e = e.next()) {
duke@1: sym = e.sym;
duke@1: Type origin = e.getOrigin().owner.type;
duke@1: if (sym.kind == MTH) {
duke@1: if (e.sym.owner.type != origin)
duke@1: sym = sym.clone(e.getOrigin().owner);
duke@1: if (!isAccessible(env, origin, sym))
duke@1: sym = new AccessError(env, origin, sym);
duke@1: bestSoFar = selectBest(env, origin,
duke@1: argtypes, typeargtypes,
duke@1: sym, bestSoFar,
duke@1: allowBoxing, useVarargs, false);
duke@1: }
duke@1: }
duke@1: if (bestSoFar.exists())
duke@1: return bestSoFar;
duke@1:
duke@1: e = env.toplevel.starImportScope.lookup(name);
duke@1: for (; e.scope != null; e = e.next()) {
duke@1: sym = e.sym;
duke@1: Type origin = e.getOrigin().owner.type;
duke@1: if (sym.kind == MTH) {
duke@1: if (e.sym.owner.type != origin)
duke@1: sym = sym.clone(e.getOrigin().owner);
duke@1: if (!isAccessible(env, origin, sym))
duke@1: sym = new AccessError(env, origin, sym);
duke@1: bestSoFar = selectBest(env, origin,
duke@1: argtypes, typeargtypes,
duke@1: sym, bestSoFar,
duke@1: allowBoxing, useVarargs, false);
duke@1: }
duke@1: }
duke@1: return bestSoFar;
duke@1: }
duke@1:
duke@1: /** Load toplevel or member class with given fully qualified name and
duke@1: * verify that it is accessible.
duke@1: * @param env The current environment.
duke@1: * @param name The fully qualified name of the class to be loaded.
duke@1: */
duke@1: Symbol loadClass(Env env, Name name) {
duke@1: try {
duke@1: ClassSymbol c = reader.loadClass(name);
duke@1: return isAccessible(env, c) ? c : new AccessError(c);
duke@1: } catch (ClassReader.BadClassFile err) {
duke@1: throw err;
duke@1: } catch (CompletionFailure ex) {
duke@1: return typeNotFound;
duke@1: }
duke@1: }
duke@1:
duke@1: /** Find qualified member type.
duke@1: * @param env The current environment.
duke@1: * @param site The original type from where the selection takes
duke@1: * place.
duke@1: * @param name The type's name.
duke@1: * @param c The class to search for the member type. This is
duke@1: * always a superclass or implemented interface of
duke@1: * site's class.
duke@1: */
duke@1: Symbol findMemberType(Env env,
duke@1: Type site,
duke@1: Name name,
duke@1: TypeSymbol c) {
duke@1: Symbol bestSoFar = typeNotFound;
duke@1: Symbol sym;
duke@1: Scope.Entry e = c.members().lookup(name);
duke@1: while (e.scope != null) {
duke@1: if (e.sym.kind == TYP) {
duke@1: return isAccessible(env, site, e.sym)
duke@1: ? e.sym
duke@1: : new AccessError(env, site, e.sym);
duke@1: }
duke@1: e = e.next();
duke@1: }
duke@1: Type st = types.supertype(c.type);
duke@1: if (st != null && st.tag == CLASS) {
duke@1: sym = findMemberType(env, site, name, st.tsym);
duke@1: if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1: }
duke@1: for (List l = types.interfaces(c.type);
duke@1: bestSoFar.kind != AMBIGUOUS && l.nonEmpty();
duke@1: l = l.tail) {
duke@1: sym = findMemberType(env, site, name, l.head.tsym);
duke@1: if (bestSoFar.kind < AMBIGUOUS && sym.kind < AMBIGUOUS &&
duke@1: sym.owner != bestSoFar.owner)
duke@1: bestSoFar = new AmbiguityError(bestSoFar, sym);
duke@1: else if (sym.kind < bestSoFar.kind)
duke@1: bestSoFar = sym;
duke@1: }
duke@1: return bestSoFar;
duke@1: }
duke@1:
duke@1: /** Find a global type in given scope and load corresponding class.
duke@1: * @param env The current environment.
duke@1: * @param scope The scope in which to look for the type.
duke@1: * @param name The type's name.
duke@1: */
duke@1: Symbol findGlobalType(Env env, Scope scope, Name name) {
duke@1: Symbol bestSoFar = typeNotFound;
duke@1: for (Scope.Entry e = scope.lookup(name); e.scope != null; e = e.next()) {
duke@1: Symbol sym = loadClass(env, e.sym.flatName());
duke@1: if (bestSoFar.kind == TYP && sym.kind == TYP &&
duke@1: bestSoFar != sym)
duke@1: return new AmbiguityError(bestSoFar, sym);
duke@1: else if (sym.kind < bestSoFar.kind)
duke@1: bestSoFar = sym;
duke@1: }
duke@1: return bestSoFar;
duke@1: }
duke@1:
duke@1: /** Find an unqualified type symbol.
duke@1: * @param env The current environment.
duke@1: * @param name The type's name.
duke@1: */
duke@1: Symbol findType(Env env, Name name) {
duke@1: Symbol bestSoFar = typeNotFound;
duke@1: Symbol sym;
duke@1: boolean staticOnly = false;
duke@1: for (Env env1 = env; env1.outer != null; env1 = env1.outer) {
duke@1: if (isStatic(env1)) staticOnly = true;
duke@1: for (Scope.Entry e = env1.info.scope.lookup(name);
duke@1: e.scope != null;
duke@1: e = e.next()) {
duke@1: if (e.sym.kind == TYP) {
duke@1: if (staticOnly &&
duke@1: e.sym.type.tag == TYPEVAR &&
duke@1: e.sym.owner.kind == TYP) return new StaticError(e.sym);
duke@1: return e.sym;
duke@1: }
duke@1: }
duke@1:
duke@1: sym = findMemberType(env1, env1.enclClass.sym.type, name,
duke@1: env1.enclClass.sym);
duke@1: if (staticOnly && sym.kind == TYP &&
duke@1: sym.type.tag == CLASS &&
duke@1: sym.type.getEnclosingType().tag == CLASS &&
duke@1: env1.enclClass.sym.type.isParameterized() &&
duke@1: sym.type.getEnclosingType().isParameterized())
duke@1: return new StaticError(sym);
duke@1: else if (sym.exists()) return sym;
duke@1: else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1:
duke@1: JCClassDecl encl = env1.baseClause ? (JCClassDecl)env1.tree : env1.enclClass;
duke@1: if ((encl.sym.flags() & STATIC) != 0)
duke@1: staticOnly = true;
duke@1: }
duke@1:
jjg@1127: if (!env.tree.hasTag(IMPORT)) {
duke@1: sym = findGlobalType(env, env.toplevel.namedImportScope, name);
duke@1: if (sym.exists()) return sym;
duke@1: else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1:
duke@1: sym = findGlobalType(env, env.toplevel.packge.members(), name);
duke@1: if (sym.exists()) return sym;
duke@1: else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1:
duke@1: sym = findGlobalType(env, env.toplevel.starImportScope, name);
duke@1: if (sym.exists()) return sym;
duke@1: else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1: }
duke@1:
duke@1: return bestSoFar;
duke@1: }
duke@1:
duke@1: /** Find an unqualified identifier which matches a specified kind set.
duke@1: * @param env The current environment.
duke@1: * @param name The indentifier's name.
duke@1: * @param kind Indicates the possible symbol kinds
duke@1: * (a subset of VAL, TYP, PCK).
duke@1: */
duke@1: Symbol findIdent(Env env, Name name, int kind) {
duke@1: Symbol bestSoFar = typeNotFound;
duke@1: Symbol sym;
duke@1:
duke@1: if ((kind & VAR) != 0) {
duke@1: sym = findVar(env, name);
duke@1: if (sym.exists()) return sym;
duke@1: else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1: }
duke@1:
duke@1: if ((kind & TYP) != 0) {
duke@1: sym = findType(env, name);
duke@1: if (sym.exists()) return sym;
duke@1: else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1: }
duke@1:
duke@1: if ((kind & PCK) != 0) return reader.enterPackage(name);
duke@1: else return bestSoFar;
duke@1: }
duke@1:
duke@1: /** Find an identifier in a package which matches a specified kind set.
duke@1: * @param env The current environment.
duke@1: * @param name The identifier's name.
duke@1: * @param kind Indicates the possible symbol kinds
duke@1: * (a nonempty subset of TYP, PCK).
duke@1: */
duke@1: Symbol findIdentInPackage(Env env, TypeSymbol pck,
duke@1: Name name, int kind) {
duke@1: Name fullname = TypeSymbol.formFullName(name, pck);
duke@1: Symbol bestSoFar = typeNotFound;
duke@1: PackageSymbol pack = null;
duke@1: if ((kind & PCK) != 0) {
duke@1: pack = reader.enterPackage(fullname);
duke@1: if (pack.exists()) return pack;
duke@1: }
duke@1: if ((kind & TYP) != 0) {
duke@1: Symbol sym = loadClass(env, fullname);
duke@1: if (sym.exists()) {
duke@1: // don't allow programs to use flatnames
duke@1: if (name == sym.name) return sym;
duke@1: }
duke@1: else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1: }
duke@1: return (pack != null) ? pack : bestSoFar;
duke@1: }
duke@1:
duke@1: /** Find an identifier among the members of a given type `site'.
duke@1: * @param env The current environment.
duke@1: * @param site The type containing the symbol to be found.
duke@1: * @param name The identifier's name.
duke@1: * @param kind Indicates the possible symbol kinds
duke@1: * (a subset of VAL, TYP).
duke@1: */
duke@1: Symbol findIdentInType(Env env, Type site,
duke@1: Name name, int kind) {
duke@1: Symbol bestSoFar = typeNotFound;
duke@1: Symbol sym;
duke@1: if ((kind & VAR) != 0) {
duke@1: sym = findField(env, site, name, site.tsym);
duke@1: if (sym.exists()) return sym;
duke@1: else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1: }
duke@1:
duke@1: if ((kind & TYP) != 0) {
duke@1: sym = findMemberType(env, site, name, site.tsym);
duke@1: if (sym.exists()) return sym;
duke@1: else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1: }
duke@1: return bestSoFar;
duke@1: }
duke@1:
duke@1: /* ***************************************************************************
duke@1: * Access checking
duke@1: * The following methods convert ResolveErrors to ErrorSymbols, issuing
duke@1: * an error message in the process
duke@1: ****************************************************************************/
duke@1:
duke@1: /** If `sym' is a bad symbol: report error and return errSymbol
duke@1: * else pass through unchanged,
duke@1: * additional arguments duplicate what has been used in trying to find the
duke@1: * symbol (--> flyweight pattern). This improves performance since we
duke@1: * expect misses to happen frequently.
duke@1: *
duke@1: * @param sym The symbol that was found, or a ResolveError.
duke@1: * @param pos The position to use for error reporting.
duke@1: * @param site The original type from where the selection took place.
duke@1: * @param name The symbol's name.
duke@1: * @param argtypes The invocation's value arguments,
duke@1: * if we looked for a method.
duke@1: * @param typeargtypes The invocation's type arguments,
duke@1: * if we looked for a method.
duke@1: */
duke@1: Symbol access(Symbol sym,
duke@1: DiagnosticPosition pos,
mcimadamore@829: Symbol location,
duke@1: Type site,
duke@1: Name name,
duke@1: boolean qualified,
duke@1: List argtypes,
duke@1: List typeargtypes) {
duke@1: if (sym.kind >= AMBIGUOUS) {
mcimadamore@302: ResolveError errSym = (ResolveError)sym;
duke@1: if (!site.isErroneous() &&
duke@1: !Type.isErroneous(argtypes) &&
duke@1: (typeargtypes==null || !Type.isErroneous(typeargtypes)))
mcimadamore@829: logResolveError(errSym, pos, location, site, name, argtypes, typeargtypes);
mcimadamore@302: sym = errSym.access(name, qualified ? site.tsym : syms.noSymbol);
duke@1: }
duke@1: return sym;
duke@1: }
duke@1:
mcimadamore@829: /** Same as original access(), but without location.
mcimadamore@829: */
mcimadamore@829: Symbol access(Symbol sym,
mcimadamore@829: DiagnosticPosition pos,
mcimadamore@829: Type site,
mcimadamore@829: Name name,
mcimadamore@829: boolean qualified,
mcimadamore@829: List argtypes,
mcimadamore@829: List typeargtypes) {
mcimadamore@829: return access(sym, pos, site.tsym, site, name, qualified, argtypes, typeargtypes);
mcimadamore@829: }
mcimadamore@829:
mcimadamore@829: /** Same as original access(), but without type arguments and arguments.
mcimadamore@829: */
mcimadamore@829: Symbol access(Symbol sym,
mcimadamore@829: DiagnosticPosition pos,
mcimadamore@829: Symbol location,
mcimadamore@829: Type site,
mcimadamore@829: Name name,
mcimadamore@829: boolean qualified) {
mcimadamore@829: if (sym.kind >= AMBIGUOUS)
mcimadamore@829: return access(sym, pos, location, site, name, qualified, List.nil(), null);
mcimadamore@829: else
mcimadamore@829: return sym;
mcimadamore@829: }
mcimadamore@829:
mcimadamore@829: /** Same as original access(), but without location, type arguments and arguments.
duke@1: */
duke@1: Symbol access(Symbol sym,
duke@1: DiagnosticPosition pos,
duke@1: Type site,
duke@1: Name name,
duke@1: boolean qualified) {
mcimadamore@829: return access(sym, pos, site.tsym, site, name, qualified);
duke@1: }
duke@1:
duke@1: /** Check that sym is not an abstract method.
duke@1: */
duke@1: void checkNonAbstract(DiagnosticPosition pos, Symbol sym) {
duke@1: if ((sym.flags() & ABSTRACT) != 0)
duke@1: log.error(pos, "abstract.cant.be.accessed.directly",
duke@1: kindName(sym), sym, sym.location());
duke@1: }
duke@1:
duke@1: /* ***************************************************************************
duke@1: * Debugging
duke@1: ****************************************************************************/
duke@1:
duke@1: /** print all scopes starting with scope s and proceeding outwards.
duke@1: * used for debugging.
duke@1: */
duke@1: public void printscopes(Scope s) {
duke@1: while (s != null) {
duke@1: if (s.owner != null)
duke@1: System.err.print(s.owner + ": ");
duke@1: for (Scope.Entry e = s.elems; e != null; e = e.sibling) {
duke@1: if ((e.sym.flags() & ABSTRACT) != 0)
duke@1: System.err.print("abstract ");
duke@1: System.err.print(e.sym + " ");
duke@1: }
duke@1: System.err.println();
duke@1: s = s.next;
duke@1: }
duke@1: }
duke@1:
duke@1: void printscopes(Env env) {
duke@1: while (env.outer != null) {
duke@1: System.err.println("------------------------------");
duke@1: printscopes(env.info.scope);
duke@1: env = env.outer;
duke@1: }
duke@1: }
duke@1:
duke@1: public void printscopes(Type t) {
duke@1: while (t.tag == CLASS) {
duke@1: printscopes(t.tsym.members());
duke@1: t = types.supertype(t);
duke@1: }
duke@1: }
duke@1:
duke@1: /* ***************************************************************************
duke@1: * Name resolution
duke@1: * Naming conventions are as for symbol lookup
duke@1: * Unlike the find... methods these methods will report access errors
duke@1: ****************************************************************************/
duke@1:
duke@1: /** Resolve an unqualified (non-method) identifier.
duke@1: * @param pos The position to use for error reporting.
duke@1: * @param env The environment current at the identifier use.
duke@1: * @param name The identifier's name.
duke@1: * @param kind The set of admissible symbol kinds for the identifier.
duke@1: */
duke@1: Symbol resolveIdent(DiagnosticPosition pos, Env env,
duke@1: Name name, int kind) {
duke@1: return access(
duke@1: findIdent(env, name, kind),
duke@1: pos, env.enclClass.sym.type, name, false);
duke@1: }
duke@1:
duke@1: /** Resolve an unqualified method identifier.
duke@1: * @param pos The position to use for error reporting.
duke@1: * @param env The environment current at the method invocation.
duke@1: * @param name The identifier's name.
duke@1: * @param argtypes The types of the invocation's value arguments.
duke@1: * @param typeargtypes The types of the invocation's type arguments.
duke@1: */
duke@1: Symbol resolveMethod(DiagnosticPosition pos,
duke@1: Env env,
duke@1: Name name,
duke@1: List argtypes,
duke@1: List typeargtypes) {
mcimadamore@689: Symbol sym = startResolution();
mcimadamore@160: List steps = methodResolutionSteps;
mcimadamore@160: while (steps.nonEmpty() &&
mcimadamore@160: steps.head.isApplicable(boxingEnabled, varargsEnabled) &&
mcimadamore@160: sym.kind >= ERRONEOUS) {
mcimadamore@689: currentStep = steps.head;
mcimadamore@160: sym = findFun(env, name, argtypes, typeargtypes,
mcimadamore@160: steps.head.isBoxingRequired,
mcimadamore@160: env.info.varArgs = steps.head.isVarargsRequired);
mcimadamore@160: methodResolutionCache.put(steps.head, sym);
mcimadamore@160: steps = steps.tail;
duke@1: }
mcimadamore@160: if (sym.kind >= AMBIGUOUS) {//if nothing is found return the 'first' error
mcimadamore@160: MethodResolutionPhase errPhase =
mcimadamore@160: firstErroneousResolutionPhase();
mcimadamore@160: sym = access(methodResolutionCache.get(errPhase),
mcimadamore@160: pos, env.enclClass.sym.type, name, false, argtypes, typeargtypes);
mcimadamore@160: env.info.varArgs = errPhase.isVarargsRequired;
duke@1: }
duke@1: return sym;
duke@1: }
duke@1:
mcimadamore@689: private Symbol startResolution() {
mcimadamore@689: wrongMethod.clear();
mcimadamore@689: wrongMethods.clear();
mcimadamore@689: return methodNotFound;
mcimadamore@689: }
mcimadamore@689:
duke@1: /** Resolve a qualified method identifier
duke@1: * @param pos The position to use for error reporting.
duke@1: * @param env The environment current at the method invocation.
duke@1: * @param site The type of the qualifying expression, in which
duke@1: * identifier is searched.
duke@1: * @param name The identifier's name.
duke@1: * @param argtypes The types of the invocation's value arguments.
duke@1: * @param typeargtypes The types of the invocation's type arguments.
duke@1: */
duke@1: Symbol resolveQualifiedMethod(DiagnosticPosition pos, Env env,
duke@1: Type site, Name name, List argtypes,
duke@1: List typeargtypes) {
mcimadamore@829: return resolveQualifiedMethod(pos, env, site.tsym, site, name, argtypes, typeargtypes);
mcimadamore@829: }
mcimadamore@829: Symbol resolveQualifiedMethod(DiagnosticPosition pos, Env env,
mcimadamore@829: Symbol location, Type site, Name name, List argtypes,
mcimadamore@829: List typeargtypes) {
mcimadamore@689: Symbol sym = startResolution();
mcimadamore@160: List steps = methodResolutionSteps;
mcimadamore@160: while (steps.nonEmpty() &&
mcimadamore@160: steps.head.isApplicable(boxingEnabled, varargsEnabled) &&
mcimadamore@160: sym.kind >= ERRONEOUS) {
mcimadamore@689: currentStep = steps.head;
mcimadamore@160: sym = findMethod(env, site, name, argtypes, typeargtypes,
mcimadamore@160: steps.head.isBoxingRequired(),
mcimadamore@160: env.info.varArgs = steps.head.isVarargsRequired(), false);
mcimadamore@160: methodResolutionCache.put(steps.head, sym);
mcimadamore@160: steps = steps.tail;
duke@1: }
mcimadamore@674: if (sym.kind >= AMBIGUOUS) {
mcimadamore@820: if (site.tsym.isPolymorphicSignatureGeneric()) {
mcimadamore@674: //polymorphic receiver - synthesize new method symbol
mcimadamore@674: env.info.varArgs = false;
mcimadamore@674: sym = findPolymorphicSignatureInstance(env,
mcimadamore@820: site, name, null, argtypes);
mcimadamore@674: }
mcimadamore@674: else {
mcimadamore@674: //if nothing is found return the 'first' error
mcimadamore@674: MethodResolutionPhase errPhase =
mcimadamore@674: firstErroneousResolutionPhase();
mcimadamore@674: sym = access(methodResolutionCache.get(errPhase),
mcimadamore@829: pos, location, site, name, true, argtypes, typeargtypes);
mcimadamore@674: env.info.varArgs = errPhase.isVarargsRequired;
mcimadamore@674: }
mcimadamore@674: } else if (allowMethodHandles && sym.isPolymorphicSignatureGeneric()) {
mcimadamore@674: //non-instantiated polymorphic signature - synthesize new method symbol
mcimadamore@674: env.info.varArgs = false;
mcimadamore@674: sym = findPolymorphicSignatureInstance(env,
mcimadamore@820: site, name, (MethodSymbol)sym, argtypes);
duke@1: }
duke@1: return sym;
duke@1: }
duke@1:
mcimadamore@674: /** Find or create an implicit method of exactly the given type (after erasure).
mcimadamore@674: * Searches in a side table, not the main scope of the site.
mcimadamore@674: * This emulates the lookup process required by JSR 292 in JVM.
mcimadamore@674: * @param env Attribution environment
mcimadamore@674: * @param site The original type from where the selection takes place.
mcimadamore@674: * @param name The method's name.
mcimadamore@674: * @param spMethod A template for the implicit method, or null.
mcimadamore@674: * @param argtypes The required argument types.
mcimadamore@674: * @param typeargtypes The required type arguments.
mcimadamore@674: */
mcimadamore@674: Symbol findPolymorphicSignatureInstance(Env env, Type site,
mcimadamore@674: Name name,
mcimadamore@674: MethodSymbol spMethod, // sig. poly. method or null if none
mcimadamore@820: List argtypes) {
mcimadamore@674: Type mtype = infer.instantiatePolymorphicSignatureInstance(env,
mcimadamore@820: site, name, spMethod, argtypes);
mcimadamore@674: long flags = ABSTRACT | HYPOTHETICAL | POLYMORPHIC_SIGNATURE |
mcimadamore@674: (spMethod != null ?
mcimadamore@674: spMethod.flags() & Flags.AccessFlags :
mcimadamore@674: Flags.PUBLIC | Flags.STATIC);
mcimadamore@674: Symbol m = null;
mcimadamore@674: for (Scope.Entry e = polymorphicSignatureScope.lookup(name);
mcimadamore@674: e.scope != null;
mcimadamore@674: e = e.next()) {
mcimadamore@674: Symbol sym = e.sym;
mcimadamore@674: if (types.isSameType(mtype, sym.type) &&
mcimadamore@674: (sym.flags() & Flags.STATIC) == (flags & Flags.STATIC) &&
mcimadamore@674: types.isSameType(sym.owner.type, site)) {
mcimadamore@674: m = sym;
mcimadamore@674: break;
mcimadamore@674: }
mcimadamore@674: }
mcimadamore@674: if (m == null) {
mcimadamore@674: // create the desired method
mcimadamore@674: m = new MethodSymbol(flags, name, mtype, site.tsym);
mcimadamore@674: polymorphicSignatureScope.enter(m);
mcimadamore@674: }
mcimadamore@674: return m;
mcimadamore@674: }
mcimadamore@674:
duke@1: /** Resolve a qualified method identifier, throw a fatal error if not
duke@1: * found.
duke@1: * @param pos The position to use for error reporting.
duke@1: * @param env The environment current at the method invocation.
duke@1: * @param site The type of the qualifying expression, in which
duke@1: * identifier is searched.
duke@1: * @param name The identifier's name.
duke@1: * @param argtypes The types of the invocation's value arguments.
duke@1: * @param typeargtypes The types of the invocation's type arguments.
duke@1: */
duke@1: public MethodSymbol resolveInternalMethod(DiagnosticPosition pos, Env env,
duke@1: Type site, Name name,
duke@1: List argtypes,
duke@1: List typeargtypes) {
mcimadamore@1114: boolean prevInternal = internalResolution;
mcimadamore@1114: try {
mcimadamore@1114: internalResolution = true;
mcimadamore@1114: Symbol sym = resolveQualifiedMethod(
mcimadamore@1114: pos, env, site.tsym, site, name, argtypes, typeargtypes);
mcimadamore@1114: if (sym.kind == MTH) return (MethodSymbol)sym;
mcimadamore@1114: else throw new FatalError(
mcimadamore@1114: diags.fragment("fatal.err.cant.locate.meth",
mcimadamore@1114: name));
mcimadamore@1114: }
mcimadamore@1114: finally {
mcimadamore@1114: internalResolution = prevInternal;
mcimadamore@1114: }
duke@1: }
duke@1:
duke@1: /** Resolve constructor.
duke@1: * @param pos The position to use for error reporting.
duke@1: * @param env The environment current at the constructor invocation.
duke@1: * @param site The type of class for which a constructor is searched.
duke@1: * @param argtypes The types of the constructor invocation's value
duke@1: * arguments.
duke@1: * @param typeargtypes The types of the constructor invocation's type
duke@1: * arguments.
duke@1: */
duke@1: Symbol resolveConstructor(DiagnosticPosition pos,
duke@1: Env env,
duke@1: Type site,
duke@1: List argtypes,
duke@1: List typeargtypes) {
mcimadamore@689: Symbol sym = startResolution();
mcimadamore@160: List steps = methodResolutionSteps;
mcimadamore@160: while (steps.nonEmpty() &&
mcimadamore@160: steps.head.isApplicable(boxingEnabled, varargsEnabled) &&
mcimadamore@160: sym.kind >= ERRONEOUS) {
mcimadamore@689: currentStep = steps.head;
mcimadamore@160: sym = resolveConstructor(pos, env, site, argtypes, typeargtypes,
mcimadamore@160: steps.head.isBoxingRequired(),
mcimadamore@160: env.info.varArgs = steps.head.isVarargsRequired());
mcimadamore@160: methodResolutionCache.put(steps.head, sym);
mcimadamore@160: steps = steps.tail;
duke@1: }
mcimadamore@160: if (sym.kind >= AMBIGUOUS) {//if nothing is found return the 'first' error
mcimadamore@160: MethodResolutionPhase errPhase = firstErroneousResolutionPhase();
mcimadamore@160: sym = access(methodResolutionCache.get(errPhase),
mcimadamore@160: pos, site, names.init, true, argtypes, typeargtypes);
mcimadamore@160: env.info.varArgs = errPhase.isVarargsRequired();
duke@1: }
duke@1: return sym;
duke@1: }
duke@1:
mcimadamore@537: /** Resolve constructor using diamond inference.
mcimadamore@537: * @param pos The position to use for error reporting.
mcimadamore@537: * @param env The environment current at the constructor invocation.
mcimadamore@537: * @param site The type of class for which a constructor is searched.
mcimadamore@537: * The scope of this class has been touched in attribution.
mcimadamore@537: * @param argtypes The types of the constructor invocation's value
mcimadamore@537: * arguments.
mcimadamore@537: * @param typeargtypes The types of the constructor invocation's type
mcimadamore@537: * arguments.
mcimadamore@537: */
mcimadamore@537: Symbol resolveDiamond(DiagnosticPosition pos,
mcimadamore@537: Env env,
mcimadamore@537: Type site,
mcimadamore@537: List argtypes,
mcimadamore@631: List typeargtypes) {
mcimadamore@689: Symbol sym = startResolution();
mcimadamore@537: List steps = methodResolutionSteps;
mcimadamore@537: while (steps.nonEmpty() &&
mcimadamore@537: steps.head.isApplicable(boxingEnabled, varargsEnabled) &&
mcimadamore@537: sym.kind >= ERRONEOUS) {
mcimadamore@689: currentStep = steps.head;
mcimadamore@537: sym = resolveConstructor(pos, env, site, argtypes, typeargtypes,
mcimadamore@537: steps.head.isBoxingRequired(),
mcimadamore@537: env.info.varArgs = steps.head.isVarargsRequired());
mcimadamore@537: methodResolutionCache.put(steps.head, sym);
mcimadamore@537: steps = steps.tail;
mcimadamore@537: }
mcimadamore@631: if (sym.kind >= AMBIGUOUS) {
mcimadamore@689: final JCDiagnostic details = sym.kind == WRONG_MTH ?
mcimadamore@689: ((InapplicableSymbolError)sym).explanation :
mcimadamore@689: null;
mcimadamore@631: Symbol errSym = new ResolveError(WRONG_MTH, "diamond error") {
mcimadamore@631: @Override
mcimadamore@829: JCDiagnostic getDiagnostic(DiagnosticType dkind, DiagnosticPosition pos,
mcimadamore@829: Symbol location, Type site, Name name, List argtypes, List typeargtypes) {
mcimadamore@631: String key = details == null ?
mcimadamore@631: "cant.apply.diamond" :
mcimadamore@631: "cant.apply.diamond.1";
mcimadamore@829: return diags.create(dkind, log.currentSource(), pos, key,
mcimadamore@829: diags.fragment("diamond", site.tsym), details);
mcimadamore@631: }
mcimadamore@631: };
mcimadamore@631: MethodResolutionPhase errPhase = firstErroneousResolutionPhase();
mcimadamore@631: sym = access(errSym, pos, site, names.init, true, argtypes, typeargtypes);
mcimadamore@631: env.info.varArgs = errPhase.isVarargsRequired();
mcimadamore@537: }
mcimadamore@537: return sym;
mcimadamore@537: }
mcimadamore@537:
duke@1: /** Resolve constructor.
duke@1: * @param pos The position to use for error reporting.
duke@1: * @param env The environment current at the constructor invocation.
duke@1: * @param site The type of class for which a constructor is searched.
duke@1: * @param argtypes The types of the constructor invocation's value
duke@1: * arguments.
duke@1: * @param typeargtypes The types of the constructor invocation's type
duke@1: * arguments.
duke@1: * @param allowBoxing Allow boxing and varargs conversions.
duke@1: * @param useVarargs Box trailing arguments into an array for varargs.
duke@1: */
duke@1: Symbol resolveConstructor(DiagnosticPosition pos, Env env,
duke@1: Type site, List argtypes,
duke@1: List typeargtypes,
duke@1: boolean allowBoxing,
duke@1: boolean useVarargs) {
duke@1: Symbol sym = findMethod(env, site,
duke@1: names.init, argtypes,
duke@1: typeargtypes, allowBoxing,
duke@1: useVarargs, false);
mcimadamore@852: chk.checkDeprecated(pos, env.info.scope.owner, sym);
duke@1: return sym;
duke@1: }
duke@1:
duke@1: /** Resolve a constructor, throw a fatal error if not found.
duke@1: * @param pos The position to use for error reporting.
duke@1: * @param env The environment current at the method invocation.
duke@1: * @param site The type to be constructed.
duke@1: * @param argtypes The types of the invocation's value arguments.
duke@1: * @param typeargtypes The types of the invocation's type arguments.
duke@1: */
duke@1: public MethodSymbol resolveInternalConstructor(DiagnosticPosition pos, Env env,
duke@1: Type site,
duke@1: List argtypes,
duke@1: List typeargtypes) {
duke@1: Symbol sym = resolveConstructor(
duke@1: pos, env, site, argtypes, typeargtypes);
duke@1: if (sym.kind == MTH) return (MethodSymbol)sym;
duke@1: else throw new FatalError(
mcimadamore@89: diags.fragment("fatal.err.cant.locate.ctor", site));
duke@1: }
duke@1:
duke@1: /** Resolve operator.
duke@1: * @param pos The position to use for error reporting.
duke@1: * @param optag The tag of the operation tree.
duke@1: * @param env The environment current at the operation.
duke@1: * @param argtypes The types of the operands.
duke@1: */
jjg@1127: Symbol resolveOperator(DiagnosticPosition pos, JCTree.Tag optag,
duke@1: Env env, List argtypes) {
mcimadamore@1110: startResolution();
duke@1: Name name = treeinfo.operatorName(optag);
duke@1: Symbol sym = findMethod(env, syms.predefClass.type, name, argtypes,
duke@1: null, false, false, true);
duke@1: if (boxingEnabled && sym.kind >= WRONG_MTHS)
duke@1: sym = findMethod(env, syms.predefClass.type, name, argtypes,
duke@1: null, true, false, true);
duke@1: return access(sym, pos, env.enclClass.sym.type, name,
duke@1: false, argtypes, null);
duke@1: }
duke@1:
duke@1: /** Resolve operator.
duke@1: * @param pos The position to use for error reporting.
duke@1: * @param optag The tag of the operation tree.
duke@1: * @param env The environment current at the operation.
duke@1: * @param arg The type of the operand.
duke@1: */
jjg@1127: Symbol resolveUnaryOperator(DiagnosticPosition pos, JCTree.Tag optag, Env env, Type arg) {
duke@1: return resolveOperator(pos, optag, env, List.of(arg));
duke@1: }
duke@1:
duke@1: /** Resolve binary operator.
duke@1: * @param pos The position to use for error reporting.
duke@1: * @param optag The tag of the operation tree.
duke@1: * @param env The environment current at the operation.
duke@1: * @param left The types of the left operand.
duke@1: * @param right The types of the right operand.
duke@1: */
duke@1: Symbol resolveBinaryOperator(DiagnosticPosition pos,
jjg@1127: JCTree.Tag optag,
duke@1: Env env,
duke@1: Type left,
duke@1: Type right) {
duke@1: return resolveOperator(pos, optag, env, List.of(left, right));
duke@1: }
duke@1:
duke@1: /**
duke@1: * Resolve `c.name' where name == this or name == super.
duke@1: * @param pos The position to use for error reporting.
duke@1: * @param env The environment current at the expression.
duke@1: * @param c The qualifier.
duke@1: * @param name The identifier's name.
duke@1: */
duke@1: Symbol resolveSelf(DiagnosticPosition pos,
duke@1: Env env,
duke@1: TypeSymbol c,
duke@1: Name name) {
duke@1: Env env1 = env;
duke@1: boolean staticOnly = false;
duke@1: while (env1.outer != null) {
duke@1: if (isStatic(env1)) staticOnly = true;
duke@1: if (env1.enclClass.sym == c) {
duke@1: Symbol sym = env1.info.scope.lookup(name).sym;
duke@1: if (sym != null) {
duke@1: if (staticOnly) sym = new StaticError(sym);
duke@1: return access(sym, pos, env.enclClass.sym.type,
duke@1: name, true);
duke@1: }
duke@1: }
duke@1: if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true;
duke@1: env1 = env1.outer;
duke@1: }
duke@1: log.error(pos, "not.encl.class", c);
duke@1: return syms.errSymbol;
duke@1: }
duke@1:
duke@1: /**
duke@1: * Resolve `c.this' for an enclosing class c that contains the
duke@1: * named member.
duke@1: * @param pos The position to use for error reporting.
duke@1: * @param env The environment current at the expression.
duke@1: * @param member The member that must be contained in the result.
duke@1: */
duke@1: Symbol resolveSelfContaining(DiagnosticPosition pos,
duke@1: Env env,
mcimadamore@901: Symbol member,
mcimadamore@901: boolean isSuperCall) {
duke@1: Name name = names._this;
mcimadamore@901: Env env1 = isSuperCall ? env.outer : env;
duke@1: boolean staticOnly = false;
mcimadamore@901: if (env1 != null) {
mcimadamore@901: while (env1 != null && env1.outer != null) {
mcimadamore@901: if (isStatic(env1)) staticOnly = true;
mcimadamore@901: if (env1.enclClass.sym.isSubClass(member.owner, types)) {
mcimadamore@901: Symbol sym = env1.info.scope.lookup(name).sym;
mcimadamore@901: if (sym != null) {
mcimadamore@901: if (staticOnly) sym = new StaticError(sym);
mcimadamore@901: return access(sym, pos, env.enclClass.sym.type,
mcimadamore@901: name, true);
mcimadamore@901: }
duke@1: }
mcimadamore@901: if ((env1.enclClass.sym.flags() & STATIC) != 0)
mcimadamore@901: staticOnly = true;
mcimadamore@901: env1 = env1.outer;
duke@1: }
duke@1: }
duke@1: log.error(pos, "encl.class.required", member);
duke@1: return syms.errSymbol;
duke@1: }
duke@1:
duke@1: /**
duke@1: * Resolve an appropriate implicit this instance for t's container.
jjh@972: * JLS 8.8.5.1 and 15.9.2
duke@1: */
duke@1: Type resolveImplicitThis(DiagnosticPosition pos, Env env, Type t) {
mcimadamore@901: return resolveImplicitThis(pos, env, t, false);
mcimadamore@901: }
mcimadamore@901:
mcimadamore@901: Type resolveImplicitThis(DiagnosticPosition pos, Env env, Type t, boolean isSuperCall) {
duke@1: Type thisType = (((t.tsym.owner.kind & (MTH|VAR)) != 0)
duke@1: ? resolveSelf(pos, env, t.getEnclosingType().tsym, names._this)
mcimadamore@901: : resolveSelfContaining(pos, env, t.tsym, isSuperCall)).type;
duke@1: if (env.info.isSelfCall && thisType.tsym == env.enclClass.sym)
duke@1: log.error(pos, "cant.ref.before.ctor.called", "this");
duke@1: return thisType;
duke@1: }
duke@1:
duke@1: /* ***************************************************************************
duke@1: * ResolveError classes, indicating error situations when accessing symbols
duke@1: ****************************************************************************/
duke@1:
duke@1: public void logAccessError(Env env, JCTree tree, Type type) {
duke@1: AccessError error = new AccessError(env, type.getEnclosingType(), type.tsym);
mcimadamore@829: logResolveError(error, tree.pos(), type.getEnclosingType().tsym, type.getEnclosingType(), null, null, null);
mcimadamore@302: }
mcimadamore@302: //where
mcimadamore@302: private void logResolveError(ResolveError error,
mcimadamore@302: DiagnosticPosition pos,
mcimadamore@829: Symbol location,
mcimadamore@302: Type site,
mcimadamore@302: Name name,
mcimadamore@302: List argtypes,
mcimadamore@302: List typeargtypes) {
mcimadamore@302: JCDiagnostic d = error.getDiagnostic(JCDiagnostic.DiagnosticType.ERROR,
mcimadamore@829: pos, location, site, name, argtypes, typeargtypes);
jjg@643: if (d != null) {
jjg@643: d.setFlag(DiagnosticFlag.RESOLVE_ERROR);
mcimadamore@302: log.report(d);
jjg@643: }
duke@1: }
duke@1:
mcimadamore@161: private final LocalizedString noArgs = new LocalizedString("compiler.misc.no.args");
mcimadamore@161:
mcimadamore@161: public Object methodArguments(List argtypes) {
mcimadamore@1114: return argtypes == null || argtypes.isEmpty() ? noArgs : argtypes;
mcimadamore@161: }
mcimadamore@161:
mcimadamore@302: /**
mcimadamore@302: * Root class for resolution errors. Subclass of ResolveError
mcimadamore@302: * represent a different kinds of resolution error - as such they must
mcimadamore@302: * specify how they map into concrete compiler diagnostics.
duke@1: */
mcimadamore@302: private abstract class ResolveError extends Symbol {
duke@1:
mcimadamore@302: /** The name of the kind of error, for debugging only. */
mcimadamore@302: final String debugName;
mcimadamore@302:
mcimadamore@302: ResolveError(int kind, String debugName) {
duke@1: super(kind, 0, null, null, null);
duke@1: this.debugName = debugName;
duke@1: }
duke@1:
mcimadamore@302: @Override
duke@1: public R accept(ElementVisitor v, P p) {
duke@1: throw new AssertionError();
duke@1: }
duke@1:
mcimadamore@302: @Override
duke@1: public String toString() {
mcimadamore@302: return debugName;
duke@1: }
duke@1:
mcimadamore@302: @Override
mcimadamore@302: public boolean exists() {
mcimadamore@302: return false;
duke@1: }
duke@1:
mcimadamore@302: /**
mcimadamore@302: * Create an external representation for this erroneous symbol to be
mcimadamore@302: * used during attribution - by default this returns the symbol of a
mcimadamore@302: * brand new error type which stores the original type found
mcimadamore@302: * during resolution.
mcimadamore@302: *
mcimadamore@302: * @param name the name used during resolution
mcimadamore@302: * @param location the location from which the symbol is accessed
duke@1: */
mcimadamore@302: protected Symbol access(Name name, TypeSymbol location) {
mcimadamore@302: return types.createErrorType(name, location, syms.errSymbol.type).tsym;
duke@1: }
duke@1:
mcimadamore@302: /**
mcimadamore@302: * Create a diagnostic representing this resolution error.
mcimadamore@302: *
mcimadamore@302: * @param dkind The kind of the diagnostic to be created (e.g error).
mcimadamore@302: * @param pos The position to be used for error reporting.
mcimadamore@302: * @param site The original type from where the selection took place.
mcimadamore@302: * @param name The name of the symbol to be resolved.
mcimadamore@302: * @param argtypes The invocation's value arguments,
mcimadamore@302: * if we looked for a method.
mcimadamore@302: * @param typeargtypes The invocation's type arguments,
mcimadamore@302: * if we looked for a method.
mcimadamore@302: */
mcimadamore@302: abstract JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
mcimadamore@302: DiagnosticPosition pos,
mcimadamore@829: Symbol location,
mcimadamore@302: Type site,
mcimadamore@302: Name name,
mcimadamore@302: List argtypes,
mcimadamore@302: List typeargtypes);
duke@1:
mcimadamore@302: /**
mcimadamore@302: * A name designates an operator if it consists
mcimadamore@302: * of a non-empty sequence of operator symbols +-~!/*%&|^<>=
mcimadamore@80: */
mcimadamore@80: boolean isOperator(Name name) {
mcimadamore@80: int i = 0;
jjg@113: while (i < name.getByteLength() &&
jjg@113: "+-~!*/%&|^<>=".indexOf(name.getByteAt(i)) >= 0) i++;
jjg@113: return i > 0 && i == name.getByteLength();
mcimadamore@80: }
duke@1: }
duke@1:
mcimadamore@302: /**
mcimadamore@302: * This class is the root class of all resolution errors caused by
mcimadamore@302: * an invalid symbol being found during resolution.
duke@1: */
mcimadamore@302: abstract class InvalidSymbolError extends ResolveError {
mcimadamore@302:
mcimadamore@302: /** The invalid symbol found during resolution */
mcimadamore@302: Symbol sym;
mcimadamore@302:
mcimadamore@302: InvalidSymbolError(int kind, Symbol sym, String debugName) {
mcimadamore@302: super(kind, debugName);
mcimadamore@302: this.sym = sym;
mcimadamore@302: }
mcimadamore@302:
mcimadamore@302: @Override
mcimadamore@302: public boolean exists() {
mcimadamore@302: return true;
mcimadamore@302: }
mcimadamore@302:
mcimadamore@302: @Override
mcimadamore@302: public String toString() {
mcimadamore@302: return super.toString() + " wrongSym=" + sym;
mcimadamore@302: }
mcimadamore@302:
mcimadamore@302: @Override
mcimadamore@302: public Symbol access(Name name, TypeSymbol location) {
mcimadamore@302: if (sym.kind >= AMBIGUOUS)
mcimadamore@302: return ((ResolveError)sym).access(name, location);
mcimadamore@302: else if ((sym.kind & ERRONEOUS) == 0 && (sym.kind & TYP) != 0)
mcimadamore@302: return types.createErrorType(name, location, sym.type).tsym;
mcimadamore@302: else
mcimadamore@302: return sym;
mcimadamore@302: }
mcimadamore@302: }
mcimadamore@302:
mcimadamore@302: /**
mcimadamore@302: * InvalidSymbolError error class indicating that a symbol matching a
mcimadamore@302: * given name does not exists in a given site.
mcimadamore@302: */
mcimadamore@302: class SymbolNotFoundError extends ResolveError {
mcimadamore@302:
mcimadamore@302: SymbolNotFoundError(int kind) {
mcimadamore@302: super(kind, "symbol not found error");
mcimadamore@302: }
mcimadamore@302:
mcimadamore@302: @Override
mcimadamore@302: JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
mcimadamore@302: DiagnosticPosition pos,
mcimadamore@829: Symbol location,
mcimadamore@302: Type site,
mcimadamore@302: Name name,
mcimadamore@302: List argtypes,
mcimadamore@302: List typeargtypes) {
mcimadamore@302: argtypes = argtypes == null ? List.nil() : argtypes;
mcimadamore@302: typeargtypes = typeargtypes == null ? List.nil() : typeargtypes;
mcimadamore@302: if (name == names.error)
mcimadamore@302: return null;
mcimadamore@302:
mcimadamore@302: if (isOperator(name)) {
mcimadamore@829: boolean isUnaryOp = argtypes.size() == 1;
mcimadamore@829: String key = argtypes.size() == 1 ?
mcimadamore@829: "operator.cant.be.applied" :
mcimadamore@829: "operator.cant.be.applied.1";
mcimadamore@829: Type first = argtypes.head;
mcimadamore@829: Type second = !isUnaryOp ? argtypes.tail.head : null;
jjg@612: return diags.create(dkind, log.currentSource(), pos,
mcimadamore@829: key, name, first, second);
mcimadamore@302: }
mcimadamore@302: boolean hasLocation = false;
mcimadamore@855: if (location == null) {
mcimadamore@855: location = site.tsym;
mcimadamore@855: }
mcimadamore@829: if (!location.name.isEmpty()) {
mcimadamore@829: if (location.kind == PCK && !site.tsym.exists()) {
jjg@612: return diags.create(dkind, log.currentSource(), pos,
mcimadamore@829: "doesnt.exist", location);
mcimadamore@302: }
mcimadamore@829: hasLocation = !location.name.equals(names._this) &&
mcimadamore@829: !location.name.equals(names._super);
mcimadamore@302: }
mcimadamore@302: boolean isConstructor = kind == ABSENT_MTH &&
mcimadamore@302: name == names.table.names.init;
mcimadamore@302: KindName kindname = isConstructor ? KindName.CONSTRUCTOR : absentKind(kind);
mcimadamore@302: Name idname = isConstructor ? site.tsym.name : name;
mcimadamore@302: String errKey = getErrorKey(kindname, typeargtypes.nonEmpty(), hasLocation);
mcimadamore@302: if (hasLocation) {
jjg@612: return diags.create(dkind, log.currentSource(), pos,
mcimadamore@302: errKey, kindname, idname, //symbol kindname, name
mcimadamore@302: typeargtypes, argtypes, //type parameters and arguments (if any)
mcimadamore@855: getLocationDiag(location, site)); //location kindname, type
mcimadamore@302: }
mcimadamore@302: else {
jjg@612: return diags.create(dkind, log.currentSource(), pos,
mcimadamore@302: errKey, kindname, idname, //symbol kindname, name
mcimadamore@302: typeargtypes, argtypes); //type parameters and arguments (if any)
mcimadamore@302: }
mcimadamore@302: }
mcimadamore@302: //where
mcimadamore@302: private String getErrorKey(KindName kindname, boolean hasTypeArgs, boolean hasLocation) {
mcimadamore@302: String key = "cant.resolve";
mcimadamore@302: String suffix = hasLocation ? ".location" : "";
mcimadamore@302: switch (kindname) {
mcimadamore@302: case METHOD:
mcimadamore@302: case CONSTRUCTOR: {
mcimadamore@302: suffix += ".args";
mcimadamore@302: suffix += hasTypeArgs ? ".params" : "";
mcimadamore@302: }
mcimadamore@302: }
mcimadamore@302: return key + suffix;
mcimadamore@302: }
mcimadamore@855: private JCDiagnostic getLocationDiag(Symbol location, Type site) {
mcimadamore@855: if (location.kind == VAR) {
mcimadamore@855: return diags.fragment("location.1",
mcimadamore@829: kindName(location),
mcimadamore@829: location,
mcimadamore@855: location.type);
mcimadamore@855: } else {
mcimadamore@855: return diags.fragment("location",
mcimadamore@855: typeKindName(site),
mcimadamore@855: site,
mcimadamore@855: null);
mcimadamore@855: }
mcimadamore@829: }
mcimadamore@302: }
mcimadamore@302:
mcimadamore@302: /**
mcimadamore@302: * InvalidSymbolError error class indicating that a given symbol
mcimadamore@302: * (either a method, a constructor or an operand) is not applicable
mcimadamore@302: * given an actual arguments/type argument list.
mcimadamore@302: */
mcimadamore@302: class InapplicableSymbolError extends InvalidSymbolError {
mcimadamore@302:
mcimadamore@302: /** An auxiliary explanation set in case of instantiation errors. */
mcimadamore@302: JCDiagnostic explanation;
mcimadamore@302:
mcimadamore@302: InapplicableSymbolError(Symbol sym) {
mcimadamore@302: super(WRONG_MTH, sym, "inapplicable symbol error");
mcimadamore@302: }
mcimadamore@302:
mcimadamore@302: /** Update sym and explanation and return this.
mcimadamore@302: */
mcimadamore@302: InapplicableSymbolError setWrongSym(Symbol sym, JCDiagnostic explanation) {
mcimadamore@302: this.sym = sym;
mcimadamore@689: if (this.sym == sym && explanation != null)
mcimadamore@689: this.explanation = explanation; //update the details
mcimadamore@302: return this;
mcimadamore@302: }
mcimadamore@302:
mcimadamore@302: /** Update sym and return this.
mcimadamore@302: */
mcimadamore@302: InapplicableSymbolError setWrongSym(Symbol sym) {
mcimadamore@302: this.sym = sym;
mcimadamore@302: return this;
mcimadamore@302: }
mcimadamore@302:
mcimadamore@302: @Override
mcimadamore@302: public String toString() {
mcimadamore@302: return super.toString() + " explanation=" + explanation;
mcimadamore@302: }
mcimadamore@302:
mcimadamore@302: @Override
mcimadamore@302: JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
mcimadamore@302: DiagnosticPosition pos,
mcimadamore@829: Symbol location,
mcimadamore@302: Type site,
mcimadamore@302: Name name,
mcimadamore@302: List argtypes,
mcimadamore@302: List typeargtypes) {
mcimadamore@302: if (name == names.error)
mcimadamore@302: return null;
mcimadamore@302:
mcimadamore@302: if (isOperator(name)) {
mcimadamore@853: boolean isUnaryOp = argtypes.size() == 1;
mcimadamore@853: String key = argtypes.size() == 1 ?
mcimadamore@853: "operator.cant.be.applied" :
mcimadamore@853: "operator.cant.be.applied.1";
mcimadamore@853: Type first = argtypes.head;
mcimadamore@853: Type second = !isUnaryOp ? argtypes.tail.head : null;
mcimadamore@853: return diags.create(dkind, log.currentSource(), pos,
mcimadamore@853: key, name, first, second);
mcimadamore@302: }
mcimadamore@302: else {
mcimadamore@302: Symbol ws = sym.asMemberOf(site, types);
jjg@612: return diags.create(dkind, log.currentSource(), pos,
mcimadamore@302: "cant.apply.symbol" + (explanation != null ? ".1" : ""),
mcimadamore@302: kindName(ws),
mcimadamore@302: ws.name == names.init ? ws.owner.name : ws.name,
mcimadamore@302: methodArguments(ws.type.getParameterTypes()),
mcimadamore@302: methodArguments(argtypes),
mcimadamore@302: kindName(ws.owner),
mcimadamore@302: ws.owner.type,
mcimadamore@302: explanation);
mcimadamore@302: }
mcimadamore@302: }
mcimadamore@302:
mcimadamore@689: void clear() {
mcimadamore@689: explanation = null;
mcimadamore@689: }
mcimadamore@689:
mcimadamore@302: @Override
mcimadamore@302: public Symbol access(Name name, TypeSymbol location) {
mcimadamore@302: return types.createErrorType(name, location, syms.errSymbol.type).tsym;
mcimadamore@302: }
mcimadamore@302: }
mcimadamore@302:
mcimadamore@302: /**
mcimadamore@302: * ResolveError error class indicating that a set of symbols
mcimadamore@302: * (either methods, constructors or operands) is not applicable
mcimadamore@302: * given an actual arguments/type argument list.
mcimadamore@302: */
mcimadamore@302: class InapplicableSymbolsError extends ResolveError {
mcimadamore@689:
mcimadamore@689: private List candidates = List.nil();
mcimadamore@689:
mcimadamore@302: InapplicableSymbolsError(Symbol sym) {
mcimadamore@302: super(WRONG_MTHS, "inapplicable symbols");
mcimadamore@302: }
mcimadamore@302:
mcimadamore@302: @Override
mcimadamore@302: JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
mcimadamore@302: DiagnosticPosition pos,
mcimadamore@829: Symbol location,
mcimadamore@302: Type site,
mcimadamore@302: Name name,
mcimadamore@302: List argtypes,
mcimadamore@302: List typeargtypes) {
mcimadamore@689: if (candidates.nonEmpty()) {
mcimadamore@689: JCDiagnostic err = diags.create(dkind,
mcimadamore@689: log.currentSource(),
mcimadamore@689: pos,
mcimadamore@689: "cant.apply.symbols",
mcimadamore@689: name == names.init ? KindName.CONSTRUCTOR : absentKind(kind),
mcimadamore@689: getName(),
mcimadamore@689: argtypes);
mcimadamore@689: return new JCDiagnostic.MultilineDiagnostic(err, candidateDetails(site));
mcimadamore@689: } else {
mcimadamore@689: return new SymbolNotFoundError(ABSENT_MTH).getDiagnostic(dkind, pos,
mcimadamore@829: location, site, name, argtypes, typeargtypes);
mcimadamore@689: }
mcimadamore@689: }
mcimadamore@689:
mcimadamore@689: //where
mcimadamore@689: List candidateDetails(Type site) {
mcimadamore@689: List details = List.nil();
mcimadamore@689: for (Candidate c : candidates)
mcimadamore@689: details = details.prepend(c.getDiagnostic(site));
mcimadamore@689: return details.reverse();
mcimadamore@689: }
mcimadamore@689:
mcimadamore@689: Symbol addCandidate(MethodResolutionPhase currentStep, Symbol sym, JCDiagnostic details) {
mcimadamore@689: Candidate c = new Candidate(currentStep, sym, details);
mcimadamore@689: if (c.isValid() && !candidates.contains(c))
mcimadamore@689: candidates = candidates.append(c);
mcimadamore@689: return this;
mcimadamore@689: }
mcimadamore@689:
mcimadamore@689: void clear() {
mcimadamore@689: candidates = List.nil();
mcimadamore@689: }
mcimadamore@689:
mcimadamore@689: private Name getName() {
mcimadamore@689: Symbol sym = candidates.head.sym;
mcimadamore@689: return sym.name == names.init ?
mcimadamore@689: sym.owner.name :
mcimadamore@689: sym.name;
mcimadamore@689: }
mcimadamore@689:
mcimadamore@689: private class Candidate {
mcimadamore@689:
mcimadamore@689: final MethodResolutionPhase step;
mcimadamore@689: final Symbol sym;
mcimadamore@689: final JCDiagnostic details;
mcimadamore@689:
mcimadamore@689: private Candidate(MethodResolutionPhase step, Symbol sym, JCDiagnostic details) {
mcimadamore@689: this.step = step;
mcimadamore@689: this.sym = sym;
mcimadamore@689: this.details = details;
mcimadamore@689: }
mcimadamore@689:
mcimadamore@689: JCDiagnostic getDiagnostic(Type site) {
mcimadamore@689: return diags.fragment("inapplicable.method",
mcimadamore@689: Kinds.kindName(sym),
mcimadamore@689: sym.location(site, types),
mcimadamore@689: sym.asMemberOf(site, types),
mcimadamore@689: details);
mcimadamore@689: }
mcimadamore@689:
mcimadamore@689: @Override
mcimadamore@689: public boolean equals(Object o) {
mcimadamore@689: if (o instanceof Candidate) {
mcimadamore@689: Symbol s1 = this.sym;
mcimadamore@689: Symbol s2 = ((Candidate)o).sym;
mcimadamore@689: if ((s1 != s2 &&
mcimadamore@689: (s1.overrides(s2, s1.owner.type.tsym, types, false) ||
mcimadamore@689: (s2.overrides(s1, s2.owner.type.tsym, types, false)))) ||
mcimadamore@689: ((s1.isConstructor() || s2.isConstructor()) && s1.owner != s2.owner))
mcimadamore@689: return true;
mcimadamore@689: }
mcimadamore@689: return false;
mcimadamore@689: }
mcimadamore@689:
mcimadamore@689: boolean isValid() {
mcimadamore@689: return (((sym.flags() & VARARGS) != 0 && step == VARARITY) ||
mcimadamore@689: (sym.flags() & VARARGS) == 0 && step == (boxingEnabled ? BOX : BASIC));
mcimadamore@689: }
mcimadamore@302: }
mcimadamore@302: }
mcimadamore@302:
mcimadamore@302: /**
mcimadamore@302: * An InvalidSymbolError error class indicating that a symbol is not
mcimadamore@302: * accessible from a given site
mcimadamore@302: */
mcimadamore@302: class AccessError extends InvalidSymbolError {
mcimadamore@302:
mcimadamore@302: private Env env;
mcimadamore@302: private Type site;
duke@1:
duke@1: AccessError(Symbol sym) {
duke@1: this(null, null, sym);
duke@1: }
duke@1:
duke@1: AccessError(Env env, Type site, Symbol sym) {
duke@1: super(HIDDEN, sym, "access error");
duke@1: this.env = env;
duke@1: this.site = site;
duke@1: if (debugResolve)
duke@1: log.error("proc.messager", sym + " @ " + site + " is inaccessible.");
duke@1: }
duke@1:
mcimadamore@302: @Override
mcimadamore@302: public boolean exists() {
mcimadamore@302: return false;
mcimadamore@302: }
duke@1:
mcimadamore@302: @Override
mcimadamore@302: JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
mcimadamore@302: DiagnosticPosition pos,
mcimadamore@829: Symbol location,
mcimadamore@302: Type site,
mcimadamore@302: Name name,
mcimadamore@302: List argtypes,
mcimadamore@302: List typeargtypes) {
mcimadamore@302: if (sym.owner.type.tag == ERROR)
mcimadamore@302: return null;
mcimadamore@302:
mcimadamore@302: if (sym.name == names.init && sym.owner != site.tsym) {
mcimadamore@302: return new SymbolNotFoundError(ABSENT_MTH).getDiagnostic(dkind,
mcimadamore@829: pos, location, site, name, argtypes, typeargtypes);
mcimadamore@302: }
mcimadamore@302: else if ((sym.flags() & PUBLIC) != 0
mcimadamore@302: || (env != null && this.site != null
mcimadamore@302: && !isAccessible(env, this.site))) {
jjg@612: return diags.create(dkind, log.currentSource(),
mcimadamore@302: pos, "not.def.access.class.intf.cant.access",
mcimadamore@302: sym, sym.location());
mcimadamore@302: }
mcimadamore@302: else if ((sym.flags() & (PRIVATE | PROTECTED)) != 0) {
jjg@612: return diags.create(dkind, log.currentSource(),
mcimadamore@302: pos, "report.access", sym,
mcimadamore@302: asFlagSet(sym.flags() & (PRIVATE | PROTECTED)),
mcimadamore@302: sym.location());
mcimadamore@302: }
mcimadamore@302: else {
jjg@612: return diags.create(dkind, log.currentSource(),
mcimadamore@302: pos, "not.def.public.cant.access", sym, sym.location());
duke@1: }
duke@1: }
duke@1: }
duke@1:
mcimadamore@302: /**
mcimadamore@302: * InvalidSymbolError error class indicating that an instance member
mcimadamore@302: * has erroneously been accessed from a static context.
duke@1: */
mcimadamore@302: class StaticError extends InvalidSymbolError {
mcimadamore@302:
duke@1: StaticError(Symbol sym) {
duke@1: super(STATICERR, sym, "static error");
duke@1: }
duke@1:
mcimadamore@302: @Override
mcimadamore@302: JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
mcimadamore@302: DiagnosticPosition pos,
mcimadamore@829: Symbol location,
mcimadamore@302: Type site,
mcimadamore@302: Name name,
mcimadamore@302: List argtypes,
mcimadamore@302: List typeargtypes) {
mcimadamore@80: Symbol errSym = ((sym.kind == TYP && sym.type.tag == CLASS)
mcimadamore@80: ? types.erasure(sym.type).tsym
mcimadamore@80: : sym);
jjg@612: return diags.create(dkind, log.currentSource(), pos,
mcimadamore@302: "non-static.cant.be.ref", kindName(sym), errSym);
duke@1: }
duke@1: }
duke@1:
mcimadamore@302: /**
mcimadamore@302: * InvalidSymbolError error class indicating that a pair of symbols
mcimadamore@302: * (either methods, constructors or operands) are ambiguous
mcimadamore@302: * given an actual arguments/type argument list.
duke@1: */
mcimadamore@302: class AmbiguityError extends InvalidSymbolError {
mcimadamore@302:
mcimadamore@302: /** The other maximally specific symbol */
duke@1: Symbol sym2;
duke@1:
duke@1: AmbiguityError(Symbol sym1, Symbol sym2) {
duke@1: super(AMBIGUOUS, sym1, "ambiguity error");
duke@1: this.sym2 = sym2;
duke@1: }
duke@1:
mcimadamore@302: @Override
mcimadamore@302: JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
mcimadamore@302: DiagnosticPosition pos,
mcimadamore@829: Symbol location,
mcimadamore@302: Type site,
mcimadamore@302: Name name,
mcimadamore@302: List argtypes,
mcimadamore@302: List typeargtypes) {
duke@1: AmbiguityError pair = this;
duke@1: while (true) {
mcimadamore@302: if (pair.sym.kind == AMBIGUOUS)
mcimadamore@302: pair = (AmbiguityError)pair.sym;
duke@1: else if (pair.sym2.kind == AMBIGUOUS)
duke@1: pair = (AmbiguityError)pair.sym2;
duke@1: else break;
duke@1: }
mcimadamore@302: Name sname = pair.sym.name;
mcimadamore@302: if (sname == names.init) sname = pair.sym.owner.name;
jjg@612: return diags.create(dkind, log.currentSource(),
mcimadamore@302: pos, "ref.ambiguous", sname,
mcimadamore@302: kindName(pair.sym),
mcimadamore@302: pair.sym,
mcimadamore@302: pair.sym.location(site, types),
duke@1: kindName(pair.sym2),
duke@1: pair.sym2,
duke@1: pair.sym2.location(site, types));
duke@1: }
duke@1: }
mcimadamore@160:
mcimadamore@160: enum MethodResolutionPhase {
mcimadamore@160: BASIC(false, false),
mcimadamore@160: BOX(true, false),
mcimadamore@160: VARARITY(true, true);
mcimadamore@160:
mcimadamore@160: boolean isBoxingRequired;
mcimadamore@160: boolean isVarargsRequired;
mcimadamore@160:
mcimadamore@160: MethodResolutionPhase(boolean isBoxingRequired, boolean isVarargsRequired) {
mcimadamore@160: this.isBoxingRequired = isBoxingRequired;
mcimadamore@160: this.isVarargsRequired = isVarargsRequired;
mcimadamore@160: }
mcimadamore@160:
mcimadamore@160: public boolean isBoxingRequired() {
mcimadamore@160: return isBoxingRequired;
mcimadamore@160: }
mcimadamore@160:
mcimadamore@160: public boolean isVarargsRequired() {
mcimadamore@160: return isVarargsRequired;
mcimadamore@160: }
mcimadamore@160:
mcimadamore@160: public boolean isApplicable(boolean boxingEnabled, boolean varargsEnabled) {
mcimadamore@160: return (varargsEnabled || !isVarargsRequired) &&
mcimadamore@160: (boxingEnabled || !isBoxingRequired);
mcimadamore@160: }
mcimadamore@160: }
mcimadamore@160:
mcimadamore@160: private Map methodResolutionCache =
mcimadamore@160: new HashMap(MethodResolutionPhase.values().length);
mcimadamore@160:
mcimadamore@1114: private Map verboseResolutionCandidateDiags =
mcimadamore@1114: new LinkedHashMap();
mcimadamore@1114:
mcimadamore@160: final List methodResolutionSteps = List.of(BASIC, BOX, VARARITY);
mcimadamore@160:
mcimadamore@689: private MethodResolutionPhase currentStep = null;
mcimadamore@689:
mcimadamore@1114: private boolean internalResolution = false;
mcimadamore@1114:
mcimadamore@160: private MethodResolutionPhase firstErroneousResolutionPhase() {
mcimadamore@160: MethodResolutionPhase bestSoFar = BASIC;
mcimadamore@160: Symbol sym = methodNotFound;
mcimadamore@160: List steps = methodResolutionSteps;
mcimadamore@160: while (steps.nonEmpty() &&
mcimadamore@160: steps.head.isApplicable(boxingEnabled, varargsEnabled) &&
mcimadamore@160: sym.kind >= WRONG_MTHS) {
mcimadamore@160: sym = methodResolutionCache.get(steps.head);
mcimadamore@160: bestSoFar = steps.head;
mcimadamore@160: steps = steps.tail;
mcimadamore@160: }
mcimadamore@160: return bestSoFar;
mcimadamore@160: }
duke@1: }