duke@1: /*
xdono@54: * Copyright 1999-2008 Sun Microsystems, Inc. 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
duke@1: * published by the Free Software Foundation. Sun designates this
duke@1: * particular file as subject to the "Classpath" exception as provided
duke@1: * by Sun 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: *
duke@1: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@1: * CA 95054 USA or visit www.sun.com if you need additional information or
duke@1: * have any questions.
duke@1: */
duke@1:
duke@1: package com.sun.tools.javac.comp;
duke@1:
duke@1: import com.sun.tools.javac.util.*;
duke@1: import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
duke@1: import com.sun.tools.javac.code.*;
duke@1: import com.sun.tools.javac.jvm.*;
duke@1: import com.sun.tools.javac.tree.*;
mcimadamore@161: import com.sun.tools.javac.api.Formattable.LocalizedString;
mcimadamore@160: import static com.sun.tools.javac.comp.Resolve.MethodResolutionPhase.*;
duke@1:
duke@1: import com.sun.tools.javac.code.Type.*;
duke@1: import com.sun.tools.javac.code.Symbol.*;
duke@1: import com.sun.tools.javac.tree.JCTree.*;
duke@1:
duke@1: import static com.sun.tools.javac.code.Flags.*;
duke@1: import static com.sun.tools.javac.code.Kinds.*;
duke@1: import static com.sun.tools.javac.code.TypeTags.*;
duke@1: import javax.lang.model.element.ElementVisitor;
duke@1:
mcimadamore@160: import java.util.Map;
mcimadamore@160: import java.util.HashMap;
mcimadamore@160:
duke@1: /** Helper class for name resolution, used mostly by the attribution phase.
duke@1: *
duke@1: *
This is NOT part of any API supported by Sun Microsystems. If
duke@1: * 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();
jrose@571: public final boolean allowPolymorphicSignature;
duke@1: private final boolean debugResolve;
duke@1:
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);
duke@1: debugResolve = options.get("debugresolve") != null;
jrose@571: allowPolymorphicSignature = source.allowPolymorphicSignature() || options.get("invokedynamic") != null;
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) {
duke@1: switch ((short)(c.flags() & AccessFlags)) {
duke@1: case PRIVATE:
duke@1: return
duke@1: env.enclClass.sym.outermostClass() ==
duke@1: c.owner.outermostClass();
duke@1: case 0:
duke@1: return
duke@1: env.toplevel.packge == c.owner // fast special case
duke@1: ||
duke@1: env.toplevel.packge == c.packge()
duke@1: ||
duke@1: // Hack: this case is added since synthesized default constructors
duke@1: // of anonymous classes should be allowed to access
duke@1: // classes which would be inaccessible otherwise.
duke@1: env.enclMethod != null &&
duke@1: (env.enclMethod.mods.flags & ANONCONSTR) != 0;
duke@1: default: // error recovery
duke@1: case PUBLIC:
duke@1: return true;
duke@1: case PROTECTED:
duke@1: return
duke@1: env.toplevel.packge == c.owner // fast special case
duke@1: ||
duke@1: env.toplevel.packge == c.packge()
duke@1: ||
duke@1: isInnerSubClass(env.enclClass.sym, c.owner);
duke@1: }
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) {
duke@1: return (t.tag == ARRAY)
duke@1: ? isAccessible(env, types.elemtype(t))
duke@1: : isAccessible(env, t.tsym);
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) {
duke@1: if (sym.name == names.init && sym.owner != site.tsym) return false;
duke@1: ClassSymbol sub;
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: &&
duke@1: isAccessible(env, site)
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: &&
duke@1: isAccessible(env, site)
duke@1: &&
mcimadamore@254: notOverriddenIn(site, sym);
duke@1: default: // this case includes erroneous combinations as well
mcimadamore@254: return isAccessible(env, site) && 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@254: * speaking a member of `site'.)
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@325: return (s2 == null || s2 == sym ||
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 {
jrose@571: assert ((m.flags() & (POLYMORPHIC_SIGNATURE|HYPOTHETICAL)) != POLYMORPHIC_SIGNATURE);
duke@1: if (useVarargs && (m.flags() & VARARGS) == 0) return null;
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.
duke@1: List tvars = env.info.tvars;
duke@1: if (typeargtypes == null) typeargtypes = List.nil();
duke@1: if (mt.tag != FORALL && typeargtypes.nonEmpty()) {
duke@1: // This is not a polymorphic method, but typeargs are supplied
duke@1: // which is fine, see JLS3 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())
duke@1: return null;
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))
duke@1: return null;
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
duke@1: boolean instNeeded = tvars.tail != null/*inlined: tvars.nonEmpty()*/;
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)
duke@1: return
duke@1: infer.instantiateMethod(tvars,
duke@1: (MethodType)mt,
duke@1: argtypes,
duke@1: allowBoxing,
duke@1: useVarargs,
duke@1: warn);
duke@1: return
duke@1: argumentsAcceptable(argtypes, mt.getParameterTypes(),
duke@1: allowBoxing, useVarargs, warn)
duke@1: ? mt
duke@1: : null;
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@299: } catch (Infer.InferenceException 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: */
duke@1: boolean argumentsAcceptable(List argtypes,
duke@1: List formals,
duke@1: boolean allowBoxing,
duke@1: boolean useVarargs,
duke@1: Warner warn) {
duke@1: Type varargsFormal = useVarargs ? formals.last() : null;
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);
duke@1: if (!works) return false;
duke@1: argtypes = argtypes.tail;
duke@1: formals = formals.tail;
duke@1: }
duke@1: if (formals.head != varargsFormal) return false; // not enough args
duke@1: if (!useVarargs)
duke@1: return argtypes.isEmpty();
duke@1: Type elt = types.elemtype(varargsFormal);
duke@1: while (argtypes.nonEmpty()) {
duke@1: if (!types.isConvertible(argtypes.head, elt, warn))
duke@1: return false;
duke@1: argtypes = argtypes.tail;
duke@1: }
duke@1: return true;
duke@1: }
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: */
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;
duke@1: assert sym.kind < AMBIGUOUS;
jrose@571: if ((sym.flags() & POLYMORPHIC_SIGNATURE) != 0 && allowPolymorphicSignature) {
jrose@571: assert(site.tag == CLASS);
jrose@571: // Never match a MethodHandle.invoke directly.
jrose@571: if (useVarargs | allowBoxing | operator)
jrose@571: return bestSoFar;
jrose@571: // Supply an exactly-typed implicit method instead.
jrose@571: sym = findPolymorphicSignatureInstance(env, sym.owner.type, sym.name, (MethodSymbol) sym, argtypes, typeargtypes);
jrose@571: }
duke@1: try {
duke@1: if (rawInstantiate(env, site, sym, argtypes, typeargtypes,
duke@1: allowBoxing, useVarargs, Warner.noWarnings) == null) {
duke@1: // inapplicable
duke@1: switch (bestSoFar.kind) {
duke@1: case ABSENT_MTH: return wrongMethod.setWrongSym(sym);
duke@1: case WRONG_MTH: return wrongMethods;
duke@1: default: return bestSoFar;
duke@1: }
duke@1: }
mcimadamore@299: } catch (Infer.InferenceException ex) {
duke@1: switch (bestSoFar.kind) {
duke@1: case ABSENT_MTH:
duke@1: return wrongMethod.setWrongSym(sym, ex.getDiagnostic());
duke@1: case WRONG_MTH:
duke@1: return wrongMethods;
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;
duke@1: }
duke@1: return (bestSoFar.kind > AMBIGUOUS)
duke@1: ? sym
duke@1: : mostSpecific(sym, bestSoFar, env, site,
duke@1: allowBoxing && operator, useVarargs);
duke@1: }
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;
duke@1: Type mt1 = types.memberType(site, m1);
duke@1: noteWarner.unchecked = false;
duke@1: boolean m1SignatureMoreSpecific =
duke@1: (instantiate(env, site, m2, types.lowerBoundArgtypes(mt1), null,
duke@1: allowBoxing, false, noteWarner) != null ||
duke@1: useVarargs && instantiate(env, site, m2, types.lowerBoundArgtypes(mt1), null,
duke@1: allowBoxing, true, noteWarner) != null) &&
duke@1: !noteWarner.unchecked;
duke@1: Type mt2 = types.memberType(site, m2);
duke@1: noteWarner.unchecked = false;
duke@1: boolean m2SignatureMoreSpecific =
duke@1: (instantiate(env, site, m1, types.lowerBoundArgtypes(mt2), null,
duke@1: allowBoxing, false, noteWarner) != null ||
duke@1: useVarargs && instantiate(env, site, m1, types.lowerBoundArgtypes(mt2), null,
duke@1: allowBoxing, true, noteWarner) != null) &&
duke@1: !noteWarner.unchecked;
duke@1: if (m1SignatureMoreSpecific && m2SignatureMoreSpecific) {
duke@1: if (!types.overrideEquivalent(mt1, mt2))
duke@1: return new AmbiguityError(m1, m2);
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
duke@1: if ((m1.flags() & BRIDGE) != (m2.flags() & BRIDGE)) {
duke@1: return ((m1.flags() & BRIDGE) != 0) ? m2 : m1;
duke@1: }
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)
duke@1: return new 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()))
duke@1: return new AmbiguityError(m1, m2);
duke@1: // both abstract, neither overridden; merge throws clause and result type
mcimadamore@254: Symbol mostSpecific;
jjg@110: Type result2 = mt2.getReturnType();
duke@1: if (mt2.tag == FORALL)
duke@1: result2 = types.subst(result2, ((ForAll)mt2).tvars, ((ForAll)mt1).tvars);
duke@1: if (types.isSubtype(mt1.getReturnType(), result2)) {
mcimadamore@254: mostSpecific = m1;
duke@1: } else if (types.isSubtype(result2, mt1.getReturnType())) {
mcimadamore@254: mostSpecific = m2;
duke@1: } else {
duke@1: // Theoretically, this can't happen, but it is possible
duke@1: // due to error recovery or mixing incompatible class files
duke@1: return new AmbiguityError(m1, m2);
duke@1: }
mcimadamore@254: MethodSymbol result = new MethodSymbol(
mcimadamore@254: mostSpecific.flags(),
mcimadamore@254: mostSpecific.name,
mcimadamore@254: null,
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: };
mcimadamore@254: result.type = (Type)mostSpecific.type.clone();
duke@1: result.type.setThrown(chk.intersect(mt1.getThrownTypes(),
duke@1: mt2.getThrownTypes()));
duke@1: return result;
duke@1: }
duke@1: if (m1SignatureMoreSpecific) return m1;
duke@1: if (m2SignatureMoreSpecific) return m2;
duke@1: return new 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)
duke@1: return new AmbiguityError(m1, m2);
duke@1: else
duke@1: return new AmbiguityError(err1, err2);
duke@1: default:
duke@1: throw new AssertionError();
duke@1: }
duke@1: }
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) {
jrose@571: Symbol bestSoFar = methodNotFound;
jrose@571: if ((site.tsym.flags() & POLYMORPHIC_SIGNATURE) != 0 &&
jrose@571: allowPolymorphicSignature &&
jrose@571: site.tag == CLASS &&
jrose@571: !(useVarargs | allowBoxing | operator)) {
jrose@571: // supply an exactly-typed implicit method in java.dyn.InvokeDynamic
jrose@571: bestSoFar = findPolymorphicSignatureInstance(env, site, name, null, argtypes, typeargtypes);
jrose@571: }
duke@1: return 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,
duke@1: operator);
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,
duke@1: boolean operator) {
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@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,
duke@1: allowBoxing, useVarargs, operator);
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: }
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:
jrose@267: /** Find or create an implicit method of exactly the given type (after erasure).
jrose@267: * Searches in a side table, not the main scope of the site.
jrose@267: * This emulates the lookup process required by JSR 292 in JVM.
jrose@267: * @param env The current environment.
jrose@267: * @param site The original type from where the selection
jrose@267: * takes place.
jrose@267: * @param name The method's name.
jrose@267: * @param argtypes The method's value arguments.
jrose@267: * @param typeargtypes The method's type arguments
jrose@267: */
jrose@571: Symbol findPolymorphicSignatureInstance(Env env,
jrose@571: Type site,
jrose@571: Name name,
jrose@571: MethodSymbol spMethod, // sig. poly. method or null if none
jrose@571: List argtypes,
jrose@571: List typeargtypes) {
jrose@571: assert allowPolymorphicSignature;
jrose@571: //assert site == syms.invokeDynamicType || site == syms.methodHandleType : site;
jrose@267: ClassSymbol c = (ClassSymbol) site.tsym;
jrose@267: Scope implicit = c.members().next;
jrose@267: if (implicit == null) {
jrose@267: c.members().next = implicit = new Scope(c);
jrose@267: }
jrose@267: Type restype;
jrose@267: if (typeargtypes.isEmpty()) {
jrose@267: restype = syms.objectType;
jrose@267: } else {
jrose@267: restype = typeargtypes.head;
jrose@267: if (!typeargtypes.tail.isEmpty())
jrose@267: return methodNotFound;
jrose@267: }
jrose@267: List paramtypes = Type.map(argtypes, implicitArgType);
jrose@571: long flags;
jrose@571: List exType;
jrose@571: if (spMethod != null) {
jrose@571: exType = spMethod.getThrownTypes();
jrose@571: flags = spMethod.flags() & AccessFlags;
jrose@571: } else {
jrose@571: // make it throw all exceptions
jrose@571: //assert(site == syms.invokeDynamicType);
jrose@571: exType = List.of(syms.throwableType);
jrose@571: flags = PUBLIC | STATIC;
jrose@571: }
jrose@267: MethodType mtype = new MethodType(paramtypes,
jrose@267: restype,
jrose@571: exType,
jrose@267: syms.methodClass);
jrose@571: flags |= ABSTRACT | HYPOTHETICAL | POLYMORPHIC_SIGNATURE;
jrose@267: Symbol m = null;
jrose@267: for (Scope.Entry e = implicit.lookup(name);
jrose@267: e.scope != null;
jrose@267: e = e.next()) {
jrose@267: Symbol sym = e.sym;
jrose@267: assert sym.kind == MTH;
jrose@267: if (types.isSameType(mtype, sym.type)
jrose@267: && (sym.flags() & STATIC) == (flags & STATIC)) {
jrose@267: m = sym;
jrose@267: break;
jrose@267: }
jrose@267: }
jrose@267: if (m == null) {
jrose@267: // create the desired method
jrose@267: m = new MethodSymbol(flags, name, mtype, c);
jrose@267: implicit.enter(m);
jrose@267: }
jrose@267: assert argumentsAcceptable(argtypes, types.memberType(site, m).getParameterTypes(),
jrose@267: false, false, Warner.noWarnings);
jrose@267: assert null != instantiate(env, site, m, argtypes, typeargtypes, false, false, Warner.noWarnings);
jrose@267: return m;
jrose@267: }
jrose@267: //where
jrose@267: Mapping implicitArgType = new Mapping ("implicitArgType") {
jrose@267: public Type apply(Type t) { return implicitArgType(t); }
jrose@267: };
jrose@267: Type implicitArgType(Type argType) {
jrose@267: argType = types.erasure(argType);
jrose@267: if (argType.tag == BOT)
jrose@267: // nulls type as the marker type Null (which has no instances)
jrose@267: // TO DO: figure out how to access java.lang.Null safely, else throw nice error
jrose@267: //argType = types.boxedClass(syms.botType).type;
jrose@267: argType = types.boxedClass(syms.voidType).type; // REMOVE
jrose@267: return argType;
jrose@267: }
jrose@267:
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:
duke@1: if (env.tree.getTag() != JCTree.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,
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@302: logResolveError(errSym, pos, 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:
duke@1: /** Same as above, but without 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) {
duke@1: if (sym.kind >= AMBIGUOUS)
duke@1: return access(sym, pos, site, name, qualified, List.nil(), null);
duke@1: else
duke@1: return sym;
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@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 >= ERRONEOUS) {
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:
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@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 >= ERRONEOUS) {
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@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, site, name, true, argtypes, typeargtypes);
mcimadamore@160: env.info.varArgs = errPhase.isVarargsRequired;
duke@1: }
duke@1: return sym;
duke@1: }
duke@1:
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) {
duke@1: Symbol sym = resolveQualifiedMethod(
duke@1: pos, env, site, name, 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.meth",
duke@1: name));
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@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 >= ERRONEOUS) {
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@537: List typeargtypes, boolean reportErrors) {
mcimadamore@537: Symbol sym = methodNotFound;
mcimadamore@537: JCDiagnostic explanation = null;
mcimadamore@537: List steps = methodResolutionSteps;
mcimadamore@537: while (steps.nonEmpty() &&
mcimadamore@537: steps.head.isApplicable(boxingEnabled, varargsEnabled) &&
mcimadamore@537: sym.kind >= ERRONEOUS) {
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: if (sym.kind == WRONG_MTH &&
mcimadamore@537: ((InapplicableSymbolError)sym).explanation != null) {
mcimadamore@537: //if the symbol is an inapplicable method symbol, then the
mcimadamore@537: //explanation contains the reason for which inference failed
mcimadamore@537: explanation = ((InapplicableSymbolError)sym).explanation;
mcimadamore@537: }
mcimadamore@537: steps = steps.tail;
mcimadamore@537: }
mcimadamore@537: if (sym.kind >= AMBIGUOUS && reportErrors) {
mcimadamore@537: String key = explanation == null ?
mcimadamore@537: "cant.apply.diamond" :
mcimadamore@537: "cant.apply.diamond.1";
mcimadamore@537: log.error(pos, key, diags.fragment("diamond", site.tsym), explanation);
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);
duke@1: if ((sym.flags() & DEPRECATED) != 0 &&
duke@1: (env.info.scope.owner.flags() & DEPRECATED) == 0 &&
duke@1: env.info.scope.owner.outermostClass() != sym.outermostClass())
duke@1: chk.warnDeprecated(pos, 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: */
duke@1: Symbol resolveOperator(DiagnosticPosition pos, int optag,
duke@1: Env env, List argtypes) {
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: */
duke@1: Symbol resolveUnaryOperator(DiagnosticPosition pos, int 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,
duke@1: int 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,
duke@1: Symbol member) {
duke@1: Name name = names._this;
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.isSubClass(member.owner, types) &&
duke@1: isAccessible(env, env1.enclClass.sym.type, member)) {
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)
duke@1: staticOnly = true;
duke@1: env1 = env1.outer;
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.
duke@1: * JLS2 8.8.5.1 and 15.9.2
duke@1: */
duke@1: Type resolveImplicitThis(DiagnosticPosition pos, Env env, Type t) {
duke@1: Type thisType = (((t.tsym.owner.kind & (MTH|VAR)) != 0)
duke@1: ? resolveSelf(pos, env, t.getEnclosingType().tsym, names._this)
duke@1: : resolveSelfContaining(pos, env, t.tsym)).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@302: logResolveError(error, tree.pos(), type.getEnclosingType(), null, null, null);
mcimadamore@302: }
mcimadamore@302: //where
mcimadamore@302: private void logResolveError(ResolveError error,
mcimadamore@302: DiagnosticPosition pos,
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@302: pos, site, name, argtypes, typeargtypes);
mcimadamore@302: if (d != null)
mcimadamore@302: log.report(d);
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@161: return 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@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@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@302: return diags.create(dkind, false, log.currentSource(), pos,
mcimadamore@302: "operator.cant.be.applied", name, argtypes);
mcimadamore@302: }
mcimadamore@302: boolean hasLocation = false;
mcimadamore@302: if (!site.tsym.name.isEmpty()) {
mcimadamore@302: if (site.tsym.kind == PCK && !site.tsym.exists()) {
mcimadamore@302: return diags.create(dkind, false, log.currentSource(), pos,
mcimadamore@302: "doesnt.exist", site.tsym);
mcimadamore@302: }
mcimadamore@302: hasLocation = true;
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) {
mcimadamore@302: return diags.create(dkind, false, log.currentSource(), pos,
mcimadamore@302: errKey, kindname, idname, //symbol kindname, name
mcimadamore@302: typeargtypes, argtypes, //type parameters and arguments (if any)
mcimadamore@302: typeKindName(site), site); //location kindname, type
mcimadamore@302: }
mcimadamore@302: else {
mcimadamore@302: return diags.create(dkind, false, 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@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@302: this.explanation = explanation;
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: this.explanation = null;
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@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@302: return diags.create(dkind, false, log.currentSource(),
mcimadamore@302: pos, "operator.cant.be.applied", name, argtypes);
mcimadamore@302: }
mcimadamore@302: else {
mcimadamore@302: Symbol ws = sym.asMemberOf(site, types);
mcimadamore@302: return diags.create(dkind, false, 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@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@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@302: Type site,
mcimadamore@302: Name name,
mcimadamore@302: List argtypes,
mcimadamore@302: List typeargtypes) {
mcimadamore@302: return new SymbolNotFoundError(ABSENT_MTH).getDiagnostic(dkind, pos,
mcimadamore@302: site, name, argtypes, typeargtypes);
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@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@302: pos, 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))) {
mcimadamore@302: return diags.create(dkind, false, 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) {
mcimadamore@302: return diags.create(dkind, false, log.currentSource(),
mcimadamore@302: pos, "report.access", sym,
mcimadamore@302: asFlagSet(sym.flags() & (PRIVATE | PROTECTED)),
mcimadamore@302: sym.location());
mcimadamore@302: }
mcimadamore@302: else {
mcimadamore@302: return diags.create(dkind, false, 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@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);
mcimadamore@302: return diags.create(dkind, false, 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@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;
mcimadamore@302: return diags.create(dkind, false, 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@160: final List methodResolutionSteps = List.of(BASIC, BOX, VARARITY);
mcimadamore@160:
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: }