duke@1: /*
vromero@2260: * Copyright (c) 1999, 2014, 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:
vromero@2193: import com.sun.source.tree.MemberReferenceTree.ReferenceMode;
mcimadamore@1114: import com.sun.tools.javac.api.Formattable.LocalizedString;
duke@1: import com.sun.tools.javac.code.*;
mcimadamore@1342: import com.sun.tools.javac.code.Symbol.*;
mcimadamore@1114: import com.sun.tools.javac.code.Type.*;
mcimadamore@1238: import com.sun.tools.javac.comp.Attr.ResultInfo;
mcimadamore@1238: import com.sun.tools.javac.comp.Check.CheckContext;
mcimadamore@1347: import com.sun.tools.javac.comp.DeferredAttr.AttrMode;
mcimadamore@1347: import com.sun.tools.javac.comp.DeferredAttr.DeferredAttrContext;
mcimadamore@1347: import com.sun.tools.javac.comp.DeferredAttr.DeferredType;
mcimadamore@1337: import com.sun.tools.javac.comp.Infer.InferenceContext;
mcimadamore@1550: import com.sun.tools.javac.comp.Infer.FreeTypeListener;
mcimadamore@1215: import com.sun.tools.javac.comp.Resolve.MethodResolutionContext.Candidate;
mcimadamore@1759: import com.sun.tools.javac.comp.Resolve.MethodResolutionDiagHelper.DiagnosticRewriter;
mcimadamore@1759: import com.sun.tools.javac.comp.Resolve.MethodResolutionDiagHelper.Template;
duke@1: import com.sun.tools.javac.jvm.*;
mcimadamore@1759: import com.sun.tools.javac.main.Option;
duke@1: import com.sun.tools.javac.tree.*;
mcimadamore@1114: import com.sun.tools.javac.tree.JCTree.*;
mcimadamore@1352: import com.sun.tools.javac.tree.JCTree.JCMemberReference.ReferenceKind;
mcimadamore@1510: import com.sun.tools.javac.tree.JCTree.JCPolyExpression.*;
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@1342: import java.util.Iterator;
mcimadamore@1394: import java.util.LinkedHashMap;
mcimadamore@1394: import java.util.LinkedHashSet;
mcimadamore@1114: import java.util.Map;
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;
jjg@1374: import static com.sun.tools.javac.code.TypeTag.*;
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;
mcimadamore@1238: Attr attr;
mcimadamore@1347: DeferredAttr deferredAttr;
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;
vromero@2261: public final boolean boxingEnabled;
vromero@2261: public final boolean varargsEnabled;
mcimadamore@674: public final boolean allowMethodHandles;
dlsmith@2395: public final boolean allowFunctionalInterfaceMostSpecific;
vromero@2535: public final boolean checkVarargsAccessAfterResolution;
duke@1: private final boolean debugResolve;
mcimadamore@1759: private final boolean compactMethodDiags;
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: methodNotFound = new
mcimadamore@1215: SymbolNotFoundError(ABSENT_MTH);
vromero@2193: methodWithCorrectStaticnessNotFound = new
vromero@2193: SymbolNotFoundError(WRONG_STATICNESS,
vromero@2193: "method found has incorrect staticness");
mcimadamore@1215: typeNotFound = new
mcimadamore@1215: SymbolNotFoundError(ABSENT_TYP);
mcimadamore@1215:
mcimadamore@1215: names = Names.instance(context);
mcimadamore@1215: log = Log.instance(context);
mcimadamore@1238: attr = Attr.instance(context);
mcimadamore@1347: deferredAttr = DeferredAttr.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@1759: compactMethodDiags = options.isSet(Option.XDIAGS, "compact") ||
mcimadamore@1759: options.isUnset(Option.XDIAGS) && options.isUnset("rawDiagnostics");
mcimadamore@1215: verboseResolutionMode = VerboseResolutionMode.getVerboseResolutionMode(options);
mcimadamore@1215: Target target = Target.instance(context);
mcimadamore@1215: allowMethodHandles = target.hasMethodHandles();
dlsmith@2395: allowFunctionalInterfaceMostSpecific = source.allowFunctionalInterfaceMostSpecific();
vromero@2535: checkVarargsAccessAfterResolution =
vromero@2531: source.allowPostApplicabilityVarargsAccessCheck();
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 SymbolNotFoundError methodNotFound;
vromero@2193: private final SymbolNotFoundError methodWithCorrectStaticnessNotFound;
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:
vromero@1442: final 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;
alundblad@2047: ListBuffer subDiags = new ListBuffer<>();
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@1347: List argtypes2 = Type.map(argtypes,
mcimadamore@1347: deferredAttr.new RecoveryDeferredTypeMap(AttrMode.SPECULATIVE, bestSoFar, currentResolutionContext.step));
mcimadamore@1215: JCDiagnostic main = diags.note(log.currentSource(), dpos, key, name,
mcimadamore@1215: site.tsym, mostSpecificPos, currentResolutionContext.step,
mcimadamore@1347: methodArguments(argtypes2),
mcimadamore@1347: 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;
jjg@1374: if (sym.type.hasTag(FORALL)) {
mcimadamore@1268: subDiag = diags.fragment("partial.inst.sig", inst);
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: */
ksrini@1346: protected static boolean isStatic(Env env) {
alundblad@2814: return env.outer != null && 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) {
jjg@1374: return (t.hasTag(ARRAY))
dlsmith@2400: ? isAccessible(env, types.cvarUpperBound(types.elemtype(t)))
mcimadamore@741: : isAccessible(env, t.tsym, checkInner);
duke@1: }
duke@1:
vromero@1790: /** Is symbol accessible as a member of given type in given environment?
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@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) {
vromero@2117: Type newSite = site.hasTag(TYPEVAR) ? site.getUpperBound() : 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.
vromero@2117: ((sym.flags() & STATIC) != 0 || sym.kind == TYP || newSite.tsym.isSubClass(c, types))))
duke@1: c = c.owner.enclClass();
duke@1: return c != null;
duke@1: }
duke@1:
mcimadamore@1415: /**
mcimadamore@1415: * Performs a recursive scan of a type looking for accessibility problems
mcimadamore@1415: * from current attribution environment
mcimadamore@1415: */
mcimadamore@1415: void checkAccessibleType(Env env, Type t) {
mcimadamore@1415: accessibilityChecker.visit(t, env);
mcimadamore@1415: }
mcimadamore@1415:
mcimadamore@1415: /**
mcimadamore@1415: * Accessibility type-visitor
mcimadamore@1415: */
mcimadamore@1415: Types.SimpleVisitor> accessibilityChecker =
mcimadamore@1415: new Types.SimpleVisitor>() {
mcimadamore@1415:
mcimadamore@1415: void visit(List ts, Env env) {
mcimadamore@1415: for (Type t : ts) {
mcimadamore@1415: visit(t, env);
mcimadamore@1415: }
mcimadamore@1415: }
mcimadamore@1415:
mcimadamore@1415: public Void visitType(Type t, Env env) {
mcimadamore@1415: return null;
mcimadamore@1415: }
mcimadamore@1415:
mcimadamore@1415: @Override
mcimadamore@1415: public Void visitArrayType(ArrayType t, Env env) {
mcimadamore@1415: visit(t.elemtype, env);
mcimadamore@1415: return null;
mcimadamore@1415: }
mcimadamore@1415:
mcimadamore@1415: @Override
mcimadamore@1415: public Void visitClassType(ClassType t, Env env) {
mcimadamore@1415: visit(t.getTypeArguments(), env);
mcimadamore@1415: if (!isAccessible(env, t, true)) {
mcimadamore@1415: accessBase(new AccessError(t.tsym), env.tree.pos(), env.enclClass.sym, t, t.tsym.name, true);
mcimadamore@1415: }
mcimadamore@1415: return null;
mcimadamore@1415: }
mcimadamore@1415:
mcimadamore@1415: @Override
mcimadamore@1415: public Void visitWildcardType(WildcardType t, Env env) {
mcimadamore@1415: visit(t.type, env);
mcimadamore@1415: return null;
mcimadamore@1415: }
mcimadamore@1415:
mcimadamore@1415: @Override
mcimadamore@1415: public Void visitMethodType(MethodType t, Env env) {
mcimadamore@1415: visit(t.getParameterTypes(), env);
mcimadamore@1415: visit(t.getReturnType(), env);
mcimadamore@1415: visit(t.getThrownTypes(), env);
mcimadamore@1415: return null;
mcimadamore@1415: }
mcimadamore@1415: };
mcimadamore@1415:
duke@1: /** Try to instantiate the type of a method so that it fits
vromero@1790: * given type arguments and argument types. If successful, 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
vromero@1790: * of an under determined 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,
mcimadamore@1268: ResultInfo resultInfo,
duke@1: List argtypes,
duke@1: List typeargtypes,
duke@1: boolean allowBoxing,
duke@1: boolean useVarargs,
mcimadamore@1394: Warner warn) throws Infer.InferenceException {
mcimadamore@1394:
duke@1: Type mt = types.memberType(site, m);
duke@1: // tvars is the list of formal type variables for which type arguments
duke@1: // need to inferred.
mcimadamore@1268: List tvars = List.nil();
duke@1: if (typeargtypes == null) typeargtypes = List.nil();
jjg@1374: if (!mt.hasTag(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
jjg@1374: } else if (mt.hasTag(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);
jjg@1374: } else if (mt.hasTag(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@1239: boolean instNeeded = tvars.tail != null; /*inlined: tvars.nonEmpty()*/
duke@1: for (List l = argtypes;
duke@1: l.tail != null/*inlined: l.nonEmpty()*/ && !instNeeded;
duke@1: l = l.tail) {
jjg@1374: if (l.head.hasTag(FORALL)) instNeeded = true;
duke@1: }
duke@1:
duke@1: if (instNeeded)
mcimadamore@1239: return infer.instantiateMethod(env,
mcimadamore@547: tvars,
duke@1: (MethodType)mt,
mcimadamore@1268: resultInfo,
vromero@2382: (MethodSymbol)m,
duke@1: argtypes,
duke@1: allowBoxing,
duke@1: useVarargs,
mcimadamore@1347: currentResolutionContext,
duke@1: warn);
mcimadamore@689:
vromero@2000: DeferredAttr.DeferredAttrContext dc = currentResolutionContext.deferredAttrContext(m, infer.emptyContext, resultInfo, warn);
vromero@2000: currentResolutionContext.methodCheck.argumentsAcceptable(env, dc,
mcimadamore@1479: argtypes, mt.getParameterTypes(), warn);
vromero@2000: dc.complete();
mcimadamore@689: return mt;
duke@1: }
duke@1:
mcimadamore@1347: Type checkMethod(Env env,
mcimadamore@1347: Type site,
mcimadamore@1347: Symbol m,
mcimadamore@1347: ResultInfo resultInfo,
mcimadamore@1347: List argtypes,
mcimadamore@1347: List typeargtypes,
mcimadamore@1347: Warner warn) {
mcimadamore@1347: MethodResolutionContext prevContext = currentResolutionContext;
mcimadamore@1347: try {
mcimadamore@1347: currentResolutionContext = new MethodResolutionContext();
mcimadamore@1347: currentResolutionContext.attrMode = DeferredAttr.AttrMode.CHECK;
mcimadamore@1897: if (env.tree.hasTag(JCTree.Tag.REFERENCE)) {
mcimadamore@1897: //method/constructor references need special check class
mcimadamore@1897: //to handle inference variables in 'argtypes' (might happen
mcimadamore@1897: //during an unsticking round)
mcimadamore@1897: currentResolutionContext.methodCheck =
mcimadamore@1897: new MethodReferenceCheck(resultInfo.checkContext.inferenceContext());
mcimadamore@1897: }
mcimadamore@1347: MethodResolutionPhase step = currentResolutionContext.step = env.info.pendingResolutionPhase;
mcimadamore@1347: return rawInstantiate(env, site, m, resultInfo, argtypes, typeargtypes,
mcimadamore@1674: step.isBoxingRequired(), step.isVarargsRequired(), warn);
mcimadamore@1347: }
mcimadamore@1347: finally {
mcimadamore@1347: currentResolutionContext = prevContext;
mcimadamore@1347: }
mcimadamore@1347: }
mcimadamore@1347:
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,
mcimadamore@1268: ResultInfo resultInfo,
duke@1: List argtypes,
duke@1: List typeargtypes,
duke@1: boolean allowBoxing,
duke@1: boolean useVarargs,
duke@1: Warner warn) {
duke@1: try {
mcimadamore@1268: return rawInstantiate(env, site, m, resultInfo, argtypes, typeargtypes,
mcimadamore@1674: allowBoxing, useVarargs, warn);
mcimadamore@689: } catch (InapplicableMethodException ex) {
duke@1: return null;
duke@1: }
duke@1: }
duke@1:
mcimadamore@1479: /**
mcimadamore@1479: * This interface defines an entry point that should be used to perform a
mcimadamore@1479: * method check. A method check usually consist in determining as to whether
mcimadamore@1479: * a set of types (actuals) is compatible with another set of types (formals).
mcimadamore@1479: * Since the notion of compatibility can vary depending on the circumstances,
mcimadamore@1479: * this interfaces allows to easily add new pluggable method check routines.
duke@1: */
mcimadamore@1479: interface MethodCheck {
mcimadamore@1479: /**
mcimadamore@1479: * Main method check routine. A method check usually consist in determining
mcimadamore@1479: * as to whether a set of types (actuals) is compatible with another set of
mcimadamore@1479: * types (formals). If an incompatibility is found, an unchecked exception
mcimadamore@1479: * is assumed to be thrown.
mcimadamore@1479: */
mcimadamore@1479: void argumentsAcceptable(Env env,
mcimadamore@1479: DeferredAttrContext deferredAttrContext,
mcimadamore@845: List argtypes,
duke@1: List formals,
mcimadamore@1479: Warner warn);
mcimadamore@1674:
mcimadamore@1674: /**
mcimadamore@1674: * Retrieve the method check object that will be used during a
mcimadamore@1674: * most specific check.
mcimadamore@1674: */
mcimadamore@1674: MethodCheck mostSpecificCheck(List actuals, boolean strict);
mcimadamore@1479: }
mcimadamore@1479:
mcimadamore@1479: /**
mcimadamore@1479: * Helper enum defining all method check diagnostics (used by resolveMethodCheck).
mcimadamore@1479: */
mcimadamore@1479: enum MethodCheckDiag {
mcimadamore@1479: /**
mcimadamore@1479: * Actuals and formals differs in length.
mcimadamore@1479: */
mcimadamore@1479: ARITY_MISMATCH("arg.length.mismatch", "infer.arg.length.mismatch"),
mcimadamore@1479: /**
mcimadamore@1479: * An actual is incompatible with a formal.
mcimadamore@1479: */
mcimadamore@1479: ARG_MISMATCH("no.conforming.assignment.exists", "infer.no.conforming.assignment.exists"),
mcimadamore@1479: /**
mcimadamore@1479: * An actual is incompatible with the varargs element type.
mcimadamore@1479: */
mcimadamore@1479: VARARG_MISMATCH("varargs.argument.mismatch", "infer.varargs.argument.mismatch"),
mcimadamore@1479: /**
mcimadamore@1479: * The varargs element type is inaccessible.
mcimadamore@1479: */
mcimadamore@1479: INACCESSIBLE_VARARGS("inaccessible.varargs.type", "inaccessible.varargs.type");
mcimadamore@1479:
mcimadamore@1479: final String basicKey;
mcimadamore@1479: final String inferKey;
mcimadamore@1479:
mcimadamore@1479: MethodCheckDiag(String basicKey, String inferKey) {
mcimadamore@1479: this.basicKey = basicKey;
mcimadamore@1479: this.inferKey = inferKey;
mcimadamore@689: }
mcimadamore@1759:
mcimadamore@1759: String regex() {
mcimadamore@1759: return String.format("([a-z]*\\.)*(%s|%s)", basicKey, inferKey);
mcimadamore@1759: }
mcimadamore@689: }
mcimadamore@1186:
mcimadamore@1186: /**
mcimadamore@1674: * Dummy method check object. All methods are deemed applicable, regardless
mcimadamore@1674: * of their formal parameter types.
mcimadamore@1674: */
mcimadamore@1674: MethodCheck nilMethodCheck = new MethodCheck() {
mcimadamore@1674: public void argumentsAcceptable(Env env, DeferredAttrContext deferredAttrContext, List argtypes, List formals, Warner warn) {
mcimadamore@1674: //do nothing - method always applicable regardless of actuals
mcimadamore@1674: }
mcimadamore@1674:
mcimadamore@1674: public MethodCheck mostSpecificCheck(List actuals, boolean strict) {
mcimadamore@1674: return this;
mcimadamore@1674: }
mcimadamore@1674: };
mcimadamore@1674:
mcimadamore@1674: /**
mcimadamore@1674: * Base class for 'real' method checks. The class defines the logic for
mcimadamore@1674: * iterating through formals and actuals and provides and entry point
mcimadamore@1674: * that can be used by subclasses in order to define the actual check logic.
mcimadamore@1674: */
mcimadamore@1674: abstract class AbstractMethodCheck implements MethodCheck {
mcimadamore@1674: @Override
mcimadamore@1674: public void argumentsAcceptable(final Env env,
mcimadamore@1674: DeferredAttrContext deferredAttrContext,
mcimadamore@1674: List argtypes,
mcimadamore@1674: List formals,
mcimadamore@1674: Warner warn) {
mcimadamore@1674: //should we expand formals?
mcimadamore@1674: boolean useVarargs = deferredAttrContext.phase.isVarargsRequired();
sadayapalam@3005: JCTree callTree = treeForDiagnostics(env);
sadayapalam@3005: List trees = TreeInfo.args(callTree);
mcimadamore@1674:
mcimadamore@1674: //inference context used during this method check
mcimadamore@1674: InferenceContext inferenceContext = deferredAttrContext.inferenceContext;
mcimadamore@1674:
mcimadamore@1674: Type varargsFormal = useVarargs ? formals.last() : null;
mcimadamore@1674:
mcimadamore@1674: if (varargsFormal == null &&
mcimadamore@1674: argtypes.size() != formals.size()) {
sadayapalam@3005: reportMC(callTree, MethodCheckDiag.ARITY_MISMATCH, inferenceContext); // not enough args
mcimadamore@1674: }
mcimadamore@1674:
mcimadamore@1674: while (argtypes.nonEmpty() && formals.head != varargsFormal) {
mcimadamore@1759: DiagnosticPosition pos = trees != null ? trees.head : null;
mcimadamore@1759: checkArg(pos, false, argtypes.head, formals.head, deferredAttrContext, warn);
mcimadamore@1674: argtypes = argtypes.tail;
mcimadamore@1674: formals = formals.tail;
mcimadamore@1759: trees = trees != null ? trees.tail : trees;
mcimadamore@1674: }
mcimadamore@1674:
mcimadamore@1674: if (formals.head != varargsFormal) {
sadayapalam@3005: reportMC(callTree, MethodCheckDiag.ARITY_MISMATCH, inferenceContext); // not enough args
mcimadamore@1674: }
mcimadamore@1674:
mcimadamore@1674: if (useVarargs) {
mcimadamore@1674: //note: if applicability check is triggered by most specific test,
mcimadamore@1674: //the last argument of a varargs is _not_ an array type (see JLS 15.12.2.5)
mcimadamore@1674: final Type elt = types.elemtype(varargsFormal);
mcimadamore@1674: while (argtypes.nonEmpty()) {
mcimadamore@1759: DiagnosticPosition pos = trees != null ? trees.head : null;
mcimadamore@1759: checkArg(pos, true, argtypes.head, elt, deferredAttrContext, warn);
mcimadamore@1674: argtypes = argtypes.tail;
mcimadamore@1759: trees = trees != null ? trees.tail : trees;
mcimadamore@1674: }
mcimadamore@1674: }
mcimadamore@1674: }
mcimadamore@1674:
sadayapalam@3005: // where
sadayapalam@3005: private JCTree treeForDiagnostics(Env env) {
sadayapalam@3005: return env.info.preferredTreeForDiagnostics != null ? env.info.preferredTreeForDiagnostics : env.tree;
sadayapalam@3005: }
sadayapalam@3005:
mcimadamore@1674: /**
mcimadamore@1674: * Does the actual argument conforms to the corresponding formal?
mcimadamore@1674: */
mcimadamore@1759: abstract void checkArg(DiagnosticPosition pos, boolean varargs, Type actual, Type formal, DeferredAttrContext deferredAttrContext, Warner warn);
mcimadamore@1759:
mcimadamore@1759: protected void reportMC(DiagnosticPosition pos, MethodCheckDiag diag, InferenceContext inferenceContext, Object... args) {
mcimadamore@1674: boolean inferDiag = inferenceContext != infer.emptyContext;
mcimadamore@1674: InapplicableMethodException ex = inferDiag ?
mcimadamore@1674: infer.inferenceException : inapplicableMethodException;
mcimadamore@1674: if (inferDiag && (!diag.inferKey.equals(diag.basicKey))) {
mcimadamore@1674: Object[] args2 = new Object[args.length + 1];
mcimadamore@1674: System.arraycopy(args, 0, args2, 1, args.length);
mcimadamore@1674: args2[0] = inferenceContext.inferenceVars();
mcimadamore@1674: args = args2;
mcimadamore@1674: }
mcimadamore@1759: String key = inferDiag ? diag.inferKey : diag.basicKey;
mcimadamore@1759: throw ex.setMessage(diags.create(DiagnosticType.FRAGMENT, log.currentSource(), pos, key, args));
mcimadamore@1674: }
mcimadamore@1674:
mcimadamore@1674: public MethodCheck mostSpecificCheck(List actuals, boolean strict) {
mcimadamore@1674: return nilMethodCheck;
mcimadamore@1674: }
vromero@2382:
mcimadamore@1674: }
mcimadamore@1674:
mcimadamore@1674: /**
mcimadamore@1674: * Arity-based method check. A method is applicable if the number of actuals
mcimadamore@1674: * supplied conforms to the method signature.
mcimadamore@1674: */
mcimadamore@1674: MethodCheck arityMethodCheck = new AbstractMethodCheck() {
mcimadamore@1674: @Override
mcimadamore@1759: void checkArg(DiagnosticPosition pos, boolean varargs, Type actual, Type formal, DeferredAttrContext deferredAttrContext, Warner warn) {
mcimadamore@1674: //do nothing - actual always compatible to formals
mcimadamore@1674: }
vromero@2382:
vromero@2382: @Override
vromero@2382: public String toString() {
vromero@2382: return "arityMethodCheck";
vromero@2382: }
mcimadamore@1674: };
mcimadamore@1674:
mcimadamore@1897: List dummyArgs(int length) {
alundblad@2047: ListBuffer buf = new ListBuffer<>();
mcimadamore@1897: for (int i = 0 ; i < length ; i++) {
mcimadamore@1897: buf.append(Type.noType);
mcimadamore@1897: }
mcimadamore@1897: return buf.toList();
mcimadamore@1897: }
mcimadamore@1897:
mcimadamore@1674: /**
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@1347: * type-inference, a (possibly empty) inference context is used to convert
mcimadamore@1347: * formal types to the corresponding 'undet' form ahead of a compatibility
mcimadamore@1347: * check so that constraints can be propagated and collected.
mcimadamore@1186: *
mcimadamore@1347: * Moreover, if one or more types in A is a deferred type, this routine uses
mcimadamore@1347: * DeferredAttr in order to perform deferred attribution. If one or more actual
mcimadamore@1347: * deferred types are stuck, they are placed in a queue and revisited later
mcimadamore@1347: * after the remainder of the arguments have been seen. If this is not sufficient
mcimadamore@1347: * to 'unstuck' the argument, a cyclic inference error is called out.
mcimadamore@1186: *
mcimadamore@1186: * A method check handler (see above) is used in order to report errors.
mcimadamore@1186: */
mcimadamore@1674: MethodCheck resolveMethodCheck = new AbstractMethodCheck() {
mcimadamore@1674:
mcimadamore@1674: @Override
mcimadamore@1759: void checkArg(DiagnosticPosition pos, boolean varargs, Type actual, Type formal, DeferredAttrContext deferredAttrContext, Warner warn) {
mcimadamore@1674: ResultInfo mresult = methodCheckResult(varargs, formal, deferredAttrContext, warn);
mcimadamore@1759: mresult.check(pos, actual);
mcimadamore@1674: }
mcimadamore@1674:
mcimadamore@1479: @Override
mcimadamore@1479: public void argumentsAcceptable(final Env env,
mcimadamore@1479: DeferredAttrContext deferredAttrContext,
mcimadamore@1479: List argtypes,
mcimadamore@1479: List formals,
mcimadamore@1479: Warner warn) {
mcimadamore@1674: super.argumentsAcceptable(env, deferredAttrContext, argtypes, formals, warn);
dlsmith@2788: // should we check varargs element type accessibility?
vromero@2535: if (deferredAttrContext.phase.isVarargsRequired()) {
dlsmith@2788: if (deferredAttrContext.mode == AttrMode.CHECK || !checkVarargsAccessAfterResolution) {
dlsmith@2788: varargsAccessible(env, types.elemtype(formals.last()), deferredAttrContext.inferenceContext);
vromero@2535: }
mcimadamore@1479: }
mcimadamore@1479: }
mcimadamore@1479:
dlsmith@2788: /**
dlsmith@2788: * Test that the runtime array element type corresponding to 't' is accessible. 't' should be the
dlsmith@2788: * varargs element type of either the method invocation type signature (after inference completes)
dlsmith@2788: * or the method declaration signature (before inference completes).
dlsmith@2788: */
mcimadamore@1479: private void varargsAccessible(final Env env, final Type t, final InferenceContext inferenceContext) {
mcimadamore@1479: if (inferenceContext.free(t)) {
mcimadamore@1479: inferenceContext.addFreeTypeListener(List.of(t), new FreeTypeListener() {
mcimadamore@1479: @Override
mcimadamore@1479: public void typesInferred(InferenceContext inferenceContext) {
mcimadamore@1550: varargsAccessible(env, inferenceContext.asInstType(t), inferenceContext);
mcimadamore@1479: }
mcimadamore@1479: });
mcimadamore@1479: } else {
dlsmith@2788: if (!isAccessible(env, types.erasure(t))) {
mcimadamore@1479: Symbol location = env.enclClass.sym;
mcimadamore@1759: reportMC(env.tree, MethodCheckDiag.INACCESSIBLE_VARARGS, inferenceContext, t, Kinds.kindName(location), location);
mcimadamore@1479: }
mcimadamore@1479: }
mcimadamore@1479: }
mcimadamore@1479:
mcimadamore@1479: private ResultInfo methodCheckResult(final boolean varargsCheck, Type to,
mcimadamore@1479: final DeferredAttr.DeferredAttrContext deferredAttrContext, Warner rsWarner) {
mcimadamore@1479: CheckContext checkContext = new MethodCheckContext(!deferredAttrContext.phase.isBoxingRequired(), deferredAttrContext, rsWarner) {
mcimadamore@1479: MethodCheckDiag methodDiag = varargsCheck ?
mcimadamore@1479: MethodCheckDiag.VARARG_MISMATCH : MethodCheckDiag.ARG_MISMATCH;
mcimadamore@1479:
mcimadamore@1479: @Override
mcimadamore@1479: public void report(DiagnosticPosition pos, JCDiagnostic details) {
mcimadamore@1759: reportMC(pos, methodDiag, deferredAttrContext.inferenceContext, details);
mcimadamore@1479: }
mcimadamore@1479: };
mcimadamore@1479: return new MethodResultInfo(to, checkContext);
mcimadamore@1479: }
mcimadamore@1674:
mcimadamore@1674: @Override
mcimadamore@1674: public MethodCheck mostSpecificCheck(List actuals, boolean strict) {
mcimadamore@1674: return new MostSpecificCheck(strict, actuals);
mcimadamore@1674: }
vromero@2382:
vromero@2382: @Override
vromero@2382: public String toString() {
vromero@2382: return "resolveMethodCheck";
vromero@2382: }
mcimadamore@1479: };
mcimadamore@689:
vromero@2193: /**
vromero@2193: * This class handles method reference applicability checks; since during
vromero@2193: * these checks it's sometime possible to have inference variables on
vromero@2193: * the actual argument types list, the method applicability check must be
vromero@2193: * extended so that inference variables are 'opened' as needed.
vromero@2193: */
mcimadamore@1897: class MethodReferenceCheck extends AbstractMethodCheck {
mcimadamore@1897:
mcimadamore@1897: InferenceContext pendingInferenceContext;
mcimadamore@1897:
mcimadamore@1897: MethodReferenceCheck(InferenceContext pendingInferenceContext) {
mcimadamore@1897: this.pendingInferenceContext = pendingInferenceContext;
mcimadamore@1897: }
mcimadamore@1897:
mcimadamore@1897: @Override
mcimadamore@1897: void checkArg(DiagnosticPosition pos, boolean varargs, Type actual, Type formal, DeferredAttrContext deferredAttrContext, Warner warn) {
mcimadamore@1897: ResultInfo mresult = methodCheckResult(varargs, formal, deferredAttrContext, warn);
mcimadamore@1897: mresult.check(pos, actual);
mcimadamore@1897: }
mcimadamore@1897:
mcimadamore@1897: private ResultInfo methodCheckResult(final boolean varargsCheck, Type to,
mcimadamore@1897: final DeferredAttr.DeferredAttrContext deferredAttrContext, Warner rsWarner) {
mcimadamore@1897: CheckContext checkContext = new MethodCheckContext(!deferredAttrContext.phase.isBoxingRequired(), deferredAttrContext, rsWarner) {
mcimadamore@1897: MethodCheckDiag methodDiag = varargsCheck ?
mcimadamore@1897: MethodCheckDiag.VARARG_MISMATCH : MethodCheckDiag.ARG_MISMATCH;
mcimadamore@1897:
mcimadamore@1897: @Override
mcimadamore@1897: public boolean compatible(Type found, Type req, Warner warn) {
vromero@2368: found = pendingInferenceContext.asUndetVar(found);
vromero@2382: if (found.hasTag(UNDETVAR) && req.isPrimitive()) {
vromero@2382: req = types.boxedClass(req).type;
vromero@2382: }
mcimadamore@1897: return super.compatible(found, req, warn);
mcimadamore@1897: }
mcimadamore@1897:
mcimadamore@1897: @Override
mcimadamore@1897: public void report(DiagnosticPosition pos, JCDiagnostic details) {
mcimadamore@1897: reportMC(pos, methodDiag, deferredAttrContext.inferenceContext, details);
mcimadamore@1897: }
mcimadamore@1897: };
mcimadamore@1897: return new MethodResultInfo(to, checkContext);
mcimadamore@1897: }
mcimadamore@1897:
mcimadamore@1897: @Override
mcimadamore@1897: public MethodCheck mostSpecificCheck(List actuals, boolean strict) {
mcimadamore@1897: return new MostSpecificCheck(strict, actuals);
mcimadamore@1897: }
mcimadamore@1897: };
mcimadamore@1897:
mcimadamore@1238: /**
mcimadamore@1238: * Check context to be used during method applicability checks. A method check
mcimadamore@1238: * context might contain inference variables.
mcimadamore@1238: */
mcimadamore@1238: abstract class MethodCheckContext implements CheckContext {
mcimadamore@689:
mcimadamore@1479: boolean strict;
mcimadamore@1347: DeferredAttrContext deferredAttrContext;
mcimadamore@1238: Warner rsWarner;
mcimadamore@1238:
mcimadamore@1479: public MethodCheckContext(boolean strict, DeferredAttrContext deferredAttrContext, Warner rsWarner) {
mcimadamore@1479: this.strict = strict;
mcimadamore@1479: this.deferredAttrContext = deferredAttrContext;
mcimadamore@1479: this.rsWarner = rsWarner;
mcimadamore@1238: }
mcimadamore@1238:
mcimadamore@1479: public boolean compatible(Type found, Type req, Warner warn) {
vromero@2543: InferenceContext inferenceContext = deferredAttrContext.inferenceContext;
mcimadamore@1479: return strict ?
vromero@2543: types.isSubtypeUnchecked(inferenceContext.asUndetVar(found), inferenceContext.asUndetVar(req), warn) :
vromero@2543: types.isConvertible(inferenceContext.asUndetVar(found), inferenceContext.asUndetVar(req), warn);
mcimadamore@1479: }
mcimadamore@1479:
mcimadamore@1296: public void report(DiagnosticPosition pos, JCDiagnostic details) {
mcimadamore@1479: throw inapplicableMethodException.setMessage(details);
mcimadamore@1238: }
mcimadamore@1238:
mcimadamore@1238: public Warner checkWarner(DiagnosticPosition pos, Type found, Type req) {
mcimadamore@1238: return rsWarner;
mcimadamore@1238: }
mcimadamore@1337:
mcimadamore@1337: public InferenceContext inferenceContext() {
mcimadamore@1479: return deferredAttrContext.inferenceContext;
mcimadamore@1337: }
mcimadamore@1347:
mcimadamore@1347: public DeferredAttrContext deferredAttrContext() {
mcimadamore@1347: return deferredAttrContext;
mcimadamore@1347: }
vromero@2382:
vromero@2382: @Override
vromero@2382: public String toString() {
vromero@2382: return "MethodReferenceCheck";
vromero@2382: }
vromero@2382:
mcimadamore@1238: }
mcimadamore@1238:
mcimadamore@1238: /**
mcimadamore@1479: * ResultInfo class to be used during method applicability checks. Check
mcimadamore@1479: * for deferred types goes through special path.
mcimadamore@1238: */
mcimadamore@1347: class MethodResultInfo extends ResultInfo {
mcimadamore@1347:
mcimadamore@1479: public MethodResultInfo(Type pt, CheckContext checkContext) {
mcimadamore@1347: attr.super(VAL, pt, checkContext);
mcimadamore@1347: }
mcimadamore@1347:
mcimadamore@1347: @Override
mcimadamore@1347: protected Type check(DiagnosticPosition pos, Type found) {
jjg@1374: if (found.hasTag(DEFERRED)) {
mcimadamore@1347: DeferredType dt = (DeferredType)found;
mcimadamore@1347: return dt.check(this);
mcimadamore@1347: } else {
vromero@2382: Type uResult = U(found.baseType());
vromero@2382: Type capturedType = pos == null || pos.getTree() == null ?
vromero@2382: types.capture(uResult) :
vromero@2382: checkContext.inferenceContext()
vromero@2382: .cachedCapture(pos.getTree(), uResult, true);
vromero@2382: return super.check(pos, chk.checkNonVoid(pos, capturedType));
mcimadamore@689: }
mcimadamore@1347: }
mcimadamore@1347:
mcimadamore@1898: /**
mcimadamore@1898: * javac has a long-standing 'simplification' (see 6391995):
mcimadamore@1898: * given an actual argument type, the method check is performed
mcimadamore@1898: * on its upper bound. This leads to inconsistencies when an
mcimadamore@1898: * argument type is checked against itself. For example, given
mcimadamore@1898: * a type-variable T, it is not true that {@code U(T) <: T},
mcimadamore@1898: * so we need to guard against that.
mcimadamore@1898: */
mcimadamore@1898: private Type U(Type found) {
mcimadamore@1898: return found == pt ?
dlsmith@2400: found : types.cvarUpperBound(found);
mcimadamore@1898: }
mcimadamore@1898:
mcimadamore@1347: @Override
mcimadamore@1347: protected MethodResultInfo dup(Type newPt) {
mcimadamore@1479: return new MethodResultInfo(newPt, checkContext);
mcimadamore@1415: }
mcimadamore@1415:
mcimadamore@1415: @Override
mcimadamore@1415: protected ResultInfo dup(CheckContext newContext) {
mcimadamore@1479: return new MethodResultInfo(pt, newContext);
mcimadamore@1347: }
mcimadamore@1238: }
mcimadamore@689:
mcimadamore@1510: /**
mcimadamore@1510: * Most specific method applicability routine. Given a list of actual types A,
mcimadamore@1510: * a list of formal types F1, and a list of formal types F2, the routine determines
mcimadamore@1510: * as to whether the types in F1 can be considered more specific than those in F2 w.r.t.
mcimadamore@1510: * argument types A.
mcimadamore@1510: */
mcimadamore@1510: class MostSpecificCheck implements MethodCheck {
mcimadamore@1510:
mcimadamore@1510: boolean strict;
mcimadamore@1510: List actuals;
mcimadamore@1510:
mcimadamore@1510: MostSpecificCheck(boolean strict, List actuals) {
mcimadamore@1510: this.strict = strict;
mcimadamore@1510: this.actuals = actuals;
mcimadamore@1510: }
mcimadamore@1510:
mcimadamore@1510: @Override
mcimadamore@1510: public void argumentsAcceptable(final Env env,
mcimadamore@1510: DeferredAttrContext deferredAttrContext,
mcimadamore@1510: List formals1,
mcimadamore@1510: List formals2,
mcimadamore@1510: Warner warn) {
mcimadamore@1510: formals2 = adjustArgs(formals2, deferredAttrContext.msym, formals1.length(), deferredAttrContext.phase.isVarargsRequired());
mcimadamore@1510: while (formals2.nonEmpty()) {
mcimadamore@1510: ResultInfo mresult = methodCheckResult(formals2.head, deferredAttrContext, warn, actuals.head);
mcimadamore@1510: mresult.check(null, formals1.head);
mcimadamore@1510: formals1 = formals1.tail;
mcimadamore@1510: formals2 = formals2.tail;
mcimadamore@1510: actuals = actuals.isEmpty() ? actuals : actuals.tail;
mcimadamore@1510: }
mcimadamore@1510: }
mcimadamore@1510:
mcimadamore@1510: /**
mcimadamore@1510: * Create a method check context to be used during the most specific applicability check
mcimadamore@1510: */
mcimadamore@1510: ResultInfo methodCheckResult(Type to, DeferredAttr.DeferredAttrContext deferredAttrContext,
mcimadamore@1510: Warner rsWarner, Type actual) {
mcimadamore@1510: return attr.new ResultInfo(Kinds.VAL, to,
mcimadamore@1510: new MostSpecificCheckContext(strict, deferredAttrContext, rsWarner, actual));
mcimadamore@1510: }
mcimadamore@1510:
mcimadamore@1510: /**
mcimadamore@1510: * Subclass of method check context class that implements most specific
mcimadamore@1510: * method conversion. If the actual type under analysis is a deferred type
mcimadamore@1510: * a full blown structural analysis is carried out.
mcimadamore@1510: */
mcimadamore@1510: class MostSpecificCheckContext extends MethodCheckContext {
mcimadamore@1510:
mcimadamore@1510: Type actual;
mcimadamore@1510:
mcimadamore@1510: public MostSpecificCheckContext(boolean strict, DeferredAttrContext deferredAttrContext, Warner rsWarner, Type actual) {
mcimadamore@1510: super(strict, deferredAttrContext, rsWarner);
mcimadamore@1510: this.actual = actual;
mcimadamore@1510: }
mcimadamore@1510:
mcimadamore@1510: public boolean compatible(Type found, Type req, Warner warn) {
dlsmith@2395: if (allowFunctionalInterfaceMostSpecific &&
dlsmith@2395: unrelatedFunctionalInterfaces(found, req) &&
dlsmith@2395: (actual != null && actual.getTag() == DEFERRED)) {
dlsmith@2395: DeferredType dt = (DeferredType) actual;
dlsmith@2395: DeferredType.SpeculativeCache.Entry e =
dlsmith@2395: dt.speculativeCache.get(deferredAttrContext.msym, deferredAttrContext.phase);
dlsmith@2395: if (e != null && e.speculativeTree != deferredAttr.stuckTree) {
dlsmith@2395: return functionalInterfaceMostSpecific(found, req, e.speculativeTree, warn);
mcimadamore@1510: }
mcimadamore@1510: }
dlsmith@2395: return super.compatible(found, req, warn);
mcimadamore@1510: }
mcimadamore@1510:
dlsmith@2395: /** Whether {@code t} and {@code s} are unrelated functional interface types. */
dlsmith@2395: private boolean unrelatedFunctionalInterfaces(Type t, Type s) {
dlsmith@2395: return types.isFunctionalInterface(t.tsym) &&
dlsmith@2395: types.isFunctionalInterface(s.tsym) &&
dlsmith@2395: types.asSuper(t, s.tsym) == null &&
dlsmith@2395: types.asSuper(s, t.tsym) == null;
dlsmith@2395: }
dlsmith@2395:
dlsmith@2395: /** Parameters {@code t} and {@code s} are unrelated functional interface types. */
dlsmith@2395: private boolean functionalInterfaceMostSpecific(Type t, Type s, JCTree tree, Warner warn) {
dlsmith@2395: FunctionalInterfaceMostSpecificChecker msc = new FunctionalInterfaceMostSpecificChecker(t, s, warn);
mcimadamore@1510: msc.scan(tree);
mcimadamore@1510: return msc.result;
mcimadamore@1510: }
mcimadamore@1510:
mcimadamore@1510: /**
dlsmith@2395: * Tests whether one functional interface type can be considered more specific
dlsmith@2395: * than another unrelated functional interface type for the scanned expression.
mcimadamore@1510: */
dlsmith@2395: class FunctionalInterfaceMostSpecificChecker extends DeferredAttr.PolyScanner {
mcimadamore@1510:
mcimadamore@1510: final Type t;
mcimadamore@1510: final Type s;
mcimadamore@1510: final Warner warn;
mcimadamore@1510: boolean result;
mcimadamore@1510:
dlsmith@2395: /** Parameters {@code t} and {@code s} are unrelated functional interface types. */
dlsmith@2395: FunctionalInterfaceMostSpecificChecker(Type t, Type s, Warner warn) {
mcimadamore@1510: this.t = t;
mcimadamore@1510: this.s = s;
mcimadamore@1510: this.warn = warn;
mcimadamore@1510: result = true;
mcimadamore@1510: }
mcimadamore@1510:
mcimadamore@1510: @Override
mcimadamore@1510: void skip(JCTree tree) {
dlsmith@2395: result &= false;
mcimadamore@1510: }
mcimadamore@1510:
mcimadamore@1510: @Override
mcimadamore@1510: public void visitConditional(JCConditional tree) {
dlsmith@2395: scan(tree.truepart);
dlsmith@2395: scan(tree.falsepart);
dlsmith@2395: }
dlsmith@2395:
dlsmith@2395: @Override
dlsmith@2395: public void visitReference(JCMemberReference tree) {
dlsmith@2395: Type desc_t = types.findDescriptorType(t);
dlsmith@2395: Type desc_s = types.findDescriptorType(s);
dlsmith@2395: // use inference variables here for more-specific inference (18.5.4)
dlsmith@2395: if (!types.isSameTypes(desc_t.getParameterTypes(),
dlsmith@2395: inferenceContext().asUndetVars(desc_s.getParameterTypes()))) {
dlsmith@2395: result &= false;
mcimadamore@1510: } else {
dlsmith@2395: // compare return types
dlsmith@2395: Type ret_t = desc_t.getReturnType();
dlsmith@2395: Type ret_s = desc_s.getReturnType();
dlsmith@2395: if (ret_s.hasTag(VOID)) {
dlsmith@2395: result &= true;
dlsmith@2395: } else if (ret_t.hasTag(VOID)) {
dlsmith@2395: result &= false;
dlsmith@2395: } else if (ret_t.isPrimitive() != ret_s.isPrimitive()) {
dlsmith@2395: boolean retValIsPrimitive =
dlsmith@2395: tree.refPolyKind == PolyKind.STANDALONE &&
dlsmith@2395: tree.sym.type.getReturnType().isPrimitive();
dlsmith@2395: result &= (retValIsPrimitive == ret_t.isPrimitive()) &&
dlsmith@2395: (retValIsPrimitive != ret_s.isPrimitive());
dlsmith@2395: } else {
dlsmith@2395: result &= MostSpecificCheckContext.super.compatible(ret_t, ret_s, warn);
dlsmith@2395: }
mcimadamore@1510: }
mcimadamore@1510: }
mcimadamore@1510:
mcimadamore@1510: @Override
dlsmith@2395: public void visitLambda(JCLambda tree) {
dlsmith@2395: Type desc_t = types.findDescriptorType(t);
dlsmith@2395: Type desc_s = types.findDescriptorType(s);
dlsmith@2395: // use inference variables here for more-specific inference (18.5.4)
dlsmith@2395: if (!types.isSameTypes(desc_t.getParameterTypes(),
dlsmith@2395: inferenceContext().asUndetVars(desc_s.getParameterTypes()))) {
dlsmith@2395: result &= false;
dlsmith@2395: } else {
dlsmith@2395: // compare return types
dlsmith@2395: Type ret_t = desc_t.getReturnType();
dlsmith@2395: Type ret_s = desc_s.getReturnType();
dlsmith@2395: if (ret_s.hasTag(VOID)) {
dlsmith@2395: result &= true;
dlsmith@2395: } else if (ret_t.hasTag(VOID)) {
dlsmith@2395: result &= false;
dlsmith@2395: } else if (unrelatedFunctionalInterfaces(ret_t, ret_s)) {
dlsmith@2395: for (JCExpression expr : lambdaResults(tree)) {
dlsmith@2395: result &= functionalInterfaceMostSpecific(ret_t, ret_s, expr, warn);
mcimadamore@1510: }
dlsmith@2395: } else if (ret_t.isPrimitive() != ret_s.isPrimitive()) {
dlsmith@2395: for (JCExpression expr : lambdaResults(tree)) {
dlsmith@2395: boolean retValIsPrimitive = expr.isStandalone() && expr.type.isPrimitive();
dlsmith@2395: result &= (retValIsPrimitive == ret_t.isPrimitive()) &&
dlsmith@2395: (retValIsPrimitive != ret_s.isPrimitive());
dlsmith@2395: }
dlsmith@2395: } else {
dlsmith@2395: result &= MostSpecificCheckContext.super.compatible(ret_t, ret_s, warn);
mcimadamore@1510: }
mcimadamore@1510: }
mcimadamore@1510: }
mcimadamore@1510: //where
mcimadamore@1510:
dlsmith@2395: private List lambdaResults(JCLambda lambda) {
mcimadamore@1510: if (lambda.getBodyKind() == JCTree.JCLambda.BodyKind.EXPRESSION) {
dlsmith@2395: return List.of((JCExpression) lambda.body);
mcimadamore@1510: } else {
dlsmith@2395: final ListBuffer buffer = new ListBuffer<>();
mcimadamore@1510: DeferredAttr.LambdaReturnScanner lambdaScanner =
mcimadamore@1510: new DeferredAttr.LambdaReturnScanner() {
mcimadamore@1510: @Override
mcimadamore@1510: public void visitReturn(JCReturn tree) {
mcimadamore@1510: if (tree.expr != null) {
dlsmith@2395: buffer.append(tree.expr);
mcimadamore@1510: }
mcimadamore@1510: }
mcimadamore@1510: };
mcimadamore@1510: lambdaScanner.scan(lambda.body);
dlsmith@2395: return buffer.toList();
mcimadamore@1510: }
mcimadamore@1510: }
mcimadamore@1510: }
dlsmith@2395:
mcimadamore@1510: }
mcimadamore@1674:
mcimadamore@1674: public MethodCheck mostSpecificCheck(List actuals, boolean strict) {
mcimadamore@1674: Assert.error("Cannot get here!");
mcimadamore@1674: return null;
mcimadamore@1674: }
mcimadamore@1510: }
mcimadamore@1510:
mcimadamore@1238: public static class InapplicableMethodException extends RuntimeException {
mcimadamore@1238: private static final long serialVersionUID = 0;
mcimadamore@1238:
mcimadamore@1238: JCDiagnostic diagnostic;
mcimadamore@1238: JCDiagnostic.Factory diags;
mcimadamore@1238:
mcimadamore@1238: InapplicableMethodException(JCDiagnostic.Factory diags) {
mcimadamore@1238: this.diagnostic = null;
mcimadamore@1238: this.diags = diags;
mcimadamore@689: }
mcimadamore@1238: InapplicableMethodException setMessage() {
mcimadamore@1337: return setMessage((JCDiagnostic)null);
mcimadamore@1238: }
mcimadamore@1238: InapplicableMethodException setMessage(String key) {
mcimadamore@1337: return setMessage(key != null ? diags.fragment(key) : null);
mcimadamore@1238: }
mcimadamore@1238: InapplicableMethodException setMessage(String key, Object... args) {
mcimadamore@1337: return setMessage(key != null ? diags.fragment(key, args) : null);
mcimadamore@1238: }
mcimadamore@1238: InapplicableMethodException setMessage(JCDiagnostic diag) {
mcimadamore@1238: this.diagnostic = diag;
mcimadamore@1238: return this;
mcimadamore@1238: }
mcimadamore@1238:
mcimadamore@1238: public JCDiagnostic getDiagnostic() {
mcimadamore@1238: return diagnostic;
mcimadamore@1238: }
mcimadamore@1238: }
mcimadamore@1238: 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) {
jjg@1374: while (c.type.hasTag(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);
jjg@1374: if (st != null && (st.hasTag(CLASS) || st.hasTag(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);
mcimadamore@1809: if (bestSoFar.exists() && sym.exists() &&
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:
mcimadamore@1945: Symbol origin = null;
mcimadamore@1945: for (Scope sc : new Scope[] { env.toplevel.namedImportScope, env.toplevel.starImportScope }) {
mcimadamore@1945: Scope.Entry e = sc.lookup(name);
mcimadamore@1945: for (; e.scope != null; e = e.next()) {
mcimadamore@1945: sym = e.sym;
mcimadamore@1945: if (sym.kind != VAR)
mcimadamore@1945: continue;
mcimadamore@1945: // invariant: sym.kind == VAR
mcimadamore@1945: if (bestSoFar.kind < AMBIGUOUS && sym.owner != bestSoFar.owner)
mcimadamore@1945: return new AmbiguityError(bestSoFar, sym);
mcimadamore@1945: else if (bestSoFar.kind >= VAR) {
mcimadamore@1945: origin = e.getOrigin().owner;
mcimadamore@1945: bestSoFar = isAccessible(env, origin.type, sym)
mcimadamore@1945: ? sym : new AccessError(env, origin.type, sym);
mcimadamore@1945: }
duke@1: }
mcimadamore@1945: if (bestSoFar.exists()) break;
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) {
mcimadamore@1394: if (sym.kind == ERR ||
mcimadamore@1599: !sym.isInheritedIn(site.tsym, types)) {
mcimadamore@1394: return bestSoFar;
mcimadamore@1599: } else if (useVarargs && (sym.flags() & VARARGS) == 0) {
mcimadamore@1599: return bestSoFar.kind >= ERRONEOUS ?
vromero@2392: new BadVarargsMethod((ResolveError)bestSoFar.baseSymbol()) :
mcimadamore@1599: bestSoFar;
mcimadamore@1394: }
jjg@816: Assert.check(sym.kind < AMBIGUOUS);
duke@1: try {
mcimadamore@1268: Type mt = rawInstantiate(env, site, sym, null, argtypes, typeargtypes,
mcimadamore@1674: allowBoxing, useVarargs, types.noWarnings);
mcimadamore@1779: if (!operator || verboseResolutionMode.contains(VerboseResolutionMode.PREDEF))
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) {
mcimadamore@1394: case ABSENT_MTH:
mcimadamore@1394: return new InapplicableSymbolError(currentResolutionContext);
mcimadamore@1394: case WRONG_MTH:
mcimadamore@1394: if (operator) return bestSoFar;
mcimadamore@1394: bestSoFar = new InapplicableSymbolsError(currentResolutionContext);
mcimadamore@1394: default:
mcimadamore@1394: 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
mcimadamore@1348: : mostSpecific(argtypes, 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: */
mcimadamore@1348: Symbol mostSpecific(List argtypes, 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@1348: boolean m1SignatureMoreSpecific =
mcimadamore@1348: signatureMoreSpecific(argtypes, env, site, m1, m2, allowBoxing, useVarargs);
mcimadamore@1348: boolean m2SignatureMoreSpecific =
mcimadamore@1348: signatureMoreSpecific(argtypes, 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
mcimadamore@1480: return ambiguityError(m1, m2);
duke@1: }
duke@1: if (m1SignatureMoreSpecific) return m1;
duke@1: if (m2SignatureMoreSpecific) return m2;
mcimadamore@844: return ambiguityError(m1, m2);
duke@1: case AMBIGUOUS:
jlahoda@2387: //compare m1 to ambiguous methods in m2
vromero@2219: AmbiguityError e = (AmbiguityError)m2.baseSymbol();
jlahoda@2387: boolean m1MoreSpecificThanAnyAmbiguous = true;
jlahoda@2387: boolean allAmbiguousMoreSpecificThanM1 = true;
mcimadamore@1480: for (Symbol s : e.ambiguousSyms) {
jlahoda@2387: Symbol moreSpecific = mostSpecific(argtypes, m1, s, env, site, allowBoxing, useVarargs);
jlahoda@2387: m1MoreSpecificThanAnyAmbiguous &= moreSpecific == m1;
jlahoda@2387: allAmbiguousMoreSpecificThanM1 &= moreSpecific == s;
mcimadamore@1480: }
jlahoda@2387: if (m1MoreSpecificThanAnyAmbiguous)
jlahoda@2387: return m1;
jlahoda@2387: //if m1 is more specific than some ambiguous methods, but other ambiguous methods are
jlahoda@2387: //more specific than m1, add it as a new ambiguous method:
jlahoda@2387: if (!allAmbiguousMoreSpecificThanM1)
jlahoda@2387: e.addAmbiguousSymbol(m1);
jlahoda@2387: return e;
duke@1: default:
duke@1: throw new AssertionError();
duke@1: }
duke@1: }
mcimadamore@775: //where
mcimadamore@1348: private boolean signatureMoreSpecific(List actuals, Env env, Type site, Symbol m1, Symbol m2, boolean allowBoxing, boolean useVarargs) {
mcimadamore@1510: noteWarner.clear();
mcimadamore@1510: int maxLength = Math.max(
mcimadamore@1510: Math.max(m1.type.getParameterTypes().length(), actuals.length()),
mcimadamore@1510: m2.type.getParameterTypes().length());
mcimadamore@1674: MethodResolutionContext prevResolutionContext = currentResolutionContext;
mcimadamore@1674: try {
mcimadamore@1674: currentResolutionContext = new MethodResolutionContext();
mcimadamore@1674: currentResolutionContext.step = prevResolutionContext.step;
mcimadamore@1674: currentResolutionContext.methodCheck =
mcimadamore@1674: prevResolutionContext.methodCheck.mostSpecificCheck(actuals, !allowBoxing);
mcimadamore@1674: Type mst = instantiate(env, site, m2, null,
dlsmith@2384: adjustArgs(types.cvarLowerBounds(types.memberType(site, m1).getParameterTypes()), m1, maxLength, useVarargs), null,
mcimadamore@1674: allowBoxing, useVarargs, noteWarner);
mcimadamore@1674: return mst != null &&
mcimadamore@1674: !noteWarner.hasLint(Lint.LintCategory.UNCHECKED);
mcimadamore@1674: } finally {
mcimadamore@1674: currentResolutionContext = prevResolutionContext;
mcimadamore@1674: }
mcimadamore@1510: }
vromero@2000:
vromero@2000: List adjustArgs(List args, Symbol msym, int length, boolean allowVarargs) {
mcimadamore@1510: if ((msym.flags() & VARARGS) != 0 && allowVarargs) {
mcimadamore@1510: Type varargsElem = types.elemtype(args.last());
mcimadamore@1510: if (varargsElem == null) {
mcimadamore@1510: Assert.error("Bad varargs = " + args.last() + " " + msym);
mcimadamore@1510: }
mcimadamore@1510: List newArgs = args.reverse().tail.prepend(varargsElem).reverse();
mcimadamore@1510: while (newArgs.length() < length) {
mcimadamore@1510: newArgs = newArgs.append(newArgs.last());
mcimadamore@1510: }
mcimadamore@1510: return newArgs;
mcimadamore@1510: } else {
mcimadamore@1510: return args;
mcimadamore@1348: }
mcimadamore@1348: }
mcimadamore@1348: //where
mcimadamore@1059: Type mostSpecificReturnType(Type mt1, Type mt2) {
mcimadamore@1059: Type rt1 = mt1.getReturnType();
mcimadamore@1059: Type rt2 = mt2.getReturnType();
mcimadamore@1059:
jjg@1374: if (mt1.hasTag(FORALL) && mt2.hasTag(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:
mcimadamore@1394: Symbol findMethodInScope(Env env,
mcimadamore@1393: Type site,
mcimadamore@1393: Name name,
mcimadamore@1393: List argtypes,
mcimadamore@1393: List typeargtypes,
mcimadamore@1393: Scope sc,
mcimadamore@1393: Symbol bestSoFar,
mcimadamore@1393: boolean allowBoxing,
mcimadamore@1393: boolean useVarargs,
mcimadamore@1393: boolean operator,
mcimadamore@1393: boolean abstractok) {
mcimadamore@1393: for (Symbol s : sc.getElementsByName(name, new LookupFilter(abstractok))) {
mcimadamore@1393: bestSoFar = selectBest(env, site, argtypes, typeargtypes, s,
mcimadamore@1393: bestSoFar, allowBoxing, useVarargs, operator);
mcimadamore@1393: }
mcimadamore@1393: return bestSoFar;
mcimadamore@1393: }
mcimadamore@1393: //where
mcimadamore@1393: class LookupFilter implements Filter {
mcimadamore@1393:
mcimadamore@1393: boolean abstractOk;
mcimadamore@1393:
mcimadamore@1393: LookupFilter(boolean abstractOk) {
mcimadamore@1393: this.abstractOk = abstractOk;
mcimadamore@1393: }
mcimadamore@1393:
mcimadamore@1393: public boolean accepts(Symbol s) {
mcimadamore@1393: long flags = s.flags();
mcimadamore@1393: return s.kind == MTH &&
mcimadamore@1393: (flags & SYNTHETIC) == 0 &&
mcimadamore@1393: (abstractOk ||
mcimadamore@1393: (flags & DEFAULT) != 0 ||
mcimadamore@1393: (flags & ABSTRACT) == 0);
mcimadamore@1393: }
mcimadamore@1393: };
mcimadamore@1393:
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,
jrose@571: bestSoFar,
duke@1: allowBoxing,
duke@1: useVarargs,
mcimadamore@1335: operator);
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: Symbol bestSoFar,
duke@1: boolean allowBoxing,
duke@1: boolean useVarargs,
mcimadamore@1335: boolean operator) {
mcimadamore@1393: @SuppressWarnings({"unchecked","rawtypes"})
mcimadamore@1393: List[] itypes = (List[])new List[] { List.nil(), List.nil() };
mcimadamore@1393: InterfaceLookupPhase iphase = InterfaceLookupPhase.ABSTRACT_OK;
mcimadamore@1335: for (TypeSymbol s : superclasses(intype)) {
mcimadamore@1394: bestSoFar = findMethodInScope(env, site, name, argtypes, typeargtypes,
mcimadamore@1335: s.members(), bestSoFar, allowBoxing, useVarargs, operator, true);
mcimadamore@1393: if (name == names.init) return bestSoFar;
mcimadamore@1393: iphase = (iphase == null) ? null : iphase.update(s, this);
mcimadamore@1393: if (iphase != null) {
mcimadamore@1335: for (Type itype : types.interfaces(s.type)) {
mcimadamore@1393: itypes[iphase.ordinal()] = types.union(types.closure(itype), itypes[iphase.ordinal()]);
duke@1: }
duke@1: }
mcimadamore@1335: }
mcimadamore@1335:
mcimadamore@1335: Symbol concrete = bestSoFar.kind < ERR &&
mcimadamore@1335: (bestSoFar.flags() & ABSTRACT) == 0 ?
mcimadamore@1335: bestSoFar : methodNotFound;
mcimadamore@1335:
mcimadamore@1393: for (InterfaceLookupPhase iphase2 : InterfaceLookupPhase.values()) {
mcimadamore@1335: //keep searching for abstract methods
mcimadamore@1393: for (Type itype : itypes[iphase2.ordinal()]) {
mcimadamore@1335: if (!itype.isInterface()) continue; //skip j.l.Object (included by Types.closure())
mcimadamore@1393: if (iphase2 == InterfaceLookupPhase.DEFAULT_OK &&
mcimadamore@1393: (itype.tsym.flags() & DEFAULT) == 0) continue;
mcimadamore@1394: bestSoFar = findMethodInScope(env, site, name, argtypes, typeargtypes,
mcimadamore@1393: itype.tsym.members(), bestSoFar, allowBoxing, useVarargs, operator, true);
mcimadamore@1393: if (concrete != bestSoFar &&
mcimadamore@1393: concrete.kind < ERR && bestSoFar.kind < ERR &&
mcimadamore@1393: types.isSubSignature(concrete.type, bestSoFar.type)) {
mcimadamore@1393: //this is an hack - as javac does not do full membership checks
mcimadamore@1393: //most specific ends up comparing abstract methods that might have
mcimadamore@1393: //been implemented by some concrete method in a subclass and,
mcimadamore@1393: //because of raw override, it is possible for an abstract method
mcimadamore@1393: //to be more specific than the concrete method - so we need
mcimadamore@1393: //to explicitly call that out (see CR 6178365)
mcimadamore@1393: bestSoFar = concrete;
mcimadamore@1393: }
duke@1: }
duke@1: }
duke@1: return bestSoFar;
duke@1: }
duke@1:
mcimadamore@1393: enum InterfaceLookupPhase {
mcimadamore@1393: ABSTRACT_OK() {
mcimadamore@1393: @Override
mcimadamore@1393: InterfaceLookupPhase update(Symbol s, Resolve rs) {
mcimadamore@1393: //We should not look for abstract methods if receiver is a concrete class
mcimadamore@1393: //(as concrete classes are expected to implement all abstracts coming
mcimadamore@1393: //from superinterfaces)
mcimadamore@1393: if ((s.flags() & (ABSTRACT | INTERFACE | ENUM)) != 0) {
mcimadamore@1393: return this;
vromero@2261: } else {
mcimadamore@1393: return DEFAULT_OK;
mcimadamore@1393: }
mcimadamore@1393: }
mcimadamore@1393: },
mcimadamore@1393: DEFAULT_OK() {
mcimadamore@1393: @Override
mcimadamore@1393: InterfaceLookupPhase update(Symbol s, Resolve rs) {
mcimadamore@1393: return this;
mcimadamore@1393: }
mcimadamore@1393: };
mcimadamore@1393:
mcimadamore@1393: abstract InterfaceLookupPhase update(Symbol s, Resolve rs);
mcimadamore@1393: }
mcimadamore@1393:
mcimadamore@1335: /**
mcimadamore@1335: * Return an Iterable object to scan the superclasses of a given type.
mcimadamore@1335: * It's crucial that the scan is done lazily, as we don't want to accidentally
mcimadamore@1335: * access more supertypes than strictly needed (as this could trigger completion
mcimadamore@1335: * errors if some of the not-needed supertypes are missing/ill-formed).
mcimadamore@1335: */
mcimadamore@1335: Iterable superclasses(final Type intype) {
mcimadamore@1335: return new Iterable() {
mcimadamore@1335: public Iterator iterator() {
mcimadamore@1335: return new Iterator() {
mcimadamore@1335:
mcimadamore@1335: List seen = List.nil();
mcimadamore@1342: TypeSymbol currentSym = symbolFor(intype);
mcimadamore@1342: TypeSymbol prevSym = null;
mcimadamore@1335:
mcimadamore@1335: public boolean hasNext() {
mcimadamore@1342: if (currentSym == syms.noSymbol) {
mcimadamore@1342: currentSym = symbolFor(types.supertype(prevSym.type));
mcimadamore@1342: }
mcimadamore@1335: return currentSym != null;
mcimadamore@1335: }
mcimadamore@1335:
mcimadamore@1335: public TypeSymbol next() {
mcimadamore@1342: prevSym = currentSym;
mcimadamore@1342: currentSym = syms.noSymbol;
mcimadamore@1342: Assert.check(prevSym != null || prevSym != syms.noSymbol);
mcimadamore@1335: return prevSym;
mcimadamore@1335: }
mcimadamore@1335:
mcimadamore@1335: public void remove() {
mcimadamore@1342: throw new UnsupportedOperationException();
mcimadamore@1335: }
mcimadamore@1335:
mcimadamore@1342: TypeSymbol symbolFor(Type t) {
jjg@1374: if (!t.hasTag(CLASS) &&
jjg@1374: !t.hasTag(TYPEVAR)) {
mcimadamore@1335: return null;
mcimadamore@1335: }
jjg@1374: while (t.hasTag(TYPEVAR))
mcimadamore@1342: t = t.getUpperBound();
mcimadamore@1342: if (seen.contains(t.tsym)) {
mcimadamore@1335: //degenerate case in which we have a circular
mcimadamore@1335: //class hierarchy - because of ill-formed classfiles
mcimadamore@1335: return null;
mcimadamore@1335: }
mcimadamore@1342: seen = seen.prepend(t.tsym);
mcimadamore@1342: return t.tsym;
mcimadamore@1335: }
mcimadamore@1335: };
mcimadamore@1335: }
mcimadamore@1335: };
mcimadamore@1335: }
mcimadamore@1335:
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;
sadayapalam@3005: Assert.check(env1.info.preferredTreeForDiagnostics == null);
sadayapalam@3005: env1.info.preferredTreeForDiagnostics = env.tree;
sadayapalam@3005: try {
sadayapalam@3005: sym = findMethod(
sadayapalam@3005: env1, env1.enclClass.sym.type, name, argtypes, typeargtypes,
sadayapalam@3005: allowBoxing, useVarargs, false);
sadayapalam@3005: if (sym.exists()) {
sadayapalam@3005: if (staticOnly &&
sadayapalam@3005: sym.kind == MTH &&
sadayapalam@3005: sym.owner.kind == TYP &&
sadayapalam@3005: (sym.flags() & STATIC) == 0) return new StaticError(sym);
sadayapalam@3005: else return sym;
sadayapalam@3005: } else if (sym.kind < bestSoFar.kind) {
sadayapalam@3005: bestSoFar = sym;
sadayapalam@3005: }
sadayapalam@3005: } finally {
sadayapalam@3005: env1.info.preferredTreeForDiagnostics = null;
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:
emc@1970:
emc@1970: /**
emc@1970: * Find a type declared in a scope (not inherited). Return null
emc@1970: * if none is found.
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: */
emc@1970: Symbol findImmediateMemberType(Env env,
emc@1970: Type site,
emc@1970: Name name,
emc@1970: TypeSymbol c) {
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: }
emc@1970: return typeNotFound;
emc@1970: }
emc@1970:
emc@1970: /** Find a member type inherited from a superclass or interface.
emc@1970: * @param env The current environment.
emc@1970: * @param site The original type from where the selection takes
emc@1970: * place.
emc@1970: * @param name The type's name.
emc@1970: * @param c The class to search for the member type. This is
emc@1970: * always a superclass or implemented interface of
emc@1970: * site's class.
emc@1970: */
emc@1970: Symbol findInheritedMemberType(Env env,
emc@1970: Type site,
emc@1970: Name name,
emc@1970: TypeSymbol c) {
emc@1970: Symbol bestSoFar = typeNotFound;
emc@1970: Symbol sym;
duke@1: Type st = types.supertype(c.type);
jjg@1374: if (st != null && st.hasTag(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:
emc@1970: /** Find qualified member type.
emc@1970: * @param env The current environment.
emc@1970: * @param site The original type from where the selection takes
emc@1970: * place.
emc@1970: * @param name The type's name.
emc@1970: * @param c The class to search for the member type. This is
emc@1970: * always a superclass or implemented interface of
emc@1970: * site's class.
emc@1970: */
emc@1970: Symbol findMemberType(Env env,
emc@1970: Type site,
emc@1970: Name name,
emc@1970: TypeSymbol c) {
emc@1970: Symbol sym = findImmediateMemberType(env, site, name, c);
emc@1970:
emc@1970: if (sym != typeNotFound)
emc@1970: return sym;
emc@1970:
emc@1970: return findInheritedMemberType(env, site, name, c);
emc@1970:
emc@1970: }
emc@1970:
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:
emc@1970: Symbol findTypeVar(Env env, Name name, boolean staticOnly) {
emc@1970: for (Scope.Entry e = env.info.scope.lookup(name);
emc@1970: e.scope != null;
emc@1970: e = e.next()) {
emc@1970: if (e.sym.kind == TYP) {
emc@1970: if (staticOnly &&
emc@1970: e.sym.type.hasTag(TYPEVAR) &&
emc@1970: e.sym.owner.kind == TYP)
emc@1970: return new StaticError(e.sym);
emc@1970: return e.sym;
emc@1970: }
emc@1970: }
emc@1970: return typeNotFound;
emc@1970: }
emc@1970:
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;
emc@1970: // First, look for a type variable and the first member type
emc@1970: final Symbol tyvar = findTypeVar(env1, name, staticOnly);
emc@1970: sym = findImmediateMemberType(env1, env1.enclClass.sym.type,
emc@1970: name, env1.enclClass.sym);
emc@1970:
emc@1970: // Return the type variable if we have it, and have no
emc@1970: // immediate member, OR the type variable is for a method.
emc@1970: if (tyvar != typeNotFound) {
emc@1970: if (sym == typeNotFound ||
emc@1970: (tyvar.kind == TYP && tyvar.exists() &&
emc@1970: tyvar.owner.kind == MTH))
emc@1970: return tyvar;
duke@1: }
duke@1:
emc@1970: // If the environment is a class def, finish up,
emc@1970: // otherwise, do the entire findMemberType
emc@1970: if (sym == typeNotFound)
emc@1970: sym = findInheritedMemberType(env1, env1.enclClass.sym.type,
emc@1970: name, env1.enclClass.sym);
emc@1970:
duke@1: if (staticOnly && sym.kind == TYP &&
jjg@1374: sym.type.hasTag(CLASS) &&
jjg@1374: sym.type.getEnclosingType().hasTag(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.
jjg@1409: * @param name The identifier'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);
ohrstrom@1460: if (sym.kind==TYP) {
ohrstrom@1460: reportDependence(env.enclClass.sym, sym);
ohrstrom@1460: }
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:
ohrstrom@1460: /** Report dependencies.
ohrstrom@1460: * @param from The enclosing class sym
ohrstrom@1460: * @param to The found identifier that the class depends on.
ohrstrom@1460: */
ohrstrom@1460: public void reportDependence(Symbol from, Symbol to) {
ohrstrom@1460: // Override if you want to collect the reported dependencies.
ohrstrom@1460: }
ohrstrom@1460:
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
jjg@1326: * symbol {@literal (--> 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.
mcimadamore@1347: * @param location The symbol the served as a context for this lookup
duke@1: * @param site The original type from where the selection took place.
duke@1: * @param name The symbol's name.
mcimadamore@1347: * @param qualified Did we get here through a qualified expression resolution?
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.
mcimadamore@1347: * @param logResolveHelper helper class used to log resolve errors
duke@1: */
mcimadamore@1347: Symbol accessInternal(Symbol sym,
mcimadamore@1347: DiagnosticPosition pos,
mcimadamore@1347: Symbol location,
mcimadamore@1347: Type site,
mcimadamore@1347: Name name,
mcimadamore@1347: boolean qualified,
mcimadamore@1347: List argtypes,
mcimadamore@1347: List typeargtypes,
mcimadamore@1347: LogResolveHelper logResolveHelper) {
mcimadamore@1347: if (sym.kind >= AMBIGUOUS) {
vromero@2392: ResolveError errSym = (ResolveError)sym.baseSymbol();
mcimadamore@1347: sym = errSym.access(name, qualified ? site.tsym : syms.noSymbol);
mcimadamore@1347: argtypes = logResolveHelper.getArgumentTypes(errSym, sym, name, argtypes);
mcimadamore@1347: if (logResolveHelper.resolveDiagnosticNeeded(site, argtypes, typeargtypes)) {
mcimadamore@1347: logResolveError(errSym, pos, location, site, name, argtypes, typeargtypes);
mcimadamore@1347: }
mcimadamore@1347: }
mcimadamore@1347: return sym;
mcimadamore@1347: }
mcimadamore@1347:
mcimadamore@1347: /**
mcimadamore@1347: * Variant of the generalized access routine, to be used for generating method
mcimadamore@1347: * resolution diagnostics
mcimadamore@1347: */
mcimadamore@1347: Symbol accessMethod(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) {
mcimadamore@1347: return accessInternal(sym, pos, location, site, name, qualified, argtypes, typeargtypes, methodLogResolveHelper);
duke@1: }
duke@1:
mcimadamore@1347: /** Same as original accessMethod(), but without location.
mcimadamore@829: */
mcimadamore@1347: Symbol accessMethod(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@1347: return accessMethod(sym, pos, site.tsym, site, name, qualified, argtypes, typeargtypes);
mcimadamore@829: }
mcimadamore@829:
mcimadamore@1347: /**
mcimadamore@1347: * Variant of the generalized access routine, to be used for generating variable,
mcimadamore@1347: * type resolution diagnostics
mcimadamore@829: */
mcimadamore@1347: Symbol accessBase(Symbol sym,
mcimadamore@829: DiagnosticPosition pos,
mcimadamore@829: Symbol location,
mcimadamore@829: Type site,
mcimadamore@829: Name name,
mcimadamore@829: boolean qualified) {
mcimadamore@1347: return accessInternal(sym, pos, location, site, name, qualified, List.nil(), null, basicLogResolveHelper);
mcimadamore@829: }
mcimadamore@829:
mcimadamore@1347: /** Same as original accessBase(), but without location.
duke@1: */
mcimadamore@1347: Symbol accessBase(Symbol sym,
duke@1: DiagnosticPosition pos,
duke@1: Type site,
duke@1: Name name,
duke@1: boolean qualified) {
mcimadamore@1347: return accessBase(sym, pos, site.tsym, site, name, qualified);
duke@1: }
duke@1:
mcimadamore@1347: interface LogResolveHelper {
mcimadamore@1347: boolean resolveDiagnosticNeeded(Type site, List argtypes, List typeargtypes);
mcimadamore@1347: List getArgumentTypes(ResolveError errSym, Symbol accessedSym, Name name, List argtypes);
mcimadamore@1347: }
mcimadamore@1347:
mcimadamore@1347: LogResolveHelper basicLogResolveHelper = new LogResolveHelper() {
mcimadamore@1347: public boolean resolveDiagnosticNeeded(Type site, List argtypes, List typeargtypes) {
mcimadamore@1347: return !site.isErroneous();
mcimadamore@1347: }
mcimadamore@1347: public List getArgumentTypes(ResolveError errSym, Symbol accessedSym, Name name, List argtypes) {
mcimadamore@1347: return argtypes;
mcimadamore@1347: }
mcimadamore@1347: };
mcimadamore@1347:
mcimadamore@1347: LogResolveHelper methodLogResolveHelper = new LogResolveHelper() {
mcimadamore@1347: public boolean resolveDiagnosticNeeded(Type site, List argtypes, List typeargtypes) {
mcimadamore@1347: return !site.isErroneous() &&
mcimadamore@1347: !Type.isErroneous(argtypes) &&
mcimadamore@1347: (typeargtypes == null || !Type.isErroneous(typeargtypes));
mcimadamore@1347: }
mcimadamore@1347: public List getArgumentTypes(ResolveError errSym, Symbol accessedSym, Name name, List argtypes) {
mcimadamore@1415: return (syms.operatorNames.contains(name)) ?
mcimadamore@1415: argtypes :
vromero@2000: Type.map(argtypes, new ResolveDeferredRecoveryMap(AttrMode.SPECULATIVE, accessedSym, currentResolutionContext.step));
mcimadamore@1347: }
mcimadamore@1347: };
mcimadamore@1347:
vromero@2000: class ResolveDeferredRecoveryMap extends DeferredAttr.RecoveryDeferredTypeMap {
vromero@2000:
vromero@2000: public ResolveDeferredRecoveryMap(AttrMode mode, Symbol msym, MethodResolutionPhase step) {
vromero@2000: deferredAttr.super(mode, msym, step);
vromero@2000: }
vromero@2000:
vromero@2000: @Override
vromero@2000: protected Type typeOf(DeferredType dt) {
vromero@2000: Type res = super.typeOf(dt);
vromero@2000: if (!res.isErroneous()) {
vromero@2000: switch (TreeInfo.skipParens(dt.tree).getTag()) {
vromero@2000: case LAMBDA:
vromero@2000: case REFERENCE:
vromero@2000: return dt;
vromero@2000: case CONDEXPR:
vromero@2000: return res == Type.recoveryType ?
vromero@2000: dt : res;
vromero@2000: }
vromero@2000: }
vromero@2000: return res;
vromero@2000: }
vromero@2000: }
vromero@2000:
duke@1: /** Check that sym is not an abstract method.
duke@1: */
duke@1: void checkNonAbstract(DiagnosticPosition pos, Symbol sym) {
mcimadamore@1393: if ((sym.flags() & ABSTRACT) != 0 && (sym.flags() & DEFAULT) == 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) {
jjg@1374: while (t.hasTag(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) {
mcimadamore@1347: return accessBase(
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@1674: return lookupMethod(env, pos, env.enclClass.sym, resolveMethodCheck,
mcimadamore@1674: new BasicLookupHelper(name, env.enclClass.sym.type, argtypes, typeargtypes) {
mcimadamore@1674: @Override
mcimadamore@1875: Symbol doLookup(Env env, MethodResolutionPhase phase) {
mcimadamore@1674: return findFun(env, name, argtypes, typeargtypes,
mcimadamore@1674: phase.isBoxingRequired(),
mcimadamore@1674: phase.isVarargsRequired());
mcimadamore@1674: }});
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@1394: return lookupMethod(env, pos, location, resolveContext, new BasicLookupHelper(name, site, argtypes, typeargtypes) {
mcimadamore@1394: @Override
mcimadamore@1875: Symbol doLookup(Env env, MethodResolutionPhase phase) {
mcimadamore@1394: return findMethod(env, site, name, argtypes, typeargtypes,
mcimadamore@1394: phase.isBoxingRequired(),
mcimadamore@1394: phase.isVarargsRequired(), false);
mcimadamore@1215: }
mcimadamore@1394: @Override
mcimadamore@1394: Symbol access(Env env, DiagnosticPosition pos, Symbol location, Symbol sym) {
mcimadamore@1394: if (sym.kind >= AMBIGUOUS) {
mcimadamore@1394: sym = super.access(env, pos, location, sym);
mcimadamore@1394: } else if (allowMethodHandles) {
mcimadamore@1394: MethodSymbol msym = (MethodSymbol)sym;
vromero@1820: if ((msym.flags() & SIGNATURE_POLYMORPHIC) != 0) {
mcimadamore@1394: return findPolymorphicSignatureInstance(env, sym, argtypes);
mcimadamore@1394: }
mcimadamore@1215: }
mcimadamore@1394: return sym;
mcimadamore@674: }
mcimadamore@1394: });
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@1239: * @param spMethod signature polymorphic method - i.e. MH.invokeExact
mcimadamore@1239: * @param argtypes The required argument types
mcimadamore@674: */
mcimadamore@1239: Symbol findPolymorphicSignatureInstance(Env env,
mcimadamore@1415: final Symbol spMethod,
mcimadamore@820: List argtypes) {
mcimadamore@674: Type mtype = infer.instantiatePolymorphicSignatureInstance(env,
mcimadamore@1347: (MethodSymbol)spMethod, currentResolutionContext, argtypes);
mcimadamore@1239: for (Symbol sym : polymorphicSignatureScope.getElementsByName(spMethod.name)) {
mcimadamore@1239: if (types.isSameType(mtype, sym.type)) {
mcimadamore@1239: return sym;
mcimadamore@674: }
mcimadamore@674: }
mcimadamore@1239:
mcimadamore@1239: // create the desired method
mcimadamore@1239: long flags = ABSTRACT | HYPOTHETICAL | spMethod.flags() & Flags.AccessFlags;
mcimadamore@1415: Symbol msym = new MethodSymbol(flags, spMethod.name, mtype, spMethod.owner) {
mcimadamore@1415: @Override
mcimadamore@1415: public Symbol baseSymbol() {
mcimadamore@1415: return spMethod;
mcimadamore@1415: }
mcimadamore@1415: };
mcimadamore@3075: if (!mtype.isErroneous()) { // Cache only if kosher.
mcimadamore@3075: polymorphicSignatureScope.enter(msym);
mcimadamore@3075: }
mcimadamore@1239: return msym;
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@1394:
mcimadamore@1215: private Symbol resolveConstructor(MethodResolutionContext resolveContext,
mcimadamore@1394: final DiagnosticPosition pos,
mcimadamore@1215: Env env,
mcimadamore@1215: Type site,
mcimadamore@1215: List argtypes,
mcimadamore@1215: List typeargtypes) {
mcimadamore@1394: return lookupMethod(env, pos, site.tsym, resolveContext, new BasicLookupHelper(names.init, site, argtypes, typeargtypes) {
mcimadamore@1394: @Override
mcimadamore@1875: Symbol doLookup(Env env, MethodResolutionPhase phase) {
mcimadamore@1394: return findConstructor(pos, env, site, argtypes, typeargtypes,
mcimadamore@1394: phase.isBoxingRequired(),
mcimadamore@1394: phase.isVarargsRequired());
mcimadamore@1215: }
mcimadamore@1394: });
mcimadamore@1394: }
mcimadamore@1394:
mcimadamore@1394: /** Resolve a constructor, throw a fatal error if not found.
mcimadamore@1394: * @param pos The position to use for error reporting.
mcimadamore@1394: * @param env The environment current at the method invocation.
mcimadamore@1394: * @param site The type to be constructed.
mcimadamore@1394: * @param argtypes The types of the invocation's value arguments.
mcimadamore@1394: * @param typeargtypes The types of the invocation's type arguments.
mcimadamore@1394: */
mcimadamore@1394: public MethodSymbol resolveInternalConstructor(DiagnosticPosition pos, Env env,
mcimadamore@1394: Type site,
mcimadamore@1394: List argtypes,
mcimadamore@1394: List typeargtypes) {
mcimadamore@1394: MethodResolutionContext resolveContext = new MethodResolutionContext();
mcimadamore@1394: resolveContext.internalResolution = true;
mcimadamore@1394: Symbol sym = resolveConstructor(resolveContext, pos, env, site, argtypes, typeargtypes);
mcimadamore@1394: if (sym.kind == MTH) return (MethodSymbol)sym;
mcimadamore@1394: else throw new FatalError(
mcimadamore@1394: diags.fragment("fatal.err.cant.locate.ctor", site));
mcimadamore@1394: }
mcimadamore@1394:
mcimadamore@1394: Symbol findConstructor(DiagnosticPosition pos, Env env,
mcimadamore@1394: Type site, List argtypes,
mcimadamore@1394: List typeargtypes,
mcimadamore@1394: boolean allowBoxing,
mcimadamore@1394: boolean useVarargs) {
mcimadamore@1394: Symbol sym = findMethod(env, site,
mcimadamore@1394: names.init, argtypes,
mcimadamore@1394: typeargtypes, allowBoxing,
mcimadamore@1394: useVarargs, false);
mcimadamore@1394: chk.checkDeprecated(pos, env.info.scope.owner, sym);
mcimadamore@1394: return sym;
duke@1: }
duke@1:
mcimadamore@537: /** Resolve constructor using diamond inference.
mcimadamore@537: * @param pos The position to use for error reporting.
mcimadamore@537: * @param env The environment current at the constructor invocation.
mcimadamore@537: * @param site The type of class for which a constructor is searched.
mcimadamore@537: * The scope of this class has been touched in attribution.
mcimadamore@537: * @param argtypes The types of the constructor invocation's value
mcimadamore@537: * arguments.
mcimadamore@537: * @param typeargtypes The types of the constructor invocation's type
mcimadamore@537: * arguments.
mcimadamore@537: */
mcimadamore@537: Symbol resolveDiamond(DiagnosticPosition pos,
mcimadamore@537: Env env,
mcimadamore@537: Type site,
mcimadamore@537: List argtypes,
mcimadamore@631: List typeargtypes) {
mcimadamore@1674: return lookupMethod(env, pos, site.tsym, resolveMethodCheck,
mcimadamore@1674: new BasicLookupHelper(names.init, site, argtypes, typeargtypes) {
mcimadamore@1674: @Override
mcimadamore@1875: Symbol doLookup(Env env, MethodResolutionPhase phase) {
mcimadamore@1674: return findDiamond(env, site, argtypes, typeargtypes,
mcimadamore@1674: phase.isBoxingRequired(),
mcimadamore@1674: phase.isVarargsRequired());
mcimadamore@1674: }
mcimadamore@1674: @Override
mcimadamore@1674: Symbol access(Env env, DiagnosticPosition pos, Symbol location, Symbol sym) {
mcimadamore@1674: if (sym.kind >= AMBIGUOUS) {
vromero@2026: if (sym.kind != WRONG_MTH && sym.kind != WRONG_MTHS) {
vromero@2003: sym = super.access(env, pos, location, sym);
vromero@2003: } else {
vromero@2003: final JCDiagnostic details = sym.kind == WRONG_MTH ?
vromero@2392: ((InapplicableSymbolError)sym.baseSymbol()).errCandidate().snd :
vromero@2003: null;
vromero@2003: sym = new InapplicableSymbolError(sym.kind, "diamondError", currentResolutionContext) {
vromero@2003: @Override
vromero@2003: JCDiagnostic getDiagnostic(DiagnosticType dkind, DiagnosticPosition pos,
vromero@2003: Symbol location, Type site, Name name, List argtypes, List typeargtypes) {
vromero@2003: String key = details == null ?
vromero@2003: "cant.apply.diamond" :
vromero@2003: "cant.apply.diamond.1";
vromero@2003: return diags.create(dkind, log.currentSource(), pos, key,
vromero@2003: diags.fragment("diamond", site.tsym), details);
vromero@2003: }
vromero@2003: };
vromero@2003: sym = accessMethod(sym, pos, site, names.init, true, argtypes, typeargtypes);
vromero@2003: env.info.pendingResolutionPhase = currentResolutionContext.step;
vromero@2003: }
mcimadamore@1394: }
mcimadamore@1674: return sym;
mcimadamore@1674: }});
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:
jjg@1326: * {@code 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:
jjg@1326: * {@code 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@1341: final Symbol sym = e.sym;
mcimadamore@1217: //- System.out.println(" e " + e.sym);
mcimadamore@1341: if (sym.kind == MTH &&
mcimadamore@1341: (sym.flags_field & SYNTHETIC) == 0) {
jjg@1374: List oldParams = e.sym.type.hasTag(FORALL) ?
mcimadamore@1341: ((ForAll)sym.type).tvars :
mcimadamore@1217: List.nil();
mcimadamore@1217: Type constrType = new ForAll(site.tsym.type.getTypeArguments().appendList(oldParams),
mcimadamore@1341: types.createMethodTypeWithReturn(sym.type.asMethodType(), site));
mcimadamore@1341: MethodSymbol newConstr = new MethodSymbol(sym.flags(), names.init, constrType, site.tsym) {
mcimadamore@1341: @Override
mcimadamore@1341: public Symbol baseSymbol() {
mcimadamore@1341: return sym;
mcimadamore@1341: }
mcimadamore@1341: };
mcimadamore@1217: bestSoFar = selectBest(env, site, argtypes, typeargtypes,
mcimadamore@1341: newConstr,
mcimadamore@1217: bestSoFar,
mcimadamore@1217: allowBoxing,
mcimadamore@1217: useVarargs,
mcimadamore@1217: false);
mcimadamore@1217: }
mcimadamore@1217: }
mcimadamore@1217: return bestSoFar;
mcimadamore@1217: }
mcimadamore@1217:
mcimadamore@1394:
mcimadamore@1394:
mcimadamore@1394: /** Resolve operator.
mcimadamore@1394: * @param pos The position to use for error reporting.
mcimadamore@1394: * @param optag The tag of the operation tree.
mcimadamore@1394: * @param env The environment current at the operation.
mcimadamore@1394: * @param argtypes The types of the operands.
mcimadamore@1394: */
mcimadamore@1394: Symbol resolveOperator(DiagnosticPosition pos, JCTree.Tag optag,
mcimadamore@1394: Env env, List argtypes) {
mcimadamore@1394: MethodResolutionContext prevResolutionContext = currentResolutionContext;
mcimadamore@1394: try {
mcimadamore@1394: currentResolutionContext = new MethodResolutionContext();
mcimadamore@1394: Name name = treeinfo.operatorName(optag);
mcimadamore@1779: return lookupMethod(env, pos, syms.predefClass, currentResolutionContext,
mcimadamore@1779: new BasicLookupHelper(name, syms.predefClass.type, argtypes, null, BOX) {
mcimadamore@1779: @Override
mcimadamore@1875: Symbol doLookup(Env env, MethodResolutionPhase phase) {
mcimadamore@1779: return findMethod(env, site, name, argtypes, typeargtypes,
mcimadamore@1779: phase.isBoxingRequired(),
mcimadamore@1779: phase.isVarargsRequired(), true);
mcimadamore@1779: }
mcimadamore@1779: @Override
mcimadamore@1779: Symbol access(Env env, DiagnosticPosition pos, Symbol location, Symbol sym) {
mcimadamore@1779: return accessMethod(sym, pos, env.enclClass.sym.type, name,
mcimadamore@1394: false, argtypes, null);
mcimadamore@1779: }
mcimadamore@1779: });
mcimadamore@1779: } finally {
mcimadamore@1394: currentResolutionContext = prevResolutionContext;
mcimadamore@1394: }
mcimadamore@1394: }
mcimadamore@1394:
mcimadamore@1394: /** Resolve operator.
mcimadamore@1394: * @param pos The position to use for error reporting.
mcimadamore@1394: * @param optag The tag of the operation tree.
mcimadamore@1394: * @param env The environment current at the operation.
mcimadamore@1394: * @param arg The type of the operand.
mcimadamore@1394: */
mcimadamore@1394: Symbol resolveUnaryOperator(DiagnosticPosition pos, JCTree.Tag optag, Env env, Type arg) {
mcimadamore@1394: return resolveOperator(pos, optag, env, List.of(arg));
mcimadamore@1394: }
mcimadamore@1394:
mcimadamore@1394: /** Resolve binary operator.
mcimadamore@1394: * @param pos The position to use for error reporting.
mcimadamore@1394: * @param optag The tag of the operation tree.
mcimadamore@1394: * @param env The environment current at the operation.
mcimadamore@1394: * @param left The types of the left operand.
mcimadamore@1394: * @param right The types of the right operand.
mcimadamore@1394: */
mcimadamore@1394: Symbol resolveBinaryOperator(DiagnosticPosition pos,
mcimadamore@1394: JCTree.Tag optag,
mcimadamore@1394: Env env,
mcimadamore@1394: Type left,
mcimadamore@1394: Type right) {
mcimadamore@1394: return resolveOperator(pos, optag, env, List.of(left, right));
mcimadamore@1394: }
mcimadamore@1394:
vromero@2193: Symbol getMemberReference(DiagnosticPosition pos,
vromero@2193: Env env,
vromero@2193: JCMemberReference referenceTree,
vromero@2193: Type site,
vromero@2193: Name name) {
vromero@2193:
vromero@2193: site = types.capture(site);
vromero@2193:
vromero@2193: ReferenceLookupHelper lookupHelper = makeReferenceLookupHelper(
vromero@2193: referenceTree, site, name, List.nil(), null, VARARITY);
vromero@2193:
vromero@2193: Env newEnv = env.dup(env.tree, env.info.dup());
vromero@2193: Symbol sym = lookupMethod(newEnv, env.tree.pos(), site.tsym,
vromero@2193: nilMethodCheck, lookupHelper);
vromero@2193:
vromero@2193: env.info.pendingResolutionPhase = newEnv.info.pendingResolutionPhase;
vromero@2193:
vromero@2193: return sym;
vromero@2193: }
vromero@2193:
vromero@2193: ReferenceLookupHelper makeReferenceLookupHelper(JCMemberReference referenceTree,
vromero@2193: Type site,
vromero@2193: Name name,
vromero@2193: List argtypes,
vromero@2193: List typeargtypes,
vromero@2193: MethodResolutionPhase maxPhase) {
vromero@2193: ReferenceLookupHelper result;
vromero@2193: if (!name.equals(names.init)) {
vromero@2193: //method reference
vromero@2193: result =
vromero@2193: new MethodReferenceLookupHelper(referenceTree, name, site, argtypes, typeargtypes, maxPhase);
vromero@2193: } else {
vromero@2193: if (site.hasTag(ARRAY)) {
vromero@2193: //array constructor reference
vromero@2193: result =
vromero@2193: new ArrayConstructorReferenceLookupHelper(referenceTree, site, argtypes, typeargtypes, maxPhase);
vromero@2193: } else {
vromero@2193: //class constructor reference
vromero@2193: result =
vromero@2193: new ConstructorReferenceLookupHelper(referenceTree, site, argtypes, typeargtypes, maxPhase);
vromero@2193: }
vromero@2193: }
vromero@2193: return result;
vromero@2193: }
vromero@2193:
vromero@2193: Symbol resolveMemberReferenceByArity(Env env,
vromero@2193: JCMemberReference referenceTree,
vromero@2193: Type site,
vromero@2193: Name name,
vromero@2193: List argtypes,
vromero@2193: InferenceContext inferenceContext) {
vromero@2193:
vromero@2193: boolean isStaticSelector = TreeInfo.isStaticSelector(referenceTree.expr, names);
vromero@2193: site = types.capture(site);
vromero@2193:
vromero@2193: ReferenceLookupHelper boundLookupHelper = makeReferenceLookupHelper(
vromero@2193: referenceTree, site, name, argtypes, null, VARARITY);
vromero@2193: //step 1 - bound lookup
vromero@2193: Env boundEnv = env.dup(env.tree, env.info.dup());
vromero@2193: Symbol boundSym = lookupMethod(boundEnv, env.tree.pos(), site.tsym,
vromero@2193: arityMethodCheck, boundLookupHelper);
vromero@2193: if (isStaticSelector &&
vromero@2193: !name.equals(names.init) &&
vromero@2193: !boundSym.isStatic() &&
vromero@2193: boundSym.kind < ERRONEOUS) {
vromero@2193: boundSym = methodNotFound;
vromero@2193: }
vromero@2193:
vromero@2193: //step 2 - unbound lookup
vromero@2193: Symbol unboundSym = methodNotFound;
vromero@2193: ReferenceLookupHelper unboundLookupHelper = null;
vromero@2193: Env unboundEnv = env.dup(env.tree, env.info.dup());
vromero@2193: if (isStaticSelector) {
vromero@2193: unboundLookupHelper = boundLookupHelper.unboundLookup(inferenceContext);
vromero@2193: unboundSym = lookupMethod(unboundEnv, env.tree.pos(), site.tsym,
vromero@2193: arityMethodCheck, unboundLookupHelper);
vromero@2193: if (unboundSym.isStatic() &&
vromero@2193: unboundSym.kind < ERRONEOUS) {
vromero@2193: unboundSym = methodNotFound;
vromero@2193: }
vromero@2193: }
vromero@2193:
vromero@2193: //merge results
vromero@2193: Symbol bestSym = choose(boundSym, unboundSym);
vromero@2193: env.info.pendingResolutionPhase = bestSym == unboundSym ?
vromero@2193: unboundEnv.info.pendingResolutionPhase :
vromero@2193: boundEnv.info.pendingResolutionPhase;
vromero@2193:
vromero@2193: return bestSym;
vromero@2193: }
vromero@2193:
mcimadamore@1352: /**
mcimadamore@1352: * Resolution of member references is typically done as a single
mcimadamore@1352: * overload resolution step, where the argument types A are inferred from
mcimadamore@1352: * the target functional descriptor.
mcimadamore@1352: *
mcimadamore@1352: * If the member reference is a method reference with a type qualifier,
mcimadamore@1352: * a two-step lookup process is performed. The first step uses the
mcimadamore@1352: * expected argument list A, while the second step discards the first
mcimadamore@1352: * type from A (which is treated as a receiver type).
mcimadamore@1352: *
mcimadamore@1352: * There are two cases in which inference is performed: (i) if the member
mcimadamore@1352: * reference is a constructor reference and the qualifier type is raw - in
mcimadamore@1352: * which case diamond inference is used to infer a parameterization for the
mcimadamore@1352: * type qualifier; (ii) if the member reference is an unbound reference
mcimadamore@1352: * where the type qualifier is raw - in that case, during the unbound lookup
mcimadamore@1352: * the receiver argument type is used to infer an instantiation for the raw
mcimadamore@1352: * qualifier type.
mcimadamore@1352: *
mcimadamore@1352: * When a multi-step resolution process is exploited, it is an error
mcimadamore@1352: * if two candidates are found (ambiguity).
mcimadamore@1352: *
mcimadamore@1352: * This routine returns a pair (T,S), where S is the member reference symbol,
mcimadamore@1352: * and T is the type of the class in which S is defined. This is necessary as
mcimadamore@1352: * the type T might be dynamically inferred (i.e. if constructor reference
mcimadamore@1352: * has a raw qualifier).
mcimadamore@1352: */
vromero@2193: Pair resolveMemberReference(Env env,
mcimadamore@1352: JCMemberReference referenceTree,
mcimadamore@1352: Type site,
vromero@2193: Name name,
vromero@2193: List argtypes,
mcimadamore@1352: List typeargtypes,
mcimadamore@1897: MethodCheck methodCheck,
vromero@2193: InferenceContext inferenceContext,
vromero@2193: AttrMode mode) {
mcimadamore@1496:
mcimadamore@1921: site = types.capture(site);
vromero@2193: ReferenceLookupHelper boundLookupHelper = makeReferenceLookupHelper(
vromero@2193: referenceTree, site, name, argtypes, typeargtypes, VARARITY);
mcimadamore@1496:
mcimadamore@1352: //step 1 - bound lookup
mcimadamore@1352: Env boundEnv = env.dup(env.tree, env.info.dup());
vromero@2193: Symbol origBoundSym;
vromero@2193: boolean staticErrorForBound = false;
vromero@2193: MethodResolutionContext boundSearchResolveContext = new MethodResolutionContext();
vromero@2193: boundSearchResolveContext.methodCheck = methodCheck;
vromero@2193: Symbol boundSym = origBoundSym = lookupMethod(boundEnv, env.tree.pos(),
vromero@2193: site.tsym, boundSearchResolveContext, boundLookupHelper);
vromero@2193: SearchResultKind boundSearchResultKind = SearchResultKind.NOT_APPLICABLE_MATCH;
vromero@2193: boolean isStaticSelector = TreeInfo.isStaticSelector(referenceTree.expr, names);
vromero@2193: boolean shouldCheckForStaticness = isStaticSelector &&
vromero@2193: referenceTree.getMode() == ReferenceMode.INVOKE;
vromero@2193: if (boundSym.kind != WRONG_MTHS && boundSym.kind != WRONG_MTH) {
vromero@2193: if (shouldCheckForStaticness) {
vromero@2193: if (!boundSym.isStatic()) {
vromero@2193: staticErrorForBound = true;
vromero@2193: if (hasAnotherApplicableMethod(
vromero@2193: boundSearchResolveContext, boundSym, true)) {
vromero@2193: boundSearchResultKind = SearchResultKind.BAD_MATCH_MORE_SPECIFIC;
vromero@2193: } else {
vromero@2193: boundSearchResultKind = SearchResultKind.BAD_MATCH;
vromero@2193: if (boundSym.kind < ERRONEOUS) {
vromero@2193: boundSym = methodWithCorrectStaticnessNotFound;
vromero@2193: }
vromero@2193: }
vromero@2193: } else if (boundSym.kind < ERRONEOUS) {
vromero@2193: boundSearchResultKind = SearchResultKind.GOOD_MATCH;
vromero@2193: }
vromero@2193: }
vromero@2193: }
mcimadamore@1352:
mcimadamore@1352: //step 2 - unbound lookup
vromero@2193: Symbol origUnboundSym = null;
vromero@2193: Symbol unboundSym = methodNotFound;
vromero@2193: ReferenceLookupHelper unboundLookupHelper = null;
mcimadamore@1352: Env unboundEnv = env.dup(env.tree, env.info.dup());
vromero@2193: SearchResultKind unboundSearchResultKind = SearchResultKind.NOT_APPLICABLE_MATCH;
vromero@2193: boolean staticErrorForUnbound = false;
vromero@2193: if (isStaticSelector) {
vromero@2193: unboundLookupHelper = boundLookupHelper.unboundLookup(inferenceContext);
vromero@2193: MethodResolutionContext unboundSearchResolveContext =
vromero@2193: new MethodResolutionContext();
vromero@2193: unboundSearchResolveContext.methodCheck = methodCheck;
vromero@2193: unboundSym = origUnboundSym = lookupMethod(unboundEnv, env.tree.pos(),
vromero@2193: site.tsym, unboundSearchResolveContext, unboundLookupHelper);
vromero@2193:
vromero@2193: if (unboundSym.kind != WRONG_MTH && unboundSym.kind != WRONG_MTHS) {
vromero@2193: if (shouldCheckForStaticness) {
vromero@2193: if (unboundSym.isStatic()) {
vromero@2193: staticErrorForUnbound = true;
vromero@2193: if (hasAnotherApplicableMethod(
vromero@2193: unboundSearchResolveContext, unboundSym, false)) {
vromero@2193: unboundSearchResultKind = SearchResultKind.BAD_MATCH_MORE_SPECIFIC;
vromero@2193: } else {
vromero@2193: unboundSearchResultKind = SearchResultKind.BAD_MATCH;
vromero@2193: if (unboundSym.kind < ERRONEOUS) {
vromero@2193: unboundSym = methodWithCorrectStaticnessNotFound;
vromero@2193: }
vromero@2193: }
vromero@2193: } else if (unboundSym.kind < ERRONEOUS) {
vromero@2193: unboundSearchResultKind = SearchResultKind.GOOD_MATCH;
vromero@2193: }
vromero@2193: }
vromero@2193: }
vromero@2193: }
mcimadamore@1352:
mcimadamore@1352: //merge results
mcimadamore@1352: Pair res;
mcimadamore@1921: Symbol bestSym = choose(boundSym, unboundSym);
vromero@2193: if (bestSym.kind < ERRONEOUS && (staticErrorForBound || staticErrorForUnbound)) {
vromero@2193: if (staticErrorForBound) {
vromero@2193: boundSym = methodWithCorrectStaticnessNotFound;
vromero@2193: }
vromero@2193: if (staticErrorForUnbound) {
vromero@2193: unboundSym = methodWithCorrectStaticnessNotFound;
vromero@2193: }
vromero@2193: bestSym = choose(boundSym, unboundSym);
vromero@2193: }
vromero@2193: if (bestSym == methodWithCorrectStaticnessNotFound && mode == AttrMode.CHECK) {
vromero@2193: Symbol symToPrint = origBoundSym;
vromero@2193: String errorFragmentToPrint = "non-static.cant.be.ref";
vromero@2193: if (staticErrorForBound && staticErrorForUnbound) {
vromero@2193: if (unboundSearchResultKind == SearchResultKind.BAD_MATCH_MORE_SPECIFIC) {
vromero@2193: symToPrint = origUnboundSym;
vromero@2193: errorFragmentToPrint = "static.method.in.unbound.lookup";
vromero@2193: }
vromero@2193: } else {
vromero@2193: if (!staticErrorForBound) {
vromero@2193: symToPrint = origUnboundSym;
vromero@2193: errorFragmentToPrint = "static.method.in.unbound.lookup";
vromero@2193: }
vromero@2193: }
vromero@2193: log.error(referenceTree.expr.pos(), "invalid.mref",
vromero@2193: Kinds.kindName(referenceTree.getMode()),
vromero@2193: diags.fragment(errorFragmentToPrint,
vromero@2193: Kinds.kindName(symToPrint), symToPrint));
vromero@2193: }
vromero@2193: res = new Pair<>(bestSym,
mcimadamore@1921: bestSym == unboundSym ? unboundLookupHelper : boundLookupHelper);
mcimadamore@1921: env.info.pendingResolutionPhase = bestSym == unboundSym ?
mcimadamore@1921: unboundEnv.info.pendingResolutionPhase :
mcimadamore@1921: boundEnv.info.pendingResolutionPhase;
mcimadamore@1352:
mcimadamore@1352: return res;
mcimadamore@1352: }
vromero@2193:
vromero@2193: enum SearchResultKind {
vromero@2193: GOOD_MATCH, //type I
vromero@2193: BAD_MATCH_MORE_SPECIFIC, //type II
vromero@2193: BAD_MATCH, //type III
vromero@2193: NOT_APPLICABLE_MATCH //type IV
vromero@2193: }
vromero@2193:
vromero@2193: boolean hasAnotherApplicableMethod(MethodResolutionContext resolutionContext,
vromero@2193: Symbol bestSoFar, boolean staticMth) {
vromero@2193: for (Candidate c : resolutionContext.candidates) {
vromero@2193: if (resolutionContext.step != c.step ||
vromero@2193: !c.isApplicable() ||
vromero@2193: c.sym == bestSoFar) {
vromero@2193: continue;
vromero@2193: } else {
vromero@2193: if (c.sym.isStatic() == staticMth) {
vromero@2193: return true;
vromero@2193: }
vromero@2193: }
vromero@2193: }
vromero@2193: return false;
vromero@2193: }
vromero@2193:
mcimadamore@1921: //where
vromero@2193: private Symbol choose(Symbol boundSym, Symbol unboundSym) {
vromero@2193: if (lookupSuccess(boundSym) && lookupSuccess(unboundSym)) {
vromero@2193: return ambiguityError(boundSym, unboundSym);
vromero@2193: } else if (lookupSuccess(boundSym) ||
vromero@2193: (canIgnore(unboundSym) && !canIgnore(boundSym))) {
vromero@2193: return boundSym;
vromero@2193: } else if (lookupSuccess(unboundSym) ||
vromero@2193: (canIgnore(boundSym) && !canIgnore(unboundSym))) {
vromero@2193: return unboundSym;
mcimadamore@1921: } else {
vromero@2193: return boundSym;
mcimadamore@1921: }
mcimadamore@1921: }
mcimadamore@1921:
mcimadamore@1921: private boolean lookupSuccess(Symbol s) {
mcimadamore@1581: return s.kind == MTH || s.kind == AMBIGUOUS;
mcimadamore@1581: }
mcimadamore@1352:
mcimadamore@1921: private boolean canIgnore(Symbol s) {
mcimadamore@1921: switch (s.kind) {
mcimadamore@1921: case ABSENT_MTH:
mcimadamore@1921: return true;
mcimadamore@1921: case WRONG_MTH:
mcimadamore@1921: InapplicableSymbolError errSym =
vromero@2392: (InapplicableSymbolError)s.baseSymbol();
mcimadamore@1921: return new Template(MethodCheckDiag.ARITY_MISMATCH.regex())
mcimadamore@1921: .matches(errSym.errCandidate().snd);
mcimadamore@1921: case WRONG_MTHS:
mcimadamore@1921: InapplicableSymbolsError errSyms =
vromero@2392: (InapplicableSymbolsError)s.baseSymbol();
mcimadamore@1921: return errSyms.filterCandidates(errSyms.mapCandidates()).isEmpty();
vromero@2193: case WRONG_STATICNESS:
vromero@2193: return false;
mcimadamore@1921: default:
mcimadamore@1921: return false;
mcimadamore@1921: }
mcimadamore@1921: }
mcimadamore@1921:
mcimadamore@1352: /**
mcimadamore@1352: * Helper for defining custom method-like lookup logic; a lookup helper
mcimadamore@1352: * provides hooks for (i) the actual lookup logic and (ii) accessing the
mcimadamore@1352: * lookup result (this step might result in compiler diagnostics to be generated)
mcimadamore@1352: */
mcimadamore@1352: abstract class LookupHelper {
mcimadamore@1352:
mcimadamore@1352: /** name of the symbol to lookup */
mcimadamore@1352: Name name;
mcimadamore@1352:
mcimadamore@1352: /** location in which the lookup takes place */
mcimadamore@1352: Type site;
mcimadamore@1352:
mcimadamore@1352: /** actual types used during the lookup */
mcimadamore@1352: List argtypes;
mcimadamore@1352:
mcimadamore@1352: /** type arguments used during the lookup */
mcimadamore@1352: List typeargtypes;
mcimadamore@1352:
mcimadamore@1394: /** Max overload resolution phase handled by this helper */
mcimadamore@1394: MethodResolutionPhase maxPhase;
mcimadamore@1394:
mcimadamore@1394: LookupHelper(Name name, Type site, List argtypes, List typeargtypes, MethodResolutionPhase maxPhase) {
mcimadamore@1352: this.name = name;
mcimadamore@1352: this.site = site;
mcimadamore@1352: this.argtypes = argtypes;
mcimadamore@1352: this.typeargtypes = typeargtypes;
mcimadamore@1394: this.maxPhase = maxPhase;
mcimadamore@1394: }
mcimadamore@1394:
mcimadamore@1394: /**
mcimadamore@1394: * Should lookup stop at given phase with given result
mcimadamore@1394: */
mcimadamore@2559: final boolean shouldStop(Symbol sym, MethodResolutionPhase phase) {
mcimadamore@1394: return phase.ordinal() > maxPhase.ordinal() ||
mcimadamore@1394: sym.kind < ERRONEOUS || sym.kind == AMBIGUOUS;
mcimadamore@1352: }
mcimadamore@1352:
mcimadamore@1352: /**
mcimadamore@1352: * Search for a symbol under a given overload resolution phase - this method
mcimadamore@1352: * is usually called several times, once per each overload resolution phase
mcimadamore@1352: */
mcimadamore@1352: abstract Symbol lookup(Env env, MethodResolutionPhase phase);
mcimadamore@1352:
mcimadamore@1352: /**
mcimadamore@1875: * Dump overload resolution info
mcimadamore@1875: */
mcimadamore@1875: void debug(DiagnosticPosition pos, Symbol sym) {
mcimadamore@1875: //do nothing
mcimadamore@1875: }
mcimadamore@1875:
mcimadamore@1875: /**
mcimadamore@1352: * Validate the result of the lookup
mcimadamore@1352: */
mcimadamore@1394: abstract Symbol access(Env env, DiagnosticPosition pos, Symbol location, Symbol sym);
mcimadamore@1394: }
mcimadamore@1394:
mcimadamore@1394: abstract class BasicLookupHelper extends LookupHelper {
mcimadamore@1394:
mcimadamore@1394: BasicLookupHelper(Name name, Type site, List argtypes, List typeargtypes) {
mcimadamore@1779: this(name, site, argtypes, typeargtypes, MethodResolutionPhase.VARARITY);
mcimadamore@1779: }
mcimadamore@1779:
mcimadamore@1779: BasicLookupHelper(Name name, Type site, List argtypes, List typeargtypes, MethodResolutionPhase maxPhase) {
mcimadamore@1779: super(name, site, argtypes, typeargtypes, maxPhase);
mcimadamore@1394: }
mcimadamore@1394:
mcimadamore@1394: @Override
mcimadamore@1875: final Symbol lookup(Env env, MethodResolutionPhase phase) {
mcimadamore@1875: Symbol sym = doLookup(env, phase);
mcimadamore@1480: if (sym.kind == AMBIGUOUS) {
vromero@2219: AmbiguityError a_err = (AmbiguityError)sym.baseSymbol();
mcimadamore@1480: sym = a_err.mergeAbstracts(site);
mcimadamore@1480: }
mcimadamore@1875: return sym;
mcimadamore@1875: }
mcimadamore@1875:
mcimadamore@1875: abstract Symbol doLookup(Env env, MethodResolutionPhase phase);
mcimadamore@1875:
mcimadamore@1875: @Override
mcimadamore@1875: Symbol access(Env env, DiagnosticPosition pos, Symbol location, Symbol sym) {
mcimadamore@1394: if (sym.kind >= AMBIGUOUS) {
mcimadamore@1394: //if nothing is found return the 'first' error
mcimadamore@1394: sym = accessMethod(sym, pos, location, site, name, true, argtypes, typeargtypes);
mcimadamore@1394: }
mcimadamore@1394: return sym;
mcimadamore@1394: }
mcimadamore@1875:
mcimadamore@1875: @Override
mcimadamore@1875: void debug(DiagnosticPosition pos, Symbol sym) {
mcimadamore@1875: reportVerboseResolutionDiagnostic(pos, name, site, argtypes, typeargtypes, sym);
mcimadamore@1875: }
mcimadamore@1352: }
mcimadamore@1352:
mcimadamore@1352: /**
mcimadamore@1352: * Helper class for member reference lookup. A reference lookup helper
mcimadamore@1352: * defines the basic logic for member reference lookup; a method gives
mcimadamore@1352: * access to an 'unbound' helper used to perform an unbound member
mcimadamore@1352: * reference lookup.
mcimadamore@1352: */
mcimadamore@1352: abstract class ReferenceLookupHelper extends LookupHelper {
mcimadamore@1352:
mcimadamore@1352: /** The member reference tree */
mcimadamore@1352: JCMemberReference referenceTree;
mcimadamore@1352:
mcimadamore@1352: ReferenceLookupHelper(JCMemberReference referenceTree, Name name, Type site,
mcimadamore@1394: List argtypes, List typeargtypes, MethodResolutionPhase maxPhase) {
mcimadamore@1394: super(name, site, argtypes, typeargtypes, maxPhase);
mcimadamore@1352: this.referenceTree = referenceTree;
mcimadamore@1352: }
mcimadamore@1352:
mcimadamore@1352: /**
mcimadamore@1352: * Returns an unbound version of this lookup helper. By default, this
mcimadamore@1352: * method returns an dummy lookup helper.
mcimadamore@1352: */
mcimadamore@1897: ReferenceLookupHelper unboundLookup(InferenceContext inferenceContext) {
mcimadamore@1352: //dummy loopkup helper that always return 'methodNotFound'
mcimadamore@1394: return new ReferenceLookupHelper(referenceTree, name, site, argtypes, typeargtypes, maxPhase) {
mcimadamore@1352: @Override
mcimadamore@1897: ReferenceLookupHelper unboundLookup(InferenceContext inferenceContext) {
mcimadamore@1352: return this;
mcimadamore@1352: }
mcimadamore@1352: @Override
mcimadamore@1394: Symbol lookup(Env env, MethodResolutionPhase phase) {
mcimadamore@1352: return methodNotFound;
mcimadamore@1352: }
mcimadamore@1352: @Override
mcimadamore@1352: ReferenceKind referenceKind(Symbol sym) {
mcimadamore@1352: Assert.error();
mcimadamore@1352: return null;
mcimadamore@1352: }
mcimadamore@1352: };
mcimadamore@1352: }
mcimadamore@1352:
mcimadamore@1352: /**
mcimadamore@1352: * Get the kind of the member reference
mcimadamore@1352: */
mcimadamore@1352: abstract JCMemberReference.ReferenceKind referenceKind(Symbol sym);
mcimadamore@1352:
mcimadamore@1394: Symbol access(Env env, DiagnosticPosition pos, Symbol location, Symbol sym) {
mcimadamore@1480: if (sym.kind == AMBIGUOUS) {
vromero@2219: AmbiguityError a_err = (AmbiguityError)sym.baseSymbol();
mcimadamore@1480: sym = a_err.mergeAbstracts(site);
mcimadamore@1480: }
mcimadamore@1394: //skip error reporting
mcimadamore@1352: return sym;
mcimadamore@1352: }
mcimadamore@1352: }
mcimadamore@1352:
mcimadamore@1352: /**
mcimadamore@1352: * Helper class for method reference lookup. The lookup logic is based
mcimadamore@1352: * upon Resolve.findMethod; in certain cases, this helper class has a
mcimadamore@1352: * corresponding unbound helper class (see UnboundMethodReferenceLookupHelper).
mcimadamore@1352: * In such cases, non-static lookup results are thrown away.
mcimadamore@1352: */
mcimadamore@1352: class MethodReferenceLookupHelper extends ReferenceLookupHelper {
mcimadamore@1352:
mcimadamore@1352: MethodReferenceLookupHelper(JCMemberReference referenceTree, Name name, Type site,
mcimadamore@1394: List argtypes, List typeargtypes, MethodResolutionPhase maxPhase) {
mcimadamore@1394: super(referenceTree, name, site, argtypes, typeargtypes, maxPhase);
mcimadamore@1352: }
mcimadamore@1352:
mcimadamore@1610: @Override
mcimadamore@1610: final Symbol lookup(Env env, MethodResolutionPhase phase) {
mcimadamore@1352: return findMethod(env, site, name, argtypes, typeargtypes,
mcimadamore@1352: phase.isBoxingRequired(), phase.isVarargsRequired(), syms.operatorNames.contains(name));
mcimadamore@1352: }
mcimadamore@1352:
mcimadamore@1352: @Override
mcimadamore@1897: ReferenceLookupHelper unboundLookup(InferenceContext inferenceContext) {
mcimadamore@1352: if (TreeInfo.isStaticSelector(referenceTree.expr, names) &&
mcimadamore@1352: argtypes.nonEmpty() &&
mcimadamore@1897: (argtypes.head.hasTag(NONE) ||
vromero@2368: types.isSubtypeUnchecked(inferenceContext.asUndetVar(argtypes.head), site))) {
mcimadamore@1352: return new UnboundMethodReferenceLookupHelper(referenceTree, name,
mcimadamore@1394: site, argtypes, typeargtypes, maxPhase);
mcimadamore@1352: } else {
mcimadamore@1897: return super.unboundLookup(inferenceContext);
mcimadamore@1352: }
mcimadamore@1352: }
mcimadamore@1352:
mcimadamore@1352: @Override
mcimadamore@1352: ReferenceKind referenceKind(Symbol sym) {
mcimadamore@1352: if (sym.isStatic()) {
mcimadamore@1435: return ReferenceKind.STATIC;
mcimadamore@1352: } else {
mcimadamore@1352: Name selName = TreeInfo.name(referenceTree.getQualifierExpression());
mcimadamore@1352: return selName != null && selName == names._super ?
mcimadamore@1352: ReferenceKind.SUPER :
mcimadamore@1352: ReferenceKind.BOUND;
mcimadamore@1352: }
mcimadamore@1352: }
mcimadamore@1352: }
mcimadamore@1352:
mcimadamore@1352: /**
mcimadamore@1352: * Helper class for unbound method reference lookup. Essentially the same
mcimadamore@1352: * as the basic method reference lookup helper; main difference is that static
mcimadamore@1352: * lookup results are thrown away. If qualifier type is raw, an attempt to
mcimadamore@1352: * infer a parameterized type is made using the first actual argument (that
mcimadamore@1352: * would otherwise be ignored during the lookup).
mcimadamore@1352: */
mcimadamore@1352: class UnboundMethodReferenceLookupHelper extends MethodReferenceLookupHelper {
mcimadamore@1352:
mcimadamore@1352: UnboundMethodReferenceLookupHelper(JCMemberReference referenceTree, Name name, Type site,
mcimadamore@1394: List argtypes, List typeargtypes, MethodResolutionPhase maxPhase) {
mcimadamore@1581: super(referenceTree, name, site, argtypes.tail, typeargtypes, maxPhase);
mcimadamore@1674: if (site.isRaw() && !argtypes.head.hasTag(NONE)) {
mcimadamore@1674: Type asSuperSite = types.asSuper(argtypes.head, site.tsym);
vromero@2611: this.site = types.capture(asSuperSite);
mcimadamore@1581: }
mcimadamore@1352: }
mcimadamore@1352:
mcimadamore@1352: @Override
mcimadamore@1897: ReferenceLookupHelper unboundLookup(InferenceContext inferenceContext) {
mcimadamore@1352: return this;
mcimadamore@1352: }
mcimadamore@1352:
mcimadamore@1352: @Override
mcimadamore@1352: ReferenceKind referenceKind(Symbol sym) {
mcimadamore@1352: return ReferenceKind.UNBOUND;
mcimadamore@1352: }
mcimadamore@1352: }
mcimadamore@1352:
mcimadamore@1352: /**
mcimadamore@1496: * Helper class for array constructor lookup; an array constructor lookup
mcimadamore@1496: * is simulated by looking up a method that returns the array type specified
mcimadamore@1496: * as qualifier, and that accepts a single int parameter (size of the array).
mcimadamore@1496: */
mcimadamore@1496: class ArrayConstructorReferenceLookupHelper extends ReferenceLookupHelper {
mcimadamore@1496:
mcimadamore@1496: ArrayConstructorReferenceLookupHelper(JCMemberReference referenceTree, Type site, List argtypes,
mcimadamore@1496: List typeargtypes, MethodResolutionPhase maxPhase) {
mcimadamore@1496: super(referenceTree, names.init, site, argtypes, typeargtypes, maxPhase);
mcimadamore@1496: }
mcimadamore@1496:
mcimadamore@1496: @Override
mcimadamore@1496: protected Symbol lookup(Env env, MethodResolutionPhase phase) {
mcimadamore@1496: Scope sc = new Scope(syms.arrayClass);
mcimadamore@1496: MethodSymbol arrayConstr = new MethodSymbol(PUBLIC, name, null, site.tsym);
vromero@1853: arrayConstr.type = new MethodType(List.of(syms.intType), site, List.nil(), syms.methodClass);
mcimadamore@1496: sc.enter(arrayConstr);
mcimadamore@1496: return findMethodInScope(env, site, name, argtypes, typeargtypes, sc, methodNotFound, phase.isBoxingRequired(), phase.isVarargsRequired(), false, false);
mcimadamore@1496: }
mcimadamore@1496:
mcimadamore@1496: @Override
mcimadamore@1496: ReferenceKind referenceKind(Symbol sym) {
mcimadamore@1496: return ReferenceKind.ARRAY_CTOR;
mcimadamore@1496: }
mcimadamore@1496: }
mcimadamore@1496:
mcimadamore@1496: /**
mcimadamore@1352: * Helper class for constructor reference lookup. The lookup logic is based
mcimadamore@1352: * upon either Resolve.findMethod or Resolve.findDiamond - depending on
mcimadamore@1352: * whether the constructor reference needs diamond inference (this is the case
mcimadamore@1352: * if the qualifier type is raw). A special erroneous symbol is returned
mcimadamore@1352: * if the lookup returns the constructor of an inner class and there's no
mcimadamore@1352: * enclosing instance in scope.
mcimadamore@1352: */
mcimadamore@1352: class ConstructorReferenceLookupHelper extends ReferenceLookupHelper {
mcimadamore@1352:
mcimadamore@1352: boolean needsInference;
mcimadamore@1352:
mcimadamore@1352: ConstructorReferenceLookupHelper(JCMemberReference referenceTree, Type site, List argtypes,
mcimadamore@1394: List typeargtypes, MethodResolutionPhase maxPhase) {
mcimadamore@1394: super(referenceTree, names.init, site, argtypes, typeargtypes, maxPhase);
mcimadamore@1352: if (site.isRaw()) {
mcimadamore@1352: this.site = new ClassType(site.getEnclosingType(), site.tsym.type.getTypeArguments(), site.tsym);
mcimadamore@1352: needsInference = true;
mcimadamore@1352: }
mcimadamore@1352: }
mcimadamore@1352:
mcimadamore@1352: @Override
mcimadamore@1394: protected Symbol lookup(Env env, MethodResolutionPhase phase) {
mcimadamore@1352: Symbol sym = needsInference ?
mcimadamore@1352: findDiamond(env, site, argtypes, typeargtypes, phase.isBoxingRequired(), phase.isVarargsRequired()) :
mcimadamore@1352: findMethod(env, site, name, argtypes, typeargtypes,
mcimadamore@1352: phase.isBoxingRequired(), phase.isVarargsRequired(), syms.operatorNames.contains(name));
mcimadamore@1352: return sym.kind != MTH ||
jjg@1374: site.getEnclosingType().hasTag(NONE) ||
mcimadamore@1352: hasEnclosingInstance(env, site) ?
mcimadamore@1352: sym : new InvalidSymbolError(Kinds.MISSING_ENCL, sym, null) {
mcimadamore@1352: @Override
mcimadamore@1352: JCDiagnostic getDiagnostic(DiagnosticType dkind, DiagnosticPosition pos, Symbol location, Type site, Name name, List argtypes, List typeargtypes) {
mcimadamore@1352: return diags.create(dkind, log.currentSource(), pos,
mcimadamore@1352: "cant.access.inner.cls.constr", site.tsym.name, argtypes, site.getEnclosingType());
mcimadamore@1352: }
mcimadamore@1352: };
mcimadamore@1352: }
mcimadamore@1352:
mcimadamore@1352: @Override
mcimadamore@1352: ReferenceKind referenceKind(Symbol sym) {
jjg@1374: return site.getEnclosingType().hasTag(NONE) ?
mcimadamore@1352: ReferenceKind.TOPLEVEL : ReferenceKind.IMPLICIT_INNER;
mcimadamore@1352: }
mcimadamore@1352: }
mcimadamore@1352:
mcimadamore@1352: /**
mcimadamore@1394: * Main overload resolution routine. On each overload resolution step, a
mcimadamore@1394: * lookup helper class is used to perform the method/constructor lookup;
mcimadamore@1394: * at the end of the lookup, the helper is used to validate the results
mcimadamore@1394: * (this last step might trigger overload resolution diagnostics).
mcimadamore@1352: */
mcimadamore@1674: Symbol lookupMethod(Env env, DiagnosticPosition pos, Symbol location, MethodCheck methodCheck, LookupHelper lookupHelper) {
mcimadamore@1674: MethodResolutionContext resolveContext = new MethodResolutionContext();
mcimadamore@1674: resolveContext.methodCheck = methodCheck;
mcimadamore@1674: return lookupMethod(env, pos, location, resolveContext, lookupHelper);
mcimadamore@1394: }
mcimadamore@1394:
mcimadamore@1394: Symbol lookupMethod(Env env, DiagnosticPosition pos, Symbol location,
mcimadamore@1394: MethodResolutionContext resolveContext, LookupHelper lookupHelper) {
mcimadamore@1352: MethodResolutionContext prevResolutionContext = currentResolutionContext;
mcimadamore@1352: try {
mcimadamore@1394: Symbol bestSoFar = methodNotFound;
mcimadamore@1394: currentResolutionContext = resolveContext;
mcimadamore@1394: for (MethodResolutionPhase phase : methodResolutionSteps) {
mcimadamore@1394: if (!phase.isApplicable(boxingEnabled, varargsEnabled) ||
mcimadamore@1394: lookupHelper.shouldStop(bestSoFar, phase)) break;
mcimadamore@1394: MethodResolutionPhase prevPhase = currentResolutionContext.step;
mcimadamore@1394: Symbol prevBest = bestSoFar;
mcimadamore@1394: currentResolutionContext.step = phase;
mcimadamore@1875: Symbol sym = lookupHelper.lookup(env, phase);
mcimadamore@1875: lookupHelper.debug(pos, sym);
mcimadamore@1875: bestSoFar = phase.mergeResults(bestSoFar, sym);
mcimadamore@1394: env.info.pendingResolutionPhase = (prevBest == bestSoFar) ? prevPhase : phase;
mcimadamore@1352: }
mcimadamore@1394: return lookupHelper.access(env, pos, location, bestSoFar);
mcimadamore@1394: } finally {
mcimadamore@1352: currentResolutionContext = prevResolutionContext;
mcimadamore@1352: }
mcimadamore@1352: }
mcimadamore@1352:
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);
mcimadamore@1347: return accessBase(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: }
vromero@2261: if (c.isInterface() &&
vromero@2261: name == names._super && !isStatic(env) &&
vromero@2261: types.isDirectSuperInterface(c, env.enclClass.sym)) {
mcimadamore@1393: //this might be a default super call if one of the superinterfaces is 'c'
mcimadamore@1393: for (Type t : pruneInterfaces(env.enclClass.type)) {
mcimadamore@1393: if (t.tsym == c) {
mcimadamore@1393: env.info.defaultSuperCallSite = t;
mcimadamore@1393: return new VarSymbol(0, names._super,
mcimadamore@1393: types.asSuper(env.enclClass.type, c), env.enclClass.sym);
mcimadamore@1393: }
mcimadamore@1393: }
mcimadamore@1393: //find a direct superinterface that is a subtype of 'c'
mcimadamore@1393: for (Type i : types.interfaces(env.enclClass.type)) {
mcimadamore@1393: if (i.tsym.isSubClass(c, types) && i.tsym != c) {
mcimadamore@1393: log.error(pos, "illegal.default.super.call", c,
mcimadamore@1393: diags.fragment("redundant.supertype", c, i));
mcimadamore@1393: return syms.errSymbol;
mcimadamore@1393: }
mcimadamore@1393: }
mcimadamore@1393: Assert.error();
mcimadamore@1393: }
duke@1: log.error(pos, "not.encl.class", c);
duke@1: return syms.errSymbol;
duke@1: }
mcimadamore@1393: //where
mcimadamore@1393: private List pruneInterfaces(Type t) {
alundblad@2047: ListBuffer result = new ListBuffer<>();
mcimadamore@1393: for (Type t1 : types.interfaces(t)) {
mcimadamore@1393: boolean shouldAdd = true;
mcimadamore@1393: for (Type t2 : types.interfaces(t)) {
mcimadamore@1393: if (t1 != t2 && types.isSubtypeNoCapture(t2, t1)) {
mcimadamore@1393: shouldAdd = false;
mcimadamore@1393: }
mcimadamore@1393: }
mcimadamore@1393: if (shouldAdd) {
mcimadamore@1393: result.append(t1);
mcimadamore@1393: }
mcimadamore@1393: }
mcimadamore@1393: return result.toList();
mcimadamore@1393: }
mcimadamore@1393:
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) {
mcimadamore@1352: Symbol sym = resolveSelfContainingInternal(env, member, isSuperCall);
mcimadamore@1352: if (sym == null) {
mcimadamore@1352: log.error(pos, "encl.class.required", member);
mcimadamore@1352: return syms.errSymbol;
mcimadamore@1352: } else {
mcimadamore@1352: return accessBase(sym, pos, env.enclClass.sym.type, sym.name, true);
mcimadamore@1352: }
mcimadamore@1352: }
mcimadamore@1352:
mcimadamore@1352: boolean hasEnclosingInstance(Env env, Type type) {
mcimadamore@1352: Symbol encl = resolveSelfContainingInternal(env, type.tsym, false);
mcimadamore@1352: return encl != null && encl.kind < ERRONEOUS;
mcimadamore@1352: }
mcimadamore@1352:
mcimadamore@1352: private Symbol resolveSelfContainingInternal(Env env,
mcimadamore@1352: Symbol member,
mcimadamore@1352: 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@1352: return sym;
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: }
mcimadamore@1352: return null;
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:
mcimadamore@1221: //used by TransTypes when checking target type of synthetic cast
mcimadamore@1221: public void logAccessErrorInternal(Env env, JCTree tree, Type type) {
mcimadamore@1221: AccessError error = new AccessError(env, env.enclClass.type, type.tsym);
mcimadamore@1221: logResolveError(error, tree.pos(), env.enclClass.sym, env.enclClass.type, 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