duke@1: /* mcimadamore@1186: * Copyright (c) 1999, 2012, 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.*; mcimadamore@1215: import com.sun.tools.javac.comp.Resolve.MethodResolutionContext.Candidate; 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@1215: import java.util.EnumMap; mcimadamore@1114: import java.util.EnumSet; mcimadamore@1114: import java.util.HashSet; 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@1215: protected Resolve(Context context) { mcimadamore@1215: context.put(resolveKey, this); mcimadamore@1215: syms = Symtab.instance(context); mcimadamore@1215: mcimadamore@1215: varNotFound = new mcimadamore@1215: SymbolNotFoundError(ABSENT_VAR); mcimadamore@1215: wrongMethod = new mcimadamore@1215: InapplicableSymbolError(); mcimadamore@1215: wrongMethods = new mcimadamore@1215: InapplicableSymbolsError(); mcimadamore@1215: methodNotFound = new mcimadamore@1215: SymbolNotFoundError(ABSENT_MTH); mcimadamore@1215: typeNotFound = new mcimadamore@1215: SymbolNotFoundError(ABSENT_TYP); mcimadamore@1215: mcimadamore@1215: names = Names.instance(context); mcimadamore@1215: log = Log.instance(context); mcimadamore@1215: chk = Check.instance(context); mcimadamore@1215: infer = Infer.instance(context); mcimadamore@1215: reader = ClassReader.instance(context); mcimadamore@1215: treeinfo = TreeInfo.instance(context); mcimadamore@1215: types = Types.instance(context); mcimadamore@1215: diags = JCDiagnostic.Factory.instance(context); mcimadamore@1215: Source source = Source.instance(context); mcimadamore@1215: boxingEnabled = source.allowBoxing(); mcimadamore@1215: varargsEnabled = source.allowVarargs(); mcimadamore@1215: Options options = Options.instance(context); mcimadamore@1215: debugResolve = options.isSet("debugresolve"); mcimadamore@1215: verboseResolutionMode = VerboseResolutionMode.getVerboseResolutionMode(options); mcimadamore@1215: Target target = Target.instance(context); mcimadamore@1215: allowMethodHandles = target.hasMethodHandles(); mcimadamore@1215: polymorphicSignatureScope = new Scope(syms.noSymbol); mcimadamore@1215: mcimadamore@1215: inapplicableMethodException = new InapplicableMethodException(diags); mcimadamore@1215: } mcimadamore@1215: mcimadamore@1215: /** error symbols, which are returned when resolution fails mcimadamore@1215: */ mcimadamore@1215: private final SymbolNotFoundError varNotFound; mcimadamore@1215: private final InapplicableSymbolError wrongMethod; mcimadamore@1215: private final InapplicableSymbolsError wrongMethods; mcimadamore@1215: private final SymbolNotFoundError methodNotFound; mcimadamore@1215: private final SymbolNotFoundError typeNotFound; mcimadamore@1215: mcimadamore@1215: public static Resolve instance(Context context) { mcimadamore@1215: Resolve instance = context.get(resolveKey); mcimadamore@1215: if (instance == null) mcimadamore@1215: instance = new Resolve(context); mcimadamore@1215: return instance; mcimadamore@1215: } mcimadamore@1215: mcimadamore@1215: // 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: mcimadamore@1215: void reportVerboseResolutionDiagnostic(DiagnosticPosition dpos, Name name, Type site, mcimadamore@1215: List argtypes, List typeargtypes, Symbol bestSoFar) { mcimadamore@1215: boolean success = bestSoFar.kind < ERRONEOUS; mcimadamore@1215: mcimadamore@1215: if (success && !verboseResolutionMode.contains(VerboseResolutionMode.SUCCESS)) { mcimadamore@1215: return; mcimadamore@1215: } else if (!success && !verboseResolutionMode.contains(VerboseResolutionMode.FAILURE)) { mcimadamore@1215: return; mcimadamore@1215: } mcimadamore@1215: mcimadamore@1215: if (bestSoFar.name == names.init && mcimadamore@1215: bestSoFar.owner == syms.objectType.tsym && mcimadamore@1215: !verboseResolutionMode.contains(VerboseResolutionMode.OBJECT_INIT)) { mcimadamore@1215: return; //skip diags for Object constructor resolution mcimadamore@1215: } else if (site == syms.predefClass.type && mcimadamore@1215: !verboseResolutionMode.contains(VerboseResolutionMode.PREDEF)) { mcimadamore@1215: return; //skip spurious diags for predef symbols (i.e. operators) mcimadamore@1215: } else if (currentResolutionContext.internalResolution && mcimadamore@1215: !verboseResolutionMode.contains(VerboseResolutionMode.INTERNAL)) { mcimadamore@1215: return; mcimadamore@1215: } mcimadamore@1215: mcimadamore@1215: int pos = 0; mcimadamore@1215: int mostSpecificPos = -1; mcimadamore@1215: ListBuffer subDiags = ListBuffer.lb(); mcimadamore@1215: for (Candidate c : currentResolutionContext.candidates) { mcimadamore@1215: if (currentResolutionContext.step != c.step || mcimadamore@1215: (c.isApplicable() && !verboseResolutionMode.contains(VerboseResolutionMode.APPLICABLE)) || mcimadamore@1215: (!c.isApplicable() && !verboseResolutionMode.contains(VerboseResolutionMode.INAPPLICABLE))) { mcimadamore@1215: continue; mcimadamore@1215: } else { mcimadamore@1215: subDiags.append(c.isApplicable() ? mcimadamore@1215: getVerboseApplicableCandidateDiag(pos, c.sym, c.mtype) : mcimadamore@1215: getVerboseInapplicableCandidateDiag(pos, c.sym, c.details)); mcimadamore@1215: if (c.sym == bestSoFar) mcimadamore@1215: mostSpecificPos = pos; mcimadamore@1215: pos++; mcimadamore@1215: } mcimadamore@1215: } mcimadamore@1215: String key = success ? "verbose.resolve.multi" : "verbose.resolve.multi.1"; mcimadamore@1215: JCDiagnostic main = diags.note(log.currentSource(), dpos, key, name, mcimadamore@1215: site.tsym, mostSpecificPos, currentResolutionContext.step, mcimadamore@1215: methodArguments(argtypes), methodArguments(typeargtypes)); mcimadamore@1215: JCDiagnostic d = new JCDiagnostic.MultilineDiagnostic(main, subDiags.toList()); mcimadamore@1215: log.report(d); duke@1: } duke@1: mcimadamore@1215: JCDiagnostic getVerboseApplicableCandidateDiag(int pos, Symbol sym, Type inst) { mcimadamore@1215: JCDiagnostic subDiag = null; mcimadamore@1215: if (inst.getReturnType().tag == FORALL) { mcimadamore@1215: Type diagType = types.createMethodTypeWithReturn(inst.asMethodType(), mcimadamore@1215: ((ForAll)inst.getReturnType()).qtype); mcimadamore@1215: subDiag = diags.fragment("partial.inst.sig", diagType); mcimadamore@1215: } else if (sym.type.tag == FORALL) { mcimadamore@1215: subDiag = diags.fragment("full.inst.sig", inst.asMethodType()); mcimadamore@1215: } duke@1: mcimadamore@1215: String key = subDiag == null ? mcimadamore@1215: "applicable.method.found" : mcimadamore@1215: "applicable.method.found.1"; duke@1: mcimadamore@1215: return diags.fragment(key, pos, sym, subDiag); duke@1: } duke@1: mcimadamore@1215: JCDiagnostic getVerboseInapplicableCandidateDiag(int pos, Symbol sym, JCDiagnostic subDiag) { mcimadamore@1215: return diags.fragment("not.applicable.method.found", pos, sym, subDiag); mcimadamore@1215: } mcimadamore@1215: // 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@1186: /** mcimadamore@1186: * A check handler is used by the main method applicability routine in order mcimadamore@1186: * to handle specific method applicability failures. It is assumed that a class mcimadamore@1186: * implementing this interface should throw exceptions that are a subtype of mcimadamore@1186: * InapplicableMethodException (see below). Such exception will terminate the mcimadamore@1186: * method applicability check and propagate important info outwards (for the mcimadamore@1186: * purpose of generating better diagnostics). mcimadamore@1186: */ mcimadamore@1186: interface MethodCheckHandler { mcimadamore@1186: /* The number of actuals and formals differ */ mcimadamore@1186: InapplicableMethodException arityMismatch(); mcimadamore@1186: /* An actual argument type does not conform to the corresponding formal type */ mcimadamore@1186: InapplicableMethodException argumentMismatch(boolean varargs, Type found, Type expected); mcimadamore@1186: /* The element type of a varargs is not accessible in the current context */ mcimadamore@1186: InapplicableMethodException inaccessibleVarargs(Symbol location, Type expected); mcimadamore@1186: } mcimadamore@1186: mcimadamore@1186: /** mcimadamore@1186: * Basic method check handler used within Resolve - all methods end up mcimadamore@1186: * throwing InapplicableMethodException; a diagnostic fragment that describes mcimadamore@1186: * the cause as to why the method is not applicable is set on the exception mcimadamore@1186: * before it is thrown. mcimadamore@1186: */ mcimadamore@1186: MethodCheckHandler resolveHandler = new MethodCheckHandler() { mcimadamore@1186: public InapplicableMethodException arityMismatch() { mcimadamore@1186: return inapplicableMethodException.setMessage("arg.length.mismatch"); mcimadamore@1186: } mcimadamore@1186: public InapplicableMethodException argumentMismatch(boolean varargs, Type found, Type expected) { mcimadamore@1186: String key = varargs ? mcimadamore@1186: "varargs.argument.mismatch" : mcimadamore@1186: "no.conforming.assignment.exists"; mcimadamore@1186: return inapplicableMethodException.setMessage(key, mcimadamore@1186: found, expected); mcimadamore@1186: } mcimadamore@1186: public InapplicableMethodException inaccessibleVarargs(Symbol location, Type expected) { mcimadamore@1186: return inapplicableMethodException.setMessage("inaccessible.varargs.type", mcimadamore@1186: expected, Kinds.kindName(location), location); mcimadamore@1186: } mcimadamore@1186: }; mcimadamore@1186: 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) { mcimadamore@1186: checkRawArgumentsAcceptable(env, List.nil(), argtypes, formals, mcimadamore@1186: allowBoxing, useVarargs, warn, resolveHandler); mcimadamore@1186: } mcimadamore@1186: mcimadamore@1186: /** mcimadamore@1186: * Main method applicability routine. Given a list of actual types A, mcimadamore@1186: * a list of formal types F, determines whether the types in A are mcimadamore@1186: * compatible (by method invocation conversion) with the types in F. mcimadamore@1186: * mcimadamore@1186: * Since this routine is shared between overload resolution and method mcimadamore@1186: * type-inference, it is crucial that actual types are converted to the mcimadamore@1186: * corresponding 'undet' form (i.e. where inference variables are replaced mcimadamore@1186: * with undetvars) so that constraints can be propagated and collected. mcimadamore@1186: * mcimadamore@1186: * Moreover, if one or more types in A is a poly type, this routine calls mcimadamore@1186: * Infer.instantiateArg in order to complete the poly type (this might involve mcimadamore@1186: * deferred attribution). mcimadamore@1186: * mcimadamore@1186: * A method check handler (see above) is used in order to report errors. mcimadamore@1186: */ mcimadamore@1186: List checkRawArgumentsAcceptable(Env env, mcimadamore@1186: List undetvars, mcimadamore@1186: List argtypes, mcimadamore@1186: List formals, mcimadamore@1186: boolean allowBoxing, mcimadamore@1186: boolean useVarargs, mcimadamore@1186: Warner warn, mcimadamore@1186: MethodCheckHandler handler) { duke@1: Type varargsFormal = useVarargs ? formals.last() : null; mcimadamore@1186: ListBuffer checkedArgs = ListBuffer.lb(); mcimadamore@1186: mcimadamore@689: if (varargsFormal == null && mcimadamore@689: argtypes.size() != formals.size()) { mcimadamore@1186: throw handler.arityMismatch(); // not enough args mcimadamore@689: } mcimadamore@689: duke@1: while (argtypes.nonEmpty() && formals.head != varargsFormal) { mcimadamore@1186: Type undetFormal = infer.asUndetType(formals.head, undetvars); mcimadamore@1186: Type capturedActual = types.capture(argtypes.head); mcimadamore@1186: boolean works = allowBoxing ? mcimadamore@1186: types.isConvertible(capturedActual, undetFormal, warn) : mcimadamore@1186: types.isSubtypeUnchecked(capturedActual, undetFormal, warn); mcimadamore@1186: if (!works) { mcimadamore@1186: throw handler.argumentMismatch(false, argtypes.head, formals.head); mcimadamore@1186: } mcimadamore@1186: checkedArgs.append(capturedActual); duke@1: argtypes = argtypes.tail; duke@1: formals = formals.tail; duke@1: } mcimadamore@689: mcimadamore@1186: if (formals.head != varargsFormal) { mcimadamore@1186: throw handler.arityMismatch(); // not enough args mcimadamore@1186: } mcimadamore@689: mcimadamore@689: if (useVarargs) { mcimadamore@1186: //note: if applicability check is triggered by most specific test, mcimadamore@1186: //the last argument of a varargs is _not_ an array type (see JLS 15.12.2.5) mcimadamore@1006: Type elt = types.elemtype(varargsFormal); mcimadamore@1186: Type eltUndet = infer.asUndetType(elt, undetvars); mcimadamore@689: while (argtypes.nonEmpty()) { mcimadamore@1186: Type capturedActual = types.capture(argtypes.head); mcimadamore@1186: if (!types.isConvertible(capturedActual, eltUndet, warn)) { mcimadamore@1186: throw handler.argumentMismatch(true, argtypes.head, elt); mcimadamore@1186: } mcimadamore@1186: checkedArgs.append(capturedActual); mcimadamore@689: argtypes = argtypes.tail; mcimadamore@689: } mcimadamore@845: //check varargs element type accessibility mcimadamore@1186: if (undetvars.isEmpty() && !isAccessible(env, elt)) { mcimadamore@845: Symbol location = env.enclClass.sym; mcimadamore@1186: throw handler.inaccessibleVarargs(location, elt); mcimadamore@845: } duke@1: } mcimadamore@1186: return checkedArgs.toList(); 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@1215: if (!operator) mcimadamore@1215: currentResolutionContext.addApplicableCandidate(sym, mt); mcimadamore@689: } catch (InapplicableMethodException ex) { mcimadamore@1215: if (!operator) mcimadamore@1215: currentResolutionContext.addInapplicableCandidate(sym, ex.getDiagnostic()); duke@1: switch (bestSoFar.kind) { duke@1: case ABSENT_MTH: mcimadamore@1215: return wrongMethod; duke@1: case WRONG_MTH: mcimadamore@1110: if (operator) return bestSoFar; mcimadamore@689: case WRONG_MTHS: mcimadamore@1215: 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; mcimadamore@1215: } 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; 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) { 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: } 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@1215: MethodResolutionContext prevResolutionContext = currentResolutionContext; mcimadamore@1215: try { mcimadamore@1215: currentResolutionContext = new MethodResolutionContext(); mcimadamore@1215: Symbol sym = methodNotFound; mcimadamore@1215: List steps = methodResolutionSteps; mcimadamore@1215: while (steps.nonEmpty() && mcimadamore@1215: steps.head.isApplicable(boxingEnabled, varargsEnabled) && mcimadamore@1215: sym.kind >= ERRONEOUS) { mcimadamore@1215: currentResolutionContext.step = steps.head; mcimadamore@1215: sym = findFun(env, name, argtypes, typeargtypes, mcimadamore@1215: steps.head.isBoxingRequired, mcimadamore@1215: env.info.varArgs = steps.head.isVarargsRequired); mcimadamore@1215: currentResolutionContext.resolutionCache.put(steps.head, sym); mcimadamore@1215: steps = steps.tail; mcimadamore@1215: } mcimadamore@1215: if (sym.kind >= AMBIGUOUS) {//if nothing is found return the 'first' error mcimadamore@1215: MethodResolutionPhase errPhase = mcimadamore@1215: currentResolutionContext.firstErroneousResolutionPhase(); mcimadamore@1215: sym = access(currentResolutionContext.resolutionCache.get(errPhase), mcimadamore@1215: pos, env.enclClass.sym.type, name, false, argtypes, typeargtypes); mcimadamore@1215: env.info.varArgs = errPhase.isVarargsRequired; mcimadamore@1215: } mcimadamore@1215: return sym; duke@1: } mcimadamore@1215: finally { mcimadamore@1215: currentResolutionContext = prevResolutionContext; duke@1: } 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@1215: return resolveQualifiedMethod(new MethodResolutionContext(), pos, env, location, site, name, argtypes, typeargtypes); mcimadamore@1215: } mcimadamore@1215: private Symbol resolveQualifiedMethod(MethodResolutionContext resolveContext, mcimadamore@1215: DiagnosticPosition pos, Env env, mcimadamore@1215: Symbol location, Type site, Name name, List argtypes, mcimadamore@1215: List typeargtypes) { mcimadamore@1215: MethodResolutionContext prevResolutionContext = currentResolutionContext; mcimadamore@1215: try { mcimadamore@1215: currentResolutionContext = resolveContext; mcimadamore@1215: Symbol sym = methodNotFound; mcimadamore@1215: List steps = methodResolutionSteps; mcimadamore@1215: while (steps.nonEmpty() && mcimadamore@1215: steps.head.isApplicable(boxingEnabled, varargsEnabled) && mcimadamore@1215: sym.kind >= ERRONEOUS) { mcimadamore@1215: currentResolutionContext.step = steps.head; mcimadamore@1215: sym = findMethod(env, site, name, argtypes, typeargtypes, mcimadamore@1215: steps.head.isBoxingRequired(), mcimadamore@1215: env.info.varArgs = steps.head.isVarargsRequired(), false); mcimadamore@1215: currentResolutionContext.resolutionCache.put(steps.head, sym); mcimadamore@1215: steps = steps.tail; mcimadamore@1215: } mcimadamore@1215: if (sym.kind >= AMBIGUOUS) { mcimadamore@1215: if (site.tsym.isPolymorphicSignatureGeneric()) { mcimadamore@1215: //polymorphic receiver - synthesize new method symbol mcimadamore@1215: env.info.varArgs = false; mcimadamore@1215: sym = findPolymorphicSignatureInstance(env, mcimadamore@1215: site, name, null, argtypes); mcimadamore@1215: } mcimadamore@1215: else { mcimadamore@1215: //if nothing is found return the 'first' error mcimadamore@1215: MethodResolutionPhase errPhase = mcimadamore@1215: currentResolutionContext.firstErroneousResolutionPhase(); mcimadamore@1215: sym = access(currentResolutionContext.resolutionCache.get(errPhase), mcimadamore@1215: pos, location, site, name, true, argtypes, typeargtypes); mcimadamore@1215: env.info.varArgs = errPhase.isVarargsRequired; mcimadamore@1215: } mcimadamore@1215: } else if (allowMethodHandles && sym.isPolymorphicSignatureGeneric()) { mcimadamore@1215: //non-instantiated polymorphic signature - synthesize new method symbol mcimadamore@674: env.info.varArgs = false; mcimadamore@674: sym = findPolymorphicSignatureInstance(env, mcimadamore@1215: site, name, (MethodSymbol)sym, argtypes); mcimadamore@674: } mcimadamore@1215: return sym; duke@1: } mcimadamore@1215: finally { mcimadamore@1215: currentResolutionContext = prevResolutionContext; mcimadamore@1215: } 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@1215: MethodResolutionContext resolveContext = new MethodResolutionContext(); mcimadamore@1215: resolveContext.internalResolution = true; mcimadamore@1215: Symbol sym = resolveQualifiedMethod(resolveContext, pos, env, site.tsym, mcimadamore@1215: site, name, argtypes, typeargtypes); mcimadamore@1215: if (sym.kind == MTH) return (MethodSymbol)sym; mcimadamore@1215: else throw new FatalError( mcimadamore@1215: diags.fragment("fatal.err.cant.locate.meth", mcimadamore@1215: 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@1215: return resolveConstructor(new MethodResolutionContext(), pos, env, site, argtypes, typeargtypes); mcimadamore@1215: } mcimadamore@1215: private Symbol resolveConstructor(MethodResolutionContext resolveContext, mcimadamore@1215: DiagnosticPosition pos, mcimadamore@1215: Env env, mcimadamore@1215: Type site, mcimadamore@1215: List argtypes, mcimadamore@1215: List typeargtypes) { mcimadamore@1215: MethodResolutionContext prevResolutionContext = currentResolutionContext; mcimadamore@1215: try { mcimadamore@1215: currentResolutionContext = resolveContext; mcimadamore@1215: Symbol sym = methodNotFound; mcimadamore@1215: List steps = methodResolutionSteps; mcimadamore@1215: while (steps.nonEmpty() && mcimadamore@1215: steps.head.isApplicable(boxingEnabled, varargsEnabled) && mcimadamore@1215: sym.kind >= ERRONEOUS) { mcimadamore@1215: currentResolutionContext.step = steps.head; mcimadamore@1215: sym = findConstructor(pos, env, site, argtypes, typeargtypes, mcimadamore@1215: steps.head.isBoxingRequired(), mcimadamore@1215: env.info.varArgs = steps.head.isVarargsRequired()); mcimadamore@1215: currentResolutionContext.resolutionCache.put(steps.head, sym); mcimadamore@1215: steps = steps.tail; mcimadamore@1215: } mcimadamore@1215: if (sym.kind >= AMBIGUOUS) {//if nothing is found return the 'first' error mcimadamore@1215: MethodResolutionPhase errPhase = currentResolutionContext.firstErroneousResolutionPhase(); mcimadamore@1215: sym = access(currentResolutionContext.resolutionCache.get(errPhase), mcimadamore@1215: pos, site, names.init, true, argtypes, typeargtypes); mcimadamore@1215: env.info.varArgs = errPhase.isVarargsRequired(); mcimadamore@1215: } mcimadamore@1215: return sym; duke@1: } mcimadamore@1215: finally { mcimadamore@1215: currentResolutionContext = prevResolutionContext; duke@1: } 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@1215: MethodResolutionContext prevResolutionContext = currentResolutionContext; mcimadamore@1215: try { mcimadamore@1215: currentResolutionContext = new MethodResolutionContext(); mcimadamore@1215: Symbol sym = methodNotFound; mcimadamore@1215: List steps = methodResolutionSteps; mcimadamore@1215: while (steps.nonEmpty() && mcimadamore@1215: steps.head.isApplicable(boxingEnabled, varargsEnabled) && mcimadamore@1215: sym.kind >= ERRONEOUS) { mcimadamore@1215: currentResolutionContext.step = steps.head; mcimadamore@1217: sym = findDiamond(env, site, argtypes, typeargtypes, mcimadamore@1215: steps.head.isBoxingRequired(), mcimadamore@1215: env.info.varArgs = steps.head.isVarargsRequired()); mcimadamore@1215: currentResolutionContext.resolutionCache.put(steps.head, sym); mcimadamore@1215: steps = steps.tail; mcimadamore@1215: } mcimadamore@1215: if (sym.kind >= AMBIGUOUS) { mcimadamore@1215: final JCDiagnostic details = sym.kind == WRONG_MTH ? mcimadamore@1215: currentResolutionContext.candidates.head.details : mcimadamore@1215: null; mcimadamore@1215: Symbol errSym = new ResolveError(WRONG_MTH, "diamond error") { mcimadamore@1215: @Override mcimadamore@1215: JCDiagnostic getDiagnostic(DiagnosticType dkind, DiagnosticPosition pos, mcimadamore@1215: Symbol location, Type site, Name name, List argtypes, List typeargtypes) { mcimadamore@1215: String key = details == null ? mcimadamore@1215: "cant.apply.diamond" : mcimadamore@1215: "cant.apply.diamond.1"; mcimadamore@1215: return diags.create(dkind, log.currentSource(), pos, key, mcimadamore@1215: diags.fragment("diamond", site.tsym), details); mcimadamore@1215: } mcimadamore@1215: }; mcimadamore@1215: MethodResolutionPhase errPhase = currentResolutionContext.firstErroneousResolutionPhase(); mcimadamore@1215: sym = access(errSym, pos, site, names.init, true, argtypes, typeargtypes); mcimadamore@1215: env.info.varArgs = errPhase.isVarargsRequired(); mcimadamore@1215: } mcimadamore@1215: return sym; mcimadamore@537: } mcimadamore@1215: finally { mcimadamore@1215: currentResolutionContext = prevResolutionContext; mcimadamore@537: } mcimadamore@537: } mcimadamore@537: mcimadamore@1217: /** This method scans all the constructor symbol in a given class scope - mcimadamore@1217: * assuming that the original scope contains a constructor of the kind: mcimadamore@1217: * Foo(X x, Y y), where X,Y are class type-variables declared in Foo, mcimadamore@1217: * a method check is executed against the modified constructor type: mcimadamore@1217: * Foo(X x, Y y). This is crucial in order to enable diamond mcimadamore@1217: * inference. The inferred return type of the synthetic constructor IS mcimadamore@1217: * the inferred type for the diamond operator. mcimadamore@1217: */ mcimadamore@1217: private Symbol findDiamond(Env env, mcimadamore@1217: Type site, mcimadamore@1217: List argtypes, mcimadamore@1217: List typeargtypes, mcimadamore@1217: boolean allowBoxing, mcimadamore@1217: boolean useVarargs) { mcimadamore@1217: Symbol bestSoFar = methodNotFound; mcimadamore@1217: for (Scope.Entry e = site.tsym.members().lookup(names.init); mcimadamore@1217: e.scope != null; mcimadamore@1217: e = e.next()) { mcimadamore@1217: //- System.out.println(" e " + e.sym); mcimadamore@1217: if (e.sym.kind == MTH && mcimadamore@1217: (e.sym.flags_field & SYNTHETIC) == 0) { mcimadamore@1217: List oldParams = e.sym.type.tag == FORALL ? mcimadamore@1217: ((ForAll)e.sym.type).tvars : mcimadamore@1217: List.nil(); mcimadamore@1217: Type constrType = new ForAll(site.tsym.type.getTypeArguments().appendList(oldParams), mcimadamore@1217: types.createMethodTypeWithReturn(e.sym.type.asMethodType(), site)); mcimadamore@1217: bestSoFar = selectBest(env, site, argtypes, typeargtypes, mcimadamore@1217: new MethodSymbol(e.sym.flags(), names.init, constrType, site.tsym), mcimadamore@1217: bestSoFar, mcimadamore@1217: allowBoxing, mcimadamore@1217: useVarargs, mcimadamore@1217: false); mcimadamore@1217: } mcimadamore@1217: } mcimadamore@1217: return bestSoFar; mcimadamore@1217: } mcimadamore@1217: 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) { mcimadamore@1215: MethodResolutionContext prevResolutionContext = currentResolutionContext; mcimadamore@1215: try { mcimadamore@1215: currentResolutionContext = new MethodResolutionContext(); mcimadamore@1215: return findConstructor(pos, env, site, argtypes, typeargtypes, allowBoxing, useVarargs); mcimadamore@1215: } mcimadamore@1215: finally { mcimadamore@1215: currentResolutionContext = prevResolutionContext; mcimadamore@1215: } mcimadamore@1215: } mcimadamore@1215: mcimadamore@1215: Symbol findConstructor(DiagnosticPosition pos, Env env, mcimadamore@1215: Type site, List argtypes, mcimadamore@1215: List typeargtypes, mcimadamore@1215: boolean allowBoxing, mcimadamore@1215: boolean useVarargs) { duke@1: Symbol sym = findMethod(env, site, mcimadamore@1215: names.init, argtypes, mcimadamore@1215: typeargtypes, allowBoxing, mcimadamore@1215: 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) { mcimadamore@1215: MethodResolutionContext resolveContext = new MethodResolutionContext(); mcimadamore@1215: resolveContext.internalResolution = true; mcimadamore@1215: Symbol sym = resolveConstructor(resolveContext, 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@1215: MethodResolutionContext prevResolutionContext = currentResolutionContext; mcimadamore@1215: try { mcimadamore@1215: currentResolutionContext = new MethodResolutionContext(); mcimadamore@1215: Name name = treeinfo.operatorName(optag); mcimadamore@1215: Symbol sym = findMethod(env, syms.predefClass.type, name, argtypes, mcimadamore@1215: null, false, false, true); mcimadamore@1215: if (boxingEnabled && sym.kind >= WRONG_MTHS) mcimadamore@1215: sym = findMethod(env, syms.predefClass.type, name, argtypes, mcimadamore@1215: null, true, false, true); mcimadamore@1215: return access(sym, pos, env.enclClass.sym.type, name, mcimadamore@1215: false, argtypes, null); mcimadamore@1215: } mcimadamore@1215: finally { mcimadamore@1215: currentResolutionContext = prevResolutionContext; mcimadamore@1215: } 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@1215: class InapplicableSymbolError extends ResolveError { mcimadamore@302: mcimadamore@1215: InapplicableSymbolError() { mcimadamore@1215: super(WRONG_MTH, "inapplicable symbol error"); mcimadamore@302: } mcimadamore@302: mcimadamore@1215: protected InapplicableSymbolError(int kind, String debugName) { mcimadamore@1215: super(kind, debugName); mcimadamore@302: } mcimadamore@302: mcimadamore@302: @Override mcimadamore@302: public String toString() { mcimadamore@1215: return super.toString(); mcimadamore@1215: } mcimadamore@1215: mcimadamore@1215: @Override mcimadamore@1215: public boolean exists() { mcimadamore@1215: return true; 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@1215: Candidate c = errCandidate(); mcimadamore@1215: Symbol ws = c.sym.asMemberOf(site, types); jjg@612: return diags.create(dkind, log.currentSource(), pos, mcimadamore@1215: "cant.apply.symbol" + (c.details != 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@1215: c.details); 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@1215: mcimadamore@1215: protected boolean shouldReport(Candidate c) { mcimadamore@1215: return !c.isApplicable() && mcimadamore@1215: (((c.sym.flags() & VARARGS) != 0 && c.step == VARARITY) || mcimadamore@1215: (c.sym.flags() & VARARGS) == 0 && c.step == (boxingEnabled ? BOX : BASIC)); mcimadamore@1215: } mcimadamore@1215: mcimadamore@1215: private Candidate errCandidate() { mcimadamore@1215: for (Candidate c : currentResolutionContext.candidates) { mcimadamore@1215: if (shouldReport(c)) { mcimadamore@1215: return c; mcimadamore@1215: } mcimadamore@1215: } mcimadamore@1215: Assert.error(); mcimadamore@1215: return null; mcimadamore@1215: } 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@1215: class InapplicableSymbolsError extends InapplicableSymbolError { mcimadamore@689: mcimadamore@1215: InapplicableSymbolsError() { 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@1215: if (currentResolutionContext.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@1215: for (Candidate c : currentResolutionContext.candidates) { mcimadamore@1215: if (!shouldReport(c)) continue; mcimadamore@1215: JCDiagnostic detailDiag = diags.fragment("inapplicable.method", mcimadamore@1215: Kinds.kindName(c.sym), mcimadamore@1215: c.sym.location(site, types), mcimadamore@1215: c.sym.asMemberOf(site, types), mcimadamore@1215: c.details); mcimadamore@1215: details = details.prepend(detailDiag); mcimadamore@1215: } mcimadamore@689: return details.reverse(); mcimadamore@689: } mcimadamore@689: mcimadamore@689: private Name getName() { mcimadamore@1215: Symbol sym = currentResolutionContext.candidates.head.sym; mcimadamore@689: return sym.name == names.init ? mcimadamore@689: sym.owner.name : mcimadamore@689: sym.name; mcimadamore@689: } 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: final List methodResolutionSteps = List.of(BASIC, BOX, VARARITY); mcimadamore@160: mcimadamore@1215: /** mcimadamore@1215: * A resolution context is used to keep track of intermediate results of mcimadamore@1215: * overload resolution, such as list of method that are not applicable mcimadamore@1215: * (used to generate more precise diagnostics) and so on. Resolution contexts mcimadamore@1215: * can be nested - this means that when each overload resolution routine should mcimadamore@1215: * work within the resolution context it created. mcimadamore@1215: */ mcimadamore@1215: class MethodResolutionContext { mcimadamore@689: mcimadamore@1215: private List candidates = List.nil(); mcimadamore@1114: mcimadamore@1215: private Map resolutionCache = mcimadamore@1215: new EnumMap(MethodResolutionPhase.class); mcimadamore@1215: mcimadamore@1215: private MethodResolutionPhase step = null; mcimadamore@1215: mcimadamore@1215: private boolean internalResolution = false; mcimadamore@1215: mcimadamore@1215: private MethodResolutionPhase firstErroneousResolutionPhase() { mcimadamore@1215: MethodResolutionPhase bestSoFar = BASIC; mcimadamore@1215: Symbol sym = methodNotFound; mcimadamore@1215: List steps = methodResolutionSteps; mcimadamore@1215: while (steps.nonEmpty() && mcimadamore@1215: steps.head.isApplicable(boxingEnabled, varargsEnabled) && mcimadamore@1215: sym.kind >= WRONG_MTHS) { mcimadamore@1215: sym = resolutionCache.get(steps.head); mcimadamore@1215: bestSoFar = steps.head; mcimadamore@1215: steps = steps.tail; mcimadamore@1215: } mcimadamore@1215: return bestSoFar; mcimadamore@160: } mcimadamore@1215: mcimadamore@1215: void addInapplicableCandidate(Symbol sym, JCDiagnostic details) { mcimadamore@1215: Candidate c = new Candidate(currentResolutionContext.step, sym, details, null); mcimadamore@1215: if (!candidates.contains(c)) mcimadamore@1215: candidates = candidates.append(c); mcimadamore@1215: } mcimadamore@1215: mcimadamore@1215: void addApplicableCandidate(Symbol sym, Type mtype) { mcimadamore@1215: Candidate c = new Candidate(currentResolutionContext.step, sym, null, mtype); mcimadamore@1215: candidates = candidates.append(c); mcimadamore@1215: } mcimadamore@1215: mcimadamore@1215: /** mcimadamore@1215: * This class represents an overload resolution candidate. There are two mcimadamore@1215: * kinds of candidates: applicable methods and inapplicable methods; mcimadamore@1215: * applicable methods have a pointer to the instantiated method type, mcimadamore@1215: * while inapplicable candidates contain further details about the mcimadamore@1215: * reason why the method has been considered inapplicable. mcimadamore@1215: */ mcimadamore@1215: class Candidate { mcimadamore@1215: mcimadamore@1215: final MethodResolutionPhase step; mcimadamore@1215: final Symbol sym; mcimadamore@1215: final JCDiagnostic details; mcimadamore@1215: final Type mtype; mcimadamore@1215: mcimadamore@1215: private Candidate(MethodResolutionPhase step, Symbol sym, JCDiagnostic details, Type mtype) { mcimadamore@1215: this.step = step; mcimadamore@1215: this.sym = sym; mcimadamore@1215: this.details = details; mcimadamore@1215: this.mtype = mtype; mcimadamore@1215: } mcimadamore@1215: mcimadamore@1215: @Override mcimadamore@1215: public boolean equals(Object o) { mcimadamore@1215: if (o instanceof Candidate) { mcimadamore@1215: Symbol s1 = this.sym; mcimadamore@1215: Symbol s2 = ((Candidate)o).sym; mcimadamore@1215: if ((s1 != s2 && mcimadamore@1215: (s1.overrides(s2, s1.owner.type.tsym, types, false) || mcimadamore@1215: (s2.overrides(s1, s2.owner.type.tsym, types, false)))) || mcimadamore@1215: ((s1.isConstructor() || s2.isConstructor()) && s1.owner != s2.owner)) mcimadamore@1215: return true; mcimadamore@1215: } mcimadamore@1215: return false; mcimadamore@1215: } mcimadamore@1215: mcimadamore@1215: boolean isApplicable() { mcimadamore@1215: return mtype != null; mcimadamore@1215: } mcimadamore@1215: } mcimadamore@160: } mcimadamore@1215: mcimadamore@1215: MethodResolutionContext currentResolutionContext = null; duke@1: }